审查视图

pkg/application/summary_evaluation/service/service2.go 29.4 KB
1 2 3
package service

import (
4
	"fmt"
5
	"strconv"
6 7 8
	"strings"
	"time"
9
	"github.com/linmadan/egglib-go/core/application"
10
	"github.com/linmadan/egglib-go/utils/tool_funs"
11 12 13 14 15
	"gitlab.fjmaimaimai.com/allied-creation/performance/pkg/application/factory"
	service "gitlab.fjmaimaimai.com/allied-creation/performance/pkg/application/role"
	"gitlab.fjmaimaimai.com/allied-creation/performance/pkg/application/summary_evaluation/adapter"
	"gitlab.fjmaimaimai.com/allied-creation/performance/pkg/application/summary_evaluation/command"
	"gitlab.fjmaimaimai.com/allied-creation/performance/pkg/domain"
16
	"gitlab.fjmaimaimai.com/allied-creation/performance/pkg/infrastructure/dao"
17 18
)
19
// Evaluation360List 获取360综评列表
郑周 authored
20
func (srv *SummaryEvaluationService) Evaluation360List(param *command.QueryEvaluation360List) (map[string]interface{}, error) {
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44
	transactionContext, err := factory.ValidateStartTransaction(param)
	if err != nil {
		return nil, err
	}
	defer func() {
		_ = transactionContext.RollbackTransaction()
	}()

	evaluationRepo := factory.CreateSummaryEvaluationRepository(map[string]interface{}{"transactionContext": transactionContext})
	evaluationItemRepo := factory.CreateEvaluationItemUsedRepository(map[string]interface{}{"transactionContext": transactionContext})
	itemValueRepo := factory.CreateSummaryEvaluationValueRepository(map[string]interface{}{"transactionContext": transactionContext})

	var searchTargetName string
	if len(param.SearchName) > 0 {
		searchTargetName = "%" + param.SearchName + "%"
	} else {
		searchTargetName = ""
	}
	limit := param.PageSize
	offset := limit * (param.PageNumber - 1)
	if offset < 0 {
		offset = 0
	}
	count, list, err := evaluationRepo.Find(map[string]interface{}{
郑周 authored
45 46 47 48 49 50 51
		"companyId":      param.CompanyId,
		"cycleId":        param.CycleId,
		"executorId":     param.UserId,
		"types":          domain.Evaluation360,
		"targetUserName": searchTargetName,
		"limit":          limit,
		"offset":         offset,
52
		"beginTime":      time.Now(),
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
	})
	if err != nil {
		return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
	}

	ids := make([]int, 0)
	projectIds := make([]int, 0)
	for i := range list {
		ids = append(ids, list[i].Id)
		projectIds = append(projectIds, list[i].EvaluationProjectId)
	}

	// 固定列
	changeableHeader := []adapter.TableHeader{
		{Key: "targetUserName", Name: "姓名"},
		{Key: "statusVal", Name: "状态"},
		{Key: "relation", Name: "360°评估关系"},
		{Key: "endTime", Name: "360°评估截止日期"},
	}
	changeableRowMap := map[int]map[string]interface{}{}    // k,v(用户ID -> 数据集合)行数据内容
	itemMap := map[int][]*domain.EvaluationItemUsed{}       // k,v(项目ID -> 评估数组)归类评估内容
	valueMap := map[string]*domain.SummaryEvaluationValue{} // k,v(周期+评估 -> 提交值)
	uniqueColumnsMap := map[string]string{}                 // 列名称唯一

	if len(ids) > 0 {
		// 评估内容(注.使用自评模板,并过滤出360综评人的评估内容)
79
		_, itemList, err := evaluationItemRepo.Find(map[string]interface{}{"evaluationProjectIds": projectIds, "nodeType": domain.LinkNodeSelfAssessment, "evaluatorId": param.UserId})
80 81 82 83
		if err != nil {
			return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
		}
		// 填写值
郑周 authored
84
		_, valueList, err := itemValueRepo.Find(map[string]interface{}{"summaryEvaluationIdList": ids})
85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106
		if err != nil {
			return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
		}

		if err := transactionContext.CommitTransaction(); err != nil {
			return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
		}

		for i := range valueList {
			key := fmt.Sprintf("%d-%d", valueList[i].SummaryEvaluationId, valueList[i].EvaluationItemId)
			valueMap[key] = valueList[i]
		}

		for i := range itemList {
			item := itemList[i]
			key := fmt.Sprintf("k%d", i)
			name := fmt.Sprintf("%s-%s", item.Category, item.Name)
			if _, ok := uniqueColumnsMap[name]; !ok {
				changeableHeader = append(changeableHeader, adapter.TableHeader{Key: key, Name: name})
				uniqueColumnsMap[name] = key
			}
107 108 109
			pList, ok := itemMap[item.EvaluationProjectId]
			if !ok {
				pList = make([]*domain.EvaluationItemUsed, 0)
110
			}
111
			itemMap[item.EvaluationProjectId] = append(pList, item)
112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137
		}
	}

	now := time.Now().Local() // 当前时间
	var rowMap map[string]interface{}
	for i := range list {
		v := list[i]
		if rm, ok := changeableRowMap[v.TargetUser.UserId]; ok {
			rowMap = rm
		} else {
			rowMap = map[string]interface{}{}
			changeableRowMap[v.TargetUser.UserId] = rowMap
		}

		endTime := v.EndTime.Local()
		statusVal := ""
		if v.Status == domain.EvaluationCompleted {
			statusVal = "已完成"
		} else {
			if now.After(endTime) {
				statusVal = "已逾期"
			} else {
				statusVal = "待完成"
			}
		}
		rowMap["id"] = v.Id
138
		rowMap["targetUserId"] = strconv.Itoa(v.TargetUser.UserId)
139 140 141
		rowMap["targetUserName"] = v.TargetUser.UserName
		rowMap["statusVal"] = statusVal
		rowMap["relation"] = "项目组员"
郑周 authored
142
		rowMap["endTime"] = endTime.Local().Format("2006-01-02 15:04")
143 144 145 146 147 148 149 150

		// 当前项目所有评估内容
		if pList, ok := itemMap[v.EvaluationProjectId]; ok {
			for _, item := range pList {
				name := fmt.Sprintf("%s-%s", item.Category, item.Name)
				if key, ok := uniqueColumnsMap[name]; ok { // 动态列用name -> key
					valueKey := fmt.Sprintf("%d-%d", v.Id, item.Id)
					if it, ok := valueMap[valueKey]; ok {
tangxvhui authored
151 152 153 154 155
						if it.Weight > 0 {
							rowMap[key] = it.Score
						} else {
							rowMap[key] = it.Rating.Code
						}
156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173
					} else {
						rowMap[key] = ""
					}
				}
			}
		}
	}

	targetList := make([]map[string]interface{}, 0)
	for _, v1 := range list {
		rowMap := changeableRowMap[v1.TargetUser.UserId]
		for _, v2 := range changeableHeader { // 填充部分动态列缺失字段
			if _, ok := rowMap[v2.Key]; !ok {
				rowMap[v2.Key] = ""
			}
		}
		targetList = append(targetList, rowMap)
	}
郑周 authored
174 175 176 177 178 179 180

	// 外层显示截止时间
	endTime := ""
	if len(list) > 0 {
		endTime = list[0].EndTime.Local().Format("2006-01-02 15:04:05")
	}
181 182 183 184 185
	result := &adapter.SummaryEvaluationAdapter{
		TableHeader: changeableHeader,
		Total:       count,
		List:        targetList,
	}
郑周 authored
186 187 188 189 190
	grid := map[string]interface{}{
		"grid":    result,
		"endTime": endTime,
	}
	return grid, nil
191 192
}
郑周 authored
193
// GetEvaluation360 获取360综评详情(我评估目标用户)
194
func (srv *SummaryEvaluationService) GetEvaluation360(param *command.QueryEvaluation360My) (map[string]interface{}, error) {
195 196 197 198 199 200 201 202 203 204 205 206
	transactionContext, err := factory.ValidateStartTransaction(param)
	if err != nil {
		return nil, err
	}
	defer func() {
		_ = transactionContext.RollbackTransaction()
	}()

	evaluationRepo := factory.CreateSummaryEvaluationRepository(map[string]interface{}{"transactionContext": transactionContext})
	evaluationItemRepo := factory.CreateEvaluationItemUsedRepository(map[string]interface{}{"transactionContext": transactionContext})
	itemValueRepo := factory.CreateSummaryEvaluationValueRepository(map[string]interface{}{"transactionContext": transactionContext})
郑周 authored
207
	sEvaluation, err := evaluationRepo.FindOne(map[string]interface{}{"id": param.SummaryEvaluationId})
208 209 210
	if err != nil {
		return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
	}
郑周 authored
211 212
	if err != nil {
		return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
213
	}
214
215
	// 360评估内容(自评模板、筛选项目评估人)
216 217 218
	_, itemList, err := evaluationItemRepo.Find(map[string]interface{}{
		"evaluationProjectId": sEvaluation.EvaluationProjectId,
		"nodeType":            domain.LinkNodeSelfAssessment,
郑周 authored
219
		"companyId":           param.CompanyId,
220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236
		"evaluatorId":         param.UserId,
	})
	if err != nil {
		return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
	}

	// 评估内容对应的分数
	_, itemValues, err := itemValueRepo.Find(map[string]interface{}{"summaryEvaluationId": sEvaluation.Id})
	if err != nil {
		return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
	}

	if err := transactionContext.CommitTransaction(); err != nil {
		return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
	}

	itemValuesAdapter := srv.buildSummaryItemValue(itemList, itemValues)
郑周 authored
237 238 239
	for i := range itemValuesAdapter { // 评估人名称赋值
		itemValuesAdapter[i].EvaluatorName = sEvaluation.Executor.UserName
	}
240 241 242
	// 评估项截止时间
	endTime := sEvaluation.EndTime.Local().Format("2006-01-02 15:04:05")
	return map[string]interface{}{"endTime": endTime, "list": itemValuesAdapter}, nil
243 244
}
245 246
// GetEvaluation360Complete 获取360综评详情(他人评估目标用户)(已完成状态)
func (srv *SummaryEvaluationService) GetEvaluation360Complete(param *command.QueryEvaluation360) (*[]adapter.EvaluationItemAdapter, error) {
247 248 249 250 251 252 253 254 255 256 257 258 259 260 261
	transactionContext, err := factory.ValidateStartTransaction(param)
	if err != nil {
		return nil, err
	}
	defer func() {
		_ = transactionContext.RollbackTransaction()
	}()

	evaluationRepo := factory.CreateSummaryEvaluationRepository(map[string]interface{}{"transactionContext": transactionContext})
	evaluationItemRepo := factory.CreateEvaluationItemUsedRepository(map[string]interface{}{"transactionContext": transactionContext})
	itemValueRepo := factory.CreateSummaryEvaluationValueRepository(map[string]interface{}{"transactionContext": transactionContext})
	_, evaluations, err := evaluationRepo.Find(map[string]interface{}{
		"companyId":    param.CompanyId,
		"cycleId":      param.CycleId,
		"targetUserId": param.TargetUserId,
262 263 264 265
		"types":        domain.Evaluation360,
		"status":       domain.EvaluationCompleted,
		"limit":        99999,
	})
266 267 268
	if err != nil {
		return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
	}
郑周 authored
269
270
	if len(evaluations) == 0 {
郑周 authored
271 272
		list := make([]adapter.EvaluationItemAdapter, 0)
		return &list, nil
273 274
	}
郑周 authored
275 276 277 278 279 280 281 282 283 284
	executorMap := map[int]domain.StaffDesc{} // 执行人
	evaluationIds := make([]int, 0)           // 评估周期ID
	for i := range evaluations {
		executor := evaluations[i].Executor
		executorMap[executor.UserId] = executor

		evaluationIds = append(evaluationIds, evaluations[i].Id)
	}
	// 得到项目ID
	evaluationProjectId := evaluations[0].EvaluationProjectId
285 286
	// 360评估内容(自评模板)
	_, itemList, err := evaluationItemRepo.Find(map[string]interface{}{
郑周 authored
287
		"evaluationProjectId": evaluationProjectId,
288 289 290 291 292 293 294
		"nodeType":            domain.LinkNodeSelfAssessment,
	})
	if err != nil {
		return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
	}

	// 评估内容对应的分数
郑周 authored
295 296 297 298
	_, itemValues, err := itemValueRepo.Find(map[string]interface{}{
		"summaryEvaluationIdList": evaluationIds,
		"types":                   domain.Evaluation360,
	})
299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315
	if err != nil {
		return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
	}

	filterItemList := make([]*domain.EvaluationItemUsed, 0) // 筛选有评估人的评估内容
	for i := range itemList {
		if itemList[i].EvaluatorId > 0 {
			filterItemList = append(filterItemList, itemList[i])
		}
	}

	if err := transactionContext.CommitTransaction(); err != nil {
		return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
	}

	itemValuesAdapter := srv.buildSummaryItemValue(filterItemList, itemValues)
	for i := range itemValuesAdapter { // 评估人名称赋值
郑周 authored
316 317
		if executor, ok := executorMap[itemValuesAdapter[i].EvaluatorId]; ok {
			itemValuesAdapter[i].EvaluatorName = executor.UserName
318 319 320
		}
	}
郑周 authored
321 322 323 324 325 326 327 328 329 330 331 332 333 334 335
	//result := adapter.EvaluationInfoAdapter{
	//	SummaryEvaluationId:   sEvaluation.Id,
	//	CycleId:               int(sEvaluation.CycleId),
	//	CycleName:             sEvaluation.CycleName,
	//	EvaluationProjectId:   sEvaluation.EvaluationProjectId,
	//	EvaluationProjectName: sEvaluation.EvaluationProjectName,
	//	LinkNodeId:            sEvaluation.NodeId,
	//	BeginTime:             sEvaluation.BeginTime.Format("2006-01-02 15:04:05"),
	//	EndTime:               sEvaluation.EndTime.Format("2006-01-02 15:04:05"),
	//	TargetUserId:          sEvaluation.TargetUser.UserId,
	//	TargetUserName:        sEvaluation.TargetUser.UserName,
	//	Status:                string(sEvaluation.Status),
	//	EvaluationItems:       itemValuesAdapter,
	//}
	return &itemValuesAdapter, nil
336 337
}
338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380
// EditEvaluation360 编辑提交360综评
func (srv *SummaryEvaluationService) EditEvaluation360(param *command.EditEvaluationValue) (map[string][]adapter.EvaluationItemAdapter, error) {
	transactionContext, err := factory.CreateTransactionContext(nil)
	if err != nil {
		return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
	}
	if err := transactionContext.StartTransaction(); err != nil {
		return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
	}
	defer func() {
		_ = transactionContext.RollbackTransaction()
	}()
	evaluationRepo := factory.CreateSummaryEvaluationRepository(map[string]interface{}{"transactionContext": transactionContext})
	itemUsedRepo := factory.CreateEvaluationItemUsedRepository(map[string]interface{}{"transactionContext": transactionContext})
	itemValueRepo := factory.CreateSummaryEvaluationValueRepository(map[string]interface{}{"transactionContext": transactionContext})

	summaryEvaluation, err := evaluationRepo.FindOne(map[string]interface{}{"id": param.SummaryEvaluationId})
	if err != nil {
		return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
	}
	if summaryEvaluation.Executor.UserId != param.ExecutorId {
		return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, "没有操作权限")
	}
	if summaryEvaluation.CompanyId != param.CompanyId {
		return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, "没有操作权限")
	}

	// 自评评估内容(自评模板、筛选项目评估人)
	_, itemList, err := itemUsedRepo.Find(map[string]interface{}{
		"evaluationProjectId": summaryEvaluation.EvaluationProjectId,
		"nodeType":            domain.LinkNodeSelfAssessment,
		"evaluatorId":         param.ExecutorId,
	})
	if err != nil {
		return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
	}

	// 评估内容对应的分数
	_, itemValueList, err := itemValueRepo.Find(map[string]interface{}{"summaryEvaluationId": summaryEvaluation.Id})
	if err != nil {
		return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
	}
