package service

import (
	"fmt"
	"sort"
	"strconv"
	"time"

	"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/notify"
	"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/log"
)

// 员工评绩效评估
type StaffAssessServeice struct {
}

func NewStaffAssessServeice() *StaffAssessServeice {
	newService := &StaffAssessServeice{}
	return newService
}

// 获取HRBP标记值
func (srv StaffAssessServeice) getHRBP(transactionContext application.TransactionContext, companyId int, operatorId int) (int, error) {
	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": companyId})
	if err != nil {
		return -1, application.ThrowError(application.INTERNAL_SERVER_ERROR, "获取角色信息列表"+err.Error())
	}
	_, userRoleList, err := roleUserRepo.Find(map[string]interface{}{"companyId": companyId, "userId": operatorId})
	if err != nil {
		return -1, 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
		}
	}
	return hrbp, nil
}

// 获取个人的自评反馈历史记录列表
func (srv StaffAssessServeice) AssessSelfList(param *query.AssessSelfListQuery) (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()
	}()
	//获取个人参与的评估流程
	staffAssessRepo := factory.CreateStaffAssessRepository(map[string]interface{}{
		"transactionContext": transactionContext,
	})
	var limit int = 100
	var offset int = 0
	if param.PageSize > 0 {
		limit = param.PageSize
	}
	offset = (param.PageNumber - 1) * param.PageSize
	condition := map[string]interface{}{
		"executorId": param.UserId,
		"companyId":  param.CompanyId,
		"typesList":  []string{string(domain.AssessSelf)},
		"limit":      limit,
		"endTime":    time.Now(), //获取历史记录
	}
	if offset > 0 {
		condition["offset"] = offset
	}
	//获取历史列表
	cnt, assessList, err := staffAssessRepo.Find(condition)
	if err != nil {
		return nil, application.ThrowError(application.TRANSACTION_ERROR, "获取评估任务列表,"+err.Error())
	}

	//获取公司数据
	companyRep := factory.CreateCompanyRepository(map[string]interface{}{
		"transactionContext": transactionContext,
	})

	companyData, err := companyRep.FindOne(map[string]interface{}{
		"id": param.CompanyId,
	})
	if err != nil {
		log.Logger.Error("获取公司信息," + err.Error())
		return nil, application.ThrowError(application.TRANSACTION_ERROR, "获取公司信息,"+err.Error())
	}

	//获取用户数据
	userRepo := factory.CreateUserRepository(map[string]interface{}{
		"transactionContext": transactionContext,
	})
	userData, err := userRepo.FindOne(map[string]interface{}{
		"id": param.UserId,
	})
	if err != nil {
		log.Logger.Error("获取用户信息," + err.Error())
		return nil, application.ThrowError(application.TRANSACTION_ERROR, "获取用户信息,"+err.Error())
	}

	supperUserList, _ := srv.getStaffSuper(transactionContext, *userData)
	if err := transactionContext.CommitTransaction(); err != nil {
		return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
	}
	listData := make([]adapter.AssessSelfList, 0, len(assessList))
	var itemTemp adapter.AssessSelfList
	for _, v := range assessList {
		itemTemp = adapter.AssessSelfList{
			AssessId:              v.Id,
			BeginTime:             v.BeginTime.Local().Format("2006-01-02 15:04:05"),
			EndTime:               v.EndTime.Local().Format("2006-01-02 15:04:05"),
			CycleId:               v.CycleId,
			CycleName:             v.CycleName,
			EvaluationProjectId:   v.EvaluationProjectId,
			EvaluationProjectName: v.EvaluationProjectName,
		}
		listData = append(listData, itemTemp)
	}
	userInfo := adapter.StaffInfo{
		UserName:       userData.Name,
		CompanyName:    companyData.Name,
		SupperUserName: "",
		DutyTime:       userData.EntryTime,
	}
	for _, v := range supperUserList {
		userInfo.SupperUserName = userInfo.SupperUserName + v.Name + " "
	}
	result := tool_funs.SimpleWrapGridMap(int64(cnt), listData)
	result["userInfo"] = userInfo
	return result, nil
}

