审查视图

pkg/application/summary_evaluation/service/scheduled_v2.go 12.5 KB
1 2 3 4 5 6 7
package service

import (
	"fmt"
	"strconv"
	"time"
tangxvhui authored
8
	"github.com/linmadan/egglib-go/core/application"
9 10 11 12 13 14 15 16 17 18 19 20 21 22
	"gitlab.fjmaimaimai.com/allied-creation/performance/pkg/application/factory"
	"gitlab.fjmaimaimai.com/allied-creation/performance/pkg/domain"
	"gitlab.fjmaimaimai.com/allied-creation/performance/pkg/infrastructure/dao"
	"gitlab.fjmaimaimai.com/allied-creation/performance/pkg/log"
)

// 定时下发周期评估任务
func TaskSendSummaryEvaluationV2() error {
	nowTime := time.Now()
	defer func() {
		str := fmt.Sprintf("下发周期评估耗时%.2f s", time.Since(nowTime).Seconds())
		log.Logger.Info(str)
	}()
	for {
tangxvhui authored
23
		cycleList, err := getPrepareEvaluationCycle()
24 25 26
		if err != nil {
			return err
		}
tangxvhui authored
27
		if len(cycleList) == 0 {
28 29
			break
		}
tangxvhui authored
30 31
		err = sendSummaryEvaluationByCycle(cycleList[0])
		if err != nil {
32 33 34 35 36 37
			return err
		}
	}
	return nil
}
tangxvhui authored
38 39
// 获取周期
func getPrepareEvaluationCycle() ([]*domain.EvaluationCycle, error) {
40 41 42 43 44 45 46 47 48 49
	transactionContext, err := factory.CreateTransactionContext(nil)
	if err != nil {
		return nil, err
	}
	if err := transactionContext.StartTransaction(); err != nil {
		return nil, err
	}
	defer func() {
		_ = transactionContext.RollbackTransaction()
	}()
tangxvhui authored
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104
	cycleRepo := factory.CreateEvaluationCycleRepository(map[string]interface{}{"transactionContext": transactionContext})
	cycleList, err := cycleRepo.FindCycleEnd(1)
	if err != nil {
		return nil, fmt.Errorf("获取可用的周期数据,%s", err)
	}
	if err := transactionContext.CommitTransaction(); err != nil {
		return nil, err
	}
	return cycleList, nil
}

// 获取可用的项目
// func getPrepareSummaryEvaluation(cycleId int) ([]*domain.EvaluationProject, error) {
// 	transactionContext, err := factory.CreateTransactionContext(nil)
// 	if err != nil {
// 		return nil, err
// 	}
// 	if err := transactionContext.StartTransaction(); err != nil {
// 		return nil, err
// 	}
// 	defer func() {
// 		_ = transactionContext.RollbackTransaction()
// 	}()
// 	projectRepo := factory.CreateEvaluationProjectRepository(map[string]interface{}{
// 		"transactionContext": transactionContext,
// 	})
// 	// 获取项目数据总数
// 	_, projectList, err := projectRepo.Find(map[string]interface{}{
// 		"cycleId":      cycleId,
// 		"summaryState": domain.ProjectSummaryStateNo,
// 		"state":        domain.ProjectStateEnable,
// 		"limit":        200,
// 	}, "template")
// 	if err != nil {
// 		return nil, fmt.Errorf("获取可用的项目数据,%s", err)
// 	}
// 	if err := transactionContext.CommitTransaction(); err != nil {
// 		return nil, err
// 	}
// 	return projectList, nil
// }

