作者 郑周

1.周期模型 业务逻辑编写

  1 +package adapter
  2 +
  3 +import (
  4 + "gitlab.fjmaimaimai.com/allied-creation/performance/pkg/domain"
  5 +)
  6 +
  7 +type CycleTemplateAdapter struct {
  8 + *domain.EvaluationCycle
  9 + TemplateSimples []*domain.TemplateSimple `json:"templates" comment:"模板"`
  10 +}
@@ -3,9 +3,10 @@ package command @@ -3,9 +3,10 @@ package command
3 import "github.com/beego/beego/v2/core/validation" 3 import "github.com/beego/beego/v2/core/validation"
4 4
5 type QueryCycleCommand struct { 5 type QueryCycleCommand struct {
6 - CompanyId int64 `cname:"公司ID" json:"companyId"`  
7 - PageNumber int `cname:"分页页码" json:"pageNumber" valid:"Required"`  
8 - PageSize int `cname:"分页数量" json:"pageSize" valid:"Required"` 6 + CompanyId int64 `cname:"公司ID" json:"companyId"`
  7 + Name string `cname:"周期名称" json:"name"`
  8 + PageNumber int `cname:"分页页码" json:"pageNumber" valid:"Required"`
  9 + PageSize int `cname:"分页数量" json:"pageSize" valid:"Required"`
9 } 10 }
10 11
11 func (in *QueryCycleCommand) Valid(validation *validation.Validation) { 12 func (in *QueryCycleCommand) Valid(validation *validation.Validation) {
@@ -3,10 +3,12 @@ package service @@ -3,10 +3,12 @@ package service
3 import ( 3 import (
4 "github.com/linmadan/egglib-go/core/application" 4 "github.com/linmadan/egglib-go/core/application"
5 "github.com/linmadan/egglib-go/utils/tool_funs" 5 "github.com/linmadan/egglib-go/utils/tool_funs"
  6 + "gitlab.fjmaimaimai.com/allied-creation/performance/pkg/application/evaluation_cycle/adapter"
6 "gitlab.fjmaimaimai.com/allied-creation/performance/pkg/application/evaluation_cycle/command" 7 "gitlab.fjmaimaimai.com/allied-creation/performance/pkg/application/evaluation_cycle/command"
7 "gitlab.fjmaimaimai.com/allied-creation/performance/pkg/application/factory" 8 "gitlab.fjmaimaimai.com/allied-creation/performance/pkg/application/factory"
8 "gitlab.fjmaimaimai.com/allied-creation/performance/pkg/domain" 9 "gitlab.fjmaimaimai.com/allied-creation/performance/pkg/domain"
9 "gitlab.fjmaimaimai.com/allied-creation/performance/pkg/utils" 10 "gitlab.fjmaimaimai.com/allied-creation/performance/pkg/utils"
  11 + "strconv"
10 ) 12 )
11 13
12 type EvaluationCycleService struct { 14 type EvaluationCycleService struct {
@@ -27,6 +29,7 @@ func (rs *EvaluationCycleService) Create(in *command.CreateCycleCommand) (interf @@ -27,6 +29,7 @@ func (rs *EvaluationCycleService) Create(in *command.CreateCycleCommand) (interf
27 transactionContext.RollbackTransaction() 29 transactionContext.RollbackTransaction()
28 }() 30 }()
29 cycleRepository := factory.CreateEvaluationCycleRepository(map[string]interface{}{"transactionContext": transactionContext}) 31 cycleRepository := factory.CreateEvaluationCycleRepository(map[string]interface{}{"transactionContext": transactionContext})
  32 + cycleTemplateRepository := factory.CreateEvaluationCycleTemplateRepository(map[string]interface{}{"transactionContext": transactionContext})
30 templateRepository := factory.CreateEvaluationTemplateRepository(map[string]interface{}{"transactionContext": transactionContext}) 33 templateRepository := factory.CreateEvaluationTemplateRepository(map[string]interface{}{"transactionContext": transactionContext})
31 34
32 // 检测名称重复 35 // 检测名称重复
@@ -54,16 +57,30 @@ func (rs *EvaluationCycleService) Create(in *command.CreateCycleCommand) (interf @@ -54,16 +57,30 @@ func (rs *EvaluationCycleService) Create(in *command.CreateCycleCommand) (interf
54 CompanyId: in.CompanyId, 57 CompanyId: in.CompanyId,
55 CreatorId: in.CreatorId, 58 CreatorId: in.CreatorId,
56 KpiCycle: in.KpiCycle, 59 KpiCycle: in.KpiCycle,
57 - Templates: templates,  
58 } 60 }
59 - rule, err := cycleRepository.Insert(newCycle) 61 + cycle, err := cycleRepository.Insert(newCycle)
60 if err != nil { 62 if err != nil {
61 return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error()) 63 return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
62 } 64 }
  65 +
  66 + for i := range templates {
  67 + v := templates[i]
  68 + cycleTemplate := &domain.EvaluationCycleTemplate{
  69 + Id: 0,
  70 + Name: v.Name,
  71 + Template: v,
  72 + CycleId: cycle.Id,
  73 + }
  74 + _, err := cycleTemplateRepository.Insert(cycleTemplate)
  75 + if err != nil {
  76 + return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
  77 + }
  78 + }
  79 +
63 if err := transactionContext.CommitTransaction(); err != nil { 80 if err := transactionContext.CommitTransaction(); err != nil {
64 return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error()) 81 return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
65 } 82 }
66 - return rule, nil 83 + return cycle, nil
67 84
68 } 85 }
69 86
@@ -77,6 +94,7 @@ func (rs *EvaluationCycleService) Update(in *command.UpdateCycleCommand) (interf @@ -77,6 +94,7 @@ func (rs *EvaluationCycleService) Update(in *command.UpdateCycleCommand) (interf
77 }() 94 }()
78 95
79 cycleRepository := factory.CreateEvaluationCycleRepository(map[string]interface{}{"transactionContext": transactionContext}) 96 cycleRepository := factory.CreateEvaluationCycleRepository(map[string]interface{}{"transactionContext": transactionContext})
  97 + cycleTemplateRepository := factory.CreateEvaluationCycleTemplateRepository(map[string]interface{}{"transactionContext": transactionContext})