381
	err = srv.editEvaluationValue(summaryEvaluation, &itemValueList, itemList, param.EvaluationItems, nil, param.IsTemporary)
382 383
	if err != nil {
		return nil, application.ThrowError(application.BUSINESS_ERROR, err.Error())
384
	}
385 386 387 388
	if !param.IsTemporary {
		//变更评估状态为已填写
		summaryEvaluation.Status = domain.EvaluationCompleted
	}
389
390 391
	//保存填写值
	for _, v := range itemValueList {
392 393 394 395 396
		err = itemValueRepo.Save(v)
		if err != nil {
			return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
		}
	}
397
398 399 400 401 402 403 404 405
	//保存填写值
	err = evaluationRepo.Save(summaryEvaluation)
	if err != nil {
		return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
	}
	if err := transactionContext.CommitTransaction(); err != nil {
		return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
	}
406 407 408 409 410 411 412 413

	if !param.IsTemporary {
		err = srv.AfterCompletedEvaluation360Hrbp(summaryEvaluation)
		if err != nil {
			return nil, err
		}
	}
414
	itemValueAdapter := srv.buildSummaryItemValue(itemList, itemValueList)
415 416 417 418 419
	return map[string][]adapter.EvaluationItemAdapter{
		"evaluationItems": itemValueAdapter,
	}, nil
}
420 421 422 423 424 425 426 427 428 429
// EvaluationHRBPList 获取人资综评列表
func (srv *SummaryEvaluationService) EvaluationHRBPList(param *command.QueryEvaluationHRList) (map[string]interface{}, error) {
	transactionContext, err := factory.ValidateStartTransaction(param)
	if err != nil {
		return nil, err
	}
	defer func() {
		_ = transactionContext.RollbackTransaction()
	}()
430 431
	// 必须是HRBP权限的人才能编辑操作
	hrbp, err := service.GetHRBP(transactionContext, param.CompanyId, param.UserId)
tangxvhui authored
432 433 434
	if err != nil {
		return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
	}
435 436 437 438
	if hrbp != 1 {
		return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, "没有操作权限")
	}
