export.go 5.8 KB
package service

import (
	"fmt"
	"strconv"

	"github.com/linmadan/egglib-go/core/application"
	"github.com/xuri/excelize/v2"
	"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/command"
	"gitlab.fjmaimaimai.com/allied-creation/performance/pkg/domain"
)

// 导出数据
// 综合管理-周期评估
func (srv *SummaryEvaluationService) ExportAllEvaluationSuper(param *command.QueryEvaluationList) (*excelize.File, 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()
	}()
	//判断是否是hrbp
	flagHrbp, err := roleService.GetHRBP(transactionContext, param.CompanyId, param.UserId)
	if err != nil {
		return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
	}
	if flagHrbp != 1 {
		return nil, application.ThrowError(application.BUSINESS_ERROR, "暂无数据")
	}
	//判断是否是上级
	userRepo := factory.CreateUserRepository(map[string]interface{}{
		"transactionContext": transactionContext,
	})
	_, parentUser, _ := userRepo.Find(map[string]interface{}{
		"parentId": param.UserId,
		"limit":    1,
	})
	if len(parentUser) == 0 {
		return nil, application.ThrowError(application.BUSINESS_ERROR, "暂无数据")
	}
	evaluationRepo := factory.CreateSummaryEvaluationRepository(map[string]interface{}{
		"transactionContext": transactionContext,
	})
	positionRepo := factory.CreatePositionRepository(map[string]interface{}{
		"transactionContext": transactionContext,
	})
	limit := param.PageSize
	offset := (param.PageNumber - 1) * param.PageSize

	//获取评估列表信息
	condition1 := map[string]interface{}{
		"cycleId": param.CycleId,
		"types":   int(domain.EvaluationSuper),
		"limit":   limit,
	}
	if offset > 0 {
		condition1["offset"] = offset
	}
	if len(param.TargetUserName) > 0 {
		condition1["targetUserName"] = "%" + param.TargetUserName + "%"
	}
	if len(param.SummaryEvaluationId) > 0 {
		summaryEvaluationIds := []int{}
		for _, v := range param.SummaryEvaluationId {
			id, _ := strconv.Atoi(v)
			summaryEvaluationIds = append(summaryEvaluationIds, id)
		}
		if len(summaryEvaluationIds) > 0 {
			condition1["id"] = summaryEvaluationIds
		}
	}
	//获取评估列表信息
	_, evaluationList, err := evaluationRepo.Find(condition1)
	if err != nil {
		return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
	}
	targetUserIds := []int{}
	//获取员工信息
	userMap := map[int64]*domain.User{}
	for _, v := range evaluationList {
		if _, ok := userMap[int64(v.TargetUser.UserId)]; ok {
			continue
		}
		userMap[int64(v.TargetUser.UserId)] = nil
		targetUserIds = append(targetUserIds, v.TargetUser.UserId)
	}
	if len(targetUserIds) > 0 {
		_, userList, err := userRepo.Find(map[string]interface{}{
			"ids": targetUserIds,
		})
		if err != nil {
			return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
		}
		userMap = map[int64]*domain.User{}
		for _, v := range userList {
			userMap[v.Id] = v
		}
	}
	var positionIds []int
	//获取职位列表
	positionMap := map[int64]*domain.Position{}
	for _, v := range userMap {
		for _, v2 := range v.DepartmentId {
			if _, ok := positionMap[int64(v2)]; ok {
				continue
			}
			positionMap[int64(v2)] = nil
			positionIds = append(positionIds, v2)
		}
	}
	if len(positionIds) > 0 {
		_, positionList, err := positionRepo.Find(map[string]interface{}{"ids": positionIds})
		if err != nil {
			return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, "获取部门信息"+err.Error())
		}
		positionMap = map[int64]*domain.Position{}
		for _, v := range positionList {
			positionMap[v.Id] = v
		}
	}
	if err := transactionContext.CommitTransaction(); err != nil {
		return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
	}
	ratingHeader := []string{}         //动态列,评级内容
	ratingMap := map[string]struct{}{} //过滤重复code
	evaluationRatingMap := map[int]map[string]int{}
	for _, v := range evaluationList {
		evaluationRatingMap[v.Id] = map[string]int{}
		for _, v2 := range v.TotalRating {
			if _, ok := ratingMap[v2.Code]; !ok {
				ratingMap[v2.Code] = struct{}{}
				ratingHeader = append(ratingHeader, v2.Code)
			}
			evaluationRatingMap[v.Id][v2.Code] = v2.Number
		}
	}

	xlsxFile := excelize.NewFile()
	//设置默认的第一个sheet
	sheetIndex := xlsxFile.GetActiveSheetIndex()
	firstSheetName := xlsxFile.GetSheetName(sheetIndex)
	tableHead := []string{"姓名", "部门", "职位", "最终绩效得分"}
	tableHead = append(tableHead, ratingHeader...)
	xlsxFile.SetSheetRow(firstSheetName, "A1", &tableHead)
	for i, v := range evaluationList {
		departmentName := ""
		for _, dep := range v.TargetDepartment {
			departmentName += dep.DepartmentName + " "
		}
		//填充员工信息
		positinName := ""
		if targetUser, ok := userMap[int64(v.TargetUser.UserId)]; ok {
			//填充职位信息
			for _, positionId := range targetUser.PositionId {
				if position, ok := positionMap[int64(positionId)]; ok {
					positinName += position.Name + " "
				}
			}
		}
		dataRaw := []string{
			v.TargetUser.UserName,
			departmentName,
			positinName,
			v.TotalScore,
		}
		for _, v2 := range ratingHeader {
			if num, ok := evaluationRatingMap[v.Id][v2]; ok {
				dataRaw = append(dataRaw, fmt.Sprintf("%d", num))
			} else {
				dataRaw = append(dataRaw, "0")
			}
		}
		xlsxFile.SetSheetRow(firstSheetName, fmt.Sprintf("A%d", i+2), &dataRaw)
	}
	return xlsxFile, nil
}