service_2.go 10.8 KB
package service

import (
	"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/staff_assess/adapter"
	"gitlab.fjmaimaimai.com/allied-creation/performance/pkg/application/staff_assess/command"
	"gitlab.fjmaimaimai.com/allied-creation/performance/pkg/application/staff_assess/query"
	"gitlab.fjmaimaimai.com/allied-creation/performance/pkg/domain"
	"gitlab.fjmaimaimai.com/allied-creation/performance/pkg/infrastructure/dao"
	"strconv"
)

// 调试用,手动调用CreateStaffAssessTask
func (srv StaffAssessServeice) InvokCreateStaffAssessTask(param *command.CreateStaffAssessTask) (map[string]interface{}, 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()
	}()

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

func (srv StaffAssessServeice) QuerySummary(in *query.SummaryCommand) (map[string]interface{}, error) {
	transactionContext, err := factory.ValidateStartTransaction(in)
	if err != nil {
		return nil, err
	}
	defer func() {
		transactionContext.RollbackTransaction()
	}()

	hrbp, err := srv.getHRBP(transactionContext, in.CompanyId, in.OperatorId)
	if err != nil {
		return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
	}
	assessDao := dao.NewStaffAssessDao(map[string]interface{}{"transactionContext": transactionContext})
	data, err := assessDao.SummaryAssess(in.CompanyId, in.OperatorId, in.CycleId, in.BeginDay, hrbp)
	if err != nil {
		return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, "获取用户的角色信息列表"+err.Error())
	}

	self := adapter.SummaryBlock{Name: "填写反馈自评"}
	invite := adapter.SummaryBlock{Name: "360°邀请"}
	inviteAssess := adapter.SummaryBlock{Name: "360°评估"}
	supper := adapter.SummaryBlock{Name: "上级评估"}

	inviteTargetIdMap := map[string]int{} // 过滤相同的目标用户ID

	for i := range data {
		d := data[i]
		d.EndTime = d.EndTime.Local() // 输出本地时间

		switch d.Types {
		case domain.AssessSelf:
			self.Total++
			if d.Status == domain.StaffAssessCompleted {
				self.Completed++
			}
			if self.EndTime == nil {
				self.EndTime = &d.EndTime
			}

			invite.Total++ // 发起360邀请的人数 = 自评人数
			if invite.EndTime == nil {
				invite.EndTime = &d.EndTime
			}
			break
		case domain.AssessSuper:
			supper.Total++
			if d.Status == domain.StaffAssessCompleted {
				supper.Completed++
			}
			if supper.EndTime == nil {
				supper.EndTime = &d.EndTime
			}
			break
		case domain.AssessInviteDiffSuper, domain.AssessInviteSameSuper:
			inviteAssess.Total++
			if d.Status == domain.StaffAssessCompleted {
				inviteAssess.Completed++
			}
			if inviteAssess.EndTime == nil {
				inviteAssess.EndTime = &d.EndTime
			}

			inviteTargetIdMap[d.TargetUserId] = 0 // 360评估类型都是被人邀请的评估,过滤相同的目标用户后,就是完成邀请的数量
			break
		}
	}
	invite.Completed = len(inviteTargetIdMap)

	if err := transactionContext.CommitTransaction(); err != nil {
		return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
	}
	return map[string]interface{}{"list": []adapter.SummaryBlock{self, invite, inviteAssess, supper}}, nil
}

func (srv StaffAssessServeice) QueryMemberSummary(in *query.MemberSummaryListCommand) (map[string]interface{}, error) {
	transactionContext, err := factory.ValidateStartTransaction(in)
	if err != nil {
		return nil, err
	}
	defer func() {
		transactionContext.RollbackTransaction()
	}()

	hrbp, err := srv.getHRBP(transactionContext, in.CompanyId, in.OperatorId)
	if err != nil {
		return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
	}
	assessDao := dao.NewStaffAssessDao(map[string]interface{}{"transactionContext": transactionContext})
	userRepository := factory.CreateUserRepository(map[string]interface{}{"transactionContext": transactionContext})
	departmentRepository := factory.CreateDepartmentRepository(map[string]interface{}{"transactionContext": transactionContext})
	positionRepository := factory.CreatePositionRepository(map[string]interface{}{"transactionContext": transactionContext})

	total, list, err := assessDao.MemberSummaryList(in.UserName, in.CompanyId, in.OperatorId, in.CycleId, hrbp, in.PageNumber, in.PageSize)
	if err != nil {
		return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
	}
	userMap := map[int]*domain.User{}
	departmentMap := map[int]*domain.Department{}
	positionMap := map[int]*domain.Position{}

	userIds := make([]int, 0)
	for i := range list {
		id, _ := strconv.Atoi(list[i].TargetUserId)
		userIds = append(userIds, id)
	}
	if len(userIds) > 0 {
		_, users, err := userRepository.Find(map[string]interface{}{"ids": userIds})
		if err != nil {
			return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
		}
		for i := range users {
			user := users[i]
			userMap[int(user.Id)] = user

			for i2 := range user.DepartmentId {
				departmentMap[user.DepartmentId[i2]] = nil
			}
			for i2 := range user.PositionId {
				positionMap[user.PositionId[i2]] = nil
			}
		}
		departmentIds := make([]int, 0)
		positionIds := make([]int, 0)
		for k := range departmentMap {
			departmentIds = append(departmentIds, k)
		}
		for k := range positionMap {
			positionIds = append(positionIds, k)
		}

		if len(departmentIds) > 0 {
			_, departments, err := departmentRepository.Find(map[string]interface{}{"ids": departmentIds})
			if err != nil {
				return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
			}
			for i := range departments {
				departmentMap[int(departments[i].Id)] = departments[i]
			}
		}

		if len(positionIds) > 0 {
			_, positions, err := positionRepository.Find(map[string]interface{}{"ids": positionIds})
			if err != nil {
				return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
			}
			for i := range positions {
				positionMap[int(positions[i].Id)] = positions[i]
			}
		}
	}

	adapters := make([]adapter.MemberSummaryAdapter, 0)
	for i := range list {
		it := list[i]
		id, _ := strconv.Atoi(it.TargetUserId)

		out := adapter.MemberSummaryAdapter{MemberSummaryAssess: it}
		if v := userMap[id]; v != nil {
			out.DepartmentNames = make([]string, 0)
			out.PositionNames = make([]string, 0)

			for i := range v.DepartmentId {
				if v := departmentMap[v.DepartmentId[i]]; v != nil {
					out.DepartmentNames = append(out.DepartmentNames, v.Name)
				}
			}
			for i := range v.PositionId {
				if v := positionMap[v.PositionId[i]]; v != nil {
					out.PositionNames = append(out.PositionNames, v.Name)
				}
			}
		}
		adapters = append(adapters, out)
	}

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

	return tool_funs.SimpleWrapGridMap(int64(total), adapters), nil
}

