package com.ningdatech.pmapi.projectlib.handle;

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

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.google.common.collect.Lists;
import com.ningdatech.basic.util.NdDateUtils;
import com.ningdatech.pmapi.projectlib.enumeration.InstTypeEnum;
import com.ningdatech.pmapi.projectlib.model.entity.ProjectInst;
import com.ningdatech.pmapi.projectlib.service.IProjectInstService;

import com.ningdatech.pmapi.common.constant.CommonConst;
import com.ningdatech.pmapi.projectlib.enumeration.StepStatusEnum;
import com.ningdatech.pmapi.projectlib.model.vo.ProcessDetailVO;
import com.wflow.workflow.bean.process.ProgressNode;
import com.wflow.workflow.bean.vo.ProcessProgressVo;
import com.wflow.workflow.enums.ProcessStatusEnum;
import com.wflow.workflow.service.ProcessInstanceService;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

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

    private final IProjectInstService projectInstService;

    private final ProcessInstanceService processInstanceService;

    public UnitInnerAuditHandle(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.UNIT_INNER_AUDIT.getCode())
                .orderByDesc(ProjectInst::getCreatOn)
                .last("limit 1"));
        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.UNIT_INNER_AUDIT);
        processSchedule.add(processDetailVO);
    }
}
