package com.hz.pm.api.projectlib.handle;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.hz.pm.api.common.model.constant.BizConst;
import com.hz.pm.api.common.model.constant.CommonConst;
import com.hz.pm.api.common.statemachine.event.ProjectStatusChangeEvent;
import com.hz.pm.api.projectlib.model.enumeration.ProjectStatusEnum;
import com.hz.pm.api.projectlib.model.entity.Project;
import com.hz.pm.api.projectlib.model.entity.ProjectStatusChange;
import com.hz.pm.api.projectlib.service.INdProjectStatusChangeService;
import com.hz.pm.api.projectlib.service.IProjectService;
import com.ningdatech.basic.util.CollUtils;
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 java.util.Arrays;
import java.util.List;
import java.util.Objects;

import static com.hz.pm.api.projectlib.model.enumeration.ProjectStatusEnum.*;

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

    private Integer order = 6;

    /**
     * 项目状态为：年度计划中之前的状态
     */
    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,
            JOINT_REVIEW_BY_PROVINCIAL_DEPARTMENTS_SUCCESS,
            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);

        //查出历史版本的 所有项目ID
        List<Long> allVersionProjectIds = projectService.getAllVersionProjectId(project);

        // 项目状态为年度计划中之前的状态
        if (fieldList.contains(status)) {
            processDetailVO.setStepStatus(StepStatusEnum.NOT_START);
            processDetailVO.setProcessName(CommonConst.ANNUAL_PLAN);
            processSchedule.add(processDetailVO);
            return;
        }

        // 项目状态为年度计划中
        LambdaQueryWrapper<ProjectStatusChange> pscQuery = Wrappers.lambdaQuery(ProjectStatusChange.class)
                .in(ProjectStatusChange::getProjectId, allVersionProjectIds)
                .last(BizConst.LIMIT_1);
        if (ProjectStatusEnum.IN_THE_ANNUAL_PLAN.getCode().equals(status)) {
            // 根据部门联审通过的时间获取
            pscQuery.eq(ProjectStatusChange::getEvent, ProjectStatusChangeEvent.DEPARTMENT_UNITED_REVIEW_PASS);
            ProjectStatusChange projectStatusChange = projectStatusChangeService.getOne(pscQuery);
            if (Objects.nonNull(projectStatusChange)) {
                processDetailVO.setFinishTime(projectStatusChange.getCreateOn());
                processDetailVO.setStepStatus(StepStatusEnum.ON_GOING);
            }
        } else if (ProjectStatusEnum.BE_SUSPENDED.getCode().equals(status)) {
            // 根据年度计划暂缓的时间获取
            pscQuery.eq(ProjectStatusChange::getEvent, ProjectStatusChangeEvent.ANNUAL_PLAN_SUSPEND);
            ProjectStatusChange projectStatusChange = projectStatusChangeService.getOne(pscQuery);
            if (Objects.nonNull(projectStatusChange)) {
                processDetailVO.setFinishTime(projectStatusChange.getCreateOn());
                processDetailVO.setStepStatus(StepStatusEnum.REJECTED);
            }
        } else {
            // 根据开启方案申报的时间获取
            pscQuery.eq(ProjectStatusChange::getEvent, ProjectStatusChangeEvent.ANNUAL_PLAN_PROJECT_OPEN_PLAN_DECLARE);
            ProjectStatusChange projectStatusChange = projectStatusChangeService.getOne(pscQuery);
            if (Objects.nonNull(projectStatusChange)) {
                processDetailVO.setFinishTime(projectStatusChange.getCreateOn());
                processDetailVO.setStepStatus(StepStatusEnum.COMPLETED);
            }
        }
        processDetailVO.setProcessName(CommonConst.ANNUAL_PLAN);
        processSchedule.add(processDetailVO);
    }

    @Override
    Integer getOrder() {
        return order;
    }

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