package com.hz.pm.api.projectdeclared.manage;

import cn.hutool.core.collection.CollUtil;
import com.alibaba.excel.EasyExcel;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.google.common.collect.Lists;
import com.hz.pm.api.common.enumeration.ProjectProcessStageEnum;
import com.hz.pm.api.common.helper.UserInfoHelper;
import com.hz.pm.api.common.model.constant.BizConst;
import com.hz.pm.api.common.util.ExcelDownUtil;
import com.hz.pm.api.common.util.ExcelExportStyle;
import com.hz.pm.api.projectdeclared.model.dto.DeclaredProjectExportDTO;
import com.hz.pm.api.projectdeclared.model.dto.DefaultDeclaredDTO;
import com.hz.pm.api.projectdeclared.model.vo.FinalAcceptanceVO;
import com.hz.pm.api.projectlib.helper.ProjectHelper;
import com.hz.pm.api.projectlib.manage.ProjectLibManage;
import com.hz.pm.api.projectlib.model.dto.ProjectDTO;
import com.hz.pm.api.projectlib.model.entity.Project;
import com.hz.pm.api.projectlib.model.enumeration.InstTypeEnum;
import com.hz.pm.api.projectlib.model.enumeration.ProjectStatusEnum;
import com.hz.pm.api.projectlib.model.enumeration.ProjectTypeNewEnum;
import com.hz.pm.api.projectlib.model.req.ProjectListReq;
import com.hz.pm.api.projectlib.model.vo.ProjectLibListItemVO;
import com.hz.pm.api.projectlib.service.IProjectService;
import com.hz.pm.api.sys.manage.NoticeManage;
import com.hz.pm.api.user.security.model.UserFullInfoDTO;
import com.hz.pm.api.user.util.LoginUserUtil;
import com.ningdatech.basic.exception.BizException;
import com.ningdatech.basic.function.VUtils;
import com.ningdatech.basic.model.PageVo;
import com.ningdatech.basic.util.CollUtils;
import com.ningdatech.basic.util.NdDateUtils;
import com.wflow.bean.entity.WflowModels;
import com.wflow.exception.BusinessException;
import com.wflow.workflow.bean.dto.OrgInfoDTO;
import com.wflow.workflow.bean.vo.ProcessStartParamsVo;
import com.wflow.workflow.service.ProcessInstanceService;
import com.wflow.workflow.service.ProcessModelService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletResponse;
import java.time.LocalDateTime;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.stream.Collectors;

import static com.hz.pm.api.staging.enums.MsgTypeEnum.PROJECT_REVIEW;
import static com.hz.pm.api.todocenter.constant.WorkNoticeConstant.PASS_MSG_TEMPLATE;

/**
 * @Classname FinalAcceptanceManage
 * @Description 终验
 * @Date 2023/6/2 14:48
 * @Author PoffyZhang
 */
@Component
@Slf4j
@RequiredArgsConstructor
public class FinalAcceptanceManage {

    private final IProjectService projectService;
    private final UserInfoHelper userInfoHelper;

    private final ProcessModelService processModelService;

    private final ProcessInstanceService processInstanceService;

    private final DefaultDeclaredProjectManage declaredProjectManage;

    private final NoticeManage noticeManage;

    private final ProjectLibManage projectLibManage;

    /**
     * 待采购的-项目列表
     *
     * @param req
     * @return
     */
    public PageVo<ProjectLibListItemVO> projectLibList(ProjectListReq req) {
        UserFullInfoDTO user = userInfoHelper.getUserFullInfo(LoginUserUtil.getUserId());
        LambdaQueryWrapper<Project> query = ProjectHelper.projectQuery(req);
        //待终验 并且还未过期
        //只能看自己单位的
        query.eq(Project::getBuildOrgCode, user.getMhUnitIdStr());
        query.eq(Project::getStatus, ProjectStatusEnum.TO_BE_FINALLY_INSPECTED.getCode());
        query.eq(Project::getStage, ProjectStatusEnum.PROJECT_APPROVED.getCode());
        query.ge(Project::getPlanAcceptanceTime, LocalDateTime.now());
        query.eq(Project::getNewest, Boolean.TRUE);
        query.isNotNull(Project::getApprovalDate);
        query.isNotNull(Project::getBuildCycle);
        query.orderByAsc(Project::getApprovalDate);
        Page<Project> page = projectService.page(req.page(), query);
        long total;
        if ((total = page.getTotal()) == 0) {
            return PageVo.empty();
        }

        List<ProjectLibListItemVO> records = CollUtils.convert(page.getRecords(), w -> {
            ProjectLibListItemVO item = new ProjectLibListItemVO();
            item.setId(w.getId());
            item.setProjectName(w.getProjectName());
            item.setCreateOn(w.getCreateOn());
            item.setDeclaredAmount(w.getDeclareAmount());
            item.setStage(w.getStage());
            item.setStatus(w.getStatus());
            item.setProjectType(w.getProjectType());
            item.setProjectYear(w.getProjectYear());
            item.setBuildOrg(w.getBuildOrgName());
            item.setBizDomain(w.getBizDomain());
            item.setProcessStatus(w.getProcessStatus());
            item.setInstCode(w.getInstCode());
            item.setIsHigherSuperOrg(w.getIsHigherSuperOrg());
            item.setApprovedAmount(w.getApprovalAmount());
            item.setApprovalDate(w.getApprovalDate());
            item.setBuildCycle(StringUtils.isNotBlank(w.getBuildCycle()) ?
                    Integer.valueOf(w.getBuildCycle()) : null);
            item.setPlanAcceptanceTime(w.getPlanAcceptanceTime());
            return item;
        });
        return PageVo.of(records, total);
    }

