作者 tangxvhui

调整周期评估的下发

@@ -56,7 +56,7 @@ func startSummaryEvaluation() { @@ -56,7 +56,7 @@ func startSummaryEvaluation() {
56 timer := time.NewTimer(duration) 56 timer := time.NewTimer(duration)
57 for { 57 for {
58 <-timer.C 58 <-timer.C
59 - if err := serviceSummary.TaskSendSummaryEvaluation(); err != nil { 59 + if err := serviceSummary.TaskSendSummaryEvaluationV2(); err != nil {
60 log.Logger.Error(err.Error()) 60 log.Logger.Error(err.Error())
61 } 61 }
62 timer.Reset(duration) // 重置定时 62 timer.Reset(duration) // 重置定时
@@ -138,7 +138,6 @@ func sendSummaryEvaluation(project *domain.EvaluationProject, @@ -138,7 +138,6 @@ func sendSummaryEvaluation(project *domain.EvaluationProject,
138 DepartmentName: depart.Name, 138 DepartmentName: depart.Name,
139 }) 139 })
140 } 140 }
141 - //确定自评  
142 newEvaluationList = append(newEvaluationList, evaluationTemp) 141 newEvaluationList = append(newEvaluationList, evaluationTemp)
143 if hrbpExist { 142 if hrbpExist {
144 //处理人资评估 143 //处理人资评估
@@ -146,7 +145,6 @@ func sendSummaryEvaluation(project *domain.EvaluationProject, @@ -146,7 +145,6 @@ func sendSummaryEvaluation(project *domain.EvaluationProject,
146 evaluationTemp.EndTime = endTime360 145 evaluationTemp.EndTime = endTime360
147 evaluationTemp.Executor = domain.StaffDesc{} 146 evaluationTemp.Executor = domain.StaffDesc{}
148 evaluationTemp.Types = domain.EvaluationHrbp 147 evaluationTemp.Types = domain.EvaluationHrbp
149 - //确定人资评估  
150 newEvaluationList = append(newEvaluationList, evaluationTemp) 148 newEvaluationList = append(newEvaluationList, evaluationTemp)
151 } 149 }
152 //处理360 评估 150 //处理360 评估
@@ -159,7 +157,6 @@ func sendSummaryEvaluation(project *domain.EvaluationProject, @@ -159,7 +157,6 @@ func sendSummaryEvaluation(project *domain.EvaluationProject,
159 UserName: v2.Name, 157 UserName: v2.Name,
160 } 158 }
161 evaluationTemp.Types = domain.Evaluation360 159 evaluationTemp.Types = domain.Evaluation360
162 - //确定人资评估  
163 newEvaluationList = append(newEvaluationList, evaluationTemp) 160 newEvaluationList = append(newEvaluationList, evaluationTemp)
164 } 161 }
165 162
  1 +package service
  2 +
  3 +import (
  4 + "fmt"
  5 + "strconv"
  6 + "time"
  7 +
  8 + "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/infrastructure/dao"
  11 + "gitlab.fjmaimaimai.com/allied-creation/performance/pkg/log"
  12 +)
  13 +
  14 +// 定时下发周期评估任务
  15 +func TaskSendSummaryEvaluationV2() error {
  16 + nowTime := time.Now()
  17 + defer func() {
  18 + str := fmt.Sprintf("下发周期评估耗时%.2f s", time.Since(nowTime).Seconds())
  19 + log.Logger.Info(str)
  20 + }()
  21 + var newPublisher summaryEvaluationPublisher
  22 + for {
  23 + projectList, err := sendSummaryEvaluationV2()
  24 + if err != nil {
  25 + return err
  26 + }
  27 + if len(projectList) == 0 {
  28 + break
  29 + }
  30 + newPublisher = summaryEvaluationPublisher{}
  31 + for _, val := range projectList {
  32 + err = newPublisher.sendSummaryEvaluationV2(val)
  33 + return err
  34 + }
  35 + }
  36 + return nil
  37 +}
  38 +
  39 +func sendSummaryEvaluationV2() ([]*domain.EvaluationProject, error) {
  40 + transactionContext, err := factory.CreateTransactionContext(nil)
  41 + if err != nil {
  42 + return nil, err
  43 + }
  44 + if err := transactionContext.StartTransaction(); err != nil {
  45 + return nil, err
  46 + }
  47 + defer func() {
  48 + _ = transactionContext.RollbackTransaction()
  49 + }()
  50 + projectRepo := factory.CreateEvaluationProjectRepository(map[string]interface{}{
  51 + "transactionContext": transactionContext,
  52 + })
  53 + // 获取项目数据总数
  54 + _, projectList, err := projectRepo.Find(map[string]interface{}{
  55 + "endTime": time.Now(),
  56 + "summaryState": domain.ProjectSummaryStateNo,
  57 + "state": domain.ProjectStateEnable,
  58 + "limit": 500,
  59 + }, "template")
  60 + if err != nil {
  61 + return nil, fmt.Errorf("获取可用的项目数据,%s", err)
  62 + }
  63 + if err := transactionContext.CommitTransaction(); err != nil {
  64 + return nil, err
  65 + }
  66 + return projectList, nil
  67 +}
  68 +
  69 +// 下发周期综合评估
  70 +type summaryEvaluationPublisher struct {
  71 + userCache map[int64]*domain.User
  72 + departCache map[int]*domain.Department
  73 + cycleCache map[int64]*domain.EvaluationCycle
  74 +}
  75 +
  76 +func (se *summaryEvaluationPublisher) sendSummaryEvaluationV2(projectParam *domain.EvaluationProject) error {
  77 + transactionContext, err := factory.CreateTransactionContext(nil)
  78 + if err != nil {
  79 + return err
  80 + }
  81 + if err := transactionContext.StartTransaction(); err != nil {
  82 + return err
  83 + }
  84 + defer func() {
  85 + _ = transactionContext.RollbackTransaction()
  86 + }()
  87 + userRepo := factory.CreateUserRepository(map[string]interface{}{"transactionContext": transactionContext})
  88 + departmentRepo := factory.CreateDepartmentRepository(map[string]interface{}{"transactionContext": transactionContext})
  89 + cycleRepo := factory.CreateEvaluationCycleRepository(map[string]interface{}{"transactionContext": transactionContext})
  90 + evaluationItemRepo := factory.CreateEvaluationItemUsedRepository(map[string]interface{}{"transactionContext": transactionContext})
  91 + _, evaluationItemList, err := evaluationItemRepo.Find(map[string]interface{}{
  92 + "evaluationProjectId": projectParam.Id,
  93 + "nodeType": int(domain.LinkNodeSelfAssessment),
  94 + })
  95 + if err != nil {
  96 + return err
  97 + }
  98 + if len(evaluationItemList) == 0 {
  99 + return nil
  100 + }
  101 + nodeId := evaluationItemList[0].NodeId
  102 + executor360Map := map[int64]*domain.User{}
  103 + hrbpExist := false
  104 + for _, v := range evaluationItemList {
  105 + nodeId = v.NodeId
  106 + if v.EvaluatorId < 0 {
  107 + hrbpExist = true
  108 + }
  109 + if v.EvaluatorId <= 0 {
  110 + continue
  111 + }
  112 + user360, err := se.getUserData(userRepo, int64(v.EvaluatorId))
  113 + if err != nil {
  114 + return fmt.Errorf("获取360用户%s", err)
  115 + }
  116 + executor360Map[user360.Id] = user360
  117 + }
  118 + //获取周期
  119 + cycleData, err := se.getCycleData(cycleRepo, projectParam.CycleId)
  120 + if err != nil {
  121 + return err
  122 + }
  123 + if cycleData == nil {
  124 + return nil
  125 + }
  126 + if cycleData.TimeEnd == nil {
  127 + return fmt.Errorf("周期%d:%s 结束时间错误", cycleData.Id, cycleData.Name)
  128 + }
  129 + //自评的时间范围
  130 + beginTimeSelf := *cycleData.TimeEnd
  131 + endTimeSelf := dayZeroTime(beginTimeSelf).Add(3*24*time.Hour - time.Second)
  132 + //人资、360评估的时间范围
  133 + beginTime360 := endTimeSelf
  134 + endTime360 := endTimeSelf.Add(2 * 24 * time.Hour)
  135 + //上级评估的是时间范围
  136 + beginTimeSuper := endTime360
  137 + endTimeSuper := endTime360.Add(2 * 24 * time.Hour)
  138 + // 创建周期评估任务
  139 + var newEvaluationList []domain.SummaryEvaluation
  140 + evaluationTemp := domain.SummaryEvaluation{
  141 + Id: 0,
  142 + CompanyId: int(projectParam.CompanyId),
  143 + EvaluationProjectId: int(projectParam.Id),
  144 + EvaluationProjectName: projectParam.Name,
  145 + CycleId: cycleData.Id,
  146 + CycleName: cycleData.Name,
  147 + NodeId: nodeId,
  148 + TargetUser: domain.StaffDesc{}, //待填充
  149 + TargetDepartment: []domain.StaffDepartment{}, //待填充
  150 + Executor: domain.StaffDesc{},
  151 + Types: 0, //待填充
  152 + Status: domain.EvaluationUncompleted,
  153 + CheckResult: domain.EvaluationCheckUncompleted,
  154 + BeginTime: time.Time{}, //待填充
  155 + EndTime: time.Time{}, //待填充
  156 + TotalScore: "",
  157 + CreatedAt: time.Now(),
  158 + UpdatedAt: time.Now(),
  159 + DeletedAt: nil,
  160 + }
  161 +
  162 + for _, val := range projectParam.Recipients {
  163 + targetUserId, _ := strconv.ParseInt(val, 10, 64)
  164 + if targetUserId == 0 {
  165 + continue
  166 + }
  167 + targetUser, err := se.getUserData(userRepo, targetUserId)
  168 + if err != nil {
  169 + return fmt.Errorf("获取员工数据%s", err)
  170 + }
  171 + if targetUser == nil {
  172 + continue
  173 + }
  174 + targetUserDepartment, err := se.getDepartmentData(departmentRepo, targetUser.DepartmentId)
  175 + if err != nil {
  176 + return fmt.Errorf("获取员工的部门数据%s", err)
  177 + }
  178 + evaluationTemp.TargetDepartment = []domain.StaffDepartment{}
  179 + evaluationTemp.Types = domain.EvaluationSelf
  180 + for _, d := range targetUserDepartment {
  181 + evaluationTemp.TargetDepartment = append(evaluationTemp.TargetDepartment, domain.StaffDepartment{
  182 + DepartmentId: int(d.Id),
  183 + DepartmentName: d.Name,
  184 + })
  185 + }
  186 + evaluationTemp.TargetUser = domain.StaffDesc{
  187 + UserId: int(targetUser.Id),
  188 + Account: targetUser.Account,
  189 + UserName: targetUser.Name,
  190 + }
  191 + //处理自评
  192 + {
  193 + evaluationTemp.Executor = domain.StaffDesc{
  194 + UserId: int(targetUser.Id),
  195 + Account: targetUser.Account,
  196 + UserName: targetUser.Name,
  197 + }
  198 + evaluationTemp.BeginTime = beginTimeSelf
  199 + evaluationTemp.EndTime = endTimeSelf
  200 + newEvaluationList = append(newEvaluationList, evaluationTemp)
  201 + }
  202 + //处理360评估
  203 + {
  204 + for _, val2 := range executor360Map {
  205 + evaluationTemp.BeginTime = beginTime360
  206 + evaluationTemp.EndTime = endTime360
  207 + evaluationTemp.Executor = domain.StaffDesc{
  208 + UserId: int(val2.Id),
  209 + Account: val2.Account,
  210 + UserName: val2.Name,
  211 + }
  212 + evaluationTemp.Types = domain.Evaluation360
  213 + //确定人资评估
  214 + newEvaluationList = append(newEvaluationList, evaluationTemp)
  215 + }
  216 + }
  217 + //处理人资评估
  218 + {
  219 + if hrbpExist {
  220 + //处理人资评估
  221 + evaluationTemp.BeginTime = beginTime360
  222 + evaluationTemp.EndTime = endTime360
  223 + evaluationTemp.Executor = domain.StaffDesc{}
  224 + evaluationTemp.Types = domain.EvaluationHrbp
  225 + newEvaluationList = append(newEvaluationList, evaluationTemp)
  226 + }
  227 + }
  228 + //处理上级评估
  229 + {
  230 + superUser, _ := se.getUserData(userRepo, targetUser.ParentId)
  231 + if superUser != nil {
  232 + evaluationTemp.Types = domain.EvaluationSuper
  233 + evaluationTemp.Executor = domain.StaffDesc{
  234 + UserId: int(superUser.Id),
  235 + Account: superUser.Account,
  236 + UserName: superUser.Name,
  237 + }
  238 + evaluationTemp.BeginTime = beginTimeSuper
  239 + evaluationTemp.EndTime = endTimeSuper
  240 + //确定上级评估
  241 + newEvaluationList = append(newEvaluationList, evaluationTemp)
  242 + }
  243 + }
  244 + //综合考评结果
  245 + {
  246 + evaluationTemp.Types = domain.EvaluationFinish
  247 + evaluationTemp.Executor = domain.StaffDesc{}
  248 + evaluationTemp.BeginTime = endTimeSuper
  249 + evaluationTemp.EndTime = endTimeSuper.Add(2 * 24 * time.Hour)
  250 + newEvaluationList = append(newEvaluationList, evaluationTemp)
  251 + }
  252 + }
  253 + summaryEvaluationRepo := factory.CreateSummaryEvaluationRepository(map[string]interface{}{"transactionContext": transactionContext})
  254 + for i := range newEvaluationList {
  255 + err = summaryEvaluationRepo.Save(&newEvaluationList[i])
  256 + if err != nil {
  257 + return fmt.Errorf("保存周期综合评估%s", err)
  258 + }
  259 + }
  260 + //回填项目的状态
  261 + projectDao := dao.NewEvaluationProjectDao(map[string]interface{}{"transactionContext": transactionContext})
  262 + err = projectDao.UpdateSummaryState(projectParam.Id, domain.ProjectSummaryStateYes)
  263 + if err != nil {
  264 + return fmt.Errorf("保存项目状态%s", err)
  265 + }
  266 + if err := transactionContext.CommitTransaction(); err != nil {
  267 + return err
  268 + }
  269 + err = sendSmsEvalation(newEvaluationList)
  270 + return fmt.Errorf("设置短信发送%s", err)
  271 +}
  272 +
  273 +// 获取周期设置数据
  274 +func (se *summaryEvaluationPublisher) getCycleData(cycleRepo domain.EvaluationCycleRepository, cycleId int64) (*domain.EvaluationCycle, error) {
  275 + var cycleData *domain.EvaluationCycle
  276 + if val, ok := se.cycleCache[cycleId]; ok {
  277 + cycleData = val
  278 + } else {
  279 + _, cycleList, err := cycleRepo.Find(map[string]interface{}{"id": cycleId})
  280 + if err != nil {
  281 + return nil, err
  282 + }
  283 + if len(cycleList) == 0 {
  284 + return nil, nil
  285 + }
  286 + cycleData = cycleList[0]
  287 + }
  288 + return cycleData, nil
  289 +}
  290 +
  291 +// 获取用户数据
  292 +func (se *summaryEvaluationPublisher) getUserData(userRepo domain.UserRepository, userId int64) (*domain.User, error) {
  293 + if userId == 0 {
  294 + return nil, nil
  295 + }
  296 + var userData *domain.User
  297 + if val, ok := se.userCache[userId]; ok {
  298 + userData = val
  299 + } else {
  300 + _, userList, err := userRepo.Find(map[string]interface{}{"id": userId})
  301 + if err != nil {
  302 + return nil, err
  303 + }
  304 + if len(userList) == 0 {
  305 + return nil, nil
  306 + }
  307 + userData = userList[0]
  308 + }
  309 + return userData, nil
  310 +}
  311 +
  312 +// 获取部门数据
  313 +func (se *summaryEvaluationPublisher) getDepartmentData(departmentRepo domain.DepartmentRepository, departmentIds []int) ([]*domain.Department, error) {
  314 + departmentList := []*domain.Department{}
  315 + for _, departmentId := range departmentIds {
  316 + if val, ok := se.departCache[departmentId]; ok {
  317 + departmentList = append(departmentList, val)
  318 + } else {
  319 + _, departments, err := departmentRepo.Find(map[string]interface{}{"id": departmentId})
  320 + if err != nil {
  321 + return nil, err
  322 + }
  323 + if len(departmentList) == 0 {
  324 + continue
  325 + }
  326 + departmentList = append(departmentList, departments[0])
  327 + }
  328 + }
  329 + return departmentList, nil
  330 +}