func (srv StaffAssessServeice) QueryMemberPerformanceIndicator(in *query.MemberPerformanceIndicatorCommand) (map[string]interface{}, error) {
	transactionContext, err := factory.ValidateStartTransaction(in)
	if err != nil {
		return nil, err
	}
	defer func() {
		transactionContext.RollbackTransaction()
	}()

	hrbp, err := srv.getHRBP(transactionContext, in.CompanyId, in.OperatorId)
	if err != nil {
		return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
	}
	assessDao := dao.NewStaffAssessDao(map[string]interface{}{"transactionContext": transactionContext})
	list, err := assessDao.MemberPerformanceIndicator(
		in.UserName,
		in.CompanyId,
		in.OperatorId,
		in.CycleId,
		hrbp,
		string(domain.AssessSelf),
		in.UserIds)
	if err != nil {
		return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
	}

	// (全部周期数据)筛选出第一个周期的数据,减少后续数据遍历
	dupleMap := map[string]string{}
	dupleList := make([]dao.PerformanceIndicatorAssess, 0)
	for i := range list {
		it := list[i]
		if v, ok := dupleMap[it.TargetUserId]; ok {
			if v == it.BeginDay {
				dupleList = append(dupleList, it)
			}
		} else {
			dupleMap[it.TargetUserId] = it.BeginDay
			dupleList = append(dupleList, it)
		}
	}

	piaMap := map[string]*adapter.PerformanceIndicatorAdapter{}
	categoryMap := map[string]int{} // 内容分类
	//categoryNameMap := map[string]int{} // 内容分类下的名称

	adapterList := make([]*adapter.PerformanceIndicatorAdapter, 0)
	for i := range dupleList {
		it := dupleList[i]

		var pia *adapter.PerformanceIndicatorAdapter
		if v, ok := piaMap[it.TargetUserId]; ok {
			pia = v
		} else {
			pia = &adapter.PerformanceIndicatorAdapter{
				TargetUserId:   it.TargetUserId,
				TargetUserName: it.TargetUserName,
				PIContents:     make([]adapter.PIContent, 0),
			}
			piaMap[it.TargetUserId] = pia
			adapterList = append(adapterList, pia)
		}

		// 分类名称有值才能合并分类数组
		if len(it.ContentCategory) > 0 {
			onlyKey1 := it.TargetUserId + it.ContentCategory
			if _, ok := categoryMap[onlyKey1]; !ok {
				categoryMap[onlyKey1] = 0

				// 不存在分类时,创建分类内容
				pia.PIContents = append(pia.PIContents, adapter.PIContent{
					Category: it.ContentCategory,
					Names:    make([]string, 0),
				})
			}

			// 内容名称有值合并到数组
			if len(it.ContentName) > 0 {
				// 周期筛选过的,不再需要唯一值验证判断,直接添加到对应的分类下
				for index := range pia.PIContents {
					piContent := &pia.PIContents[index]
					if piContent.Category == it.ContentCategory {
						piContent.Names = append(piContent.Names, it.ContentName)
						break
					}
				}
				//onlyKey2 := it.TargetUserId + it.ContentCategory + it.ContentName
				//if _, ok := categoryNameMap[onlyKey2]; !ok {
				//	categoryNameMap[onlyKey2] = 0
				//
				//	for index := range pia.PIContents {
				//		piContent := pia.PIContents[index]
				//		if piContent.Category == it.ContentCategory {
				//			piContent.Names = append(piContent.Names, it.ContentName)
				//			break
				//		}
				//	}
				//}
			}
		}

	}

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

	return tool_funs.SimpleWrapGridMap(int64(len(adapterList)), adapterList), nil
}