439 440 441 442
	evaluationRepo := factory.CreateSummaryEvaluationRepository(map[string]interface{}{"transactionContext": transactionContext})
	staffAssessDaoRepo := dao.NewStaffAssessDao(map[string]interface{}{"transactionContext": transactionContext})
	userRepo := factory.CreateUserRepository(map[string]interface{}{"transactionContext": transactionContext})
	positionRepo := factory.CreatePositionRepository(map[string]interface{}{"transactionContext": transactionContext})
tangxvhui authored
443
444 445 446 447 448
	limit := param.PageSize
	offset := limit * (param.PageNumber - 1)
	if offset < 0 {
		offset = 0
	}
tangxvhui authored
449 450 451 452 453 454
	condition := map[string]interface{}{
		"companyId": param.CompanyId,
		"cycleId":   param.CycleId,
		"types":     domain.EvaluationHrbp,
		"limit":     limit,
		"offset":    offset,
455
		"beginTime": time.Now(),
tangxvhui authored
456 457 458 459 460
	}
	if len(param.SearchName) > 0 {
		condition["targetUserName"] = "%" + param.SearchName + "%"
	}
	count, list, err := evaluationRepo.Find(condition)
461 462 463 464 465 466 467 468 469 470 471 472 473 474 475
	if err != nil {
		return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
	}

	userIds := make([]int, 0)
	projectIds := make([]int, 0)
	projectCountMap := map[string]int{}         // 自评逾期数量
	userMap := map[int64]*domain.User{}         // 用户
	positionMap := map[int64]*domain.Position{} // 职位

	for i := range list {
		it := list[i]
		userIds = append(userIds, it.TargetUser.UserId)
		projectIds = append(projectIds, list[i].EvaluationProjectId)
	}