@@ -42,5 +42,6 @@ func (sms *LogSms) SummaryEvaluationMessage(phone string, name string) { @@ -42,5 +42,6 @@ func (sms *LogSms) SummaryEvaluationMessage(phone string, name string) {
42 42
43 type LogSmsRepository interface { 43 type LogSmsRepository interface {
44 Save(param *LogSms) error 44 Save(param *LogSms) error
  45 + BatchInsert(params []*LogSms) error
45 Find(queryOptions map[string]interface{}) (int, []*LogSms, error) 46 Find(queryOptions map[string]interface{}) (int, []*LogSms, error)
46 } 47 }
@@ -127,6 +127,10 @@ func (repo *EvaluationCycleRepository) Find(queryOptions map[string]interface{}) @@ -127,6 +127,10 @@ func (repo *EvaluationCycleRepository) Find(queryOptions map[string]interface{})
127 query.Where("id in (?)", pg.In(v)) 127 query.Where("id in (?)", pg.In(v))
128 } 128 }
129 129
  130 + if v, ok := queryOptions["id"]; ok {
  131 + query.Where("id=?", v)
  132 + }
  133 +
130 if v, ok := queryOptions["companyId"]; ok { 134 if v, ok := queryOptions["companyId"]; ok {
131 query.Where("company_id = ?", v) 135 query.Where("company_id = ?", v)
132 } 136 }
@@ -96,3 +96,33 @@ func (repo *LogSmsRepository) TransformToDomain(d *models.LogSms) *domain.LogSms @@ -96,3 +96,33 @@ func (repo *LogSmsRepository) TransformToDomain(d *models.LogSms) *domain.LogSms
96 CreatedAt: d.CreatedAt, 96 CreatedAt: d.CreatedAt,
97 } 97 }
98 } 98 }
  99 +
  100 +//批量添加
  101 +
  102 +func (repo *LogSmsRepository) BatchInsert(params []*domain.LogSms) error {
  103 + smsList := []models.LogSms{}
  104 + for _, param := range params {
  105 + m := models.LogSms{
  106 + Id: param.Id,
  107 + Phone: param.Phone,
  108 + TemplateId: param.TemplateId,
  109 + Template: param.Template,
  110 + Value: param.Value,
  111 + CreatedAt: param.CreatedAt,
  112 + Result: param.Result,
  113 + Status: string(param.Status),
  114 + From: param.From,
  115 + Index: param.Index,
  116 + ExecuteAt: param.ExecuteAt,
  117 + }
  118 + smsList = append(smsList, m)
  119 + }
  120 +
  121 + tx := repo.transactionContext.PgTx
  122 +
  123 + _, err := tx.Model(&smsList).Insert()
  124 + if err != nil {
  125 + return err
  126 + }
  127 + return nil
  128 +}