package com.ningdatech.pmapi.projectlib.handle;

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

import com.google.common.collect.Lists;
import com.ningdatech.basic.util.NdDateUtils;
import com.wflow.workflow.bean.process.ProgressNode;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ningdatech.pmapi.common.constant.CommonConst;
import com.ningdatech.pmapi.projectlib.enumeration.InstTypeEnum;
import com.ningdatech.pmapi.projectlib.enumeration.StepStatusEnum;
import com.ningdatech.pmapi.projectlib.model.entity.ProjectInst;
import com.ningdatech.pmapi.projectlib.model.vo.ProcessDetailVO;
import com.ningdatech.pmapi.projectlib.service.IProjectInstService;
import com.wflow.workflow.bean.vo.ProcessProgressVo;
import com.wflow.workflow.enums.ProcessStatusEnum;
import com.wflow.workflow.service.ProcessInstanceService;

/**
 * 单位内部审核处理
 *
 * @author CMM
 * @since 2023/02/24 14:35
 */
@Order(4)
@Component
public class DeptUnitedReviewHandle extends AbstractProcessBusinessHandle {

    private final IProjectInstService projectInstService;
    private final ProcessInstanceService processInstanceService;

    public DeptUnitedReviewHandle(IProjectInstService projectInstService, ProcessInstanceService processInstanceService){
        this.projectInstService = projectInstService;
        this.processInstanceService = processInstanceService;
    }

    @Override
    void businessHandle(Long projectId, List<ProcessDetailVO> processSchedule) {
        ProcessDetailVO processDetailVO = new ProcessDetailVO();
        // 根据项目ID查询出部门联审流程的流程状态
        ProjectInst projectInst = projectInstService.getOne(Wrappers.lambdaQuery(ProjectInst.class)
                .eq(ProjectInst::getProjectId, projectId)
                .eq(ProjectInst::getInstType, InstTypeEnum.DEPT_UNITED_REVIEW.getCode())
                .orderByDesc(ProjectInst::getCreatOn)
                .last("limit 1"));
        if (Objects.isNull(projectInst)){
            processDetailVO.setStepStatus(StepStatusEnum.NOT_START);
            processDetailVO.setProcessName(CommonConst.DEPT_UNITED_REVIEW);
            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);
        }
        // 根据流程处理详情获取流程完成时间
        if(StepStatusEnum.contains(processDetailVO.getStepStatus(),
                Lists.newArrayList(StepStatusEnum.REJECTED,
                        StepStatusEnum.COMPLETED))){
            List<ProgressNode> progressInfo = instanceDetail.getProgressInfo();
            ProgressNode progressNode = progressInfo.get(progressInfo.size() - 1);
            LocalDateTime finishTime = NdDateUtils.date2LocalDateTime(progressNode.getFinishTime());
            processDetailVO.setFinishTime(finishTime);
        }
        processDetailVO.setProcessProgressVo(instanceDetail);
        processDetailVO.setProcessName(CommonConst.DEPT_UNITED_REVIEW);
        processSchedule.add(processDetailVO);
    }
}
