service2.go 32.8 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 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 105 106 107 108 109 110 111 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 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 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 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 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 325 326 327 328 329 330 331 332 333 334 335 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 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 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 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 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 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 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 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896
package service

import (
	"fmt"
	"strconv"
	"strings"
	"time"

	"github.com/linmadan/egglib-go/core/application"
	"github.com/linmadan/egglib-go/utils/tool_funs"
	"gitlab.fjmaimaimai.com/allied-creation/performance/pkg/application/factory"
	roleService "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"
	"gitlab.fjmaimaimai.com/allied-creation/performance/pkg/infrastructure/dao"
)

// Evaluation360List 获取360综评列表
func (srv *SummaryEvaluationService) Evaluation360List(param *command.QueryEvaluation360List) (map[string]interface{}, error) {
	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{}{
		"companyId":      param.CompanyId,
		"cycleId":        param.CycleId,
		"executorId":     param.UserId,
		"types":          domain.Evaluation360,
		"targetUserName": searchTargetName,
		"limit":          limit,
		"offset":         offset,
		"beginTime":      time.Now(),
	})
	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综评人的评估内容)
		_, itemList, err := evaluationItemRepo.Find(map[string]interface{}{"evaluationProjectIds": projectIds, "nodeType": domain.LinkNodeSelfAssessment, "evaluatorId": param.UserId})
		if err != nil {
			return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
		}
		// 填写值
		_, valueList, err := itemValueRepo.Find(map[string]interface{}{"summaryEvaluationIdList": ids})
		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
			}

			pList, ok := itemMap[item.EvaluationProjectId]
			if !ok {
				pList = make([]*domain.EvaluationItemUsed, 0)
			}
			itemMap[item.EvaluationProjectId] = append(pList, item)
		}
	}

	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
		rowMap["targetUserId"] = strconv.Itoa(v.TargetUser.UserId)
		rowMap["targetUserName"] = v.TargetUser.UserName
		rowMap["statusVal"] = statusVal
		rowMap["relation"] = "项目组员"
		rowMap["endTime"] = endTime.Local().Format("2006-01-02 15:04")

		// 当前项目所有评估内容
		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 {
						if it.Weight > 0 {
							rowMap[key] = it.Score
						} else {
							rowMap[key] = it.Rating.Code
						}
					} 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)
	}

	// 外层显示截止时间
	endTime := ""
	if len(list) > 0 {
		endTime = list[0].EndTime.Local().Format("2006-01-02 15:04:05")
	}

	result := &adapter.SummaryEvaluationAdapter{
		TableHeader: changeableHeader,
		Total:       count,
		List:        targetList,
	}
	grid := map[string]interface{}{
		"grid":    result,
		"endTime": endTime,
	}
	return grid, nil
}

// GetEvaluation360 获取360综评详情(我评估目标用户)
func (srv *SummaryEvaluationService) GetEvaluation360(param *command.QueryEvaluation360My) (map[string]interface{}, error) {
	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())
	}
	if err != nil {
		return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
	}

	// 360评估内容(自评模板、筛选项目评估人)
	_, itemList, err := evaluationItemRepo.Find(map[string]interface{}{
		"evaluationProjectId": sEvaluation.EvaluationProjectId,
		"nodeType":            domain.LinkNodeSelfAssessment,
		"companyId":           param.CompanyId,
		"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)
	for i := range itemValuesAdapter { // 评估人名称赋值
		itemValuesAdapter[i].EvaluatorName = sEvaluation.Executor.UserName
	}
	// 评估项截止时间
	endTime := sEvaluation.EndTime.Local().Format("2006-01-02 15:04:05")
	return map[string]interface{}{"endTime": endTime, "list": itemValuesAdapter}, nil
}

// GetEvaluation360Complete 获取360综评详情(他人评估目标用户)(已完成状态)
func (srv *SummaryEvaluationService) GetEvaluation360Complete(param *command.QueryEvaluation360) (*[]adapter.EvaluationItemAdapter, error) {
	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,
		"types":        domain.Evaluation360,
		"status":       domain.EvaluationCompleted,
		"limit":        99999,
	})
	if err != nil {
		return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
	}

	if len(evaluations) == 0 {
		list := make([]adapter.EvaluationItemAdapter, 0)
		return &list, nil
	}

	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
	// 360评估内容(自评模板)
	_, itemList, err := evaluationItemRepo.Find(map[string]interface{}{
		"evaluationProjectId": evaluationProjectId,
		"nodeType":            domain.LinkNodeSelfAssessment,
	})
	if err != nil {
		return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
	}

	// 评估内容对应的分数
	_, itemValues, err := itemValueRepo.Find(map[string]interface{}{
		"summaryEvaluationIdList": evaluationIds,
		"types":                   domain.Evaluation360,
	})
	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 { // 评估人名称赋值
		if executor, ok := executorMap[itemValuesAdapter[i].EvaluatorId]; ok {
			itemValuesAdapter[i].EvaluatorName = executor.UserName
		}
	}

	//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
}