// 根据项目评估的配置,创建员工的评估任务
func (srv StaffAssessServeice) CreateStaffAssessTask(transactionContext application.TransactionContext, param *command.CreateStaffAssessTask) (map[string]interface{}, error) {
	log.Logger.Debug("CreateStaffAssessTask 获取参数", map[string]interface{}{
		"param": param,
	})
	assessTaskRepo := factory.CreateStaffAssessTaskRepository(map[string]interface{}{
		"transactionContext": transactionContext,
	})
	taskBeginTime, err := time.ParseInLocation("2006-01-02 15:04:05", param.BeginTime, time.Local)
	if err != nil {
		return nil, application.ThrowError(application.ARG_ERROR, "任务开始时间填写错误,"+param.BeginTime)
	}
	taskEndTime, err := time.ParseInLocation("2006-01-02 15:04:05", param.EndTime, time.Local)
	if err != nil {
		return nil, application.ThrowError(application.ARG_ERROR, "任务结束时间填写错误,"+param.EndTime)
	}
	nowTime := time.Now()

	assessTaskData := &domain.StaffAssessTask{
		Id:                    0,
		CompanyId:             param.CompanyId,
		EvaluationProjectId:   param.EvaluationProjectId,
		EvaluationProjectName: param.EvaluationProjectName,
		CycleId:               param.CycleId,
		CycleName:             param.CycleName,
		BeginTime:             taskBeginTime,
		EndTime:               taskEndTime,
		StepList:              []domain.AssessTaskStep{},
		ExecutorId:            []int{},
		CreatedAt:             nowTime,
		UpdatedAt:             nowTime,
		DeletedAt:             nil,
		BeginDay:              taskBeginTime.Local().Format("2006-01-02"),
	}
	executorIds := []int{}              //提取评估的参与人id
	executorIdMap := map[int]struct{}{} //过滤重复的用户
	//从入参中提取参与人
	for _, v := range param.ExecutorId {
		if _, ok := executorIdMap[v]; ok {
			continue
		}
		executorIdMap[v] = struct{}{}
		executorIds = append(executorIds, v)
	}
	assessTaskData.ExecutorId = executorIds
	for _, v := range param.StepList {
		stepBeginTime, err := time.ParseInLocation("2006-01-02 15:04:05", param.BeginTime, time.Local)
		if err != nil {
			return nil, application.ThrowError(application.ARG_ERROR, "评估环节开始时间填写错误,"+param.BeginTime)
		}
		stepEndTime, err := time.ParseInLocation("2006-01-02 15:04:05", param.EndTime, time.Local)
		if err != nil {
			return nil, application.ThrowError(application.ARG_ERROR, "评估环节结束时间填写错误,"+param.EndTime)
		}
		step := domain.AssessTaskStep{
			SortBy:       v.SortBy,
			LinkNodeId:   v.LinkNodeId,
			LinkNodeName: v.LinkNodeName,
			LinkNodeType: v.LinkNodeType,
			BeginTime:    stepBeginTime,
			EndTime:      stepEndTime,
		}
		assessTaskData.StepList = append(assessTaskData.StepList, step)
	}
	//添加员工的节点任务
	assessList, err := srv.createStaffAssess(transactionContext, assessTaskData)
	if err != nil {
		return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, "生成个人评估项"+err.Error())
	}

	//合并项目评估的新旧数据
	_, assassessTaskList, err := assessTaskRepo.Find(map[string]interface{}{
		"evaluationProjectId": param.EvaluationProjectId,
		"beginDay":            taskBeginTime.Local().Format("2006-01-02"),
	})
	if err != nil {
		return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, "查询同日期同项目已存在的评估任务"+err.Error())
	}

	if len(assassessTaskList) > 0 {
		//就数据中提取参与人
		for _, v := range assessTaskData.ExecutorId {
			if _, ok := executorIdMap[v]; ok {
				continue
			}
			executorIdMap[v] = struct{}{}
			executorIds = append(executorIds, v)
		}
		assassessTaskList[0].UpdatedAt = time.Now()
		assassessTaskList[0].ExecutorId = executorIds
		//更新步骤
		assassessTaskList[0].StepList = append(assassessTaskList[0].StepList, assessTaskData.StepList...)
		assessTaskData = assassessTaskList[0]
		//排序流程环节
		stepList := domain.SortTaskStep(assessTaskData.StepList)
		sort.Sort(stepList)
	}
	_, err = assessTaskRepo.Save(assessTaskData)
	if err != nil {
		return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, "保存评估任务"+err.Error())
	}

	//保存 员工的评估任务
	assessRepo := factory.CreateStaffAssessRepository(map[string]interface{}{
		"transactionContext": transactionContext,
	})
	for i := range assessList {
		assessList[i].StaffAssessTaskId = assessTaskData.Id
		_, err = assessRepo.Save(&assessList[i])
		if err != nil {
			return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, "保存个人评估项"+err.Error())
		}
		if assessList[i].Types == domain.AssessSelf {
			//添加待发送的短信通知
			notify.AddNotifyStaffAssess(&assessList[i])
		}
	}
	return map[string]interface{}{
		"assessTaskId": assessTaskData.Id,
	}, nil
}

