package com.ningdatech.pmapi.projectlib.handle;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.*;

import com.ningdatech.basic.util.CollUtils;
import com.ningdatech.pmapi.projectlib.model.entity.Project;
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.enumeration.StepStatusEnum;
import com.ningdatech.pmapi.projectlib.manage.AnnualPlanLibManage;
import com.ningdatech.pmapi.projectlib.model.entity.ProjectStatusChange;
import com.ningdatech.pmapi.projectlib.model.vo.ProcessDetailVO;
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(7)
@Component
public class ProjectApprovalHandle extends AbstractProcessBusinessHandle {

    private final AnnualPlanLibManage annualPlanLibManage;
    private final INdProjectStatusChangeService projectStatusChangeService;
    private final IProjectService projectService;

    /**
     * 项目状态为：待立项批复之前的状态
     */
    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,
            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
    );

    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.getById(projectId);
        Integer status = project.getStatus();
        List<Integer> fieldList = CollUtils.fieldList(PROJECT_APPROVAL_LIST_STATUS, ProjectStatusEnum::getCode);

        // 项目状态为待立项批复之前的状态
        if (fieldList.contains(status)){
            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)
                    .eq(ProjectStatusChange::getProjectId, projectId)
                    .eq(ProjectStatusChange::getEvent, ProjectStatusChangeEvent.PLAN_REVIEW_PASS.name())
                    .last("limit 1"));
            processDetailVO.setFinishTime(projectStatusChange.getCreateOn());
            processDetailVO.setStepStatus(StepStatusEnum.ON_GOING);

        } else {
            // 当前项目状态为待立项批复之后的状态
            // 根据立项批复的时间获取
            LocalDate approvalDate = project.getApprovalDate();
            LocalDateTime approvalDateTime = approvalDate.atStartOfDay();
            processDetailVO.setFinishTime(approvalDateTime);
            processDetailVO.setStepStatus(StepStatusEnum.COMPLETED);
        }
        processDetailVO.setProcessName(CommonConst.PROJECT_APPROVAL);
        processSchedule.add(processDetailVO);
    }
}
