package com.ningdatech.pmapi.performance.manage;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.*;
import java.util.stream.Collectors;

import com.google.common.collect.Lists;
import com.ningdatech.pmapi.user.entity.enumeration.RoleEnum;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ningdatech.basic.exception.BizException;
import com.ningdatech.basic.function.VUtils;
import com.ningdatech.basic.model.PageVo;
import com.ningdatech.basic.util.StrPool;
import com.ningdatech.pmapi.performance.enumration.AppraisalTypeEnum;
import com.ningdatech.pmapi.performance.helper.TemplateDetailBuildHelper;
import com.ningdatech.pmapi.performance.model.dto.ProjectAppraisalDTO;
import com.ningdatech.pmapi.performance.model.dto.ProjectAppraisalInfoDTO;
import com.ningdatech.pmapi.performance.model.entity.*;
import com.ningdatech.pmapi.performance.model.req.PerformanceAppraisalListReq;
import com.ningdatech.pmapi.performance.model.vo.PerformanceAppraisalProjectVO;
import com.ningdatech.pmapi.performance.model.vo.PerformanceAppraisalVO;
import com.ningdatech.pmapi.performance.service.*;
import com.ningdatech.pmapi.portrait.service.IProjectTagService;
import com.ningdatech.pmapi.projectlib.enumeration.ProjectTypeEnum;
import com.ningdatech.pmapi.projectlib.model.entity.Project;
import com.ningdatech.pmapi.projectlib.service.IProjectService;
import com.ningdatech.pmapi.sys.model.entity.Role;
import com.ningdatech.pmapi.sys.service.IRoleService;
import com.ningdatech.pmapi.user.security.auth.model.UserInfoDetails;
import com.ningdatech.pmapi.user.util.LoginUserUtil;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;

/**
 * VerifyManage
 * @return
 * @author CMM
 * @since 2023/08/10 8:55
 */
@Component
@Slf4j
@AllArgsConstructor
public class VerifyManage {

    private final IPerformanceAppraisalService performanceAppraisalService;
    private final IPerformanceAppraisalProjectService performanceAppraisalProjectService;
    private final IProjectService projectService;
    private final IRoleService roleService;
    private final IPerformanceIndicatorProjectTemplateService indicatorProjectTemplateService;
    private final TemplateDetailBuildHelper templateDetailBuildHelper;
    private final IPerformanceIndicatorProjectTemplateDetailService indicatorProjectTemplateDetailService;
    private final IProjectTagService projectTagService;
    private final IPerformanceAppraisalScoreInfoService performanceAppraisalScoreInfoService;
    private final IPerformanceAppraisalProjectGroupService groupService;
    /**
     * 核查-评价计划列表
     * @param req
     * @return
     */
    public PageVo<PerformanceAppraisalVO> list(PerformanceAppraisalListReq req) {
        UserInfoDetails user = LoginUserUtil.loginUserDetail();
        String employeeCode = user.getEmployeeCode();
        // 获取当前登录用户的角色列表，只有专家核查人员才能查看该列表
        List<Role> userRoleList = user.getUserRoleList();
        List<Long> roleIdList = userRoleList.stream().map(Role::getId).collect(Collectors.toList());
        Role role = roleService.getOne(Wrappers.lambdaQuery(Role.class)
                .eq(Role::getName, RoleEnum.EXPERT.getDesc()));
        if (Objects.isNull(role)){
            throw new BizException("登录用户没有复评员角色!");
        }

        // 登录用户不是专家，不能查看核查计划列表
        if (!roleIdList.contains(role.getId())){
            return PageVo.empty();
        }

        // 从绩效评价 分组表中获取当前登录专家所在的分组信息
        List<PerformanceAppraisalProjectGroup> groups = Lists.newArrayList();
        List<PerformanceAppraisalProjectGroup> projectGroups = groupService.list();
        for (PerformanceAppraisalProjectGroup projectGroup : projectGroups) {
            String verificationUsers = projectGroup.getVerificationUsers();
            if (verificationUsers.contains(StrPool.COMMA)){
                if (verificationUsers.contains(employeeCode)){
                    groups.add(projectGroup);
                }
            } else if (verificationUsers.equals(employeeCode)) {
                groups.add(projectGroup);
            }
        }
        // 当前登录专家没有绩效分组信息
        if (CollUtil.isEmpty(groups)){
            return PageVo.empty();
        }

        // 获取分组所在的评价计划ID
        List<Long> appraisalIds = groups.stream().map(PerformanceAppraisalProjectGroup::getAppraisalId).collect(Collectors.toList());
        // 因为同一个核查人员只能出现在一个计划的一个分组中，所以分组信息可按照评价计划ID进行分组
        Map<Long, PerformanceAppraisalProjectGroup> groupMap = groups.stream().collect(Collectors.toMap(PerformanceAppraisalProjectGroup::getAppraisalId, g -> g));
        // 获取核查计划列表
        Page<PerformanceAppraisal> page = req.page();
        LambdaQueryWrapper<PerformanceAppraisal> wrapper = Wrappers.lambdaQuery(PerformanceAppraisal.class)
                .in(PerformanceAppraisal::getId,appraisalIds)
                .orderByDesc(PerformanceAppraisal::getUpdateOn);
        performanceAppraisalService.page(page,wrapper);

        List<PerformanceAppraisalVO> res = page.getRecords().stream().map(p -> {
            PerformanceAppraisalVO vo = BeanUtil.copyProperties(p, PerformanceAppraisalVO.class);
            PerformanceAppraisalProjectGroup group = groupMap.get(p.getId());
            if (Objects.nonNull(group)) {
                vo.setReAppraisalGroupId(group.getId());
                vo.setReAppraisalGroupName(group.getName());
            }
            return vo;
        }).collect(Collectors.toList());
        return PageVo.of(res,page.getTotal());
    }

