get_evaluation_hrbp.go 3.2 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"
)

// GetEvaluationHRBPComplete 获取人资综评详情(他人评估目标用户)(已完成状态)
func (srv *SummaryEvaluationService) GetEvaluationHRBPCompleteV2(param *command.QueryEvaluationHRBP) (*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{}{
		"limit":        2,
		"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)
		res := adapter.EvaluationInfoSelfAdapter{
			EvaluationItems: list,
		}
		return &res, 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())
	}
	// 获取组装基本信息
	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(itemList, itemValues)
	for i := range itemValuesAdapter { // 评估人名称赋值
		itemValuesAdapter[i].EvaluatorName = evaluations[0].Executor.UserName
	}
	result := adapter.EvaluationInfoSelfAdapter{
		EvaluationBaseAdapter: evaluationBase,
		EvaluationItems:       itemValuesAdapter,
	}
	return &result, nil
}