// EditEvaluation360 编辑提交360综评
// func (srv *SummaryEvaluationService) EditEvaluation360(param *command.EditEvaluationValue) (map[string][]adapter.EvaluationItemAdapter, error) {
// 	lock := xredis.NewLockSummaryEvaluationId(param.SummaryEvaluationId)
// 	err := lock.Lock()
// 	if err != nil {
// 		return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, "未能完全提交评估内容")
// 	}
// 	defer func() {
// 		lock.UnLock()
// 	}()
// 	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, "没有操作权限")
// 	}

// 	_, evaluationList, err := evaluationRepo.Find(map[string]interface{}{
// 		"targetUserId": summaryEvaluation.TargetUser.UserId,
// 		"typesList":    []int{int(domain.EvaluationFinish)},
// 		"cycleId":      summaryEvaluation.CycleId,
// 		"limit":        1,
// 	})
// 	if err != nil {
// 		return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
// 	}
// 	// 如果目标员工已经确认考核结果,就不能在进行评估编辑
// 	if len(evaluationList) > 0 && evaluationList[0].CheckResult == domain.EvaluationCheckCompleted {
// 		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())
// 	}

// 	err = srv.editEvaluationValue(summaryEvaluation, &itemValueList, itemList, param.EvaluationItems, nil, param.IsTemporary)
// 	if err != nil {
// 		return nil, application.ThrowError(application.BUSINESS_ERROR, err.Error())
// 	}
// 	if !param.IsTemporary {
// 		//变更评估状态为已填写
// 		summaryEvaluation.Status = domain.EvaluationCompleted
// 	}

// 	//保存填写值
// 	for _, v := range itemValueList {
// 		err = itemValueRepo.Save(v)
// 		if err != nil {
// 			return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
// 		}
// 	}

// 	//保存填写值
// 	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())
// 	}

// 	if !param.IsTemporary {
// 		err = srv.AfterCompletedEvaluation360Hrbp(summaryEvaluation)
// 		if err != nil {
// 			return nil, err
// 		}
// 	}

// 	itemValueAdapter := srv.buildSummaryItemValue(itemList, itemValueList)
// 	return map[string][]adapter.EvaluationItemAdapter{
// 		"evaluationItems": itemValueAdapter,
// 	}, nil
// }

// 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()
	}()

	// 必须是HRBP权限的人才能编辑操作
	hrbp, err := roleService.GetHrBp(transactionContext, param.CompanyId, param.UserId)
	if err != nil {
		return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
	}
	if hrbp != domain.RoleTypeSystem {
		return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, "没有操作权限")
	}

	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})

	limit := param.PageSize
	offset := limit * (param.PageNumber - 1)
	if offset < 0 {
		offset = 0
	}
	condition := map[string]interface{}{
		"companyId": param.CompanyId,
		"cycleId":   param.CycleId,
		"types":     domain.EvaluationHrbp,
		"limit":     limit,
		"offset":    offset,
		"beginTime": time.Now(),
	}
	if len(param.SearchName) > 0 {
		condition["targetUserName"] = "%" + param.SearchName + "%"
	}
	count, list, err := evaluationRepo.Find(condition)
	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)
	}
	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]
			positionIds = append(positionIds, users[i].PositionId...)

		}
		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]
			}
		}
	}

	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,
			EndTime:        endTime.Local().Format("2006-01-02 15:04"),
			OverdueCount:   overdueCount,
		}
		resultList = append(resultList, result)
	}

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

	// 外层显示截止时间
	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
}

