list_evaluation.go 5.9 KB
package service

import (
	"fmt"
	"strings"

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

//获取周期评估的列表 ,根据页面需要提供不同的结构变体

// 人资稽查 页面列表数据输出
// 根据周期id 获取类型为"EvaluationFinish" 的全部周期评估
func (srv *SummaryEvaluationService) ListEvaluationShow1(param command.QueryEvaluationList) (result map[string]interface{}, err error) {
	transactionContext, err := factory.ValidateStartTransaction(param)
	if err != nil {
		return nil, err
	}
	defer func() {
		_ = transactionContext.RollbackTransaction()
	}()

	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})
	staffAssessRepo := factory.CreateStaffAssessRepository(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.EvaluationFinish,
		"limit":     limit,
		"offset":    offset,
	}
	if len(param.TargetUserName) > 0 {
		condition["targetUserName"] = "%" + param.TargetUserName + "%"
	}
	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
		}
	}
	resultList := make([]*adapter.EvaluationItemAdapter2, 0)
	for i := range list {
		v := list[i]
		endTime := v.EndTime.Local()
		// 检查15日自评 完成情况状态
		statusVal := "未完成"
		_, assessList, err := staffAssessRepo.Find(map[string]interface{}{
			"cycleId":        v.CycleId,
			"targetUserId":   v.TargetUser.UserId,
			"beginDayNumber": 15,
		})
		if err == nil && len(assessList) > 0 {
			if assessList[0].Status == domain.StaffAssessCompleted {
				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.EvaluationItemAdapter2{
			CycleId:        int(v.CycleId),
			EvaluationId:   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

}