476 477 478 479 480 481 482 483
	if len(userIds) > 0 {
		_, users, err := userRepo.Find(map[string]interface{}{"ids": userIds, "companyId": param.CompanyId})
		if err != nil {
			return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
		}
		positionIds := make([]int, 0)
		for i := range users {
			userMap[users[i].Id] = users[i]
tangxvhui authored
484 485
			positionIds = append(positionIds, users[i].PositionId...)
486 487 488 489 490 491 492 493 494
		}
		if len(positionIds) > 0 {
			_, positions, err := positionRepo.Find(map[string]interface{}{"ids": positionIds, "companyId": param.CompanyId})
			if err != nil {
				return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
			}
			for i := range positions {
				positionMap[positions[i].Id] = positions[i]
			}
495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566
		}
	}

	if len(projectIds) > 0 {
		targetCount, err := staffAssessDaoRepo.CountUncompletedSelfAssess(param.CompanyId, projectIds)
		if err != nil {
			return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
		}
		for i := range targetCount {
			key := fmt.Sprintf("%d-%d", targetCount[i].EvaluationProjectId, targetCount[i].TargetUserId)
			projectCountMap[key] = targetCount[i].Cnt
		}
	}

	now := time.Now().Local() // 当前时间
	resultList := make([]*adapter.SummaryEvaluationHRBPAdapter, 0)
	for i := range list {
		v := list[i]
		endTime := v.EndTime.Local()
		// 状态
		statusVal := ""
		if v.Status == domain.EvaluationCompleted {
			statusVal = "已完成"
		} else {
			if now.After(endTime) {
				statusVal = "已逾期"
			} else {
				statusVal = "待完成"
			}
		}

		// 部门拼接
		var departmentBuild strings.Builder
		departmentBuild.WriteString("")
		for i2 := range v.TargetDepartment {
			departmentBuild.WriteString(v.TargetDepartment[i2].DepartmentName)
			if i2 != len(v.TargetDepartment)-1 {
				departmentBuild.WriteString(",")
			}
		}
		// 入职时间
		entryTime := ""
		// 职位拼接
		var positionBuild strings.Builder
		positionBuild.WriteString("")
		if user, ok := userMap[int64(v.TargetUser.UserId)]; ok {
			for i2 := range user.PositionId {
				if position, ok := positionMap[int64(user.PositionId[i2])]; ok {
					positionBuild.WriteString(position.Name)
					if i2 != len(user.PositionId)-1 {
						departmentBuild.WriteString(",")
					}
				}
			}
			entryTime = user.EntryTime
		}

		// 自评逾期数量
		overdueCount := 0
		key := fmt.Sprintf("%d-%d", v.EvaluationProjectId, v.TargetUser.UserId)
		if cnt, ok := projectCountMap[key]; ok {
			overdueCount = cnt
		}

		result := &adapter.SummaryEvaluationHRBPAdapter{
			Id:             v.Id,
			TargetUserId:   v.TargetUser.UserId,
			TargetUserName: v.TargetUser.UserName,
			Department:     departmentBuild.String(),
			Position:       positionBuild.String(),
			DutyTime:       entryTime,
			Status:         statusVal,
郑周 authored
567
			EndTime:        endTime.Local().Format("2006-01-02 15:04"),
568 569 570 571 572 573 574 575 576
			OverdueCount:   overdueCount,
		}
		resultList = append(resultList, result)
	}

	if err := transactionContext.CommitTransaction(); err != nil {
		return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
	}
