get_evaluation_360.go 3.6 KB
package service

import (
	"github.com/linmadan/egglib-go/core/application"
	"gitlab.fjmaimaimai.com/allied-creation/performance/pkg/application/factory"
	"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"
)

// GetEvaluation360Complete 获取360综评详情(他人评估目标用户)(已完成状态)
func (srv *SummaryEvaluationService) GetEvaluation360CompleteV2(param *command.QueryEvaluation360) (*adapter.EvaluationInfoSelfAdapter, 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":        1000,
	})
	if err != nil {
		return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
	}

	if len(evaluations) == 0 {
		list := make([]adapter.EvaluationItemAdapter, 0)
		res := adapter.EvaluationInfoSelfAdapter{
			EvaluationItems: list,
		}
		return &res, 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])
		}
	}
	// 获取组装基本信息
	evaluationBase := srv.getSummaryEvaluation(transactionContext, evaluations[0])
	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.EvaluationInfoSelfAdapter{
		EvaluationBaseAdapter: evaluationBase,
		EvaluationItems:       itemValuesAdapter,
	}
	return &result, nil
}