package business import ( "context" "time" "gorm.io/gorm/clause" "kra/internal/biz/business" ) type goalRepo struct{ data Provider } func NewGoalRepo(data Provider) business.GoalRepo { return &goalRepo{data: data} } func (r *goalRepo) List(ctx context.Context, begin, end string) ([]*business.PerformanceGoal, error) { var rows []performanceGoalPO if err := r.data.DB().WithContext(ctx).Where("month BETWEEN ? AND ?", begin, end).Order("month DESC,kind,target_id").Find(&rows).Error; err != nil { return nil, business.ErrStorage } out := make([]*business.PerformanceGoal, 0, len(rows)) for _, r := range rows { out = append(out, &business.PerformanceGoal{Kind: r.Kind, TargetID: r.TargetID, Month: r.Month, Open: r.Open, Effective: r.Effective, Amount: r.Amount, UpdatedBy: r.UpdatedBy, UpdatedAt: r.UpdatedAt}) } return out, nil } func (r *goalRepo) Save(ctx context.Context, g *business.PerformanceGoal) error { db := r.data.DB().WithContext(ctx) if g.Open == nil && g.Effective == nil && g.Amount == nil { return storageError(db.Where("kind=? AND target_id=? AND month=?", g.Kind, g.TargetID, g.Month).Delete(&performanceGoalPO{}).Error) } po := performanceGoalPO{Kind: g.Kind, TargetID: g.TargetID, Month: g.Month, Open: g.Open, Effective: g.Effective, Amount: g.Amount, UpdatedBy: g.UpdatedBy, UpdatedAt: time.Now()} return storageError(db.Clauses(clause.OnConflict{Columns: []clause.Column{{Name: "kind"}, {Name: "target_id"}, {Name: "month"}}, DoUpdates: clause.AssignmentColumns([]string{"open", "effective", "amount", "updated_by", "updated_at"})}).Create(&po).Error) }