80 templateRepository := factory.CreateEvaluationTemplateRepository(map[string]interface{}{"transactionContext": transactionContext}) 98 templateRepository := factory.CreateEvaluationTemplateRepository(map[string]interface{}{"transactionContext": transactionContext})
81 99
82 // 检测名称重复(排除自己) 100 // 检测名称重复(排除自己)
@@ -93,19 +111,58 @@ func (rs *EvaluationCycleService) Update(in *command.UpdateCycleCommand) (interf @@ -93,19 +111,58 @@ func (rs *EvaluationCycleService) Update(in *command.UpdateCycleCommand) (interf
93 return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error()) 111 return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
94 } 112 }
95 113
96 - _, templates, err := templateRepository.Find(map[string]interface{}{"companyId": in.CompanyId, "ids": in.TemplateIds}) 114 + _, oldCycleTemplates, err := cycleTemplateRepository.Find(map[string]interface{}{"cycleId": cycle.Id}, "template")
97 if err != nil { 115 if err != nil {
98 return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error()) 116 return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
99 } 117 }
100 - if len(templates) == 0 {  
101 - return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, "模板不存在, 请重新选择") 118 +
  119 + // 当前周期的所有模板数据
  120 + oldTemplateMap := map[int64]*domain.EvaluationCycleTemplate{}
  121 + for i := range oldCycleTemplates {
  122 + oldTemplateMap[oldCycleTemplates[i].Id] = oldCycleTemplates[i]
  123 + }
  124 + // 拆离新旧模板Id
  125 + newTemplateIds := make([]int64, 0)
  126 + for i := range in.TemplateIds {
  127 + int64Id, _ := strconv.ParseInt(in.TemplateIds[i], 10, 64)
  128 + if _, ok := oldTemplateMap[int64Id]; ok {
  129 + delete(oldTemplateMap, int64Id) // 旧模板继续被引用
  130 + } else {
  131 + newTemplateIds = append(newTemplateIds, int64Id) // 增加新模板ID
  132 + }
  133 + }
  134 + // 旧模板未被引用,则进行删除处理
  135 + for k := range oldTemplateMap {
  136 + _, err := cycleTemplateRepository.Remove(oldTemplateMap[k])
  137 + if err != nil {
  138 + return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
  139 + }
102 } 140 }
  141 + // 增加新模板数据
  142 + if len(newTemplateIds) > 0 {
  143 + _, templates, err := templateRepository.Find(map[string]interface{}{"companyId": cycle.CompanyId, "ids": newTemplateIds})
  144 + if err != nil {
  145 + return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
  146 + }
  147 + for i := range templates {
  148 + v := templates[i]
  149 + cycleTemplate := &domain.EvaluationCycleTemplate{
  150 + Id: 0,
  151 + Name: v.Name,
  152 + Template: v,
  153 + CycleId: cycle.Id,
  154 + }
  155 + _, err := cycleTemplateRepository.Insert(cycleTemplate)
  156 + if err != nil {
  157 + return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
  158 + }
  159 + }
  160 + }
  161 +
103 cycle.Name = in.Name 162 cycle.Name = in.Name
104 cycle.TimeStart = in.TimeStart 163 cycle.TimeStart = in.TimeStart
105 cycle.TimeEnd = in.TimeEnd 164 cycle.TimeEnd = in.TimeEnd
106 cycle.KpiCycle = in.KpiCycle 165 cycle.KpiCycle = in.KpiCycle
107 - cycle.Templates = templates  
108 -  
109 cycle, err = cycleRepository.Insert(cycle) 166 cycle, err = cycleRepository.Insert(cycle)
110 if err != nil { 167 if err != nil {
111 return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error()) 168 return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
@@ -126,11 +183,28 @@ func (rs *EvaluationCycleService) Get(in *command.GetCycleCommand) (interface{}, @@ -126,11 +183,28 @@ func (rs *EvaluationCycleService) Get(in *command.GetCycleCommand) (interface{},
126 return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error()) 183 return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
127 } 184 }
128 cycleRepository := factory.CreateEvaluationCycleRepository(map[string]interface{}{"transactionContext": transactionContext}) 185 cycleRepository := factory.CreateEvaluationCycleRepository(map[string]interface{}{"transactionContext": transactionContext})
  186 + cycleTemplateRepository := factory.CreateEvaluationCycleTemplateRepository(map[string]interface{}{"transactionContext": transactionContext})
  187 +
129 cycle, err := cycleRepository.FindOne(map[string]interface{}{"id": in.Id}) 188 cycle, err := cycleRepository.FindOne(map[string]interface{}{"id": in.Id})
130 if err != nil { 189 if err != nil {
131 return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error()) 190 return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
132 } 191 }
133 - return cycle, nil 192 +
  193 + _, cycleTemplates, err := cycleTemplateRepository.Find(map[string]interface{}{"cycleId": cycle.Id}, "template")
  194 + if err != nil {
  195 + return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
  196 + }
  197 +
  198 + ctAdapter := &adapter.CycleTemplateAdapter{}
  199 + ctAdapter.EvaluationCycle = cycle
  200 + for i := range cycleTemplates {
  201 + ctAdapter.TemplateSimples = append(ctAdapter.TemplateSimples, &domain.TemplateSimple{
  202 + Id: cycleTemplates[i].Id,
  203 + Name: cycleTemplates[i].Name,
  204 + CreatedAt: cycleTemplates[i].CreatedAt,
  205 + })
  206 + }
  207 + return ctAdapter, nil
