package com.ningdatech.pmapi.projectlib.handle;

import java.util.*;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ningdatech.basic.util.CollUtils;
import com.ningdatech.pmapi.common.statemachine.event.ProjectStatusChangeEvent;
import com.ningdatech.pmapi.projectlib.enumeration.ProjectStatusEnum;
import com.ningdatech.pmapi.projectlib.model.entity.ProjectStatusChange;
import com.ningdatech.pmapi.projectlib.model.entity.Project;
import com.ningdatech.pmapi.projectlib.service.INdProjectStatusChangeService;
import com.ningdatech.pmapi.projectlib.service.IProjectService;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

import com.ningdatech.pmapi.common.constant.CommonConst;
import com.ningdatech.pmapi.projectlib.enumeration.StepStatusEnum;
import com.ningdatech.pmapi.projectlib.model.vo.ProcessDetailVO;

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

/**
 * 年度计划处理
 *
 * @author CMM
 * @since 2023/02/24 14:35
 */
@Order(5)
@Component
public class AnnualPlanHandle extends AbstractProcessBusinessHandle {
    private final INdProjectStatusChangeService projectStatusChangeService;
    private final IProjectService projectService;

    /**
     * 项目状态为：年度计划中之前的状态
     */
    private static final List<ProjectStatusEnum> ANNUAL_PLAN_LIST_STATUS = Arrays.asList(
            NOT_APPROVED,
            UNDER_INTERNAL_AUDIT,
            UNDER_INTERNAL_AUDIT_NOT_PASS,
            PENDING_PREQUALIFICATION,
            JOINT_REVIEW_BY_PROVINCIAL_DEPARTMENTS,
            THE_JOINT_REVIEW_OF_PROVINCIAL_DEPARTMENTS_FAILED,
            PRE_APPLYING,
            PREQUALIFICATION_FAILED,
            DEPARTMENT_JOINT_REVIEW,
            DEPARTMENT_JOINT_REVIEW_FAILED
    );

    public AnnualPlanHandle(INdProjectStatusChangeService projectStatusChangeService, IProjectService projectService){
        this.projectStatusChangeService = projectStatusChangeService;
        this.projectService = projectService;
    }

    @Override
    void businessHandle(Long projectId, List<ProcessDetailVO> processSchedule) {
        ProcessDetailVO processDetailVO = new ProcessDetailVO();
        // 根据项目状态判断年度计划是否开始
        Project project = projectService.getById(projectId);
        Integer status = project.getStatus();
        List<Integer> fieldList = CollUtils.fieldList(ANNUAL_PLAN_LIST_STATUS, ProjectStatusEnum::getCode);
        // 项目状态为年度计划中之前的状态
        if (fieldList.contains(status)){
            processDetailVO.setStepStatus(StepStatusEnum.NOT_START);
            processDetailVO.setProcessName(CommonConst.ANNUAL_PLAN);
            processSchedule.add(processDetailVO);
            return;
        }

        // 项目状态为年度计划中
        if (ProjectStatusEnum.IN_THE_ANNUAL_PLAN.getCode().equals(status)){
            // 根据部门联审通过的时间获取
            ProjectStatusChange projectStatusChange = projectStatusChangeService.getOne(Wrappers.lambdaQuery(ProjectStatusChange.class)
                    .eq(ProjectStatusChange::getProjectId, projectId)
                    .eq(ProjectStatusChange::getEvent, ProjectStatusChangeEvent.DEPARTMENT_UNITED_REVIEW_PASS.name())
                    .last("limit 1"));
            processDetailVO.setFinishTime(projectStatusChange.getCreateOn());
            processDetailVO.setStepStatus(StepStatusEnum.ON_GOING);

        } else if (ProjectStatusEnum.BE_SUSPENDED.getCode().equals(status)) {
            // 根据年度计划暂缓的时间获取
            ProjectStatusChange projectStatusChange = projectStatusChangeService.getOne(Wrappers.lambdaQuery(ProjectStatusChange.class)
                    .eq(ProjectStatusChange::getProjectId, projectId)
                    .eq(ProjectStatusChange::getEvent, ProjectStatusChangeEvent.ANNUAL_PLAN_SUSPEND.name())
                    .last("limit 1"));
            processDetailVO.setFinishTime(projectStatusChange.getCreateOn());
            processDetailVO.setStepStatus(StepStatusEnum.REJECTED);
        } else {
            // 根据开启方案申报的时间获取
            ProjectStatusChange projectStatusChange = projectStatusChangeService.getOne(Wrappers.lambdaQuery(ProjectStatusChange.class)
                    .eq(ProjectStatusChange::getProjectId, projectId)
                    .eq(ProjectStatusChange::getEvent, ProjectStatusChangeEvent.ANNUAL_PLAN_PROJECT_OPEN_PLAN_DECLARE.name())
                    .last("limit 1"));
            processDetailVO.setFinishTime(projectStatusChange.getCreateOn());
            processDetailVO.setStepStatus(StepStatusEnum.COMPLETED);
        }
        processDetailVO.setProcessName(CommonConst.ANNUAL_PLAN);
        processSchedule.add(processDetailVO);
    }
}