    public void exportList(HttpServletResponse response, ProjectListReq param) {
        UserFullInfoDTO user = userInfoHelper.getUserFullInfo(LoginUserUtil.getUserId());
        LambdaQueryWrapper<Project> query = ProjectHelper.projectQuery(param);
        //待终验
        //只能看自己单位的
        query.eq(Project::getBuildOrgCode, user.getMhUnitId());
        query.eq(Project::getStatus, ProjectStatusEnum.TO_BE_FINALLY_INSPECTED.getCode());
        query.eq(Project::getStage, ProjectStatusEnum.PROJECT_APPROVED.getCode());
        query.eq(Project::getNewest, Boolean.TRUE);
        query.isNotNull(Project::getApprovalDate);
        query.isNotNull(Project::getBuildCycle);
        query.orderByAsc(Project::getApprovalDate);
        List<Project> records = projectService.list(query);

        AtomicInteger serialNumber = new AtomicInteger(0);
        List<DeclaredProjectExportDTO> collect = Lists.newArrayList();
        if (CollUtil.isNotEmpty(records)) {
            collect = records.stream().map(r -> {
                DeclaredProjectExportDTO exportDTO = new DeclaredProjectExportDTO();
                BeanUtils.copyProperties(r, exportDTO);
                exportDTO.setProjectTypeName(ProjectTypeNewEnum.getDesc(r.getProjectType()));
                exportDTO.setStatusName(ProjectStatusEnum.getDesc(r.getStatus()));
                String createOnStr = NdDateUtils.format(r.getCreateOn(), "yyyy-MM-dd HH:mm");
                exportDTO.setCreateOn(createOnStr);
                exportDTO.setSerialNumber(serialNumber.incrementAndGet());
                exportDTO.setPlanAcceptanceTime(Objects.nonNull(r.getPlanAcceptanceTime()) ?
                        NdDateUtils.format(r.getPlanAcceptanceTime(), "yyyy-MM-dd") : StringUtils.EMPTY);
                return exportDTO;
            }).collect(Collectors.toList());
        }

        String fileName = "待终验申请项目列表";
        ExcelDownUtil.setFileName(fileName, response);
        //数据导出处理函数
        try {
            EasyExcel.write(response.getOutputStream(), DeclaredProjectExportDTO.class)
                    .autoCloseStream(false)
                    .registerWriteHandler(ExcelExportStyle.formalStyle())
                    .sheet(fileName)
                    .doWrite(collect);
        } catch (Exception e) {
            throw new BizException(e);
        }
    }


    public FinalAcceptanceVO detailByProjectId(Long projectId) {
        FinalAcceptanceVO vo = new FinalAcceptanceVO();

        Project project = projectService.getById(projectId);
        vo.setProjectId(projectId);
        if (Objects.nonNull(project)) {
            vo.setFinalAcceptanceMaterials(project.getFinalAcceptanceMaterials());
            // TODO IRS接口获取 应用信息
            vo.setApps(Collections.emptyList());
        }

        return vo;
    }