// 添加节点任务
func (srv StaffAssessServeice) createStaffAssess(transactionContext application.TransactionContext, param *domain.StaffAssessTask) ([]domain.StaffAssess, error) {
	selfUserId := []int{}           //评估的参与人
	userIdMap := map[int]struct{}{} //过滤重复的用户
	for _, v := range param.ExecutorId {
		if _, ok := userIdMap[v]; ok {
			continue
		}
		selfUserId = append(selfUserId, v)
	}
	// 获取用户的信息
	if len(selfUserId) == 0 {
		log.Logger.Error("createStaffAssess", map[string]interface{}{
			"param": param,
		})
		return nil, application.ThrowError(application.ARG_ERROR, "未填写评估任务的执行人")
	}
	userRepo := factory.CreateUserRepository(map[string]interface{}{
		"transactionContext": transactionContext,
	})
	//获取员工信息
	_, userList, err := userRepo.Find(map[string]interface{}{
		"ids":    selfUserId,
		"status": 1,
	})
	if err != nil {
		return nil, application.ThrowError(application.ARG_ERROR, "获取员工信息"+err.Error())
	}
	departmentRepo := factory.CreateDepartmentRepository(map[string]interface{}{
		"transactionContext": transactionContext,
	})
	//获取用户的部门
	userDepartmentMap := map[int64][]*domain.Department{}
	for _, v := range userList {
		if len(v.DepartmentId) == 0 {
			continue
		}
		_, departmemtList, err := departmentRepo.Find(map[string]interface{}{
			"ids": v.DepartmentId,
		})
		if err != nil {
			return nil, application.ThrowError(application.ARG_ERROR, "获取员工的部门信息"+err.Error())
		}
		userDepartmentMap[v.Id] = departmemtList
	}
	assessList := []domain.StaffAssess{}
	//数据样板
	stepSelfTemp := domain.StaffAssess{
		Id:                    0,
		CompanyId:             param.CompanyId,
		EvaluationProjectId:   param.EvaluationProjectId,
		EvaluationProjectName: param.EvaluationProjectName,
		CycleId:               param.CycleId,
		CycleName:             param.CycleName,
		StaffAssessTaskId:     param.Id,
		// TargetUser:            domain.StaffDesc{},
		// TargetDepartment:      nil,
		// Executor:              domain.StaffDesc{},
		Types: "",
		// LinkNodeId: v.LinkNodeId,
		Status: domain.StaffAssessUncompleted,
		// BeginTime:  time.Time{},
		// EndTime:    time.Time{},
		CreatedAt: param.CreatedAt,
		UpdatedAt: param.UpdatedAt,
		DeletedAt: nil,
	}
	for _, v := range param.StepList {
		if v.LinkNodeType == domain.LinkNodeSelfAssessment {
			//员工自评
			stepSelfTemp.BeginTime = v.BeginTime
			stepSelfTemp.EndTime = v.EndTime
			stepSelfTemp.LinkNodeId = v.LinkNodeId
			stepSelfTemp.LinkNodeName = v.LinkNodeName
			stepSelfTemp.Types = domain.AssessSelf
			assessListTemp := srv.createStaffAssessSelf(stepSelfTemp, userList, userDepartmentMap)
			assessList = append(assessList, assessListTemp...)
		}
		if v.LinkNodeType == domain.LinkNodeSuperiorAssessment {
			// 创建上级评估
			stepSelfTemp.BeginTime = v.BeginTime
			stepSelfTemp.EndTime = v.EndTime
			stepSelfTemp.LinkNodeId = v.LinkNodeId
			stepSelfTemp.LinkNodeName = v.LinkNodeName
			stepSelfTemp.Types = domain.AssessSuper
			assessListTemp2, err := srv.createStaffAssessSupper(transactionContext, stepSelfTemp, userList, userDepartmentMap)
			if err != nil {
				return nil, err
			}
			assessList = append(assessList, assessListTemp2...)
		}
	}
	return assessList, nil
}

// 构建员工自评
func (srv StaffAssessServeice) createStaffAssessSelf(assessTemp domain.StaffAssess,
	userList []*domain.User, userDepartmentMap map[int64][]*domain.Department) []domain.StaffAssess {
	result := []domain.StaffAssess{}
	for _, usr := range userList {
		assessTemp.TargetUser = domain.StaffDesc{
			UserId:   int(usr.Id),
			Account:  usr.Account,
			UserName: usr.Name,
		}
		assessTemp.Executor = domain.StaffDesc{
			UserId:   int(usr.Id),
			Account:  usr.Account,
			UserName: usr.Name,
		}
		if depList, ok := userDepartmentMap[usr.Id]; ok {
			for _, dep := range depList {
				assessTemp.TargetDepartment = append(assessTemp.TargetDepartment, domain.StaffDepartment{
					DepartmentId:   int(dep.Id),
					DepartmentName: dep.Name,
				})
			}
		}
		result = append(result, assessTemp)
	}
	return result
}

// 构建员工的上级评估
func (srv StaffAssessServeice) createStaffAssessSupper(
	transactionContext application.TransactionContext,
	assessTemp domain.StaffAssess,
	userList []*domain.User, userDepartmentMap map[int64][]*domain.Department,
) ([]domain.StaffAssess, error) {
	var assessList []domain.StaffAssess
	for _, v := range userList {
		//获取上级
		chargeUserList, err := srv.getStaffSuper(transactionContext, *v)
		if err != nil {
			return nil, err
		}
		if len(chargeUserList) == 0 {
			continue
		}
		for _, v2 := range chargeUserList {
			assessTemp.TargetUser = domain.StaffDesc{
				UserId:   int(v.Id),
				Account:  v.Account,
				UserName: v.Name,
			}
			assessTemp.Executor = domain.StaffDesc{
				UserId:   int(v2.Id),
				Account:  v2.Account,
				UserName: v2.Name,
			}
			targetDepartment := []domain.StaffDepartment{}
			if departmentList, ok := userDepartmentMap[v.Id]; ok {
				for _, department := range departmentList {
					targetDepartment = append(targetDepartment, domain.StaffDepartment{
						DepartmentId:   int(department.Id),
						DepartmentName: department.Name,
					})
				}
			}
			assessTemp.TargetDepartment = targetDepartment
			assessList = append(assessList, assessTemp)
		}

	}
	return assessList, nil
}

