作者 tangxvhui

修复一个查询错误

@@ -5,6 +5,7 @@ import ( @@ -5,6 +5,7 @@ import (
5 "strconv" 5 "strconv"
6 "time" 6 "time"
7 7
  8 + "github.com/linmadan/egglib-go/core/application"
8 "gitlab.fjmaimaimai.com/allied-creation/performance/pkg/application/factory" 9 "gitlab.fjmaimaimai.com/allied-creation/performance/pkg/application/factory"
9 "gitlab.fjmaimaimai.com/allied-creation/performance/pkg/domain" 10 "gitlab.fjmaimaimai.com/allied-creation/performance/pkg/domain"
10 "gitlab.fjmaimaimai.com/allied-creation/performance/pkg/infrastructure/dao" 11 "gitlab.fjmaimaimai.com/allied-creation/performance/pkg/infrastructure/dao"
@@ -18,26 +19,24 @@ func TaskSendSummaryEvaluationV2() error { @@ -18,26 +19,24 @@ func TaskSendSummaryEvaluationV2() error {
18 str := fmt.Sprintf("下发周期评估耗时%.2f s", time.Since(nowTime).Seconds()) 19 str := fmt.Sprintf("下发周期评估耗时%.2f s", time.Since(nowTime).Seconds())
19 log.Logger.Info(str) 20 log.Logger.Info(str)
20 }() 21 }()
21 - var newPublisher summaryEvaluationPublisher  
22 for { 22 for {
23 - projectList, err := getPrepareSummaryEvaluation() 23 + cycleList, err := getPrepareEvaluationCycle()
24 if err != nil { 24 if err != nil {
25 return err 25 return err
26 } 26 }
27 - if len(projectList) == 0 { 27 + if len(cycleList) == 0 {
28 break 28 break
29 } 29 }
30 - newPublisher = summaryEvaluationPublisher{}  
31 - for _, val := range projectList {  
32 - err = newPublisher.sendSummaryEvaluationV2(val) 30 + err = sendSummaryEvaluationByCycle(cycleList[0])
  31 + if err != nil {
33 return err 32 return err
34 } 33 }
35 } 34 }
36 return nil 35 return nil
37 } 36 }
38 37
39 -func getPrepareEvaluationCycle() {}  
40 -func getPrepareSummaryEvaluation() ([]*domain.EvaluationProject, error) { 38 +// 获取周期
  39 +func getPrepareEvaluationCycle() ([]*domain.EvaluationCycle, error) {
41 transactionContext, err := factory.CreateTransactionContext(nil) 40 transactionContext, err := factory.CreateTransactionContext(nil)
42 if err != nil { 41 if err != nil {
43 return nil, err 42 return nil, err
@@ -48,56 +47,131 @@ func getPrepareSummaryEvaluation() ([]*domain.EvaluationProject, error) { @@ -48,56 +47,131 @@ func getPrepareSummaryEvaluation() ([]*domain.EvaluationProject, error) {
48 defer func() { 47 defer func() {
49 _ = transactionContext.RollbackTransaction() 48 _ = transactionContext.RollbackTransaction()
50 }() 49 }()
  50 + cycleRepo := factory.CreateEvaluationCycleRepository(map[string]interface{}{"transactionContext": transactionContext})
  51 + cycleList, err := cycleRepo.FindCycleEnd(1)
  52 + if err != nil {
  53 + return nil, fmt.Errorf("获取可用的周期数据,%s", err)
  54 + }
  55 + if err := transactionContext.CommitTransaction(); err != nil {
  56 + return nil, err
  57 + }
  58 + return cycleList, nil
  59 +}
  60 +
  61 +// 获取可用的项目
  62 +// func getPrepareSummaryEvaluation(cycleId int) ([]*domain.EvaluationProject, error) {
  63 +// transactionContext, err := factory.CreateTransactionContext(nil)
  64 +// if err != nil {
  65 +// return nil, err
  66 +// }
  67 +// if err := transactionContext.StartTransaction(); err != nil {
  68 +// return nil, err
  69 +// }
  70 +// defer func() {
  71 +// _ = transactionContext.RollbackTransaction()
  72 +// }()
  73 +// projectRepo := factory.CreateEvaluationProjectRepository(map[string]interface{}{
  74 +// "transactionContext": transactionContext,
  75 +// })
  76 +// // 获取项目数据总数
  77 +// _, projectList, err := projectRepo.Find(map[string]interface{}{
  78 +// "cycleId": cycleId,
  79 +// "summaryState": domain.ProjectSummaryStateNo,
  80 +// "state": domain.ProjectStateEnable,
  81 +// "limit": 200,
  82 +// }, "template")
  83 +// if err != nil {
  84 +// return nil, fmt.Errorf("获取可用的项目数据,%s", err)
  85 +// }
  86 +// if err := transactionContext.CommitTransaction(); err != nil {
  87 +// return nil, err
  88 +// }
  89 +// return projectList, nil
  90 +// }
  91 +
  92 +// 按周期下发 综合评估任务
  93 +func sendSummaryEvaluationByCycle(cycleParam *domain.EvaluationCycle) error {
  94 + transactionContext, err := factory.CreateTransactionContext(nil)
  95 + if err != nil {
  96 + return err
  97 + }
  98 + if err := transactionContext.StartTransaction(); err != nil {
  99 + return err
  100 + }
  101 + defer func() {
  102 + _ = transactionContext.RollbackTransaction()
  103 + }()
  104 +
51 projectRepo := factory.CreateEvaluationProjectRepository(map[string]interface{}{ 105 projectRepo := factory.CreateEvaluationProjectRepository(map[string]interface{}{
52 "transactionContext": transactionContext, 106 "transactionContext": transactionContext,
53 }) 107 })
54 // 获取项目数据总数 108 // 获取项目数据总数
55 _, projectList, err := projectRepo.Find(map[string]interface{}{ 109 _, projectList, err := projectRepo.Find(map[string]interface{}{
56 - "endTime": time.Now(), 110 + "cycleId": cycleParam.Id,
57 "summaryState": domain.ProjectSummaryStateNo, 111 "summaryState": domain.ProjectSummaryStateNo,
58 "state": domain.ProjectStateEnable, 112 "state": domain.ProjectStateEnable,
59 - "limit": 200, 113 + "limit": 500,
60 }, "template") 114 }, "template")
61 if err != nil { 115 if err != nil {
62 - return nil, fmt.Errorf("获取可用的项目数据,%s", err) 116 + return fmt.Errorf("获取可用的项目数据,%s", err)
  117 + }
  118 + var newEvaluationList []domain.SummaryEvaluation
  119 + newPublisher := summaryEvaluationPublisher{}
  120 + for _, val := range projectList {
  121 + evaluationList, err := newPublisher.sendSummaryEvaluationV2(transactionContext, val, cycleParam)
  122 + if err != nil {
  123 + return fmt.Errorf("按项目下发综合评估任务数据,%s", err)
  124 + }
  125 + newEvaluationList = append(newEvaluationList, evaluationList...)
  126 + }
  127 + // 回填周期的状态
  128 + cycleDao := dao.NewEvaluationCycleDao(map[string]interface{}{"transactionContext": transactionContext})
  129 + err = cycleDao.UpdateSummaryState(cycleParam.Id, domain.ProjectSummaryStateYes)
  130 + if err != nil {
  131 + return fmt.Errorf("保存项目周期状态%s", err)
63 } 132 }
64 if err := transactionContext.CommitTransaction(); err != nil { 133 if err := transactionContext.CommitTransaction(); err != nil {
65 - return nil, err 134 + return err
  135 + }
  136 + err = sendSmsEvalation(newEvaluationList)
  137 + if err != nil {
  138 + return fmt.Errorf("设置短信消息%s", err)
66 } 139 }
67 - return projectList, nil 140 + return nil
68 } 141 }
69 142
70 // 下发周期综合评估 143 // 下发周期综合评估
71 type summaryEvaluationPublisher struct { 144 type summaryEvaluationPublisher struct {
72 userCache map[int64]*domain.User 145 userCache map[int64]*domain.User
73 departCache map[int]*domain.Department 146 departCache map[int]*domain.Department
74 - cycleCache map[int64]*domain.EvaluationCycle  
75 } 147 }
76 148
77 -func (se *summaryEvaluationPublisher) sendSummaryEvaluationV2(projectParam *domain.EvaluationProject) error {  
78 - transactionContext, err := factory.CreateTransactionContext(nil)  
79 - if err != nil {  
80 - return err  
81 - }  
82 - if err := transactionContext.StartTransaction(); err != nil {  
83 - return err  
84 - }  
85 - defer func() {  
86 - _ = transactionContext.RollbackTransaction()  
87 - }() 149 +func (se *summaryEvaluationPublisher) sendSummaryEvaluationV2(
  150 + transactionContext application.TransactionContext,
  151 + projectParam *domain.EvaluationProject, cycleData *domain.EvaluationCycle,
  152 +) ([]domain.SummaryEvaluation, error) {
  153 + // transactionContext, err := factory.CreateTransactionContext(nil)
  154 + // if err != nil {
  155 + // return err
  156 + // }
  157 + // if err := transactionContext.StartTransaction(); err != nil {
  158 + // return err
  159 + // }
  160 + // defer func() {
  161 + // _ = transactionContext.RollbackTransaction()
  162 + // }()
88 userRepo := factory.CreateUserRepository(map[string]interface{}{"transactionContext": transactionContext}) 163 userRepo := factory.CreateUserRepository(map[string]interface{}{"transactionContext": transactionContext})
89 departmentRepo := factory.CreateDepartmentRepository(map[string]interface{}{"transactionContext": transactionContext}) 164 departmentRepo := factory.CreateDepartmentRepository(map[string]interface{}{"transactionContext": transactionContext})
90 - cycleRepo := factory.CreateEvaluationCycleRepository(map[string]interface{}{"transactionContext": transactionContext})  
91 evaluationItemRepo := factory.CreateEvaluationItemUsedRepository(map[string]interface{}{"transactionContext": transactionContext}) 165 evaluationItemRepo := factory.CreateEvaluationItemUsedRepository(map[string]interface{}{"transactionContext": transactionContext})
92 _, evaluationItemList, err := evaluationItemRepo.Find(map[string]interface{}{ 166 _, evaluationItemList, err := evaluationItemRepo.Find(map[string]interface{}{
93 "evaluationProjectId": projectParam.Id, 167 "evaluationProjectId": projectParam.Id,
94 "nodeType": int(domain.LinkNodeSelfAssessment), 168 "nodeType": int(domain.LinkNodeSelfAssessment),
95 }) 169 })
96 if err != nil { 170 if err != nil {
97 - return err 171 + return nil, err
98 } 172 }
99 if len(evaluationItemList) == 0 { 173 if len(evaluationItemList) == 0 {
100 - return nil 174 + return nil, nil
101 } 175 }
102 nodeId := evaluationItemList[0].NodeId 176 nodeId := evaluationItemList[0].NodeId
103 executor360Map := map[int64]*domain.User{} 177 executor360Map := map[int64]*domain.User{}
@@ -112,20 +186,12 @@ func (se *summaryEvaluationPublisher) sendSummaryEvaluationV2(projectParam *doma @@ -112,20 +186,12 @@ func (se *summaryEvaluationPublisher) sendSummaryEvaluationV2(projectParam *doma
112 } 186 }
113 user360, err := se.getUserData(userRepo, int64(v.EvaluatorId)) 187 user360, err := se.getUserData(userRepo, int64(v.EvaluatorId))
114 if err != nil { 188 if err != nil {
115 - return fmt.Errorf("获取360用户%s", err) 189 + return nil, fmt.Errorf("获取360用户%s", err)
116 } 190 }
117 executor360Map[user360.Id] = user360 191 executor360Map[user360.Id] = user360
118 } 192 }
119 - //获取周期  
120 - cycleData, err := se.getCycleData(cycleRepo, projectParam.CycleId)  
121 - if err != nil {  
122 - return err  
123 - }  
124 - if cycleData == nil {  
125 - return nil  
126 - }  
127 if cycleData.TimeEnd == nil { 193 if cycleData.TimeEnd == nil {
128 - return fmt.Errorf("周期%d:%s 结束时间错误", cycleData.Id, cycleData.Name) 194 + return nil, fmt.Errorf("周期%d:%s 结束时间错误", cycleData.Id, cycleData.Name)
129 } 195 }
130 //自评的时间范围 196 //自评的时间范围
131 beginTimeSelf := *cycleData.TimeEnd 197 beginTimeSelf := *cycleData.TimeEnd
@@ -167,14 +233,14 @@ func (se *summaryEvaluationPublisher) sendSummaryEvaluationV2(projectParam *doma @@ -167,14 +233,14 @@ func (se *summaryEvaluationPublisher) sendSummaryEvaluationV2(projectParam *doma
167 } 233 }
168 targetUser, err := se.getUserData(userRepo, targetUserId) 234 targetUser, err := se.getUserData(userRepo, targetUserId)
169 if err != nil { 235 if err != nil {
170 - return fmt.Errorf("获取员工数据%s", err) 236 + return nil, fmt.Errorf("获取员工数据%s", err)
171 } 237 }
172 if targetUser == nil { 238 if targetUser == nil {
173 continue 239 continue
174 } 240 }
175 targetUserDepartment, err := se.getDepartmentData(departmentRepo, targetUser.DepartmentId) 241 targetUserDepartment, err := se.getDepartmentData(departmentRepo, targetUser.DepartmentId)
176 if err != nil { 242 if err != nil {
177 - return fmt.Errorf("获取员工的部门数据%s", err) 243 + return nil, fmt.Errorf("获取员工的部门数据%s", err)
178 } 244 }
179 evaluationTemp.TargetDepartment = []domain.StaffDepartment{} 245 evaluationTemp.TargetDepartment = []domain.StaffDepartment{}
180 evaluationTemp.Types = domain.EvaluationSelf 246 evaluationTemp.Types = domain.EvaluationSelf
@@ -255,39 +321,38 @@ func (se *summaryEvaluationPublisher) sendSummaryEvaluationV2(projectParam *doma @@ -255,39 +321,38 @@ func (se *summaryEvaluationPublisher) sendSummaryEvaluationV2(projectParam *doma
255 for i := range newEvaluationList { 321 for i := range newEvaluationList {
256 err = summaryEvaluationRepo.Save(&newEvaluationList[i]) 322 err = summaryEvaluationRepo.Save(&newEvaluationList[i])
257 if err != nil { 323 if err != nil {
258 - return fmt.Errorf("保存周期综合评估%s", err) 324 + return nil, fmt.Errorf("保存周期综合评估%s", err)
259 } 325 }
260 } 326 }
261 //回填项目的状态 327 //回填项目的状态
262 projectDao := dao.NewEvaluationProjectDao(map[string]interface{}{"transactionContext": transactionContext}) 328 projectDao := dao.NewEvaluationProjectDao(map[string]interface{}{"transactionContext": transactionContext})
263 err = projectDao.UpdateSummaryState(projectParam.Id, domain.ProjectSummaryStateYes) 329 err = projectDao.UpdateSummaryState(projectParam.Id, domain.ProjectSummaryStateYes)
264 if err != nil { 330 if err != nil {
265 - return fmt.Errorf("保存项目状态%s", err)  
266 - }  
267 - if err := transactionContext.CommitTransaction(); err != nil {  
268 - return err 331 + return nil, fmt.Errorf("保存项目状态%s", err)
269 } 332 }
270 - err = sendSmsEvalation(newEvaluationList)  
271 - return fmt.Errorf("设置短信发送%s", err) 333 + // if err := transactionContext.CommitTransaction(); err != nil {
  334 + // return err
  335 + // }
  336 + return newEvaluationList, fmt.Errorf("设置短信发送%s", err)
272 } 337 }
273 338
274 // 获取周期设置数据 339 // 获取周期设置数据
275 -func (se *summaryEvaluationPublisher) getCycleData(cycleRepo domain.EvaluationCycleRepository, cycleId int64) (*domain.EvaluationCycle, error) {  
276 - var cycleData *domain.EvaluationCycle  
277 - if val, ok := se.cycleCache[cycleId]; ok {  
278 - cycleData = val  
279 - } else {  
280 - _, cycleList, err := cycleRepo.Find(map[string]interface{}{"id": cycleId})  
281 - if err != nil {  
282 - return nil, err  
283 - }  
284 - if len(cycleList) == 0 {  
285 - return nil, nil  
286 - }  
287 - cycleData = cycleList[0]  
288 - }  
289 - return cycleData, nil  
290 -} 340 +// func (se *summaryEvaluationPublisher) getCycleData(cycleRepo domain.EvaluationCycleRepository, cycleId int64) (*domain.EvaluationCycle, error) {
  341 +// var cycleData *domain.EvaluationCycle
  342 +// if val, ok := se.cycleCache[cycleId]; ok {
  343 +// cycleData = val
  344 +// } else {
  345 +// _, cycleList, err := cycleRepo.Find(map[string]interface{}{"id": cycleId})
  346 +// if err != nil {
  347 +// return nil, err
  348 +// }
  349 +// if len(cycleList) == 0 {
  350 +// return nil, nil
  351 +// }
  352 +// cycleData = cycleList[0]
  353 +// }
  354 +// return cycleData, nil
  355 +// }
291 356
292 // 获取用户数据 357 // 获取用户数据
293 func (se *summaryEvaluationPublisher) getUserData(userRepo domain.UserRepository, userId int64) (*domain.User, error) { 358 func (se *summaryEvaluationPublisher) getUserData(userRepo domain.UserRepository, userId int64) (*domain.User, error) {
@@ -40,4 +40,5 @@ type EvaluationCycleRepository interface { @@ -40,4 +40,5 @@ type EvaluationCycleRepository interface {
40 FindOne(queryOptions map[string]interface{}) (*EvaluationCycle, error) 40 FindOne(queryOptions map[string]interface{}) (*EvaluationCycle, error)
41 Find(queryOptions map[string]interface{}) (int64, []*EvaluationCycle, error) 41 Find(queryOptions map[string]interface{}) (int64, []*EvaluationCycle, error)
42 Count(queryOptions map[string]interface{}) (int64, error) 42 Count(queryOptions map[string]interface{}) (int64, error)
  43 + FindCycleEnd(limit int) ([]*EvaluationCycle, error) // 获取已结束的周期,且还没下发周期评估
43 } 44 }
  1 +package dao
  2 +
  3 +import (
  4 + pgTransaction "github.com/linmadan/egglib-go/transaction/pg"
  5 + "gitlab.fjmaimaimai.com/allied-creation/performance/pkg/domain"
  6 + "gitlab.fjmaimaimai.com/allied-creation/performance/pkg/infrastructure/pg/models"
  7 +)
  8 +
  9 +type EvaluationCycleDao struct {
  10 + transactionContext *pgTransaction.TransactionContext
  11 +}
  12 +
  13 +func NewEvaluationCycleDao(options map[string]interface{}) *EvaluationCycleDao {
  14 + var transactionContext *pgTransaction.TransactionContext
  15 + if value, ok := options["transactionContext"]; ok {
  16 + transactionContext = value.(*pgTransaction.TransactionContext)
  17 + }
  18 + return &EvaluationCycleDao{
  19 + transactionContext: transactionContext,
  20 + }
  21 +}
  22 +
  23 +func (d *EvaluationCycleDao) UpdateSummaryState(id int64, status domain.ProjectSummaryState) error {
  24 + db := d.transactionContext.PgTx
  25 + _, err := db.Model(&models.EvaluationCycle{}).
  26 + Where("id=?", id).
  27 + Set("summary_state=?", int(status)).
  28 + Update()
  29 + return err
  30 +}
@@ -1253,7 +1253,7 @@ group by level_value,category,"name" ` @@ -1253,7 +1253,7 @@ group by level_value,category,"name" `
1253 1253
1254 var result []AssessContentLevelCode 1254 var result []AssessContentLevelCode
1255 condition := []interface{}{ 1255 condition := []interface{}{
1256 - projectId, targetUserId, string(assessType), cycleId, 1256 + targetUserId, string(assessType), cycleId, projectId,
1257 } 1257 }
1258 tx := d.transactionContext.PgTx 1258 tx := d.transactionContext.PgTx
1259 _, err := tx.Query(&result, sqlStr, condition...) 1259 _, err := tx.Query(&result, sqlStr, condition...)
@@ -197,3 +197,25 @@ func (repo *EvaluationCycleRepository) Count(queryOptions map[string]interface{} @@ -197,3 +197,25 @@ func (repo *EvaluationCycleRepository) Count(queryOptions map[string]interface{}
197 } 197 }
198 return int64(count), nil 198 return int64(count), nil
199 } 199 }
  200 +
  201 +// 获取已结束的周期
  202 +func (repo *EvaluationCycleRepository) FindCycleEnd(limit int) ([]*domain.EvaluationCycle, error) {
  203 + tx := repo.transactionContext.PgTx
  204 + var m []*models.EvaluationCycle
  205 + query := tx.Model(&m).
  206 + Where("deleted_at isnull").
  207 + Where("time_end<=?", time.Now()).
  208 + Where("summary_state=0").
  209 + Limit(limit)
  210 +
  211 + err := query.Select()
  212 + if err != nil {
  213 + return nil, err
  214 + }
  215 + var arrays []*domain.EvaluationCycle
  216 + for _, v := range m {
  217 + d := repo.TransformToDomain(v)
  218 + arrays = append(arrays, &d)
  219 + }
  220 + return arrays, nil
  221 +}
  1 +-- 添加summary_cycle 表字段
  2 +ALTER TABLE public.evaluation_cycle
  3 + ADD summary_state int4 NOT NULL DEFAULT 0;
  4 +
  5 +-- 初始化数据的值
  6 +UPDATE
  7 + public.evaluation_cycle
  8 +SET
  9 + summary_state = 1
  10 +WHERE
  11 + time_end <= now()
  12 + AND summary_state = 0;
  13 +