郑周 authored
577 578 579 580 581 582 583 584 585
	// 外层显示截止时间
	endTime := ""
	if len(list) > 0 {
		endTime = list[0].EndTime.Local().Format("2006-01-02 15:04:05")
	}
	grid := tool_funs.SimpleWrapGridMap(int64(count), resultList)
	grid["endTime"] = endTime
	return grid, nil
	//return tool_funs.SimpleWrapGridMap(int64(count), resultList), nil
586 587
}
588
// GetEvaluationHRBP 获取人资综评详情
589
func (srv *SummaryEvaluationService) GetEvaluationHRBP(param *command.QueryEvaluationHRBPMy) (map[string]interface{}, error) {
590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630
	transactionContext, err := factory.ValidateStartTransaction(param)
	if err != nil {
		return nil, err
	}
	defer func() {
		_ = transactionContext.RollbackTransaction()
	}()

	evaluationRepo := factory.CreateSummaryEvaluationRepository(map[string]interface{}{"transactionContext": transactionContext})
	evaluationItemRepo := factory.CreateEvaluationItemUsedRepository(map[string]interface{}{"transactionContext": transactionContext})
	itemValueRepo := factory.CreateSummaryEvaluationValueRepository(map[string]interface{}{"transactionContext": transactionContext})

	sEvaluation, err := evaluationRepo.FindOne(map[string]interface{}{"id": param.SummaryEvaluationId})
	if err != nil {
		return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
	}

	// 自评评估内容(自评模板、筛选项目评估人)
	_, itemList, err := evaluationItemRepo.Find(map[string]interface{}{
		"evaluationProjectId": sEvaluation.EvaluationProjectId,
		"nodeType":            domain.LinkNodeSelfAssessment,
		"evaluatorId":         -1,
	})
	if err != nil {
		return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
	}

	// 评估内容对应的分数
	_, itemValues, err := itemValueRepo.Find(map[string]interface{}{"summaryEvaluationId": sEvaluation.Id})
	if err != nil {
		return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
	}

	if err := transactionContext.CommitTransaction(); err != nil {
		return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
	}

	itemValuesAdapter := srv.buildSummaryItemValue(itemList, itemValues)
	for i := range itemValuesAdapter { // 评估人名称赋值
		itemValuesAdapter[i].EvaluatorName = sEvaluation.Executor.UserName
	}
631 632 633 634

	// 评估项截止时间
	endTime := sEvaluation.EndTime.Local().Format("2006-01-02 15:04:05")
	return map[string]interface{}{"endTime": endTime, "list": itemValuesAdapter}, nil
635 636 637 638
}

