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

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.manage.AnnualPlanLibManage;
import com.hz.pm.api.projectlib.model.entity.Project;
import com.hz.pm.api.projectlib.model.entity.ProjectStatusChange;
import com.hz.pm.api.projectlib.model.enumeration.ProjectStatusEnum;
import com.hz.pm.api.projectlib.service.INdProjectStatusChangeService;
import com.hz.pm.api.projectlib.service.IProjectService;
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.List;
import java.util.Objects;

/**
 * 立项批复处理
 *
 * @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 Integer order = 8;

    public ProjectApprovalHandle(AnnualPlanLibManage annualPlanLibManage, 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.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;
    }
}