    /**
     * 是否可以核查
     *
     * @param employeeCode
     * @param projectCode
     * @param appraisalId
     * @return
     */
    private Boolean checkCanVerify(String employeeCode, String projectCode, Long appraisalId) {

        // 首次核查(核查打分信息为空)
        // 从绩效评价打分信息表中查询打分类型为专家核查、打分人员为当前登录专家用户的打分信息
        // 如果没有相关的打分信息，说明是当前登录复评用户是首次核查
        List<PerformanceAppraisalScoreInfo> scoreInfos = performanceAppraisalScoreInfoService.list(Wrappers.lambdaQuery(PerformanceAppraisalScoreInfo.class)
                .eq(PerformanceAppraisalScoreInfo::getAppraisalType, AppraisalTypeEnum.EXPERT_VERIFY.getCode())
                .eq(PerformanceAppraisalScoreInfo::getAppraisalId,appraisalId)
                .eq(PerformanceAppraisalScoreInfo::getAppraisalProjectCode,projectCode)
                .eq(PerformanceAppraisalScoreInfo::getAppraisalEmployeeCode, employeeCode));
        if (CollUtil.isEmpty(scoreInfos)){
            return true;
        }
        return Boolean.FALSE;
    }

    public PageVo<PerformanceAppraisalProjectVO> groupProjectList(Long groupId, PerformanceAppraisalListReq req) {

        UserInfoDetails userDetail = LoginUserUtil.loginUserDetail();
        String employeeCode = userDetail.getEmployeeCode();

        // 获取分组内的项目信息
        PerformanceAppraisalProjectGroup projectGroup = groupService.getById(groupId);
        String projectCodes = projectGroup.getProjectCodes();
        Long appraisalId = projectGroup.getAppraisalId();
        VUtils.isTrue(StringUtils.isBlank(projectCodes)).throwMessage("分组内项目信息不存在!");
        List<String> projectCodeList = Arrays.stream(projectCodes.split(StrPool.COMMA)).collect(Collectors.toList());

        // 筛选出分组内已完成复评的评价项目信息
        List<PerformanceAppraisalProject> appraisalProjects = performanceAppraisalProjectService.list(Wrappers.lambdaQuery(PerformanceAppraisalProject.class)
                .in(PerformanceAppraisalProject::getProjectCode, projectCodeList)
                .eq(PerformanceAppraisalProject::getIsCompleteReAppraisal, Boolean.TRUE));
        // 分组内已完成复评的项目信息不存在
        if (CollUtil.isEmpty(appraisalProjects)){
            return PageVo.empty();
        }
        List<String> proCodeList = appraisalProjects.stream().map(PerformanceAppraisalProject::getProjectCode).collect(Collectors.toList());
        Map<String, PerformanceAppraisalProject> appraisalProjectMap = appraisalProjects.stream().collect(Collectors.toMap(PerformanceAppraisalProject::getProjectCode, p -> p));

        Page<Project> page = req.page();
        LambdaQueryWrapper<Project> wrapper = Wrappers.lambdaQuery(Project.class)
                .in(Project::getProjectCode, proCodeList)
                .eq(Project::getNewest, Boolean.TRUE)
                .like(StringUtils.isNotBlank(req.getProjectName()),Project::getProjectName,req.getProjectName());
        projectService.page(page,wrapper);

        // 分组内项目信息不存在
        if (CollUtil.isEmpty(page.getRecords())){
            return PageVo.empty();
        }

        List<PerformanceAppraisalProjectVO> res = page.getRecords().stream().map(p -> {
                    PerformanceAppraisalProjectVO vo = BeanUtil.copyProperties(p, PerformanceAppraisalProjectVO.class);
                    PerformanceAppraisalProject appraisalProject = appraisalProjectMap.get(p.getProjectCode());
                    BeanUtil.copyProperties(appraisalProject, vo);
                    vo.setProjectId(appraisalProject.getProjectId());
                    vo.setProjectTypeName(ProjectTypeEnum.getDesc(p.getProjectType()));
                    vo.setCanVerify(checkCanVerify(employeeCode,appraisalProject.getProjectCode(),appraisalId));
                    // 如果登录用户是核查组长，该项目可填写整改意见
                    String verifyLeader = projectGroup.getVerifyLeader();
                    if (employeeCode.equals(verifyLeader)){
                        vo.setCanRectify(Boolean.TRUE);
                    }
                    return vo;
                })
                .collect(Collectors.toList());
        return PageVo.of(res,page.getTotal());
    }