// GetEvaluationHRBPComplete 获取人资综评详情(已完成状态)
func (srv *SummaryEvaluationService) GetEvaluationHRBPComplete(param *command.QueryEvaluationHRBP) (*[]adapter.EvaluationItemAdapter, error) {
639 640 641 642 643 644 645 646 647 648 649 650
	transactionContext, err := factory.ValidateStartTransaction(param)
	if err != nil {
		return nil, err
	}
	defer func() {
		_ = transactionContext.RollbackTransaction()
	}()

	evaluationRepo := factory.CreateSummaryEvaluationRepository(map[string]interface{}{"transactionContext": transactionContext})
	evaluationItemRepo := factory.CreateEvaluationItemUsedRepository(map[string]interface{}{"transactionContext": transactionContext})
	itemValueRepo := factory.CreateSummaryEvaluationValueRepository(map[string]interface{}{"transactionContext": transactionContext})
651
	_, evaluations, err := evaluationRepo.Find(map[string]interface{}{
652
		"limit":        99999,
653 654 655
		"companyId":    param.CompanyId,
		"cycleId":      param.CycleId,
		"targetUserId": param.TargetUserId,
656
		"status":       domain.EvaluationCompleted,
657 658
		"types":        domain.EvaluationHrbp},
	)
659 660 661
	if err != nil {
		return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
	}
郑周 authored
662 663 664 665 666 667

	if len(evaluations) == 0 {
		list := make([]adapter.EvaluationItemAdapter, 0)
		return &list, nil
	}
668
	evaluationIds := make([]int, 0) // 评估周期ID
郑周 authored
669 670
	for i := range evaluations {
		evaluationIds = append(evaluationIds, evaluations[i].Id)
671
	}
郑周 authored
672 673
	// 得到项目ID
	evaluationProjectId := evaluations[0].EvaluationProjectId
674 675 676

	// 自评评估内容(自评模板、筛选项目评估人)
	_, itemList, err := evaluationItemRepo.Find(map[string]interface{}{
郑周 authored
677
		"evaluationProjectId": evaluationProjectId,
678 679 680 681 682 683 684 685
		"nodeType":            domain.LinkNodeSelfAssessment,
		"evaluatorId":         -1,
	})
	if err != nil {
		return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
	}

	// 评估内容对应的分数
郑周 authored
686 687 688 689
	_, itemValues, err := itemValueRepo.Find(map[string]interface{}{
		"summaryEvaluationIdList": evaluationIds,
		"types":                   domain.EvaluationHrbp,
	})
690 691 692 693 694 695 696 697 698
	if err != nil {
		return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
	}

	if err := transactionContext.CommitTransaction(); err != nil {
		return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
	}

	itemValuesAdapter := srv.buildSummaryItemValue(itemList, itemValues)
郑周 authored
699
	for i := range itemValuesAdapter { // 评估人名称赋值
700
		itemValuesAdapter[i].EvaluatorName = evaluations[0].Executor.UserName
郑周 authored
701
	}
郑周 authored
702
	return &itemValuesAdapter, nil
703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724
}

