fix: 优化负利润分析报表处理逻辑

This commit is contained in:
renzhiyuan 2026-01-13 18:28:31 +08:00
parent c91e7bc95b
commit c7e1bad7ef
2 changed files with 36 additions and 15 deletions

View File

@ -45,7 +45,7 @@ func (s *SessionBiz) SessionInit(ctx context.Context, req *entitys.SessionInitRe
// 获取 当天的最新session
t := time.Now().Truncate(24 * time.Hour)
session, has, err := s.sessionRepo.FindOne(
session, has, err := s.sessionRepo.Take(
s.sessionRepo.WithUserId(req.UserId), // 条件用户ID
s.sessionRepo.WithStartTime(t), // 条件:会话开始时间
s.sysRepo.WithSysId(sysConfig.SysID),

View File

@ -43,6 +43,7 @@ type BaseRepository[P PO] interface {
FindAll(conditions ...CondFunc) ([]P, error) // 查询所有
Paginate(page, pageSize int, conditions ...CondFunc) (*PaginationResult[P], error) // 分页查询
FindOne(conditions ...CondFunc) (P, bool, error) // 查询单条记录,若未找到则返回 has=false, err=nil
Take(conditions ...CondFunc) (P, bool, error)
Create(m *P) error // 创建
BatchCreate(m *[]P) (err error) // 批量创建
Update(m *P, conditions ...CondFunc) (err error) // 更新
@ -128,6 +129,26 @@ func (this *BaseModel[P]) FindOne(conditions ...CondFunc) (P, bool, error) {
return result, true, err
}
func (this *BaseModel[P]) Take(conditions ...CondFunc) (P, bool, error) {
var (
result P
)
err := this.Db.Model(new(P)).
Scopes(conditions...).
Take(&result).
Error
if errors.Is(err, gorm.ErrRecordNotFound) {
return result, false, nil // 未找到记录
}
if err != nil {
return result, false, fmt.Errorf("查询单条记录失败: %w", err)
}
return result, true, err
}
// 创建
func (this *BaseModel[P]) Create(m *P) error {
err := this.Db.Create(m).Error