// 按周期下发 综合评估任务
func sendSummaryEvaluationByCycle(cycleParam *domain.EvaluationCycle) error {
	transactionContext, err := factory.CreateTransactionContext(nil)
	if err != nil {
		return err
	}
	if err := transactionContext.StartTransaction(); err != nil {
		return err
	}
	defer func() {
		_ = transactionContext.RollbackTransaction()
	}()
105 106 107 108 109
	projectRepo := factory.CreateEvaluationProjectRepository(map[string]interface{}{
		"transactionContext": transactionContext,
	})
	// 获取项目数据总数
	_, projectList, err := projectRepo.Find(map[string]interface{}{
tangxvhui authored
110
		"cycleId":      cycleParam.Id,
111 112
		"summaryState": domain.ProjectSummaryStateNo,
		"state":        domain.ProjectStateEnable,
tangxvhui authored
113
		"limit":        500,
114 115
	}, "template")
	if err != nil {
tangxvhui authored
116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131
		return fmt.Errorf("获取可用的项目数据,%s", err)
	}
	var newEvaluationList []domain.SummaryEvaluation
	newPublisher := summaryEvaluationPublisher{}
	for _, val := range projectList {
		evaluationList, err := newPublisher.sendSummaryEvaluationV2(transactionContext, val, cycleParam)
		if err != nil {
			return fmt.Errorf("按项目下发综合评估任务数据,%s", err)
		}
		newEvaluationList = append(newEvaluationList, evaluationList...)
	}
	// 回填周期的状态
	cycleDao := dao.NewEvaluationCycleDao(map[string]interface{}{"transactionContext": transactionContext})
	err = cycleDao.UpdateSummaryState(cycleParam.Id, domain.ProjectSummaryStateYes)
	if err != nil {
		return fmt.Errorf("保存项目周期状态%s", err)
132 133
	}
	if err := transactionContext.CommitTransaction(); err != nil {
tangxvhui authored
134
		return err
135
	}
tangxvhui authored
136 137 138 139 140
	err = sendSmsEvalation(newEvaluationList)
	if err != nil {
		return fmt.Errorf("设置短信消息%s", err)
	}
	return nil
141 142 143 144 145 146 147 148
}