// EditEvaluationHRBP 编辑提交人资综评
func (srv *SummaryEvaluationService) EditEvaluationHRBP(param *command.EditEvaluationValue) (map[string][]adapter.EvaluationItemAdapter, error) {
	transactionContext, err := factory.CreateTransactionContext(nil)
	if err != nil {
		return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
	}
	if err := transactionContext.StartTransaction(); err != nil {
		return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
	}
	defer func() {
		_ = transactionContext.RollbackTransaction()
	}()

	userRepo := factory.CreateUserRepository(map[string]interface{}{"transactionContext": transactionContext})
	evaluationRepo := factory.CreateSummaryEvaluationRepository(map[string]interface{}{"transactionContext": transactionContext})
	itemUsedRepo := factory.CreateEvaluationItemUsedRepository(map[string]interface{}{"transactionContext": transactionContext})
	itemValueRepo := factory.CreateSummaryEvaluationValueRepository(map[string]interface{}{"transactionContext": transactionContext})

	// 必须是HRBP权限的人才能编辑操作
	hrbp, err := service.GetHRBP(transactionContext, param.CompanyId, param.ExecutorId)
tangxvhui authored
725 726 727
	if err != nil {
		return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
	}
728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758
	if hrbp != 1 {
		return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, "没有操作权限")
	}

	summaryEvaluation, err := evaluationRepo.FindOne(map[string]interface{}{"id": param.SummaryEvaluationId})
	if err != nil {
		return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
	}
	if summaryEvaluation.Types != domain.EvaluationHrbp {
		return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, "没有操作权限")
	}
	if summaryEvaluation.CompanyId != param.CompanyId {
		return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, "没有操作权限")
	}

	// 自评评估内容(自评模板、筛选项目评估人)
	_, itemList, err := itemUsedRepo.Find(map[string]interface{}{
		"evaluationProjectId": summaryEvaluation.EvaluationProjectId,
		"nodeType":            domain.LinkNodeSelfAssessment,
		"evaluatorId":         -1,
	})
	if err != nil {
		return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
	}

	// 评估内容对应的分数
	_, itemValueList, err := itemValueRepo.Find(map[string]interface{}{"summaryEvaluationId": summaryEvaluation.Id})
	if err != nil {
		return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
	}