// 获取某个员工360评估邀请的人员
// func (srv StaffAssessServeice) GetAssessInviteUser(param *query.GetAssessInviteUserQuery) (*adapter.AssessInviteUserResp, 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()
// 	}()

// 	assessReps := factory.CreateStaffAssessRepository(map[string]interface{}{
// 		"transactionContext": transactionContext,
// 	})

// 	assessTaskReps := factory.CreateStaffAssessTaskRepository(map[string]interface{}{
// 		"transactionContext": transactionContext,
// 	})
// 	assessTaskData, err := assessTaskReps.FindOne(map[string]interface{}{
// 		"id": param.AssessTaskId,
// 	})
// 	if err != nil {
// 		return nil, application.ThrowError(application.ARG_ERROR, "获取评估任务"+err.Error())
// 	}
// 	_, assessList, err := assessReps.Find(map[string]interface{}{
// 		"typesList":         []string{string(domain.AssessInviteDiffSuper), string(domain.AssessInviteSameSuper)},
// 		"staffAssessTaskId": param.AssessTaskId,
// 		"targetUserId":      param.UserId,
// 	})
// 	if err != nil {
// 		return nil, application.ThrowError(application.ARG_ERROR, "获取个人评估任务"+err.Error())
// 	}
// 	if err := transactionContext.CommitTransaction(); err != nil {
// 		return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
// 	}

// 	result := adapter.AssessInviteUserResp{
// 		AssessTaskId:    assessTaskData.Id,
// 		InviteDiffSuper: []domain.StaffDesc{},
// 		InviteSameSuper: []domain.StaffDesc{},
// 	}
// 	for _, v := range assessTaskData.StepList {
// 		if v.LinkNodeType != domain.LinkNodeAllInvite {
// 			continue
// 		}
// 		result.LinkNodeId = v.LinkNodeId
// 		result.LinkNodeName = v.LinkNodeName
// 		result.BeginTime = v.BeginTime.Local().Format("2006-01-02 15:04:05")
// 		result.EndTime = v.EndTime.Local().Format("2006-01-02 15:04:05")
// 		break
// 	}
// 	for _, v := range assessList {
// 		if v.Types == domain.AssessInviteDiffSuper {
// 			result.InviteDiffSuper = append(result.InviteDiffSuper, v.Executor)
// 		}
// 		if v.Types == domain.AssessInviteSameSuper {
// 			result.InviteSameSuper = append(result.InviteSameSuper, v.Executor)
// 		}
// 	}
// 	return &result, nil
// }