// GetEvaluationHRBP 获取人资综评详情
func (srv *SummaryEvaluationService) GetEvaluationHRBP(param *command.QueryEvaluationHRBPMy) (map[string]interface{}, error) {
	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
	}

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

// GetEvaluationHRBPComplete 获取人资综评详情(已完成状态)
func (srv *SummaryEvaluationService) GetEvaluationHRBPComplete(param *command.QueryEvaluationHRBP) (*[]adapter.EvaluationItemAdapter, error) {
	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{}{
		"limit":        99999,
		"companyId":    param.CompanyId,
		"cycleId":      param.CycleId,
		"targetUserId": param.TargetUserId,
		"status":       domain.EvaluationCompleted,
		"types":        domain.EvaluationHrbp},
	)
	if err != nil {
		return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
	}

	if len(evaluations) == 0 {
		list := make([]adapter.EvaluationItemAdapter, 0)
		return &list, nil
	}

	evaluationIds := make([]int, 0) // 评估周期ID
	for i := range evaluations {
		evaluationIds = append(evaluationIds, evaluations[i].Id)
	}
	// 得到项目ID
	evaluationProjectId := evaluations[0].EvaluationProjectId

	// 自评评估内容(自评模板、筛选项目评估人)
	_, itemList, err := evaluationItemRepo.Find(map[string]interface{}{
		"evaluationProjectId": 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{}{
		"summaryEvaluationIdList": evaluationIds,
		"types":                   domain.EvaluationHrbp,
	})
	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 = evaluations[0].Executor.UserName
	}
	return &itemValuesAdapter, nil
}