134 } 208 }
135 209
136 func (rs *EvaluationCycleService) Remove(in *command.DeleteCycleCommand) (interface{}, error) { 210 func (rs *EvaluationCycleService) Remove(in *command.DeleteCycleCommand) (interface{}, error) {
@@ -143,6 +217,7 @@ func (rs *EvaluationCycleService) Remove(in *command.DeleteCycleCommand) (interf @@ -143,6 +217,7 @@ func (rs *EvaluationCycleService) Remove(in *command.DeleteCycleCommand) (interf
143 }() 217 }()
144 218
145 cycleRepository := factory.CreateEvaluationCycleRepository(map[string]interface{}{"transactionContext": transactionContext}) 219 cycleRepository := factory.CreateEvaluationCycleRepository(map[string]interface{}{"transactionContext": transactionContext})
  220 + cycleTemplateRepository := factory.CreateEvaluationCycleTemplateRepository(map[string]interface{}{"transactionContext": transactionContext})
146 221
147 cycle, err := cycleRepository.FindOne(map[string]interface{}{"id": in.Id}) 222 cycle, err := cycleRepository.FindOne(map[string]interface{}{"id": in.Id})
148 if err != nil { 223 if err != nil {
@@ -152,6 +227,11 @@ func (rs *EvaluationCycleService) Remove(in *command.DeleteCycleCommand) (interf @@ -152,6 +227,11 @@ func (rs *EvaluationCycleService) Remove(in *command.DeleteCycleCommand) (interf
152 return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error()) 227 return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
153 } 228 }
154 229
  230 + // FIXME 删除周期关联的所有模板... 还需要删除关联的定时内容
  231 + if err := cycleTemplateRepository.BatchDeleteByCycleId(cycle.Id); err != nil {
  232 + return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
  233 + }
  234 +
155 if err := transactionContext.CommitTransaction(); err != nil { 235 if err := transactionContext.CommitTransaction(); err != nil {
156 return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error()) 236 return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
157 } 237 }
@@ -167,8 +247,12 @@ func (rs *EvaluationCycleService) List(in *command.QueryCycleCommand) (interface @@ -167,8 +247,12 @@ func (rs *EvaluationCycleService) List(in *command.QueryCycleCommand) (interface
167 transactionContext.RollbackTransaction() 247 transactionContext.RollbackTransaction()
168 }() 248 }()
169 cycleRepository := factory.CreateEvaluationCycleRepository(map[string]interface{}{"transactionContext": transactionContext}) 249 cycleRepository := factory.CreateEvaluationCycleRepository(map[string]interface{}{"transactionContext": transactionContext})
  250 + inMap := tool_funs.SimpleStructToMap(in)
  251 + if len(in.Name) > 0 {
  252 + inMap["name"] = "%" + in.Name + "%"
  253 + }
170 // FIXME 总数量是否使用Count获取一个总数量 254 // FIXME 总数量是否使用Count获取一个总数量
171 - count, cycles, err := cycleRepository.Find(tool_funs.SimpleStructToMap(in), "templates") 255 + count, cycles, err := cycleRepository.Find(inMap)
172 if err != nil { 256 if err != nil {
173 return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error()) 257 return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
174 } 258 }
@@ -105,6 +105,14 @@ func CreateEvaluationCycleRepository(options map[string]interface{}) domain.Eval @@ -105,6 +105,14 @@ func CreateEvaluationCycleRepository(options map[string]interface{}) domain.Eval
105 return repository.NewEvaluationCycleRepository(transactionContext) 105 return repository.NewEvaluationCycleRepository(transactionContext)
106 } 106 }
107 107
  108 +func CreateEvaluationCycleTemplateRepository(options map[string]interface{}) domain.EvaluationCycleTemplateRepository {
  109 + var transactionContext *pg.TransactionContext
  110 + if value, ok := options["transactionContext"]; ok {
  111 + transactionContext = value.(*pg.TransactionContext)
  112 + }
  113 + return repository.NewEvaluationCycleTemplateRepository(transactionContext)
  114 +}
  115 +
