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.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.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(3)
@Component
public class ProvinceUnitedReviewHandle extends AbstractProcessBusinessHandle {
    private final IProjectService projectService;
    private final INdProjectStatusChangeService projectStatusChangeService;

    private Integer order = 3;

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

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

        //查询项目
        Project project = projectService.getNewProject(projectId);
        List<Long> allVersionProjectId = projectService.getAllVersionProjectId(projectId);

        // 根据项目ID查询出部门联审流程的流程状态
        if (Objects.isNull(project)) {
            processDetailVO.setStepStatus(StepStatusEnum.NOT_START);
            processDetailVO.setProcessName(CommonConst.PROVINCE_UNITED_REVIEW);
            processSchedule.add(processDetailVO);
            return;
        }

        Integer status = project.getStatus();
        if (ProjectStatusEnum.JOINT_REVIEW_BY_PROVINCIAL_DEPARTMENTS.getCode().equals(status)) {
            processDetailVO.setStepStatus(StepStatusEnum.ON_GOING);
            ProjectStatusChange startChange = projectStatusChangeService.getOne(Wrappers.lambdaQuery(ProjectStatusChange.class)
                    .in(ProjectStatusChange::getProjectId, allVersionProjectId)
                    .eq(ProjectStatusChange::getBeforeStatus, ProjectStatusEnum.PENDING_PREQUALIFICATION.getCode())
                    .eq(ProjectStatusChange::getAfterStatus, ProjectStatusEnum.JOINT_REVIEW_BY_PROVINCIAL_DEPARTMENTS.getCode()).orderByDesc(ProjectStatusChange::getCreateOn)
                    .orderByDesc(ProjectStatusChange::getCreateOn)
                    .last(BizConst.LIMIT_1));
            if (Objects.nonNull(startChange)) {
                processDetailVO.setFinishTime(startChange.getCreateOn());
            }
        } else if (status.compareTo(ProjectStatusEnum.JOINT_REVIEW_BY_PROVINCIAL_DEPARTMENTS.getCode()) > 0) {
            processDetailVO.setStepStatus(StepStatusEnum.COMPLETED);
            ProjectStatusChange passChange = projectStatusChangeService.getOne(Wrappers.lambdaQuery(ProjectStatusChange.class)
                    .in(ProjectStatusChange::getProjectId, allVersionProjectId)
                    .eq(ProjectStatusChange::getBeforeStatus, ProjectStatusEnum.JOINT_REVIEW_BY_PROVINCIAL_DEPARTMENTS.getCode())
                    .eq(ProjectStatusChange::getAfterStatus, ProjectStatusEnum.JOINT_REVIEW_BY_PROVINCIAL_DEPARTMENTS_SUCCESS.getCode())
                    .orderByDesc(ProjectStatusChange::getCreateOn)
                    .last(BizConst.LIMIT_1));
            if (Objects.nonNull(passChange)) {
                processDetailVO.setFinishTime(passChange.getCreateOn());
            }
        } else if (status.compareTo(ProjectStatusEnum.THE_JOINT_REVIEW_OF_PROVINCIAL_DEPARTMENTS_FAILED.getCode()) > 0) {
            processDetailVO.setStepStatus(StepStatusEnum.REJECTED);
            ProjectStatusChange rejectChange = projectStatusChangeService.getOne(Wrappers.lambdaQuery(ProjectStatusChange.class)
                    .in(ProjectStatusChange::getProjectId, allVersionProjectId)
                    .eq(ProjectStatusChange::getBeforeStatus, ProjectStatusEnum.JOINT_REVIEW_BY_PROVINCIAL_DEPARTMENTS.getCode())
                    .eq(ProjectStatusChange::getAfterStatus, ProjectStatusEnum.THE_JOINT_REVIEW_OF_PROVINCIAL_DEPARTMENTS_FAILED.getCode())
                    .orderByDesc(ProjectStatusChange::getCreateOn)
                    .last(BizConst.LIMIT_1));
            if (Objects.nonNull(rejectChange)) {
                processDetailVO.setFinishTime(rejectChange.getCreateOn());
            }
        } else {
            processDetailVO.setStepStatus(StepStatusEnum.NOT_START);
            processDetailVO.setProcessName(CommonConst.PROVINCE_UNITED_REVIEW);
            processSchedule.add(processDetailVO);
            return;
        }
        processDetailVO.setContent(project.getSjlsResult());
        processDetailVO.setProcessName(CommonConst.PROVINCE_UNITED_REVIEW);
        processSchedule.add(processDetailVO);
    }

    @Override
    Integer getOrder() {
        return order;
    }

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