// ModifyFinishScore 修改周期考核的分数
func (srv *SummaryEvaluationService) ModifyFinishScore(param *command.ModifyFinishScore) error {
	transactionContext, err := factory.CreateTransactionContext(nil)
	if err != nil {
		return application.ThrowError(application.TRANSACTION_ERROR, err.Error())
	}
	if err := transactionContext.StartTransaction(); err != nil {
		return application.ThrowError(application.TRANSACTION_ERROR, err.Error())
	}
	defer func() {
		_ = transactionContext.RollbackTransaction()
	}()
	// 只有超级管理员可以使用的功能
	superAdmin, err := roleService.GetSuperAdmin(transactionContext, param.CompanyId, param.UserId)
	if err != nil {
		return err
	}
	if superAdmin != domain.RoleTypeSuperAdmin {
		return application.ThrowError(application.INTERNAL_SERVER_ERROR, "没有操作权限")
	}

	// 获取周期评估任务finish
	evaluationRepo := factory.CreateSummaryEvaluationRepository(map[string]interface{}{"transactionContext": transactionContext})
	itemValueRepo := factory.CreateSummaryEvaluationValueRepository(map[string]interface{}{"transactionContext": transactionContext})

	_, evaluationList, err := evaluationRepo.Find(map[string]interface{}{"id": []int{param.SummaryEvaluationId}, "companyId": param.CompanyId})
	if err != nil {
		return application.ThrowError(application.BUSINESS_ERROR, err.Error())
	}
	if len(evaluationList) == 0 {
		return nil
	}
	result := evaluationList[0]
	if result.Types != domain.EvaluationFinish {
		return nil
	}

	// 获取考核结果评分
	_, itemValues, err := itemValueRepo.Find(map[string]interface{}{"summaryEvaluationId": result.Id})
	if err != nil {
		return application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
	}
	// 如果已经存在考核结果分数,只更新分数(反之先默认生成考核结果,再更新分数)
	if len(itemValues) > 0 {
		// do nothing...
	} else {
		evaluationItemRepo := factory.CreateEvaluationItemUsedRepository(map[string]interface{}{"transactionContext": transactionContext})

		// 周期内所有评估类型
		_, evaluationList, err := evaluationRepo.Find(map[string]interface{}{
			"companyId":    result.CompanyId,
			"cycleId":      result.CycleId,
			"targetUserId": result.TargetUser.UserId,
		})
		if err != nil {
			return application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
		}

		var super *domain.SummaryEvaluation // 上级评估
		for i := range evaluationList {
			it := evaluationList[i]
			if it.Types == domain.EvaluationSuper {
				super = it
				break
			}
		}

		// 获取自评模板
		_, itemList, err := evaluationItemRepo.Find(map[string]interface{}{"evaluationProjectId": result.EvaluationProjectId, "nodeType": domain.LinkNodeSelfAssessment})
		if err != nil {
			return application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
		}

		if super != nil {
			_, superValues, err := itemValueRepo.Find(map[string]interface{}{"summaryEvaluationId": super.Id}) // 获取已填写的评估内容
			if err != nil {
				return application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
			}
			// 更新填写值
			itemValues, err = srv.updateItemValuePriority(result, itemList, superValues, true)
			if err != nil {
				return application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
			}
		} else {
			// 评估项ID(除考核结果和上级)
			var evaluationIds = make([]int, 0)
			for i := range evaluationList {
				it := evaluationList[i]
				if it.Types == domain.EvaluationSelf || it.Types == domain.Evaluation360 || it.Types == domain.EvaluationHrbp {
					evaluationIds = append(evaluationIds, it.Id)
				}
			}
			if len(evaluationIds) > 0 {
				// 已填写的评估内容
				_, otherValues, err := itemValueRepo.Find(map[string]interface{}{"summaryEvaluationIdList": evaluationIds})
				if err != nil {
					return application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
				}

				// 更新填写值
				itemValues, err = srv.updateItemValuePriority(result, itemList, otherValues, false)
				if err != nil {
					return application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
				}
			}
		}
		err = itemValueRepo.RemoveBySummaryEvaluationId(result.Id)
		if err != nil {
			return application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
		}
		for i := range itemValues {
			if err := itemValueRepo.Save(itemValues[i]); err != nil {
				return application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
			}
		}
		// 重置评级汇总
		result.TotalRating = nil
		for i := range itemList {
			result.ResetTotalRating(itemList[i])
		}
		//// 计算分数
		//if err := result.EvaluationTotalScore(itemValues); err != nil {
		//	return application.ThrowError(application.TRANSACTION_ERROR, err.Error())
		//}
	}

	scoreStr := fmt.Sprintf("%.2f", param.Score)
	result.TotalScore = scoreStr
	result.Status = domain.EvaluationCompleted
	err = evaluationRepo.Save(result)
	if err != nil {
		return application.ThrowError(application.BUSINESS_ERROR, "修改考核结果"+err.Error())
	}
	if err := transactionContext.CommitTransaction(); err != nil {
		return application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
	}
	return nil
}

// GetUnconfirmedCycleList 获取未确认绩效成绩的周期列表
func (srv *SummaryEvaluationService) GetUnconfirmedCycleList(companyId int, userId int) (map[string]interface{}, error) {
	transactionContext, err := factory.StartTransaction()
	if err != nil {
		return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
	}
	defer func() {
		_ = transactionContext.RollbackTransaction()
	}()
	evaluationDao := dao.NewSummaryEvaluationDao(map[string]interface{}{"transactionContext": transactionContext})

	cycles, err := evaluationDao.TargetUnconfirmedCycleList(companyId, userId)
	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())
	}
	var cycleList []adapter.CycleListAdapter
	nowTime := time.Now()
	for _, v := range cycles {
		if nowTime.Before(v.EndTime) {
			continue
		}
		cycleList = append(cycleList, adapter.CycleListAdapter{
			CycleId:   v.CycleId,
			CycleName: v.CycleName,
		})
	}
	return tool_funs.SimpleWrapGridMap(int64(len(cycleList)), cycleList), nil
}