service_2.go 4.5 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/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"
)

//调试用,手动调用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()
	}()

	roleRepo := factory.CreateRoleRepository(map[string]interface{}{"transactionContext": transactionContext})
	roleUserRepo := factory.CreateRoleUserRepository(map[string]interface{}{"transactionContext": transactionContext})
	_, roleList, err := roleRepo.Find(map[string]interface{}{"type": domain.RoleTypeSystem, "companyId": in.CompanyId})
	if err != nil {
		return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, "获取角色信息列表"+err.Error())
	}
	_, userRoleList, err := roleUserRepo.Find(map[string]interface{}{"companyId": in.CompanyId, "userId": in.OperatorId})
	if err != nil {
		return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, "获取用户的角色信息列表"+err.Error())
	}
	// 拥有HRBP权限
	hrbp := -1
	for _, v := range userRoleList {
		for _, v2 := range roleList {
			if v.RoleId == v2.Id {
				hrbp = 1
				break
			}
		}
		if hrbp == 1 {
			break
		}
	}

	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
}