package com.ningdatech.pmapi.projectdeclared.manage;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ningdatech.basic.function.VUtils;
import com.ningdatech.file.service.FileService;
import com.ningdatech.pmapi.projectdeclared.converter.ApplicationConverter;
import com.ningdatech.pmapi.projectdeclared.model.dto.DefaultDeclaredDTO;
import com.ningdatech.pmapi.projectlib.enumeration.ProjectStatusEnum;
import com.ningdatech.pmapi.projectlib.manage.ProjectLibManage;
import com.ningdatech.pmapi.projectlib.model.dto.ProjectDTO;
import com.ningdatech.pmapi.projectlib.model.entity.Project;
import com.ningdatech.pmapi.projectlib.model.entity.ProjectApplication;
import com.ningdatech.pmapi.projectlib.service.IProjectApplicationService;
import com.ningdatech.pmapi.projectlib.service.IProjectService;
import com.ningdatech.pmapi.provincial.service.IJoinReviewProvincialBureauService;
import com.ningdatech.pmapi.staging.service.IProjectStagingService;
import com.ningdatech.pmapi.todocenter.constant.TodoCenterConstant;
import com.wflow.exception.BusinessException;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Objects;

/**
 * @Classname ReviewByProvincialDeptManage
 * @Description 省级部门联审
 * @Date 2023/2/17 14:48
 * @Author PoffyZhang
 */
@Component
@Slf4j
@RequiredArgsConstructor
public class ReviewByProvincialDeptManage {

    private final IProjectService projectService;

    private final FileService fileService;

    private final IJoinReviewProvincialBureauService joinReviewProvincialBureauService;

    private final IProjectApplicationService applicationService;

    private final ProjectLibManage projectLibManage;

    @Value("${spring.profiles.active}")
    private String active;

    /**
     * 省级部门联审
     * @param project
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    public Boolean startTheProcess(Project project) {
        VUtils.isTrue(Objects.isNull(project.getId())).throwMessage("提交失败 缺少项目ID!");
        Project projectInfo = projectService.getById(project.getId());
        VUtils.isTrue(Objects.isNull(projectInfo)).throwMessage("提交失败 此项目不存在!");
        String regionCode = projectInfo.getAreaCode();

        //首先要判断 项目当前状态 是不是 省级部门联审
        VUtils.isTrue(!ProjectStatusEnum.JOINT_REVIEW_BY_PROVINCIAL_DEPARTMENTS.getCode().equals(projectInfo.getStatus()) ||
                !ProjectStatusEnum.NOT_APPROVED.getCode().equals(projectInfo.getStage()))
                .throwMessage("提交失败 该项目不是 省级部门联审状态状态或者未立项阶段");

        // 对接省级联审的接口
        List<ProjectApplication> applications = applicationService.list(Wrappers.lambdaQuery(ProjectApplication.class)
                .eq(ProjectApplication::getProjectId, projectInfo.getId()));
        if(joinReviewProvincialBureauService.pushImportProject(
                ApplicationConverter.convertProject(projectInfo,applications,fileService,active))){
            return Boolean.TRUE;
        }

        return Boolean.FALSE;
    }

    /**
     * 省级部门联审 重新提交
     * @param declaringDTO
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    public String restartTheProcess(DefaultDeclaredDTO declaringDTO) {
        ProjectDTO project = declaringDTO.getProjectInfo();
        VUtils.isTrue(Objects.isNull(project.getId())).throwMessage("提交失败 缺少项目ID!");
        Project projectInfo = projectService.getNewProject(project.getId());
        VUtils.isTrue(Objects.isNull(projectInfo)).throwMessage("提交失败 此项目不存在!");
        String regionCode = projectInfo.getAreaCode();

        //首先要判断 项目当前状态 是不是 省级部门联审不通过
        VUtils.isTrue(!ProjectStatusEnum.THE_JOINT_REVIEW_OF_PROVINCIAL_DEPARTMENTS_FAILED.getCode().equals(projectInfo.getStatus()) ||
                        !ProjectStatusEnum.NOT_APPROVED.getCode().equals(projectInfo.getStage()))
                .throwMessage("提交失败 该项目不是 省级部门联审不通过状态或者未立项阶段");

        // 对接省级联审的接口
        Project p = projectLibManage.saveProjectWithVersionAndStatus(project,null,
                ProjectStatusEnum.JOINT_REVIEW_BY_PROVINCIAL_DEPARTMENTS.getCode(),Boolean.FALSE);

        List<ProjectApplication> applications = applicationService.list(Wrappers.lambdaQuery(ProjectApplication.class)
                .eq(ProjectApplication::getProjectId, p.getId()));

        //入库暂存表 后续处理 对接外部接口
        p.setUpdateOn(LocalDateTime.now());
        //保存一下  当前的主管单位发起人
        p.setPreStartUserId(p.getSponsor());
        //当前实例置为空
        p.setInstCode(TodoCenterConstant.Declared.NULL_INST_CODE);
        declaringDTO.getProjectInfo().setId(p.getId());
        projectService.updateById(p);
        if(!joinReviewProvincialBureauService.pushImportProject(
                ApplicationConverter.convertProject(p,applications,fileService,active))){
            throw new BusinessException("提交省级部门联审失败");
        }

        return String.valueOf(p.getId());
    }
}