    @Transactional(rollbackFor = Exception.class)
    public String submitVerify(ProjectAppraisalDTO param) {
        UserInfoDetails userDetail = LoginUserUtil.loginUserDetail();
        String employeeCode = userDetail.getEmployeeCode();
        String username = userDetail.getUsername();
        Long appraisalId = param.getAppraisalId();
        Long appraisalProjectId = param.getAppraisalProjectId();
        String appraisalProjectCode = param.getAppraisalProjectCode();
        List<ProjectAppraisalInfoDTO> appraisalInfoList = param.getAppraisalInfoList();
        Long groupId = param.getGroupId();
        PerformanceAppraisalProjectGroup group = groupService.getById(groupId);

        // 获取评价项目
        PerformanceAppraisalProject appraisalProject = performanceAppraisalProjectService.getOne(Wrappers.lambdaQuery(PerformanceAppraisalProject.class)
                .eq(PerformanceAppraisalProject::getAppraisalId, appraisalId)
                .eq(PerformanceAppraisalProject::getProjectCode,appraisalProjectCode)
                .eq(PerformanceAppraisalProject::getProjectId, appraisalProjectId));

        List<Long> ids = appraisalInfoList.stream()
                .map(ProjectAppraisalInfoDTO::getProjectTemplateDetailId)
                .collect(Collectors.toList());
        // 查出项目模版详情信息
        List<PerformanceIndicatorProjectTemplateDetail> detailList = indicatorProjectTemplateDetailService.list(Wrappers.lambdaQuery(PerformanceIndicatorProjectTemplateDetail.class)
                .in(PerformanceIndicatorProjectTemplateDetail::getId, ids));
        if (CollUtil.isEmpty(detailList)){
            throw new BizException("评价指标不存在!");
        }
        Map<Long, PerformanceIndicatorProjectTemplateDetail> detailMap = detailList.stream().collect(Collectors.toMap(PerformanceIndicatorProjectTemplateDetail::getId, d -> d));
        List<PerformanceAppraisalScoreInfo> scoreInfos = appraisalInfoList.stream().map(a -> {
            PerformanceIndicatorProjectTemplateDetail templateDetail = detailMap.get(a.getProjectTemplateDetailId());
            BigDecimal score = a.getScore();
            BigDecimal indexScore = templateDetail.getIndexScore();
            if (score.compareTo(indexScore) > 0 || score.compareTo(BigDecimal.ZERO) < 0){
                throw new BizException("核查得分必须大于等于0且小于等于指标分值!");
            }
            // 保存核查分数信息
            PerformanceAppraisalScoreInfo scoreInfo = new PerformanceAppraisalScoreInfo();
            scoreInfo.setAppraisalBasis(a.getAppraisalBasis());
            scoreInfo.setAppraisalScore(a.getScore());
            scoreInfo.setAppraisalBasisAppendix(a.getAppraisalBasisAppendix());
            scoreInfo.setAppraisalType(AppraisalTypeEnum.EXPERT_VERIFY.getCode());
            scoreInfo.setTemplateDetailId(templateDetail.getId());
            scoreInfo.setTemplateId(templateDetail.getTemplateId());
            scoreInfo.setAppraisalEmployeeCode(employeeCode);
            scoreInfo.setAppraisalEmployeeName(username);
            scoreInfo.setAppraisalId(appraisalId);
            scoreInfo.setAppraisalProjectId(appraisalProjectId);
            scoreInfo.setAppraisalProjectCode(appraisalProjectCode);
            scoreInfo.setCreateOn(LocalDateTime.now());
            scoreInfo.setCreateBy(username);
            return scoreInfo;
        }).collect(Collectors.toList());

        if (performanceAppraisalScoreInfoService.saveBatch(scoreInfos)){
            // 计算核查总分并保存
            BigDecimal verifyTotalScore = appraisalInfoList.stream()
                    .map(ProjectAppraisalInfoDTO::getScore)
                    .filter(Objects::nonNull)
                    .reduce(BigDecimal.ZERO, BigDecimal::add);
            BigDecimal lastVerifyTotalScore;
            // 判断项目所在分组内其他复评人员是否已经打分
            if (Objects.nonNull(appraisalProject.getVerifyTotalScore())){
                // 已经进行了核查，计算两个分值的平均分
                BigDecimal add = verifyTotalScore.add(appraisalProject.getReAppraisalTotalScore());
                lastVerifyTotalScore = add.divide(BigDecimal.valueOf(2));
            }else {
                // 还没有进行过复评
                lastVerifyTotalScore = verifyTotalScore;
            }
            appraisalProject.setReAppraisalTotalScore(lastVerifyTotalScore);
            // 取最新地核查打分时间
            appraisalProject.setVerifyScoreTime(LocalDateTime.now());
            // 判断当前核查人是否为该评价项目所在分组的核查人员中的最后一个核查人
            judgeCompleteVerify(group, appraisalProject);
            appraisalProject.setUpdateOn(LocalDateTime.now());
            appraisalProject.setUpdateBy(username);
            if (Objects.nonNull(param.getIsRectify()) ||
                    StringUtils.isNotBlank(param.getRectifyOpinion())) {
                appraisalProject.setIsRectify(param.getIsRectify());
                appraisalProject.setRectifyOpinion(param.getRectifyOpinion());
            }
            performanceAppraisalProjectService.updateById(appraisalProject);
            return "提交成功";
        }
        return "提交失败";
    }