// 保存某个员工360邀请的人员
func (srv StaffAssessServeice) SaveAssessInviteUser(param *command.SaveAssessInvite) (map[string]interface{}, error) {
	inviteSameSuperId := []int{}
	userIdMap := map[int]struct{}{} //过滤重复的id
	InviteDiffSuperId := []int{}
	for _, v := range param.InviteDiffSuper {
		id, err := strconv.Atoi(v)
		if err != nil {
			return nil, application.ThrowError(application.ARG_ERROR, "用户填写错误")
		}
		if _, ok := userIdMap[id]; ok {
			continue
		}
		userIdMap[id] = struct{}{}
		InviteDiffSuperId = append(InviteDiffSuperId, id)
	}
	for _, v := range param.InviteSameSuper {
		id, err := strconv.Atoi(v)
		if err != nil {
			return nil, application.ThrowError(application.ARG_ERROR, "用户填写错误")
		}
		if _, ok := userIdMap[id]; ok {
			continue
		}
		userIdMap[id] = struct{}{}
		inviteSameSuperId = append(inviteSameSuperId, id)
	}
	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()
	}()
	assessReps := factory.CreateStaffAssessRepository(map[string]interface{}{
		"transactionContext": transactionContext,
	})
	userRepo := factory.CreateUserRepository(map[string]interface{}{
		"transactionContext": transactionContext,
	})
	departmentRepo := factory.CreateDepartmentRepository(map[string]interface{}{
		"transactionContext": transactionContext,
	})

	assessTaskReps := factory.CreateStaffAssessTaskRepository(map[string]interface{}{
		"transactionContext": transactionContext,
	})
	assessTaskData, err := assessTaskReps.FindOne(map[string]interface{}{
		"id": param.AssessTaskId,
	})
	if err != nil {
		return nil, application.ThrowError(application.ARG_ERROR, "获取评估任务"+err.Error())
	}

	//获取邀请评估的节点
	var inviteNode *domain.AssessTaskStep
	for _, v := range assessTaskData.StepList {
		if v.LinkNodeType == domain.LinkNodeAllAssessment {
			cp := v
			inviteNode = &cp
			break
		}
	}
	if inviteNode == nil {
		return nil, application.ThrowError(application.ARG_ERROR, "评估任务没有邀请评估的环节")
	}
	//检查节点的结束时间
	endTimeInt := inviteNode.EndTime.Unix()
	if endTimeInt < time.Now().Unix() {
		e := fmt.Sprintf("该环节已在%s截止", inviteNode.EndTime.Local().Format("2006-01-02 15:04:05"))
		return nil, application.ThrowError(application.BUSINESS_ERROR, e)
	}

	targetUser, err := userRepo.FindOne(map[string]interface{}{
		"id": param.TargetUserId,
	})
	if err != nil {
		return nil, application.ThrowError(application.ARG_ERROR, "获取被评估员工"+err.Error())
	}
	//被评估人的部门
	var targetUserDepartment []domain.StaffDepartment
	if len(targetUser.DepartmentId) > 0 {
		_, departmentList, err := departmentRepo.Find(map[string]interface{}{
			"ids": targetUser.DepartmentId,
		})
		if err != nil {
			return nil, application.ThrowError(application.ARG_ERROR, "获取部门信息"+err.Error())
		}
		for _, v := range departmentList {
			targetUserDepartment = append(targetUserDepartment, domain.StaffDepartment{
				DepartmentId:   int(v.Id),
				DepartmentName: v.Name,
			})
		}
	}
	//邀请的相同上级的员工
	var inviteSameSuper []domain.StaffDesc
	if len(inviteSameSuperId) > 0 {
		_, userList, err := userRepo.Find(map[string]interface{}{
			"ids":    inviteSameSuperId,
			"status": 1,
		})
		if err != nil {
			return nil, application.ThrowError(application.ARG_ERROR, "获取用户信息"+err.Error())
		}
		for _, v := range userList {
			inviteSameSuper = append(inviteSameSuper, domain.StaffDesc{
				UserId:   int(v.Id),
				UserName: v.Name,
				Account:  v.Account,
			})
		}
	}
	//邀请的不同上级的员工
	var inviteDiffSuper []domain.StaffDesc
	if len(InviteDiffSuperId) > 0 {
		_, userList, err := userRepo.Find(map[string]interface{}{
			"ids":    InviteDiffSuperId,
			"status": 1,
		})
		if err != nil {
			return nil, application.ThrowError(application.ARG_ERROR, "获取用户信息"+err.Error())
		}
		for _, v := range userList {
			inviteDiffSuper = append(inviteDiffSuper, domain.StaffDesc{
				UserId:   int(v.Id),
				UserName: v.Name,
				Account:  v.Account,
			})
		}
	}
	//获取员工邀请的人
	_, assessList, err := assessReps.Find(map[string]interface{}{
		"typesList":         []string{string(domain.AssessInviteDiffSuper), string(domain.AssessInviteSameSuper)},
		"staffAssessTaskId": param.AssessTaskId,
		"targetUserId":      param.TargetUserId,
	})
	if err != nil {
		return nil, application.ThrowError(application.ARG_ERROR, "获取员工邀请的人"+err.Error())
	}
	//比对新旧数据
	nowTime := time.Now()
	assessMap := map[string]*domain.StaffAssess{}
	for _, v := range assessList {
		//假设为删除
		v.DeletedAt = &nowTime
		key := fmt.Sprintf("%s%d", string(v.Types), v.Executor.UserId)
		assessMap[key] = v
	}
	newAssessList := []*domain.StaffAssess{}
	//邀请同上级的员工
	for _, v := range inviteSameSuper {
		key := fmt.Sprintf("%s%d", string(domain.AssessInviteSameSuper), v.UserId)
		if _, ok := assessMap[key]; ok {
			assessMap[key].DeletedAt = nil
		} else {
			newAssessList = append(newAssessList, &domain.StaffAssess{
				Id:                    0,
				CompanyId:             assessTaskData.CompanyId,
				EvaluationProjectId:   assessTaskData.EvaluationProjectId,
				EvaluationProjectName: assessTaskData.EvaluationProjectName,
				CycleId:               assessTaskData.CycleId,
				CycleName:             assessTaskData.CycleName,
				StaffAssessTaskId:     assessTaskData.Id,
				TargetUser: domain.StaffDesc{
					UserId:   int(targetUser.Id),
					Account:  targetUser.Account,
					UserName: targetUser.Name,
				},
				TargetDepartment: targetUserDepartment,
				Executor:         v,
				Types:            domain.AssessInviteSameSuper,
				LinkNodeId:       inviteNode.LinkNodeId,
				LinkNodeName:     inviteNode.LinkNodeName,
				Status:           domain.StaffAssessUncompleted,
				BeginTime:        inviteNode.BeginTime,
				EndTime:          inviteNode.EndTime,
				CreatedAt:        nowTime,
				UpdatedAt:        nowTime,
				DeletedAt:        nil,
			})
		}
	}
	for _, v := range inviteDiffSuper {
		key := fmt.Sprintf("%s%d", string(domain.AssessInviteDiffSuper), v.UserId)
		if _, ok := assessMap[key]; ok {
			assessMap[key].DeletedAt = nil
		} else {
			newAssessList = append(newAssessList, &domain.StaffAssess{
				Id:                    0,
				CompanyId:             assessTaskData.CompanyId,
				EvaluationProjectId:   assessTaskData.EvaluationProjectId,
				EvaluationProjectName: assessTaskData.EvaluationProjectName,
				CycleId:               assessTaskData.CycleId,
				CycleName:             assessTaskData.CycleName,
				StaffAssessTaskId:     assessTaskData.Id,
				TargetUser: domain.StaffDesc{
					UserId:   int(targetUser.Id),
					Account:  targetUser.Account,
					UserName: targetUser.Name,
				},
				TargetDepartment: targetUserDepartment,
				Executor:         v,
				Types:            domain.AssessInviteDiffSuper,
				LinkNodeId:       inviteNode.LinkNodeId,
				LinkNodeName:     inviteNode.LinkNodeName,
				Status:           domain.StaffAssessUncompleted,
				BeginTime:        inviteNode.BeginTime,
				EndTime:          inviteNode.EndTime,
				CreatedAt:        nowTime,
				UpdatedAt:        nowTime,
				DeletedAt:        nil,
			})
		}
	}
	assessList = append(assessList, newAssessList...)
	for i := range assessList {
		_, err = assessReps.Save(assessList[i])
		if err != nil {
			return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, "保存员工邀请评估"+err.Error())
		}
	}
	if err := transactionContext.CommitTransaction(); err != nil {
		return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
	}
	result := map[string]interface{}{
		"assessTaskId": assessTaskData.Id,
	}
	return result, nil
}