    /**
     * 开启流程
     *
     * @param dto
     * @return
     */
    public String startProcess(DefaultDeclaredDTO dto) {
        UserFullInfoDTO user = userInfoHelper.getUserFullInfo(LoginUserUtil.getUserId());
        ProjectDTO projectDto = dto.getProjectInfo();
        VUtils.isTrue(Objects.isNull(projectDto.getId())).throwMessage("提交失败 缺少项目ID!");
        Project projectInfo = projectService.getNewProject(projectDto.getId());
        VUtils.isTrue(Objects.isNull(projectInfo)).throwMessage("提交失败 此项目不存在!");
        VUtils.isTrue(StringUtils.isBlank(projectDto.getFinalAcceptanceMaterials())).throwMessage("提交失败 请提交终验材料!");

        String regionCode = projectInfo.getAreaCode();

        WflowModels model = processModelService.getOne(Wrappers.lambdaQuery(WflowModels.class)
                .eq(WflowModels::getProcessType, ProjectProcessStageEnum.ACCEPTANCE_DECLARATION_APPROVAL_PROCESS.getCode())
                .last(BizConst.LIMIT_1));

        if (Objects.isNull(model)) {
            log.error("此 【{}】区域找不到 验收申报流程配置", regionCode);
            throw new BusinessException(String.format("此 【%s】区域找不到 验收申报流程配置", regionCode));
        }

        //首先要判断 项目当前状态 是不是 方案待申报
        VUtils.isTrue((!ProjectStatusEnum.TO_BE_FINALLY_INSPECTED.getCode().equals(projectInfo.getStatus()) &&
                        !ProjectStatusEnum.FINAL_ACCEPTANCE_REVIEW_FAILED.getCode().equals(projectInfo.getStatus())) ||
                        !ProjectStatusEnum.PROJECT_APPROVED.getCode().equals(projectInfo.getStage()))
                .throwMessage("提交失败 该项目不是 待终验状态|不通过或者已立项阶段");

        ProcessStartParamsVo params = new ProcessStartParamsVo();
        params.setUser(declaredProjectManage.buildUser(user.getUserId()));
        params.setProcessUsers(Collections.emptyMap());
        //放入条件判断的项目字段
        //把条件值给放入工作流
        declaredProjectManage.buildCondition(params, projectInfo);
        // 获取发起单位、发起单位主管单位、发起单位上级条线主管单位信息
        Map<String, OrgInfoDTO> orgModelMap = declaredProjectManage.buildOrgModelMap(user.getUserId(), projectInfo);
        String instanceId = processInstanceService.startProcessLs(model, params, orgModelMap);
        log.info("终验方案项目申报成功 【{}】", instanceId);

        //保存终验项目
        //生成新版本 并且进入下一状态
        projectInfo = projectLibManage.saveProjectWithVersionAndPass(projectInfo, instanceId, projectDto,
                InstTypeEnum.PROJECT_FINAL_INSPECTION.getCode(), Boolean.FALSE);

        //发送给第一个审批人消息
        noticeManage.sendFirstUser(projectInfo, model.getFormName(), instanceId, PASS_MSG_TEMPLATE, PROJECT_REVIEW);

        return instanceId;
    }

    /**
     * 重新申报
     *
     * @param dto
     * @return
     */
    public String restartProcess(DefaultDeclaredDTO dto) {
        UserFullInfoDTO user = userInfoHelper.getUserFullInfo(LoginUserUtil.getUserId());
        ProjectDTO projectDto = dto.getProjectInfo();
        VUtils.isTrue(Objects.isNull(projectDto.getId())).throwMessage("重新提交失败 缺少项目ID!");
        Project projectInfo = projectService.getNewProject(projectDto.getId());
        VUtils.isTrue(Objects.isNull(projectInfo)).throwMessage("重新提交失败 此项目不存在!");
        VUtils.isTrue(StringUtils.isBlank(projectDto.getFinalAcceptanceMaterials())).throwMessage("提交失败 请提交终验材料!");

        WflowModels model = processModelService.getOne(Wrappers.lambdaQuery(WflowModels.class)
                .eq(WflowModels::getProcessType, ProjectProcessStageEnum.ACCEPTANCE_DECLARATION_APPROVAL_PROCESS.getCode())
                .last(BizConst.LIMIT_1));

        if (Objects.isNull(model)) {
            throw new BusinessException("找不到验收申报流程配置");
        }

        //首先要判断 项目当前状态 是不是 方案待申报
        VUtils.isTrue((!ProjectStatusEnum.TO_BE_FINALLY_INSPECTED.getCode().equals(projectInfo.getStatus()) &&
                        !ProjectStatusEnum.FINAL_ACCEPTANCE_REVIEW_FAILED.getCode().equals(projectInfo.getStatus())) ||
                        !ProjectStatusEnum.PROJECT_APPROVED.getCode().equals(projectInfo.getStage()))
                .throwMessage("提交失败 该项目不是 待终验状态|不通过或者已立项阶段");

        ProcessStartParamsVo params = new ProcessStartParamsVo();
        params.setUser(declaredProjectManage.buildUser(user.getUserId()));
        params.setProcessUsers(Collections.emptyMap());
        //放入条件判断的项目字段
        //把条件值给放入工作流
        declaredProjectManage.buildCondition(params, projectInfo, dto);
        // 获取发起单位、发起单位主管单位、发起单位上级条线主管单位信息
        Map<String, OrgInfoDTO> orgModelMap = declaredProjectManage.buildOrgModelMap(user.getUserId(), projectInfo);
        String instanceId = processInstanceService.startProcessLs(model, params, orgModelMap);
        log.info("终验方案项目重新申报成功 【{}】", instanceId);

        //保存终验项目
        //生成新版本 并且进入下一状态
        projectInfo = projectLibManage.saveProjectWithVersionAndPass(projectInfo, instanceId, projectDto,
                InstTypeEnum.PROJECT_FINAL_INSPECTION.getCode(), Boolean.TRUE);

        //发送给第一个审批人消息
        noticeManage.sendFirstUser(projectInfo, model.getFormName(), instanceId, PASS_MSG_TEMPLATE, PROJECT_REVIEW);
        return instanceId;
    }
}