// 下发周期综合评估
type summaryEvaluationPublisher struct {
	userCache   map[int64]*domain.User
	departCache map[int]*domain.Department
}
tangxvhui authored
149 150 151 152 153 154 155 156 157 158 159 160 161 162
func (se *summaryEvaluationPublisher) sendSummaryEvaluationV2(
	transactionContext application.TransactionContext,
	projectParam *domain.EvaluationProject, cycleData *domain.EvaluationCycle,
) ([]domain.SummaryEvaluation, error) {
	// transactionContext, err := factory.CreateTransactionContext(nil)
	// if err != nil {
	// 	return err
	// }
	// if err := transactionContext.StartTransaction(); err != nil {
	// 	return err
	// }
	// defer func() {
	// 	_ = transactionContext.RollbackTransaction()
	// }()
163 164 165 166 167 168 169 170
	userRepo := factory.CreateUserRepository(map[string]interface{}{"transactionContext": transactionContext})
	departmentRepo := factory.CreateDepartmentRepository(map[string]interface{}{"transactionContext": transactionContext})
	evaluationItemRepo := factory.CreateEvaluationItemUsedRepository(map[string]interface{}{"transactionContext": transactionContext})
	_, evaluationItemList, err := evaluationItemRepo.Find(map[string]interface{}{
		"evaluationProjectId": projectParam.Id,
		"nodeType":            int(domain.LinkNodeSelfAssessment),
	})
	if err != nil {
tangxvhui authored
171
		return nil, err
172 173
	}
	if len(evaluationItemList) == 0 {
tangxvhui authored
174
		return nil, nil
175 176 177 178 179 180 181 182 183 184 185 186 187 188
	}
	nodeId := evaluationItemList[0].NodeId
	executor360Map := map[int64]*domain.User{}
	hrbpExist := false
	for _, v := range evaluationItemList {
		nodeId = v.NodeId
		if v.EvaluatorId < 0 {
			hrbpExist = true
		}
		if v.EvaluatorId <= 0 {
			continue
		}
		user360, err := se.getUserData(userRepo, int64(v.EvaluatorId))
		if err != nil {
tangxvhui authored
189
			return nil, fmt.Errorf("获取360用户%s", err)
190 191 192 193
		}
		executor360Map[user360.Id] = user360
	}
	if cycleData.TimeEnd == nil {
tangxvhui authored
194
		return nil, fmt.Errorf("周期%d:%s 结束时间错误", cycleData.Id, cycleData.Name)
195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235
	}
	//自评的时间范围
	beginTimeSelf := *cycleData.TimeEnd
	endTimeSelf := dayZeroTime(beginTimeSelf).Add(3*24*time.Hour - time.Second)
	//人资、360评估的时间范围
	beginTime360 := endTimeSelf
	endTime360 := endTimeSelf.Add(2 * 24 * time.Hour)
	//上级评估的是时间范围
	beginTimeSuper := endTime360
	endTimeSuper := endTime360.Add(2 * 24 * time.Hour)
	// 创建周期评估任务
	var newEvaluationList []domain.SummaryEvaluation
	evaluationTemp := domain.SummaryEvaluation{
		Id:                    0,
		CompanyId:             int(projectParam.CompanyId),
		EvaluationProjectId:   int(projectParam.Id),
		EvaluationProjectName: projectParam.Name,
		CycleId:               cycleData.Id,
		CycleName:             cycleData.Name,
		NodeId:                nodeId,
		TargetUser:            domain.StaffDesc{},         //待填充
		TargetDepartment:      []domain.StaffDepartment{}, //待填充
		Executor:              domain.StaffDesc{},
		Types:                 0, //待填充
		Status:                domain.EvaluationUncompleted,
		CheckResult:           domain.EvaluationCheckUncompleted,
		BeginTime:             time.Time{}, //待填充
		EndTime:               time.Time{}, //待填充
		TotalScore:            "",
		CreatedAt:             time.Now(),
		UpdatedAt:             time.Now(),
		DeletedAt:             nil,
	}

	for _, val := range projectParam.Recipients {
		targetUserId, _ := strconv.ParseInt(val, 10, 64)
		if targetUserId == 0 {
			continue
		}
		targetUser, err := se.getUserData(userRepo, targetUserId)
		if err != nil {
tangxvhui authored
236
			return nil, fmt.Errorf("获取员工数据%s", err)
237 238 239 240 241 242
		}
		if targetUser == nil {
			continue
		}
		targetUserDepartment, err := se.getDepartmentData(departmentRepo, targetUser.DepartmentId)
		if err != nil {
tangxvhui authored
243
			return nil, fmt.Errorf("获取员工的部门数据%s", err)
244 245
		}
		evaluationTemp.TargetDepartment = []domain.StaffDepartment{}
tangxvhui authored
246
247 248 249 250 251 252 253 254 255 256 257 258 259
		for _, d := range targetUserDepartment {
			evaluationTemp.TargetDepartment = append(evaluationTemp.TargetDepartment, domain.StaffDepartment{
				DepartmentId:   int(d.Id),
				DepartmentName: d.Name,
			})
		}
		evaluationTemp.TargetUser = domain.StaffDesc{
			UserId:   int(targetUser.Id),
			Account:  targetUser.Account,
			UserName: targetUser.Name,
		}
		//处理自评
		{
tangxvhui authored
260
			evaluationTemp.Types = domain.EvaluationSelf
261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324
			evaluationTemp.Executor = domain.StaffDesc{
				UserId:   int(targetUser.Id),
				Account:  targetUser.Account,
				UserName: targetUser.Name,
			}
			evaluationTemp.BeginTime = beginTimeSelf
			evaluationTemp.EndTime = endTimeSelf
			newEvaluationList = append(newEvaluationList, evaluationTemp)
		}
		//处理360评估
		{
			for _, val2 := range executor360Map {
				evaluationTemp.BeginTime = beginTime360
				evaluationTemp.EndTime = endTime360
				evaluationTemp.Executor = domain.StaffDesc{
					UserId:   int(val2.Id),
					Account:  val2.Account,
					UserName: val2.Name,
				}
				evaluationTemp.Types = domain.Evaluation360
				//确定人资评估
				newEvaluationList = append(newEvaluationList, evaluationTemp)
			}
		}
		//处理人资评估
		{
			if hrbpExist {
				//处理人资评估
				evaluationTemp.BeginTime = beginTime360
				evaluationTemp.EndTime = endTime360
				evaluationTemp.Executor = domain.StaffDesc{}
				evaluationTemp.Types = domain.EvaluationHrbp
				newEvaluationList = append(newEvaluationList, evaluationTemp)
			}
		}
		//处理上级评估
		{
			superUser, _ := se.getUserData(userRepo, targetUser.ParentId)
			if superUser != nil {
				evaluationTemp.Types = domain.EvaluationSuper
				evaluationTemp.Executor = domain.StaffDesc{
					UserId:   int(superUser.Id),
					Account:  superUser.Account,
					UserName: superUser.Name,
				}
				evaluationTemp.BeginTime = beginTimeSuper
				evaluationTemp.EndTime = endTimeSuper
				//确定上级评估
				newEvaluationList = append(newEvaluationList, evaluationTemp)
			}
		}
		//综合考评结果
		{
			evaluationTemp.Types = domain.EvaluationFinish
			evaluationTemp.Executor = domain.StaffDesc{}
			evaluationTemp.BeginTime = endTimeSuper
			evaluationTemp.EndTime = endTimeSuper.Add(2 * 24 * time.Hour)
			newEvaluationList = append(newEvaluationList, evaluationTemp)
		}
	}
	summaryEvaluationRepo := factory.CreateSummaryEvaluationRepository(map[string]interface{}{"transactionContext": transactionContext})
	for i := range newEvaluationList {
		err = summaryEvaluationRepo.Save(&newEvaluationList[i])
		if err != nil {
tangxvhui authored
325
			return nil, fmt.Errorf("保存周期综合评估%s", err)
326 327 328 329 330 331
		}
	}
	//回填项目的状态
	projectDao := dao.NewEvaluationProjectDao(map[string]interface{}{"transactionContext": transactionContext})
	err = projectDao.UpdateSummaryState(projectParam.Id, domain.ProjectSummaryStateYes)
	if err != nil {
tangxvhui authored
332
		return nil, fmt.Errorf("保存项目状态%s", err)
333
	}
tangxvhui authored
334 335 336
	// if err := transactionContext.CommitTransaction(); err != nil {
	// 	return err
	// }
tangxvhui authored
337
	return newEvaluationList, nil
338 339 340
}

