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

import cn.hutool.core.bean.BeanUtil;
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.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.hz.pm.api.common.model.constant.BizConst;
import com.hz.pm.api.common.enumeration.ProjectProcessStageEnum;
import com.hz.pm.api.common.helper.UserInfoHelper;
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.DelayedApplyDTO;
import com.hz.pm.api.projectdeclared.model.vo.DelayedApplyVO;
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.helper.ProjectHelper;
import com.hz.pm.api.projectlib.manage.ProjectLibManage;
import com.hz.pm.api.projectlib.model.entity.Project;
import com.hz.pm.api.projectlib.model.entity.ProjectDelayApply;
import com.hz.pm.api.projectlib.model.entity.ProjectInst;
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.INdProjectDelayApplyService;
import com.hz.pm.api.projectlib.service.IProjectInstService;
import com.hz.pm.api.projectlib.service.IProjectService;
import com.hz.pm.api.staging.enums.MsgTypeEnum;
import com.hz.pm.api.sys.manage.NoticeManage;
import com.hz.pm.api.todocenter.constant.WorkNoticeConstant;
import com.hz.pm.api.user.security.model.UserFullInfoDTO;
import com.hz.pm.api.user.util.LoginUserUtil;
import com.wflow.bean.entity.WflowModels;
import com.wflow.contants.HisProInsEndActId;
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.flowable.engine.HistoryService;
import org.flowable.engine.history.HistoricProcessInstance;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
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;

/**
 * @Classname DelayedManage
 * @Description
 * @Date 2023/5/29 14:48
 * @Author PoffyZhang
 */
@Component
@Slf4j
@RequiredArgsConstructor
public class DelayedApplyManage {

    private final IProjectService projectService;
    private final UserInfoHelper userInfoHelper;
    private final ProcessModelService processModelService;
    private final DefaultDeclaredProjectManage defaultDeclaredProjectManage;
    private final ProcessInstanceService processInstanceService;
    private final ProjectLibManage projectLibManage;
    private final NoticeManage noticeManage;
    private final IProjectInstService projectInstService;
    private final HistoryService historyService;

    private final INdProjectDelayApplyService projectDelayApplyService;