// 获取未完成的员工评估内容
func (srv StaffAssessServeice) getAssessSelfInfoUncompleted(transactionContext application.TransactionContext,
	assess *domain.StaffAssess) ([]*domain.StaffAssessContent, error) {
	projectRepo := factory.CreateEvaluationProjectRepository(map[string]interface{}{
		"transactionContext": transactionContext,
	})
	projectData, err := projectRepo.FindOne(map[string]interface{}{
		"id": assess.EvaluationProjectId,
	})
	if err != nil {
		return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, "获取项目填写内容"+err.Error())
	}
	if projectData.Template == nil {
		return nil, nil
	}
	var linkNode *domain.LinkNode
	for _, v := range projectData.Template.LinkNodes {
		if v.Id == int64(assess.LinkNodeId) {
			linkNode = v
			break
		}
	}
	if linkNode == nil {
		return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, "未获得评估环节配置"+err.Error())
	}

	var nodeContentList []*domain.NodeContent
	if len(linkNode.NodeContents) > 0 {
		nodeContentList = linkNode.NodeContents
	} else {
		//如果当前节点没有评估内容,就去 使用自评节点的评估内容
		for _, v := range projectData.Template.LinkNodes {
			if v.Type == domain.LinkNodeSelfAssessment {
				nodeContentList = v.NodeContents
			}
		}
	}
	ruleRepo := factory.CreateEvaluationRuleRepository(map[string]interface{}{
		"transactionContext": transactionContext,
	})
	ruleMap := map[int64]*domain.EvaluationRule{}
	for _, v := range nodeContentList {
		if _, ok := ruleMap[v.RuleId]; ok {
			continue
		}
		ruleData, err := ruleRepo.FindOne(map[string]interface{}{
			"id": v.RuleId,
		})
		if err == nil {
			ruleMap[v.RuleId] = ruleData
		}
	}

	var contentList []*domain.StaffAssessContent
	nowTime := time.Now()
	for i, v := range nodeContentList {
		item := &domain.StaffAssessContent{
			Id:            0,
			StaffAssessId: assess.Id,
			SortBy:        i + 1,
			Category:      v.Category,
			Name:          v.Name,
			PromptTitle:   v.PromptTitle,
			PromptText:    v.PromptText,
			Remark:        nil,
			Value:         "",
			ReteResult:    "",
			CreatedAt:     nowTime,
			Weight:        v.Weight,
			Required:      v.Required,
			UpdatedAt:     nowTime,
			DeletedAt:     nil,
			// Rule: ,
		}
		if ruleVal, ok := ruleMap[v.RuleId]; ok {
			item.Rule = *ruleVal
		}
		var remarks []domain.AssessContemtRemark
		for _, vv := range v.EntryItems {
			ritem := domain.AssessContemtRemark{
				Title:      vv.Title,
				HintText:   vv.HintText,
				Definition: vv.Definition,
				RemarkText: "",
			}
			remarks = append(remarks, ritem)
		}
		item.Remark = remarks
		contentList = append(contentList, item)
	}
	return contentList, nil
}

