package com.hz.pm.api.dashboard.handle;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.hz.pm.api.common.model.constant.BizConst;
import com.hz.pm.api.common.util.BizUtils;
import com.hz.pm.api.dashboard.constant.DashboardConstant;
import com.hz.pm.api.dashboard.model.vo.ProtraitProjectStatusSituationVO;
import com.hz.pm.api.performance.model.entity.PerformanceAppraisalProject;
import com.hz.pm.api.performance.service.IPerformanceAppraisalProjectService;
import com.hz.pm.api.projectlib.model.entity.Project;
import com.hz.pm.api.projectlib.model.entity.ProjectInst;
import com.hz.pm.api.projectlib.model.entity.ProjectStatusChange;
import com.hz.pm.api.projectlib.service.INdProjectStatusChangeService;
import com.hz.pm.api.projectlib.service.IProjectInstService;
import com.hz.pm.api.projectlib.service.IProjectService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.flowable.engine.HistoryService;
import org.flowable.engine.history.HistoricProcessInstance;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * @Classname ProjectStatusHandler
 * @Description
 * @Date 2023/10/9 14:11
 * @Author PoffyZhang
 */
@Component
@RequiredArgsConstructor
@Slf4j
public class ProjectStatusHandler {

    private final IProjectService projectService;
    private final IProjectInstService projectInstService;

    private final INdProjectStatusChangeService statusChangeService;

    private final HistoryService historyService;

    private final IPerformanceAppraisalProjectService performanceAppraisalProjectService;

    /**
     * 不同的项目状态 去求不同的审批完成时间 审批类型的
     *
     * @param project \
     * @return \
     */
    public ProtraitProjectStatusSituationVO generateApproveStatus(Project project, String statusName, Integer instType) {
        ProtraitProjectStatusSituationVO vo = new ProtraitProjectStatusSituationVO();
        if (Objects.isNull(project)) {
            return vo;
        }
        vo.setStatusName(statusName);
        //查询 单位内部审核完成的时间
        String projectCode = project.getProjectCode();
        List<Project> projects = projectService.list(Wrappers.lambdaQuery(Project.class)
                .eq(Project::getProjectCode, projectCode));
        List<Long> ids = projects.stream().map(Project::getId).collect(Collectors.toList());
        //查询 项目与实例关联表
        ProjectInst unitInst = projectInstService.getOne(Wrappers.lambdaQuery(ProjectInst.class)
                .in(ProjectInst::getProjectId, ids)
                .eq(ProjectInst::getInstType, instType)
                .last(BizConst.LIMIT_1));
        if (Objects.isNull(unitInst)) {
            return vo;
        }
        //查询 这个实例 的完成时间
        HistoricProcessInstance instance = historyService.createHistoricProcessInstanceQuery()
                .finished()
                .processInstanceId(unitInst.getInstCode())
                .singleResult();
        if (Objects.isNull(instance)) {
            return vo;
        }
        Date endTime = instance.getEndTime();
        vo.setStatusTime(BizUtils.convertDate(endTime));
        return vo;
    }

    /**
     * 不同的项目状态 去求不同的审批完成时间 非审批类型的
     *
     * @param project \
     * @return \
     */
    public ProtraitProjectStatusSituationVO generateStatus(Project project, String statusName,
                                                           Integer currStatus, Integer nextStatus) {
        ProtraitProjectStatusSituationVO vo = new ProtraitProjectStatusSituationVO();
        if (Objects.isNull(project)) {
            return vo;
        }
        vo.setStatusName(statusName);
        //查询 单位内部审核完成的时间
        String projectCode = project.getProjectCode();
        //查询 状态机流转的时间点
        LambdaQueryWrapper<ProjectStatusChange> pscQuery = Wrappers
                .lambdaQuery(ProjectStatusChange.class)
                .eq(ProjectStatusChange::getBeforeStatus, currStatus)
                .eq(ProjectStatusChange::getProjectCode, projectCode)
                .eq(ProjectStatusChange::getAfterStatus, nextStatus)
                .last(BizConst.LIMIT_1);
        ProjectStatusChange statusChange = statusChangeService.getOne(pscQuery);
        if (Objects.isNull(statusChange)) {
            return vo;
        }
        vo.setStatusTime(statusChange.getCreateOn());
        return vo;
    }

    /**
     * 绩效
     *
     * @param project \
     * @return \
     */
    public ProtraitProjectStatusSituationVO generatePerformanceStatus(Project project) {
        ProtraitProjectStatusSituationVO vo = new ProtraitProjectStatusSituationVO();
        vo.setStatusName(DashboardConstant.Protrait.PERFORMANCE);
        if (Objects.isNull(project)) {
            return vo;
        }
        //查询 绩效完成 核查时间
        String projectCode = project.getProjectCode();
        LambdaQueryWrapper<PerformanceAppraisalProject> papQuery = Wrappers
                .lambdaQuery(PerformanceAppraisalProject.class)
                .eq(PerformanceAppraisalProject::getProjectCode, projectCode)
                .isNotNull(PerformanceAppraisalProject::getVerifyTotalScore)
                .isNotNull(PerformanceAppraisalProject::getVerifyScoreTime)
                .last(BizConst.LIMIT_1);
        PerformanceAppraisalProject performance = performanceAppraisalProjectService.getOne(papQuery);
        if (Objects.nonNull(performance)) {
            vo.setStatusTime(performance.getVerifyScoreTime());
        }
        return vo;
    }
}