// 获取周期设置数据
tangxvhui authored
341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356
// func (se *summaryEvaluationPublisher) getCycleData(cycleRepo domain.EvaluationCycleRepository, cycleId int64) (*domain.EvaluationCycle, error) {
// 	var cycleData *domain.EvaluationCycle
// 	if val, ok := se.cycleCache[cycleId]; ok {
// 		cycleData = val
// 	} else {
// 		_, cycleList, err := cycleRepo.Find(map[string]interface{}{"id": cycleId})
// 		if err != nil {
// 			return nil, err
// 		}
// 		if len(cycleList) == 0 {
// 			return nil, nil
// 		}
// 		cycleData = cycleList[0]
// 	}
// 	return cycleData, nil
// }
357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397

// 获取用户数据
func (se *summaryEvaluationPublisher) getUserData(userRepo domain.UserRepository, userId int64) (*domain.User, error) {
	if userId == 0 {
		return nil, nil
	}
	var userData *domain.User
	if val, ok := se.userCache[userId]; ok {
		userData = val
	} else {
		_, userList, err := userRepo.Find(map[string]interface{}{"id": userId})
		if err != nil {
			return nil, err
		}
		if len(userList) == 0 {
			return nil, nil
		}
		userData = userList[0]
	}
	return userData, nil
}

// 获取部门数据
func (se *summaryEvaluationPublisher) getDepartmentData(departmentRepo domain.DepartmentRepository, departmentIds []int) ([]*domain.Department, error) {
	departmentList := []*domain.Department{}
	for _, departmentId := range departmentIds {
		if val, ok := se.departCache[departmentId]; ok {
			departmentList = append(departmentList, val)
		} else {
			_, departments, err := departmentRepo.Find(map[string]interface{}{"id": departmentId})
			if err != nil {
				return nil, err
			}
			if len(departmentList) == 0 {
				continue
			}
			departmentList = append(departmentList, departments[0])
		}
	}
	return departmentList, nil
}