package com.ningdatech.pmapi.projectlib.handle;

import java.util.*;
import java.util.stream.Collectors;

import cn.hutool.core.collection.CollUtil;
import com.ningdatech.basic.util.CollUtils;
import com.ningdatech.pmapi.common.constant.BizConst;
import com.ningdatech.pmapi.projectlib.model.entity.Project;
import com.wflow.workflow.bean.vo.ProcessDetailVO;
import com.wflow.workflow.enums.StepStatusEnum;
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.common.statemachine.event.ProjectStatusChangeEvent;
import com.ningdatech.pmapi.projectlib.enumeration.ProjectStatusEnum;
import com.ningdatech.pmapi.projectlib.manage.AnnualPlanLibManage;
import com.ningdatech.pmapi.projectlib.model.entity.ProjectStatusChange;
import com.ningdatech.pmapi.projectlib.service.INdProjectStatusChangeService;
import com.ningdatech.pmapi.projectlib.service.IProjectService;

import static com.ningdatech.pmapi.projectlib.enumeration.ProjectStatusEnum.*;

/**
 * 立项批复处理
 *
 * @author CMM
 * @since 2023/02/24 14:35
 */
@Order(8)
@Component
public class ProjectApprovalHandle extends AbstractProcessBusinessHandle {
    private final INdProjectStatusChangeService projectStatusChangeService;
    private final IProjectService projectService;
    private final AnnualPlanLibManage annualPlanLibManage;

    private Integer order = 8;

    /**
     * 项目状态为：待立项批复之前的状态
     */
    private static final List<ProjectStatusEnum> PROJECT_APPROVAL_LIST_STATUS = Arrays.asList(
            NOT_APPROVED,
            UNDER_INTERNAL_AUDIT,
            UNDER_INTERNAL_AUDIT_NOT_PASS,
            PENDING_PREQUALIFICATION,
            JOINT_REVIEW_BY_PROVINCIAL_DEPARTMENTS,
            JOINT_REVIEW_BY_PROVINCIAL_DEPARTMENTS_SUCCESS,
            THE_JOINT_REVIEW_OF_PROVINCIAL_DEPARTMENTS_FAILED,
            PRE_APPLYING,
            PREQUALIFICATION_FAILED,
            DEPARTMENT_JOINT_REVIEW,
            DEPARTMENT_JOINT_REVIEW_FAILED,
            IN_THE_ANNUAL_PLAN,
            BE_SUSPENDED,
            SCHEME_UNDER_REVIEW,
            SCHEME_REVIEW_FAILED,
            TO_BE_DECLARED,
            PLAN_TO_BE_DECLARED
    );

    public ProjectApprovalHandle(AnnualPlanLibManage annualPlanLibManage, INdProjectStatusChangeService projectStatusChangeService, IProjectService projectService){
        this.annualPlanLibManage = annualPlanLibManage;
        this.projectStatusChangeService = projectStatusChangeService;
        this.projectService = projectService;
    }

    @Override
    void businessHandle(Long projectId, List<ProcessDetailVO> processSchedule) {
        ProcessDetailVO processDetailVO = new ProcessDetailVO();

        Project project = projectService.getNewProject(projectId);
        //查出历史版本的 所有项目ID
        List<Long> allVersionProjectIds = projectService.getAllVersionProjectId(project);
        Integer status = project.getStatus();
        if(Objects.isNull(status)){
            return;
        }

        // 项目状态为待立项批复之前的状态
        if (status < ProjectStatusEnum.TO_BE_APPROVED.getCode()){
            processDetailVO.setStepStatus(StepStatusEnum.NOT_START);
            processDetailVO.setProcessName(CommonConst.PROJECT_APPROVAL);
            processSchedule.add(processDetailVO);
            return;
        }

        // 当前项目状态为待立项批复
        if (ProjectStatusEnum.TO_BE_APPROVED.getCode().equals(status)){
            // 根据建设方案评审通过的时间获取
            ProjectStatusChange projectStatusChange = projectStatusChangeService.getOne(Wrappers.lambdaQuery(ProjectStatusChange.class)
                    .in(ProjectStatusChange::getProjectId, allVersionProjectIds)
                    .eq(ProjectStatusChange::getEvent, ProjectStatusChangeEvent.PLAN_REVIEW_PASS.name())
                    .orderByDesc(ProjectStatusChange::getCreateOn)
                    .last(BizConst.LIMIT_1));
            processDetailVO.setFinishTime(projectStatusChange.getCreateOn());
            processDetailVO.setStepStatus(StepStatusEnum.ON_GOING);

        } else {
            // 取 状态机 改变状态时间
            ProjectStatusChange projectStatusChange = projectStatusChangeService.getOne(Wrappers.lambdaQuery(ProjectStatusChange.class)
                    .in(ProjectStatusChange::getProjectId, allVersionProjectIds)
                    .eq(ProjectStatusChange::getEvent, ProjectStatusChangeEvent.PROJECT_APPROVAL.name())
                    .last(BizConst.LIMIT_1));
            if (Objects.nonNull(projectStatusChange)) {
                processDetailVO.setFinishTime(projectStatusChange.getCreateOn());
                processDetailVO.setStepStatus(StepStatusEnum.COMPLETED);
            }
        }
        processDetailVO.setProcessName(CommonConst.PROJECT_APPROVAL);
        processSchedule.add(processDetailVO);
    }

    @Override
    Integer getOrder() {
        return order;
    }

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