    /**
     * 延期的-项目列表
     *
     * @param req
     * @return
     */
    public PageVo<ProjectLibListItemVO> projectLibList(ProjectListReq req) {
        UserFullInfoDTO user = userInfoHelper.getUserFullInfo(LoginUserUtil.getUserId());
        VUtils.isTrue(Objects.isNull(user)).throwMessage("获取登录用户失败!");
        LambdaQueryWrapper<Project> query = ProjectHelper.projectQuery(req);
        // 待终验 并且已经过期
        // 只能看自己单位的
        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.lt(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());
            // 判断当前项目是否已经开启了延期申请并且没有审批完成
            ProjectDelayApply delayApply = projectDelayApplyService.getOne(Wrappers.lambdaQuery(ProjectDelayApply.class)
                    .eq(ProjectDelayApply::getProjectId, w.getId())
                    .orderByDesc(ProjectDelayApply::getCreateOn)
                    .last("limit 1"));
            if (Objects.nonNull(delayApply)) {
                String instCode = delayApply.getInstanceId();
                // 获取流程实例
                HistoricProcessInstance newInstance = historyService.createHistoricProcessInstanceQuery()
                        .processInstanceId(instCode)
                        .singleResult();
                ProjectInst projectInst = projectInstService.getOne(Wrappers.lambdaQuery(ProjectInst.class)
                        .eq(ProjectInst::getInstCode, instCode));
                Integer instType = projectInst.getInstType();
                // 延期申请流程还未审核结束
                if (InstTypeEnum.APPLY_DELAY.getCode().equals(instType) &&
                        Objects.isNull(newInstance.getEndActivityId())) {
                    item.setCanDelayApply(Boolean.FALSE);
                }
                // 如果是延期申请审核被驳回,设置项目可以申请延期申报
                else if (InstTypeEnum.APPLY_DELAY.getCode().equals(instType) &&
                        HisProInsEndActId.REJECT.equals(newInstance.getEndActivityId())) {
                    item.setCanDelayApply(Boolean.TRUE);
                }
            }
            return item;
        });
        return PageVo.of(records, total);
    }

    public void exportList(HttpServletResponse response, ProjectListReq param) {
        UserFullInfoDTO user = userInfoHelper.getUserFullInfo(LoginUserUtil.getUserId());
        VUtils.isTrue(Objects.isNull(user)).throwMessage("获取登录用户失败!");
        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);

        List<DeclaredProjectExportDTO> collect = Lists.newArrayList();
        if (CollUtil.isNotEmpty(records)) {
            AtomicInteger rowNo = new AtomicInteger(0);
            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(rowNo.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 (IOException e) {
            log.error("待终验申请项目列表导出失败：{}", e.getMessage(), e);
            throw BizException.wrap("导出失败");
        }
    }

    /**
     * 延期申请
     *
     * @param dto
     * @return
     */
    public String delayedApply(DelayedApplyDTO dto) {
        UserFullInfoDTO user = userInfoHelper.getUserFullInfo(LoginUserUtil.getUserId());
        Long projectId = dto.getProjectId();
        Project project = projectService.getNewProject(projectId);
        VUtils.isTrue(Objects.isNull(project)).throwMessage("提交失败 此项目不存在!");

        //首先要判断 项目当前状态 是不是 以终验
        VUtils.isTrue(!ProjectStatusEnum.TO_BE_FINALLY_INSPECTED.getCode().equals(project.getStatus()) ||
                        !ProjectStatusEnum.PROJECT_APPROVED.getCode().equals(project.getStage()))
                .throwMessage("提交失败 该项目不是 已立项|待终验");

        VUtils.isTrue(Objects.isNull(project.getPlanAcceptanceTime())
                        || !project.getPlanAcceptanceTime().isAfter(LocalDateTime.now()))
                .throwMessage("当前项目还未过期验收");

        return "申请发起成功";
    }

    /**
     * 开启延期申请审批流程
     *
     * @param dto
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    public String startDelayedApplyProcess(DelayedApplyDTO dto) {
        UserFullInfoDTO user = userInfoHelper.getUserFullInfo(LoginUserUtil.getUserId());
        Long projectId = dto.getProjectId();
        Project project = projectService.getNewProject(projectId);
        VUtils.isTrue(Objects.isNull(project)).throwMessage("提交失败 此项目不存在!");

        //首先要判断 项目当前状态 是不是 以终验
        VUtils.isTrue(!ProjectStatusEnum.TO_BE_FINALLY_INSPECTED.getCode().equals(project.getStatus()) ||
                        !ProjectStatusEnum.PROJECT_APPROVED.getCode().equals(project.getStage()))
                .throwMessage("提交失败 该项目不是 已立项|待终验");

        VUtils.isTrue(Objects.isNull(project.getPlanAcceptanceTime())
                        || project.getPlanAcceptanceTime().isAfter(LocalDateTime.now()))
                .throwMessage("当前项目还未过期验收");

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

        if (Objects.isNull(model)) {
            throw new BusinessException("找不到延期申请流程配置");
        }

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

        // 保存项目延期实例相关
        projectLibManage.saveProjectByApplyDelay(dto, project, instanceId);

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

        return instanceId;
    }

    /**
     * 查询延期 详情
     *
     * @param projectId
     * @return
     */
    public DelayedApplyVO detailByProjectId(Long projectId) {
        ProjectDelayApply delayApply = projectDelayApplyService.getOne(Wrappers.lambdaQuery(ProjectDelayApply.class)
                .eq(ProjectDelayApply::getProjectId, projectId)
                .orderByDesc(ProjectDelayApply::getCreateOn)
                .last(BizConst.LIMIT_1));
        if (Objects.isNull(delayApply)) {
            return null;
        }
        return BeanUtil.copyProperties(delayApply, DelayedApplyVO.class);
    }

    /**
     * 查看某个项目的 延期列表
     *
     * @param projectId
     * @return
     */
    public List<DelayedApplyVO> delayList(Long projectId) {
        List<Long> allVersionProjectId = projectService.getAllVersionProjectId(projectId);
        List<ProjectDelayApply> delays = projectDelayApplyService.list(Wrappers.lambdaQuery(ProjectDelayApply.class)
                .in(ProjectDelayApply::getProjectId, allVersionProjectId)
                .eq(ProjectDelayApply::getSuccess, Boolean.TRUE));

        if (CollUtil.isEmpty(delays)) {
            return Collections.emptyList();
        }

        return delays.stream().map(d -> BeanUtil.copyProperties(d, DelayedApplyVO.class))
                .collect(Collectors.toList());
    }
}