// 根据staffAssessId 获取评估的填写信息
func (srv StaffAssessServeice) GetAssessInfo(param *query.AssessInfoQuery) (*adapter.AssessInfoResp, 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()
	}()
	assessReps := factory.CreateStaffAssessRepository(map[string]interface{}{
		"transactionContext": transactionContext,
	})
	//获取员工的评估
	_, assessList, err := assessReps.Find(map[string]interface{}{
		"companyId": param.CompanyId,
		"id":        param.AssessId,
	})
	if err != nil {
		return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, "获取员工的评估"+err.Error())
	}
	if len(assessList) == 0 {
		return &adapter.AssessInfoResp{}, nil
	}
	assessData := assessList[0]
	assessContentList := []*domain.StaffAssessContent{}
	if assessData.Status == domain.StaffAssessCompleted {
		//已完成
		assessContentRepo := factory.CreateStaffAssessContentRepository(map[string]interface{}{
			"transactionContext": transactionContext,
		})
		_, assessContentList, err = assessContentRepo.Find(map[string]interface{}{
			"staffAssessId": assessData.Id,
		})
		if err != nil {
			return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, "获取项目填写内容"+err.Error())
		}
	} else if assessData.Status == domain.StaffAssessUncompleted {
		//未完成
		assessContentList, err = srv.getAssessSelfInfoUncompleted(transactionContext, assessData)
		if err != nil {
			return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, "获取项目填写内容"+err.Error())
		}
	}

	// 恢复缓存数据
	if param.AcquireCache != 0 {
		srv.recoverAssessCache(transactionContext, assessData.Id, assessContentList)
	}

	targetUserDesc, err := srv.getStaffDescrip(transactionContext, int64(assessData.TargetUser.UserId))
	if err != nil {
		log.Logger.Error("获取员工描述" + err.Error())
	}
	if err := transactionContext.CommitTransaction(); err != nil {
		return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
	}

	result := adapter.AssessInfoResp{
		AssessId:              assessData.Id,
		CycleId:               assessData.CycleId,
		CycleName:             assessData.CycleName,
		LinkNodeId:            assessData.LinkNodeId,
		LinkNodeName:          assessData.LinkNodeName,
		EvaluationProjectId:   assessData.EvaluationProjectId,
		EvaluationProjectName: assessData.EvaluationProjectName,
		BeginTime:             assessData.BeginTime.Local().Format("2006-01-02 15:04:05"),
		EndTime:               assessData.EndTime.Local().Format("2006-01-02 15:04:05"),
		Status:                string(assessData.Status),
		TargetUserId:          assessData.TargetUser.UserId,
		TargetUserName:        assessData.TargetUser.UserName,
		CompanyId:             assessData.CompanyId,
		CompanyName:           "",
		SupperUser:            "",
		DutyTime:              "",
		AssessContent:         assessContentList,
	}
	if len(assessContentList) == 0 {
		result.AssessContent = []*domain.StaffAssessContent{}
	}
	if targetUserDesc != nil {
		result.CompanyName = targetUserDesc.CompanyName
		result.SupperUser = targetUserDesc.SupperUserName
		result.CompanyLogo = targetUserDesc.CompanyLogo
		result.DutyTime = targetUserDesc.DutyTime
	}
	return &result, nil
}

// 获取员工信息描述
func (srv *StaffAssessServeice) getStaffDescrip(transactionContext application.TransactionContext, userid int64) (*adapter.StaffInfo, error) {
	//获取用户数据
	userRepo := factory.CreateUserRepository(map[string]interface{}{
		"transactionContext": transactionContext,
	})
	userData, err := userRepo.FindOne(map[string]interface{}{
		"id": userid,
	})
	if err != nil {
		log.Logger.Error("获取用户信息," + err.Error())
		return nil, application.ThrowError(application.TRANSACTION_ERROR, "获取用户信息,"+err.Error())
	}

	//获取公司数据
	companyRep := factory.CreateCompanyRepository(map[string]interface{}{
		"transactionContext": transactionContext,
	})
	companyData, err := companyRep.FindOne(map[string]interface{}{
		"id": userData.CompanyId,
	})
	if err != nil {
		log.Logger.Error("获取公司信息," + err.Error())
		return nil, application.ThrowError(application.TRANSACTION_ERROR, "获取公司信息,"+err.Error())
	}
	supperUserList, _ := srv.getStaffSuper(transactionContext, *userData)
	userInfo := adapter.StaffInfo{
		UserName:       userData.Name,
		CompanyName:    companyData.Name,
		CompanyLogo:    companyData.Logo,
		SupperUserName: "",
		DutyTime:       userData.EntryTime,
	}
	for _, v := range supperUserList {
		userInfo.SupperUserName = userInfo.SupperUserName + v.Name + " "
	}
	return &userInfo, nil
}