108 func CreateEvaluationProjectRepository(options map[string]interface{}) domain.EvaluationProjectRepository { 116 func CreateEvaluationProjectRepository(options map[string]interface{}) domain.EvaluationProjectRepository {
109 var transactionContext *pg.TransactionContext 117 var transactionContext *pg.TransactionContext
110 if value, ok := options["transactionContext"]; ok { 118 if value, ok := options["transactionContext"]; ok {
@@ -4,24 +4,29 @@ import ( @@ -4,24 +4,29 @@ import (
4 "time" 4 "time"
5 ) 5 )
6 6
  7 +type TemplateSimple struct {
  8 + Id int64 `json:"id,string" comment:"模板ID"`
  9 + Name string `json:"name" comment:"模板名称"`
  10 + CreatedAt time.Time `json:"createdAt" comment:"创建时间"`
  11 +}
  12 +
7 type EvaluationCycle struct { 13 type EvaluationCycle struct {
8 - Id int64 `json:"id,string" comment:"ID"`  
9 - Name string `json:"name" comment:"名称"`  
10 - TimeStart *time.Time `json:"timeStart" comment:"起始时间"`  
11 - TimeEnd *time.Time `json:"timeEnd" comment:"截至时间"`  
12 - CompanyId int64 `json:"companyId,string" comment:"公司ID"`  
13 - CreatorId int64 `json:"creatorId,string" comment:"创建人ID"`  
14 - KpiCycle int `json:"state" comment:"考核周期(0日、1周、2月)"`  
15 - Templates []*EvaluationTemplate `json:"templates" comment:"周期使用模板"`  
16 - CreatedAt time.Time `json:"createdAt" comment:"创建时间"`  
17 - UpdatedAt time.Time `json:"updatedAt" comment:"更新时间"`  
18 - DeletedAt *time.Time `json:"deletedAt" comment:"删除时间"` 14 + Id int64 `json:"id,string" comment:"ID"`
  15 + Name string `json:"name" comment:"名称"`
  16 + TimeStart *time.Time `json:"timeStart" comment:"起始时间"`
  17 + TimeEnd *time.Time `json:"timeEnd" comment:"截至时间"`
  18 + CompanyId int64 `json:"companyId,string" comment:"公司ID"`
  19 + CreatorId int64 `json:"creatorId,string" comment:"创建人ID"`
  20 + KpiCycle int `json:"state" comment:"考核周期(0日、1周、2月)"`
  21 + CreatedAt time.Time `json:"createdAt" comment:"创建时间"`
  22 + UpdatedAt time.Time `json:"updatedAt" comment:"更新时间"`
  23 + DeletedAt *time.Time `json:"deletedAt" comment:"删除时间"`
19 } 24 }
20 25
21 type EvaluationCycleRepository interface { 26 type EvaluationCycleRepository interface {
22 Insert(cycle *EvaluationCycle) (*EvaluationCycle, error) 27 Insert(cycle *EvaluationCycle) (*EvaluationCycle, error)
23 Remove(cycle *EvaluationCycle) (*EvaluationCycle, error) 28 Remove(cycle *EvaluationCycle) (*EvaluationCycle, error)
24 FindOne(queryOptions map[string]interface{}) (*EvaluationCycle, error) 29 FindOne(queryOptions map[string]interface{}) (*EvaluationCycle, error)
25 - Find(queryOptions map[string]interface{}, excludeColumns ...string) (int64, []*EvaluationCycle, error) 30 + Find(queryOptions map[string]interface{}) (int64, []*EvaluationCycle, error)
26 Count(queryOptions map[string]interface{}) (int64, error) 31 Count(queryOptions map[string]interface{}) (int64, error)
27 } 32 }
  1 +package domain
  2 +
  3 +import (
  4 + "time"
  5 +)
  6 +
  7 +type EvaluationCycleTemplate struct {
  8 + Id int64 `json:"id,string" comment:"模板ID"`
  9 + Name string `json:"name" comment:"模板名称"`
  10 + Template *EvaluationTemplate `json:"template" comment:"模板数据"`
  11 + CycleId int64 `json:"cycleId,string" comment:"周期ID"`
  12 + CreatedAt time.Time `json:"createdAt" comment:"创建时间"`
  13 + UpdatedAt time.Time `json:"updatedAt" comment:"更新时间"`
  14 + DeletedAt *time.Time `json:"deletedAt" comment:"删除时间"`
  15 +}
  16 +
  17 +type EvaluationCycleTemplateRepository interface {
  18 + Insert(cycle *EvaluationCycleTemplate) (*EvaluationCycleTemplate, error)
  19 + Remove(cycle *EvaluationCycleTemplate) (*EvaluationCycleTemplate, error)
  20 + FindOne(queryOptions map[string]interface{}) (*EvaluationCycleTemplate, error)
  21 + Find(queryOptions map[string]interface{}, excludeColumns ...string) (int64, []*EvaluationCycleTemplate, error)
  22 + Count(queryOptions map[string]interface{}) (int64, error)
  23 + BatchDeleteByCycleId(cycleId int64) error
  24 +}
1 package models 1 package models
2 2
3 import ( 3 import (
4 - "gitlab.fjmaimaimai.com/allied-creation/performance/pkg/domain"  
5 "time" 4 "time"
6 ) 5 )
7 6
8 type EvaluationCycle struct { 7 type EvaluationCycle struct {
9 - tableName struct{} `pg:"evaluation_cycle" comment:"评估周期"`  
10 - Id int64 `pg:"pk:id" comment:"周期ID"`  
11 - Name string `comment:"名称"`  
12 - TimeStart *time.Time `comment:"起始时间"`  
13 - TimeEnd *time.Time `comment:"截至时间"`  
14 - CompanyId int64 `comment:"公司ID"`  
15 - CreatorId int64 `comment:"创建人ID"`  
16 - KpiCycle int `comment:"考核周期(0日、1周、2月)"`  
17 - Templates []*domain.EvaluationTemplate `comment:"周期使用模板"`  
18 - CreatedAt time.Time `comment:"创建时间"`  
19 - UpdatedAt time.Time `comment:"更新时间"`  
20 - DeletedAt *time.Time `comment:"删除时间"` 8 + tableName struct{} `pg:"evaluation_cycle" comment:"评估周期"`
  9 + Id int64 `pg:"pk:id" comment:"周期ID"`
  10 + Name string `comment:"名称"`
  11 + TimeStart *time.Time `comment:"起始时间"`
  12 + TimeEnd *time.Time `comment:"截至时间"`
  13 + CompanyId int64 `comment:"公司ID"`
  14 + CreatorId int64 `comment:"创建人ID"`
  15 + KpiCycle int `comment:"考核周期(0日、1周、2月)"`
  16 + CreatedAt time.Time `comment:"创建时间"`
  17 + UpdatedAt time.Time `comment:"更新时间"`
  18 + DeletedAt *time.Time `comment:"删除时间"`
21 } 19 }
  1 +package models
  2 +
  3 +import (
  4 + "gitlab.fjmaimaimai.com/allied-creation/performance/pkg/domain"
  5 + "time"
  6 +)
  7 +
  8 +type EvaluationCycleTemplate struct {
  9 + tableName struct{} `pg:"evaluation_cycle_template" comment:"评估周期模板"`
  10 + Id int64 `pg:"pk:id" comment:"模板ID"`
  11 + Name string `comment:"模板名称"`
  12 + Template *domain.EvaluationTemplate `comment:"模板数据"`
  13 + CycleId int64 `comment:"周期ID"`
  14 + CreatedAt time.Time `comment:"创建时间"`
  15 + UpdatedAt time.Time `comment:"更新时间"`
  16 + DeletedAt *time.Time `comment:"删除时间"`
  17 +}
@@ -29,7 +29,6 @@ func (repo *EvaluationCycleRepository) TransformToDomain(m *models.EvaluationCyc @@ -29,7 +29,6 @@ func (repo *EvaluationCycleRepository) TransformToDomain(m *models.EvaluationCyc
29 CompanyId: m.CompanyId, 29 CompanyId: m.CompanyId,
30 CreatorId: m.CreatorId, 30 CreatorId: m.CreatorId,
31 KpiCycle: m.KpiCycle, 31 KpiCycle: m.KpiCycle,
32 - Templates: m.Templates,  
33 CreatedAt: m.CreatedAt, 32 CreatedAt: m.CreatedAt,
34 UpdatedAt: m.UpdatedAt, 33 UpdatedAt: m.UpdatedAt,
35 DeletedAt: m.DeletedAt, 34 DeletedAt: m.DeletedAt,
@@ -45,7 +44,6 @@ func (repo *EvaluationCycleRepository) TransformToModel(d *domain.EvaluationCycl @@ -45,7 +44,6 @@ func (repo *EvaluationCycleRepository) TransformToModel(d *domain.EvaluationCycl
45 CompanyId: d.CompanyId, 44 CompanyId: d.CompanyId,
46 CreatorId: d.CreatorId, 45 CreatorId: d.CreatorId,
47 KpiCycle: d.KpiCycle, 46 KpiCycle: d.KpiCycle,
48 - Templates: d.Templates,  
49 CreatedAt: d.CreatedAt, 47 CreatedAt: d.CreatedAt,
50 UpdatedAt: d.UpdatedAt, 48 UpdatedAt: d.UpdatedAt,
51 DeletedAt: d.DeletedAt, 49 DeletedAt: d.DeletedAt,
@@ -114,23 +112,18 @@ func (repo *EvaluationCycleRepository) FindOne(queryOptions map[string]interface @@ -114,23 +112,18 @@ func (repo *EvaluationCycleRepository) FindOne(queryOptions map[string]interface
114 return &u, nil 112 return &u, nil
115 } 113 }
116 114
117 -func (repo *EvaluationCycleRepository) Find(queryOptions map[string]interface{}, excludeColumns ...string) (int64, []*domain.EvaluationCycle, error) { 115 +func (repo *EvaluationCycleRepository) Find(queryOptions map[string]interface{}) (int64, []*domain.EvaluationCycle, error) {
118 tx := repo.transactionContext.PgTx 116 tx := repo.transactionContext.PgTx
119 var m []*models.EvaluationCycle 117 var m []*models.EvaluationCycle
120 118
121 - // 排除掉保存模板的列[templates], 减少数据赋值  
122 - query := tx.Model(&m).Where("deleted_at isnull").Limit(20) 119 + query := tx.Model(&m).Where("deleted_at isnull")
123 120
124 - if len(excludeColumns) > 0 {  
125 - query.ExcludeColumn(excludeColumns...) 121 + if v, ok := queryOptions["name"]; ok && len(v.(string)) > 0 {
  122 + query.Where("name LIKE ?", v)
126 } 123 }
127 124
128 - if name, ok := queryOptions["name"]; ok {  
129 - query.Where("name = ?", name)  
130 - }  
131 -  
132 - if companyId, ok := queryOptions["companyId"]; ok {  
133 - query.Where("company_id = ?", companyId) 125 + if v, ok := queryOptions["companyId"]; ok {
  126 + query.Where("company_id = ?", v)
134 } 127 }
135 128
136 if v, ok := queryOptions["limit"].(int); ok { 129 if v, ok := queryOptions["limit"].(int); ok {
@@ -166,12 +159,12 @@ func (repo *EvaluationCycleRepository) Count(queryOptions map[string]interface{} @@ -166,12 +159,12 @@ func (repo *EvaluationCycleRepository) Count(queryOptions map[string]interface{}
166 query.Where("id != ?", notId) 159 query.Where("id != ?", notId)
167 } 160 }
168 161
169 - if name, ok := queryOptions["name"]; ok {  
170 - query.Where("name = ?", name) 162 + if v, ok := queryOptions["name"]; ok {
  163 + query.Where("name = ?", v)
171 } 164 }
172 165
173 - if companyId, ok := queryOptions["companyId"]; ok {  
174 - query.Where("company_id = ?", companyId) 166 + if v, ok := queryOptions["companyId"]; ok {
  167 + query.Where("company_id = ?", v)
175 } 168 }
176 169
177 count, err := query.Count() 170 count, err := query.Count()
  1 +package repository
  2 +
  3 +import (
  4 + "errors"
  5 + "fmt"
  6 + "github.com/go-pg/pg/v10"
  7 + "github.com/linmadan/egglib-go/persistent/pg/sqlbuilder"
  8 + pgTransaction "github.com/linmadan/egglib-go/transaction/pg"
  9 + "gitlab.fjmaimaimai.com/allied-creation/performance/pkg/domain"
  10 + "gitlab.fjmaimaimai.com/allied-creation/performance/pkg/infrastructure/pg/models"
  11 + "gitlab.fjmaimaimai.com/allied-creation/performance/pkg/utils"
  12 + "time"
  13 +)
  14 +
  15 +type EvaluationCycleTemplateRepository struct {
  16 + transactionContext *pgTransaction.TransactionContext
  17 +}
  18 +
  19 +func NewEvaluationCycleTemplateRepository(transactionContext *pgTransaction.TransactionContext) *EvaluationCycleTemplateRepository {
  20 + return &EvaluationCycleTemplateRepository{transactionContext: transactionContext}
  21 +}
  22 +
  23 +func (repo *EvaluationCycleTemplateRepository) TransformToDomain(m *models.EvaluationCycleTemplate) domain.EvaluationCycleTemplate {
  24 + return domain.EvaluationCycleTemplate{
  25 + Id: m.Id,
  26 + Name: m.Name,
  27 + Template: m.Template,
  28 + CycleId: m.CycleId,
  29 + CreatedAt: m.CreatedAt,
  30 + UpdatedAt: m.UpdatedAt,
  31 + DeletedAt: m.DeletedAt,
  32 + }
  33 +}
  34 +
  35 +func (repo *EvaluationCycleTemplateRepository) TransformToModel(d *domain.EvaluationCycleTemplate) models.EvaluationCycleTemplate {
  36 + return models.EvaluationCycleTemplate{
  37 + Id: d.Id,
  38 + Name: d.Name,
  39 + Template: d.Template,
  40 + CycleId: d.CycleId,
  41 + CreatedAt: d.CreatedAt,
  42 + UpdatedAt: d.UpdatedAt,
  43 + DeletedAt: d.DeletedAt,
  44 + }
  45 +}
  46 +
  47 +func (repo *EvaluationCycleTemplateRepository) nextIdentify() (int64, error) {
  48 + return utils.NewSnowflakeId()
  49 +}
  50 +
  51 +func (repo *EvaluationCycleTemplateRepository) Insert(d *domain.EvaluationCycleTemplate) (*domain.EvaluationCycleTemplate, error) {
  52 + var isCreate = d.Id == 0
  53 + if isCreate {
  54 + id, err := repo.nextIdentify()
  55 + if err != nil {
  56 + return d, err
  57 + }
  58 + d.Id = id
  59 + d.CreatedAt = time.Now()
  60 + d.UpdatedAt = d.CreatedAt
  61 + } else {
  62 + d.UpdatedAt = time.Now()
  63 + }
  64 + m := repo.TransformToModel(d)
  65 + tx := repo.transactionContext.PgTx
  66 + var err error
  67 + if isCreate {
  68 + _, err = tx.Model(&m).Returning("id").Insert()
  69 + } else {
  70 + _, err = tx.Model(&m).Returning("id").WherePK().Update() // 更新和删除必须增加条件
  71 + }
  72 + if err != nil {
  73 + return nil, err
  74 + }
  75 + d.Id = m.Id
  76 + return d, nil
  77 +}
  78 +
  79 +func (repo *EvaluationCycleTemplateRepository) Remove(d *domain.EvaluationCycleTemplate) (*domain.EvaluationCycleTemplate, error) {
  80 + tx := repo.transactionContext.PgTx
  81 + nowTime := time.Now()
  82 + m := repo.TransformToModel(d)
  83 + m.DeletedAt = &nowTime
  84 + if _, err := tx.Model(&m).WherePK().Update(); err != nil {
  85 + return d, err
  86 + }
  87 + return d, nil
  88 +}
  89 +
  90 +func (repo *EvaluationCycleTemplateRepository) BatchDeleteByCycleId(cycleId int64) error {
  91 + tx := repo.transactionContext.PgTx
  92 + _, err := tx.Model(&models.EvaluationCycleTemplate{}).Where("cycle_id = ?", cycleId).Delete()
  93 + return err
  94 +}
  95 +
  96 +func (repo *EvaluationCycleTemplateRepository) FindOne(queryOptions map[string]interface{}) (*domain.EvaluationCycleTemplate, error) {
  97 + tx := repo.transactionContext.PgTx
  98 + m := new(models.EvaluationCycleTemplate)
  99 + query := tx.Model(m)
  100 + // 包含删除项
  101 + if _, ok := queryOptions["includeDeleted"]; ok {
  102 +
  103 + } else {
  104 + query.Where("deleted_at isnull")
  105 + }
  106 + if id, ok := queryOptions["id"]; ok {
  107 + query.Where("id=?", id)
  108 + }
  109 + if err := query.First(); err != nil {
  110 + if errors.Is(err, pg.ErrNoRows) {
  111 + return nil, fmt.Errorf("没有此资源")
  112 + } else {
  113 + return nil, err
  114 + }
  115 + }
  116 + u := repo.TransformToDomain(m)
  117 + return &u, nil
  118 +}
  119 +
  120 +func (repo *EvaluationCycleTemplateRepository) Find(queryOptions map[string]interface{}, excludeColumns ...string) (int64, []*domain.EvaluationCycleTemplate, error) {
  121 + tx := repo.transactionContext.PgTx
  122 + var m []*models.EvaluationCycleTemplate
  123 +
  124 + query := tx.Model(&m)
  125 + // 包含删除项
  126 + if _, ok := queryOptions["includeDeleted"]; ok {
  127 +
  128 + } else {
  129 + query.Where("deleted_at isnull")
  130 + }
  131 +
  132 + // 排除掉保存模板的列[template], 减少数据赋值
  133 + if len(excludeColumns) > 0 {
  134 + query.ExcludeColumn(excludeColumns...)
  135 + }
  136 +
  137 + if v, ok := queryOptions["ids"]; ok {
  138 + query.Where("id in(?)", pg.In(v))
  139 + }
  140 +
  141 + if v, ok := queryOptions["id"]; ok {
  142 + query.Where("id = ?", v)
  143 + }
  144 +
  145 + if v, ok := queryOptions["cycleId"]; ok {
  146 + query.Where("cycle_id = ?", v)
  147 + }
  148 +
  149 + if v, ok := queryOptions["limit"].(int); ok {
  150 + query.Limit(v)
  151 + }
  152 + if v, ok := queryOptions["offset"].(int); ok {
  153 + query.Offset(v)
  154 + }
  155 +
  156 + count, err := query.SelectAndCount()
  157 + if err != nil {
  158 + return 0, nil, err
  159 + }
  160 + var arrays []*domain.EvaluationCycleTemplate
  161 + for _, v := range m {
  162 + d := repo.TransformToDomain(v)
  163 + arrays = append(arrays, &d)
  164 + }
  165 + return int64(count), arrays, nil
  166 +}
  167 +
  168 +func (repo *EvaluationCycleTemplateRepository) Count(queryOptions map[string]interface{}) (int64, error) {
  169 + tx := repo.transactionContext.PgTx
  170 + m := new(models.EvaluationCycleTemplate)
  171 + query := sqlbuilder.BuildQuery(tx.Model(m), queryOptions)
  172 + query.Where("deleted_at isnull")
  173 +
  174 + if v, ok := queryOptions["id"]; ok {
  175 + query.Where("id = ?", v)
  176 + }
  177 +
  178 + if v, ok := queryOptions["notId"]; ok {
  179 + query.Where("id != ?", v)
  180 + }
  181 +
  182 + if v, ok := queryOptions["name"]; ok {
  183 + query.Where("name = ?", v)
  184 + }
  185 +
  186 + count, err := query.Count()
  187 + if err != nil {
  188 + return 0, err
  189 + }
  190 + return int64(count), nil
  191 +}
@@ -119,16 +119,14 @@ func (repo *EvaluationProjectRepository) FindOne(queryOptions map[string]interfa @@ -119,16 +119,14 @@ func (repo *EvaluationProjectRepository) FindOne(queryOptions map[string]interfa
119 func (repo *EvaluationProjectRepository) Find(queryOptions map[string]interface{}) (int64, []*domain.EvaluationProject, error) { 119 func (repo *EvaluationProjectRepository) Find(queryOptions map[string]interface{}) (int64, []*domain.EvaluationProject, error) {
120 tx := repo.transactionContext.PgTx 120 tx := repo.transactionContext.PgTx
121 var m []*models.EvaluationProject 121 var m []*models.EvaluationProject
122 - query := tx.Model(&m).  
123 - Where("deleted_at isnull").  
124 - Limit(20) 122 + query := tx.Model(&m).Where("deleted_at isnull")
125 123
126 - if name, ok := queryOptions["name"]; ok {  
127 - query.Where("name = ?", name) 124 + if v, ok := queryOptions["name"]; ok {
  125 + query.Where("name = ?", v)
128 } 126 }
129 127
130 - if companyId, ok := queryOptions["companyId"]; ok {  
131 - query.Where("company_id = ?", companyId) 128 + if v, ok := queryOptions["companyId"]; ok {
  129 + query.Where("company_id = ?", v)
132 } 130 }
133 131
134 if v, ok := queryOptions["limit"].(int); ok { 132 if v, ok := queryOptions["limit"].(int); ok {
@@ -156,20 +154,20 @@ func (repo *EvaluationProjectRepository) Count(queryOptions map[string]interface @@ -156,20 +154,20 @@ func (repo *EvaluationProjectRepository) Count(queryOptions map[string]interface
156 query := sqlbuilder.BuildQuery(tx.Model(m), queryOptions) 154 query := sqlbuilder.BuildQuery(tx.Model(m), queryOptions)
157 query.Where("deleted_at isnull") 155 query.Where("deleted_at isnull")
158 156
159 - if id, ok := queryOptions["id"]; ok {  
160 - query.Where("id = ?", id) 157 + if v, ok := queryOptions["id"]; ok {
  158 + query.Where("id = ?", v)
161 } 159 }
162 160
163 - if notId, ok := queryOptions["notId"]; ok {  
164 - query.Where("id != ?", notId) 161 + if v, ok := queryOptions["notId"]; ok {
  162 + query.Where("id != ?", v)
165 } 163 }
166 164
167 - if name, ok := queryOptions["name"]; ok {  
168 - query.Where("name = ?", name) 165 + if v, ok := queryOptions["name"]; ok {
  166 + query.Where("name = ?", v)
169 } 167 }
170 168
171 - if companyId, ok := queryOptions["companyId"]; ok {  
172 - query.Where("company_id = ?", companyId) 169 + if v, ok := queryOptions["companyId"]; ok {
  170 + query.Where("company_id = ?", v)
173 } 171 }
174 172
175 count, err := query.Count() 173 count, err := query.Count()
@@ -117,18 +117,18 @@ func (repo *EvaluationRuleRepository) FindOne(queryOptions map[string]interface{ @@ -117,18 +117,18 @@ func (repo *EvaluationRuleRepository) FindOne(queryOptions map[string]interface{
117 func (repo *EvaluationRuleRepository) Find(queryOptions map[string]interface{}) (int64, []*domain.EvaluationRule, error) { 117 func (repo *EvaluationRuleRepository) Find(queryOptions map[string]interface{}) (int64, []*domain.EvaluationRule, error) {
118 tx := repo.transactionContext.PgTx 118 tx := repo.transactionContext.PgTx
119 var m []*models.EvaluationRule 119 var m []*models.EvaluationRule
120 - query := tx.Model(&m).Where("deleted_at isnull").Limit(20) 120 + query := tx.Model(&m).Where("deleted_at isnull")
121 121
122 - if nameOrRemark, ok := queryOptions["nameOrRemark"]; ok && len(nameOrRemark.(string)) > 0 {  
123 - query.Where("name LIKE ? or remark LIKE ?", nameOrRemark, nameOrRemark) 122 + if v, ok := queryOptions["nameOrRemark"]; ok && len(v.(string)) > 0 {
  123 + query.Where("name LIKE ? or remark LIKE ?", v, v)
124 } 124 }
125 125
126 - if name, ok := queryOptions["name"]; ok {  
127 - query.Where("name = ?", name) 126 + if v, ok := queryOptions["name"]; ok {
  127 + query.Where("name = ?", v)
128 } 128 }
129 129
130 - if companyId, ok := queryOptions["companyId"]; ok {  
131 - query.Where("company_id = ?", companyId) 130 + if v, ok := queryOptions["companyId"]; ok {
  131 + query.Where("company_id = ?", v)
132 } 132 }
133 133
134 if v, ok := queryOptions["limit"].(int); ok { 134 if v, ok := queryOptions["limit"].(int); ok {
@@ -138,7 +138,6 @@ func (repo *EvaluationRuleRepository) Find(queryOptions map[string]interface{}) @@ -138,7 +138,6 @@ func (repo *EvaluationRuleRepository) Find(queryOptions map[string]interface{})
138 query.Offset(v) 138 query.Offset(v)
139 } 139 }
140 140
141 - query.Count()  
142 count, err := query.SelectAndCount() 141 count, err := query.SelectAndCount()
143 if err != nil { 142 if err != nil {
144 return 0, nil, err 143 return 0, nil, err
@@ -157,20 +156,20 @@ func (repo *EvaluationRuleRepository) Count(queryOptions map[string]interface{}) @@ -157,20 +156,20 @@ func (repo *EvaluationRuleRepository) Count(queryOptions map[string]interface{})
157 query := sqlbuilder.BuildQuery(tx.Model(m), queryOptions) 156 query := sqlbuilder.BuildQuery(tx.Model(m), queryOptions)
158 query.Where("deleted_at isnull") 157 query.Where("deleted_at isnull")
159 158
160 - if id, ok := queryOptions["id"]; ok {  
161 - query.Where("id = ?", id) 159 + if v, ok := queryOptions["id"]; ok {
  160 + query.Where("id = ?", v)
162 } 161 }
163 162
164 - if notId, ok := queryOptions["notId"]; ok {  
165 - query.Where("id != ?", notId) 163 + if v, ok := queryOptions["notId"]; ok {
  164 + query.Where("id != ?", v)
166 } 165 }
167 166
168 - if name, ok := queryOptions["name"]; ok {  
169 - query.Where("name = ?", name) 167 + if v, ok := queryOptions["name"]; ok {
  168 + query.Where("name = ?", v)
170 } 169 }
171 170
172 - if companyId, ok := queryOptions["companyId"]; ok {  
173 - query.Where("company_id = ?", companyId) 171 + if v, ok := queryOptions["companyId"]; ok {
  172 + query.Where("company_id = ?", v)
174 } 173 }
175 174
176 count, err := query.Count() 175 count, err := query.Count()
@@ -115,9 +115,7 @@ func (repo *EvaluationTemplateRepository) FindOne(queryOptions map[string]interf @@ -115,9 +115,7 @@ func (repo *EvaluationTemplateRepository) FindOne(queryOptions map[string]interf
115 func (repo *EvaluationTemplateRepository) Find(queryOptions map[string]interface{}, excludeColumns ...string) (int64, []*domain.EvaluationTemplate, error) { 115 func (repo *EvaluationTemplateRepository) Find(queryOptions map[string]interface{}, excludeColumns ...string) (int64, []*domain.EvaluationTemplate, error) {
116 tx := repo.transactionContext.PgTx 116 tx := repo.transactionContext.PgTx
117 var m []*models.EvaluationTemplate 117 var m []*models.EvaluationTemplate
118 - query := tx.Model(&m).  
119 - Where("deleted_at isnull").  
120 - Limit(20) 118 + query := tx.Model(&m).Where("deleted_at isnull")
121 119
122 if len(excludeColumns) > 0 { 120 if len(excludeColumns) > 0 {
123 query.ExcludeColumn(excludeColumns...) 121 query.ExcludeColumn(excludeColumns...)
@@ -127,16 +125,16 @@ func (repo *EvaluationTemplateRepository) Find(queryOptions map[string]interface @@ -127,16 +125,16 @@ func (repo *EvaluationTemplateRepository) Find(queryOptions map[string]interface
127 query.Where("id in(?)", pg.In(v)) 125 query.Where("id in(?)", pg.In(v))
128 } 126 }
129 127
130 - if name, ok := queryOptions["name"]; ok && len(name.(string)) > 0 {  
131 - query.Where("name LIKE ?", name) 128 + if v, ok := queryOptions["name"]; ok && len(v.(string)) > 0 {
  129 + query.Where("name LIKE ?", v)
132 } 130 }
133 131
134 - if companyId, ok := queryOptions["companyId"]; ok {  
135 - query.Where("company_id = ?", companyId) 132 + if v, ok := queryOptions["companyId"]; ok {
  133 + query.Where("company_id = ?", v)
136 } 134 }
137 135
138 - if state, ok := queryOptions["state"]; ok {  
139 - query.Where("state = ?", state) 136 + if v, ok := queryOptions["state"]; ok {
  137 + query.Where("state = ?", v)
140 } 138 }
141 139
142 if v, ok := queryOptions["limit"].(int); ok { 140 if v, ok := queryOptions["limit"].(int); ok {
@@ -164,24 +162,24 @@ func (repo *EvaluationTemplateRepository) Count(queryOptions map[string]interfac @@ -164,24 +162,24 @@ func (repo *EvaluationTemplateRepository) Count(queryOptions map[string]interfac
164 query := sqlbuilder.BuildQuery(tx.Model(m), queryOptions) 162 query := sqlbuilder.BuildQuery(tx.Model(m), queryOptions)
165 query.Where("deleted_at isnull") 163 query.Where("deleted_at isnull")
166 164
167 - if id, ok := queryOptions["id"]; ok {  
168 - query.Where("id = ?", id) 165 + if v, ok := queryOptions["id"]; ok {
  166 + query.Where("id = ?", v)
169 } 167 }
170 168
171 - if notId, ok := queryOptions["notId"]; ok {  
172 - query.Where("id != ?", notId) 169 + if v, ok := queryOptions["notId"]; ok {
  170 + query.Where("id != ?", v)
173 } 171 }
174 172
175 - if name, ok := queryOptions["name"]; ok {  
176 - query.Where("name = ?", name) 173 + if v, ok := queryOptions["name"]; ok {
  174 + query.Where("name = ?", v)
177 } 175 }
178 176
179 - if companyId, ok := queryOptions["companyId"]; ok {  
180 - query.Where("company_id = ?", companyId) 177 + if v, ok := queryOptions["companyId"]; ok {
  178 + query.Where("company_id = ?", v)
181 } 179 }
182 180
183 - if state, ok := queryOptions["state"]; ok {  
184 - query.Where("state = ?", state) 181 + if v, ok := queryOptions["state"]; ok {
  182 + query.Where("state = ?", v)
185 } 183 }
186 184
187 count, err := query.Count() 185 count, err := query.Count()
@@ -111,9 +111,7 @@ func (repo *RoleRepository) FindOne(queryOptions map[string]interface{}) (*domai @@ -111,9 +111,7 @@ func (repo *RoleRepository) FindOne(queryOptions map[string]interface{}) (*domai
111 func (repo *RoleRepository) Find(queryOptions map[string]interface{}) (int64, []*domain.Role, error) { 111 func (repo *RoleRepository) Find(queryOptions map[string]interface{}) (int64, []*domain.Role, error) {
112 tx := repo.transactionContext.PgTx 112 tx := repo.transactionContext.PgTx
113 var m []*models.Role 113 var m []*models.Role
114 - query := tx.Model(&m).  
115 - Where("deleted_at isnull").  
116 - Limit(20) 114 + query := tx.Model(&m).Where("deleted_at isnull")
117 115
118 if name, ok := queryOptions["name"]; ok { 116 if name, ok := queryOptions["name"]; ok {
119 query.Where("name = ?", name) 117 query.Where("name = ?", name)