    private void judgeCompleteVerify(PerformanceAppraisalProjectGroup group, PerformanceAppraisalProject appraisalProject) {
        Map<String, List<PerformanceAppraisalScoreInfo>> scoreInfoMap = performanceAppraisalScoreInfoService.list(Wrappers.lambdaQuery(PerformanceAppraisalScoreInfo.class)
                        .eq(PerformanceAppraisalScoreInfo::getAppraisalType, AppraisalTypeEnum.EXPERT_VERIFY.getCode()))
                .stream().collect(Collectors.groupingBy(PerformanceAppraisalScoreInfo::getAppraisalEmployeeCode));

        List<String> verifyUsers = Lists.newArrayList();
        String verificationUsers = group.getVerificationUsers();
        String projectCodes = group.getProjectCodes();
        if (verificationUsers.contains(StrPool.COMMA)){
            verifyUsers = Arrays.asList(verificationUsers.split(StrPool.COMMA));
        }else {
            verifyUsers.add(verificationUsers);
        }
        List<String> users = Lists.newArrayList();
        for (String verifyUser : verifyUsers) {
            // 获取分组内核查人员对分组内项目的核查分数信息
            List<PerformanceAppraisalScoreInfo> scoreInfoList = scoreInfoMap.get(verifyUser);
            if (CollUtil.isNotEmpty(scoreInfoList)){
                List<Long> projectIdList = scoreInfoList.stream()
                        .map(PerformanceAppraisalScoreInfo::getAppraisalProjectId)
                        .collect(Collectors.toList());
                List<String> proCodes = projectService.listByIds(projectIdList).stream()
                        .map(Project::getProjectCode)
                        .collect(Collectors.toList());
                // 当前登录用户所在分组的核查人员 对分组内所有项目进行了核查
                if (CollUtil.isNotEmpty(proCodes)){
                    String join = String.join(StrPool.COMMA, proCodes);
                    if (join.equals(projectCodes)) {
                        users.add(verifyUser);
                    }
                }
            }
        }
        if (CollUtil.isNotEmpty(users)) {
            String join = String.join(StrPool.COMMA, users);
            if (join.equals(verificationUsers)) {
                appraisalProject.setIsCompleteVerify(Boolean.TRUE);
            }
        }
    }
}