// 提交评估数据
func (srv StaffAssessServeice) SaveAssessInfo(param *command.SaveAssessInfoCommand) (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()
	}()
	assessReps := factory.CreateStaffAssessRepository(map[string]interface{}{
		"transactionContext": transactionContext,
	})
	//获取员工的评估
	assessData, err := assessReps.FindOne(map[string]interface{}{
		"id": param.AssessId,
	})
	if err != nil {
		return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, "获取员工的评估"+err.Error())
	}

	//检查截止时间
	endTimeInt := assessData.EndTime.Unix()
	if endTimeInt < time.Now().Unix() {
		return nil, application.ThrowError(application.BUSINESS_ERROR, "当前环节已过截止时间,提交后无法修改内容")
	}
	//检查执行人
	if assessData.CompanyId != param.CompanyId {
		return nil, application.ThrowError(application.BUSINESS_ERROR, "当前用户不是评估的执行人")
	}
	if assessData.Executor.UserId != param.ExecutorId {
		return nil, application.ThrowError(application.BUSINESS_ERROR, "当前用户不是评估的执行人")
	}
	assessContentRepo := factory.CreateStaffAssessContentRepository(map[string]interface{}{
		"transactionContext": transactionContext,
	})
	//待更新的评估填写信息
	assessContentList := []*domain.StaffAssessContent{}
	if assessData.Status == domain.StaffAssessCompleted {
		//已完成
		_, assessContentList, err = assessContentRepo.Find(map[string]interface{}{
			"staffAssessId": assessData.Id,
		})
		if err != nil {
			return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, "获取项目填写内容"+err.Error())
		}
	} else if assessData.Status == domain.StaffAssessUncompleted {
		//未完成
		assessContentList, err = srv.getAssessSelfInfoUncompleted(transactionContext, assessData)
		if err != nil {
			return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, "获取项目填写内容"+err.Error())
		}
	}
	//处理提交上来的数据
	paramContentMap := map[string]domain.AssessContent{}
	for i, v := range param.AssessContent {
		key := fmt.Sprintf("%s-%s", v.Category, v.Name)
		paramContentMap[key] = param.AssessContent[i]
	}
	//更新的评估填写信息
	for _, v := range assessContentList {
		key := fmt.Sprintf("%s-%s", v.Category, v.Name)
		item, ok := paramContentMap[key]
		if !ok {
			continue
		}
		if v.Required == domain.NodeRequiredYes && len(item.Value) == 0 {
			return nil, application.ThrowError(application.BUSINESS_ERROR, v.Category+"-"+v.Name+":必填项")
		}
		v.Value = item.Value
		if len(item.Value) > 0 {
			// 转换填入的评估值
			err = v.TransformValue()
			if err != nil {
				return nil, application.ThrowError(application.BUSINESS_ERROR, v.Category+"-"+v.Name+":"+err.Error())
			}
		}
		for ii := range v.Remark {
			for _, vvv := range item.Remark {
				if v.Remark[ii].Title == vvv.Title {
					v.Remark[ii].RemarkText = vvv.RemarkText
					break
				}
			}
		}
	}
	//保存信息
	for i := range assessContentList {
		_, err = assessContentRepo.Save(assessContentList[i])
		if err != nil {
			return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, "保存评估填写内容"+err.Error())
		}
	}
	assessData.Status = domain.StaffAssessCompleted
	assessData.UpdatedAt = time.Now()
	_, err = assessReps.Save(assessData)
	if err != nil {
		return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, "保存评估任务"+err.Error())
	}

	// 删除缓存
	cacheRepository := factory.CreateStaffAssessCacheRepository(map[string]interface{}{"transactionContext": transactionContext})
	if _, caches, err := cacheRepository.Find(map[string]interface{}{"assessId": assessData.Id, "limit": 1}); err != nil {
		return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
	} else {
		for i := range caches {
			if err = cacheRepository.Remove(caches[i].Id); err != nil {
				return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
			}
		}
	}

	if err := transactionContext.CommitTransaction(); err != nil {
		return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
	}
	return map[string]interface{}{
		"assessId": assessData.Id,
	}, nil
}

// 获取员工的上级是谁
func (srv StaffAssessServeice) getStaffSuper(transactionContext application.TransactionContext, targetUser domain.User) ([]*domain.User, error) {
	if targetUser.ParentId == 0 {
		return nil, nil
	}
	userRepo := factory.CreateUserRepository(map[string]interface{}{
		"transactionContext": transactionContext,
	})

	_, userData, err := userRepo.Find(map[string]interface{}{
		"id": targetUser.ParentId,
	})
	if err != nil {
		return nil, err
	}
	return userData, nil
}

func (srv StaffAssessServeice) recoverAssessCache(context application.TransactionContext, assessId int, dataArray []*domain.StaffAssessContent) {
	cacheRepository := factory.CreateStaffAssessCacheRepository(map[string]interface{}{"transactionContext": context})
	_, caches, err := cacheRepository.Find(map[string]interface{}{"assessId": assessId, "limit": 1})
	if err != nil || len(caches) == 0 {
		return
	}
	cacheArray := caches[0].AssessContent
	cacheLen := len(cacheArray)

	for i := range dataArray {
		if cacheLen > i { // 避免数组越界
			data := dataArray[i]
			cache := cacheArray[i]

			data.Value = cache.Value // 评估填写的值
			cacheRemarkLen := len(cache.Remark)

			for j := range data.Remark {
				if cacheRemarkLen > j {
					data.Remark[j].RemarkText = cache.Remark[j].RemarkText // 评估填写文本内容
				}
			}
		}
	}
}