759
	err = srv.editEvaluationValue(summaryEvaluation, &itemValueList, itemList, param.EvaluationItems, nil, param.IsTemporary)
760 761
	if err != nil {
		return nil, application.ThrowError(application.BUSINESS_ERROR, err.Error())
762
	}
763 764 765 766
	if !param.IsTemporary {
		//变更评估状态为已填写
		summaryEvaluation.Status = domain.EvaluationCompleted
	}
767
	//保存填写值
768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783
	for _, v := range itemValueList {
		err = itemValueRepo.Save(v)
		if err != nil {
			return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
		}
	}

	// 针对HRBP类型,更新最后一次操作的HRBP人
	user, err := userRepo.FindOne(map[string]interface{}{"id": param.ExecutorId})
	if err != nil {
		return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
	}
	summaryEvaluation.Executor.UserId = param.ExecutorId
	summaryEvaluation.Executor.Account = user.Account
	summaryEvaluation.Executor.UserName = user.Name
	summaryEvaluation.Executor.CompanyName = "" // 暂时无用
784
	summaryEvaluation.Status = domain.EvaluationCompleted
785 786 787 788 789 790 791 792 793

	//保存填写值
	err = evaluationRepo.Save(summaryEvaluation)
	if err != nil {
		return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
	}
	if err := transactionContext.CommitTransaction(); err != nil {
		return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
	}
794 795 796 797 798 799 800

	if !param.IsTemporary {
		err = srv.AfterCompletedEvaluation360Hrbp(summaryEvaluation)
		if err != nil {
			return nil, err
		}
	}
801
	itemValueAdapter := srv.buildSummaryItemValue(itemList, itemValueList)
802 803 804 805
	return map[string][]adapter.EvaluationItemAdapter{
		"evaluationItems": itemValueAdapter,
	}, nil
}