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

import java.time.LocalDateTime;
import java.util.List;
import java.util.Objects;

import cn.hutool.core.collection.CollUtil;
import com.google.common.collect.Lists;
import com.ningdatech.basic.util.NdDateUtils;
import com.hz.pm.api.common.model.constant.BizConst;
import com.hz.pm.api.projectlib.model.enumeration.InstTypeEnum;
import com.hz.pm.api.projectlib.model.entity.Project;
import com.hz.pm.api.projectlib.model.entity.ProjectInst;
import com.hz.pm.api.projectlib.service.IProjectInstService;
import com.hz.pm.api.projectlib.service.IProjectService;
import com.hz.pm.api.todocenter.constant.TodoCenterConst;
import com.hz.pm.api.todocenter.utils.BuildUserUtils;
import com.wflow.workflow.bean.process.ProgressNode;
import com.wflow.workflow.bean.vo.ProcessDetailVO;
import com.wflow.workflow.bean.vo.ProcessProgressVo;
import com.wflow.workflow.enums.ProcessStatusEnum;
import com.wflow.workflow.enums.StepStatusEnum;
import com.wflow.workflow.service.ProcessInstanceService;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.hz.pm.api.common.model.constant.CommonConst;


/**
 * 项目终验处理
 *
 * @author CMM
 * @since 2023/02/24 14:35
 */
@Order(11)
@Component
public class ProjectFinalInspectionHandle extends AbstractProcessBusinessHandle {
    private final IProjectInstService projectInstService;
    private final ProcessInstanceService processInstanceService;
    private final BuildUserUtils buildUserUtils;

    private final IProjectService projectService;

    private Integer order = 11;
    public ProjectFinalInspectionHandle(IProjectInstService projectInstService, ProcessInstanceService processInstanceService,
                                        BuildUserUtils buildUserUtils,IProjectService projectService){
        this.projectInstService = projectInstService;
        this.processInstanceService = processInstanceService;
        this.buildUserUtils = buildUserUtils;
        this.projectService = projectService;
    }

    @Override
    void businessHandle(Long projectId, List<ProcessDetailVO> processSchedule) {
        ProcessDetailVO processDetailVO = new ProcessDetailVO();
        Project project = projectService.getById(projectId);
        //查出历史版本的 所有项目ID
        List<Long> allVersionProjectIds = projectService.getAllVersionProjectId(project);
        // 根据项目ID查询出项目终验流程的流程状态
        ProjectInst projectInst = projectInstService.getOne(Wrappers.lambdaQuery(ProjectInst.class)
                .in(ProjectInst::getProjectId, allVersionProjectIds)
                .eq(ProjectInst::getInstType, InstTypeEnum.PROJECT_FINAL_INSPECTION.getCode())
                .ne(ProjectInst::getInstCode, TodoCenterConst.Declared.NULL_INST_CODE)
                .orderByDesc(ProjectInst::getCreatOn)
                .last(BizConst.LIMIT_1));

        if (Objects.isNull(projectInst)){
            processDetailVO.setStepStatus(StepStatusEnum.NOT_START);
            processDetailVO.setProcessName(CommonConst.PROJECT_FINAL_INSPECTION);
            processSchedule.add(processDetailVO);
            return;
        }

        String instCode = projectInst.getInstCode();
        ProcessProgressVo instanceDetail = processInstanceService.getProgressInstanceDetail(null, instCode);
        String status = instanceDetail.getStatus();
        // 项目状态不在待终验之后的状态
        if (ProcessStatusEnum.UNDER_REVIEW.getDesc().equals(status)){
            processDetailVO.setStepStatus(StepStatusEnum.ON_GOING);
        } else if (ProcessStatusEnum.BE_REJECTED.getDesc().equals(status)) {
            processDetailVO.setStepStatus(StepStatusEnum.REJECTED);
        } else if (ProcessStatusEnum.APPROVED.getDesc().equals(status)) {
            processDetailVO.setStepStatus(StepStatusEnum.COMPLETED);
        }

        // 装配节点审核人员信息
        List<ProgressNode> progressInfo = instanceDetail.getProgressInfo();
        if (CollUtil.isNotEmpty(progressInfo)) {
            buildUserUtils.buildUserByProcessInfo(progressInfo);
            // 根据流程处理详情获取流程完成时间
            if (StepStatusEnum.contains(processDetailVO.getStepStatus(),
                    Lists.newArrayList(StepStatusEnum.REJECTED,
                            StepStatusEnum.COMPLETED))) {
                ProgressNode progressNode = progressInfo.get(progressInfo.size() - 1);
                LocalDateTime finishTime = NdDateUtils.date2LocalDateTime(progressNode.getFinishTime());
                processDetailVO.setFinishTime(finishTime);
            }
        }else {
            LocalDateTime finishTime = NdDateUtils.date2LocalDateTime(instanceDetail.getStartTime());
            processDetailVO.setFinishTime(finishTime);
        }
        processDetailVO.setProcessProgressVo(instanceDetail);
        processDetailVO.setProcessName(CommonConst.PROJECT_FINAL_INSPECTION);
        processSchedule.add(processDetailVO);
    }

    @Override
    Integer getOrder() {
        return order;
    }

    @Override
    void setOrder(Integer order) {
        this.order = order;
    }
}
