package com.hz.pm.api.scheduler.task;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.date.StopWatch;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.hz.pm.api.common.enumeration.CommonEnum;
import com.hz.pm.api.common.model.constant.BizConst;
import com.hz.pm.api.projectdeclared.model.entity.Contract;
import com.hz.pm.api.projectdeclared.model.entity.Operation;
import com.hz.pm.api.projectdeclared.service.IContractService;
import com.hz.pm.api.projectdeclared.service.IOperationService;
import com.hz.pm.api.projectlib.model.entity.Project;
import com.hz.pm.api.projectlib.model.entity.ProjectRenewalFundDeclaration;
import com.hz.pm.api.projectlib.model.enumeration.*;
import com.hz.pm.api.projectlib.service.IProjectRenewalFundDeclarationService;
import com.hz.pm.api.projectlib.service.IProjectService;
import com.hz.pm.api.sys.manage.EarlyWarningManage;
import com.hz.pm.api.sys.manage.RoleManage;
import com.hz.pm.api.sys.service.IProjectEarlyWarningService;
import com.hz.pm.api.user.model.entity.UserInfo;
import com.hz.pm.api.user.model.enumeration.RoleEnum;
import com.hz.pm.api.user.service.IUserInfoService;
import com.ningdatech.basic.util.StrPool;
import com.wflow.bean.entity.WflowEarlyWarning;
import com.wflow.enums.WarningRuleTypeEnum;
import com.wflow.service.IEarlyWarningService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.net.InetAddress;
import java.net.UnknownHostException;
import java.time.Duration;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * @author ZPF
 * @date 2023/8/3 上午9:53
 * 预警填报 超时任务
 */
@Component
@Slf4j
@RequiredArgsConstructor
public class EarlyWarningInstanceNotStartTask {

    @Value("${hostname}")
    private String HOST_NAME;

    private final IEarlyWarningService earlyWarningService;

    private final IProjectService projectService;

    private final EarlyWarningManage earlyWarningManage;

    private final IContractService contractService;

    private final IOperationService operationService;

    private final IUserInfoService userInfoService;

    private final IProjectRenewalFundDeclarationService renewalFundDeclarationService;

    private final RoleManage roleManage;

    private final IProjectEarlyWarningService projectEarlyWarningService;

    @Scheduled(cron = "0 0/2 * * * ?")
    public void doEarlyWarningDeclared() throws UnknownHostException {
        if (!HOST_NAME.equals(InetAddress.getLocalHost().getHostName())) {
            return;
        }

        log.info("=========== 预警填报超时任务开始 ========");
        StopWatch stopWatch = new StopWatch();
        stopWatch.start();

        // 1.查询 填报的 预警规则 填报类型的 每个区域 每个规则
        List<WflowEarlyWarning> warnings = earlyWarningService.list(Wrappers.lambdaQuery(WflowEarlyWarning.class)
                .eq(WflowEarlyWarning::getRuleType, WarningRuleTypeEnum.DECLARED_WARNING.getCode()));
        for (WflowEarlyWarning warning : warnings) {
            //2. 取出rule的数据
            if (!warning.getIsOpen()) {
                log.info(warning.getId() + " 此规则关闭了");
                continue;
            }

            String noticeMethod = warning.getNoticeMethod();
            if (StringUtils.isBlank(noticeMethod) ||
                    (!noticeMethod.contains(String.valueOf(CommonEnum.ZWDD.getCode())) &&
                            !noticeMethod.contains(String.valueOf(CommonEnum.MOBILE.getCode())))) {
                log.info("通知方式为空或者错误!");
                return;
            }

            String noticeContent = warning.getNoticeContent();
            String adventContent = warning.getAdventContent();
            String rule = warning.getRule();
            if (StringUtils.isNotBlank(rule)) {
                JSONArray ruleArray = JSON.parseArray(rule);
                if (CollUtil.isNotEmpty(ruleArray)) {
                    ruleArray.forEach(r -> {
                        JSONObject rJson = JSON.parseObject(JSON.toJSONString(r));
                        Integer time = rJson.getInteger("time");
                        Integer biz = rJson.getInteger("biz");
                        String notice = rJson.getString("notice");
                        Integer adventTime = rJson.getInteger("adventTime");
                        if ((Objects.isNull(time) && Objects.isNull(adventTime)) || Objects.isNull(biz)) {
                            log.info("规则数据 错误 :{}", rJson);
                            return;
                        }

                        if (StringUtils.isBlank(notice)) {
                            log.info("取不到提醒人配置 : " + rJson);
                            return;
                        }

                        WarningFlowTypeEnum flowTypeEnum = WarningFlowTypeEnum.getByCode(biz);
                        if (Objects.isNull(flowTypeEnum)) {
                            log.info("匹配不到 业务类型");
                            return;
                        }
                        String path = flowTypeEnum.getPath();

                        //得出 对应待提交的项目状态
                        Integer projectStutas = flowTypeEnum.getProjectStutas();
                        String areaCode = warning.getAreaCode();
                        //测试先用分钟
                        //查询 所有这个区域的项目 未提交的项目
                        List<Project> needNextProjects = projectService.list(Wrappers.lambdaQuery(Project.class)
                                .eq(Project::getAreaCode, areaCode)
                                .eq(Project::getNewest, Boolean.TRUE)
                                .eq(Project::getStatus, projectStutas));

                        Map<String, List<Integer>> noticeMap = Maps.newHashMap();
                        //需要发通知的项目
                        List<Project> needToWaringProjects = needNextProjects.stream()
                                .filter(p -> {
                                    //判断 当状态在 建设中的时候 是不是要初验了
                                    if (ProjectStatusEnum.UNDER_CONSTRUCTION.getCode().equals(projectStutas)) {
                                        List<Long> allVersionProjectId = projectService.getAllVersionProjectId(p);
                                        //如果合同信息提交过了 才是 待初验状态
                                        if (StringUtils.isNotBlank(p.getPreliminaryInspectionMaterials())) {
                                            if (0L == contractService.count(Wrappers.lambdaQuery(Contract.class)
                                                    .in(Contract::getProjectId, allVersionProjectId))) {
                                                return Boolean.FALSE;
                                            }
                                        }
                                    }

                                    List<Integer> noticeTypes = Lists.newArrayList();
                                    if (Objects.nonNull(time) && Duration.between(p.getUpdateOn(), LocalDateTime.now()).toMinutes() >= time * 60 && //time * 60
                                            Duration.between(p.getUpdateOn(), LocalDateTime.now()).toMinutes() <= time * 60 + 1) { //time * 60
                                        noticeTypes.add(com.wflow.enums.WarningNoticeTypeEnum.OVER.getCode());
                                    }
                                    if (Objects.nonNull(adventTime) && Duration.between(p.getUpdateOn(), LocalDateTime.now()).toMinutes() >= adventTime * 60 && //time * 60
                                            Duration.between(p.getUpdateOn(), LocalDateTime.now()).toMinutes() <= adventTime * 60 + 1) {
                                        noticeTypes.add(com.wflow.enums.WarningNoticeTypeEnum.ADVENT.getCode());
                                    }
                                    if (CollUtil.isNotEmpty(noticeTypes)) {
                                        noticeMap.put(p.getProjectCode(), noticeTypes);
                                        //项目 填报预警
                                        projectEarlyWarningService.earlyWarning(p,
                                                WarningRuleTypeEnum.DECLARED_WARNING.getCode(), noticeTypes);
                                        return Boolean.TRUE;
                                    }
                                    return Boolean.FALSE;
                                })
                                .collect(Collectors.toList());

                        if (StringUtils.isBlank(noticeMethod) ||
                                (!noticeMethod.contains(String.valueOf(CommonEnum.ZWDD.getCode())) &&
                                        !noticeMethod.contains(String.valueOf(CommonEnum.MOBILE.getCode())))) {
                            log.info("通知方式为空或者错误!");
                            return;
                        }

                        for (Project needToWaringProject : needToWaringProjects) {
                            String username = null;
                            String mobile = null;
                            //去预警通知人 1.项目联系人 2.项目负责人 如果都包含 都要发
                            List<String> batchEmployees = Lists.newArrayList();
                            if (notice.contains(WarningNoticeTypeEnum.CONTACT.getCode().toString())) {
                                String employeeCode = null;
                                username = needToWaringProject.getContactName();
                                mobile = needToWaringProject.getContactPhone();
                                if (StringUtils.isNotBlank(username) && StringUtils.isNotBlank(mobile)) {
                                    UserInfo user = userInfoService.getOne(Wrappers.lambdaQuery(UserInfo.class)
                                            .eq(UserInfo::getUsername, username)
                                            .eq(UserInfo::getMobile, mobile)
                                            .last(BizConst.LIMIT_1));
                                    if (Objects.nonNull(user)) {
                                        employeeCode = user.getEmployeeCode();
                                    }
                                    if (Objects.nonNull(employeeCode)) {
                                        batchEmployees.add(employeeCode);
                                    }
                                }
                            }
                            if (notice.contains(WarningNoticeTypeEnum.RESPONSIBLE.getCode().toString())) {
                                String employeeCode = null;
                                username = needToWaringProject.getResponsibleMan();
                                mobile = needToWaringProject.getResponsibleManMobile();
                                if (StringUtils.isNotBlank(username) && StringUtils.isNotBlank(mobile)) {
                                    UserInfo user = userInfoService.getOne(Wrappers.lambdaQuery(UserInfo.class)
                                            .eq(UserInfo::getUsername, username)
                                            .eq(UserInfo::getMobile, mobile)
                                            .last(BizConst.LIMIT_1));
                                    if (Objects.nonNull(user)) {
                                        employeeCode = user.getEmployeeCode();
                                    }
                                    if (Objects.nonNull(employeeCode)) {
                                        batchEmployees.add(employeeCode);
                                    }
                                }
                            }
                            if (CollUtil.isNotEmpty(batchEmployees)) {
                                String batchEmployeesStr = String.join(StrPool.COMMA, batchEmployees);
                                if (noticeMap.containsKey(needToWaringProject.getProjectCode())) {
                                    List<Integer> noticeTypes = noticeMap.get(needToWaringProject.getProjectCode());
                                    for (Integer noticeType : noticeTypes) {
                                        for (String employeeCode : batchEmployees) {
                                            earlyWarningManage.doEarlyWarning(noticeMethod, Objects.nonNull(noticeType) && noticeType.equals(1) ? noticeContent : adventContent,
                                                    adventTime, time, biz,
                                                    needToWaringProject.getUpdateOn(), Long.valueOf(employeeCode), needToWaringProject,
                                                    WarningRuleTypeEnum.DECLARED_WARNING.getCode(), noticeType, path, batchEmployeesStr,
                                                    null);
                                        }
                                    }
                                }
                            }
                        }
                    });
                }
            }
        }

        stopWatch.stop();
        log.info("=========== 预警填报超时任务结束 耗时{}s", stopWatch.getTotalTimeSeconds());
    }

    @Scheduled(cron = "10 0/2 * * * ?")
    public void doEarlyWarningOperation() throws UnknownHostException {
        if (!HOST_NAME.equals(InetAddress.getLocalHost().getHostName())) {
            return;
        }

        log.info("=========== 预警实施超时任务开始 ========");
        StopWatch stopWatch = new StopWatch();
        stopWatch.start();

        // 1.查询 填报的 预警规则 填报类型的 每个区域 每个规则
        List<WflowEarlyWarning> warnings = earlyWarningService.list(Wrappers.lambdaQuery(WflowEarlyWarning.class)
                .eq(WflowEarlyWarning::getRuleType, WarningRuleTypeEnum.OPERATION_WARNING.getCode()));
        for (WflowEarlyWarning warning : warnings) {
            //2. 取出rule的数据
            if (!warning.getIsOpen()) {
                log.info(warning.getId() + " 此规则关闭了");
                continue;
            }

            String noticeMethod = warning.getNoticeMethod();
            if (StringUtils.isBlank(noticeMethod) ||
                    (!noticeMethod.contains(String.valueOf(CommonEnum.ZWDD.getCode())) &&
                            !noticeMethod.contains(String.valueOf(CommonEnum.MOBILE.getCode())))) {
                log.info("通知方式为空或者错误!");
                return;
            }

            String noticeContent = warning.getNoticeContent();
            String adventContent = warning.getAdventContent();
            String rule = warning.getRule();
            if (StringUtils.isNotBlank(rule)) {
                JSONArray ruleArray = JSON.parseArray(rule);
                if (CollUtil.isNotEmpty(ruleArray)) {
                    ruleArray.forEach(r -> {
                        JSONObject rJson = JSON.parseObject(JSON.toJSONString(r));
                        Integer time = rJson.getInteger("time");
                        Integer adventTime = rJson.getInteger("adventTime");
                        Integer biz = rJson.getInteger("biz");
                        String notice = rJson.getString("notice");
                        if ((Objects.isNull(time) && Objects.isNull(adventTime)) || Objects.isNull(biz)) {
                            log.info("规则数据 错误 :{}", rJson);
                            return;
                        }

                        WarningOperationTypeEnum operationTypeEnum = WarningOperationTypeEnum.getByCode(biz);
                        if (Objects.isNull(operationTypeEnum)) {
                            log.info("匹配不到 业务类型");
                            return;
                        }

                        //得出 对应待提交的项目状态
                        Integer projectStutas = operationTypeEnum.getProjectStutas();
                        String path = operationTypeEnum.getPath();
                        String areaCode = warning.getAreaCode();
                        //测试先用分钟
                        //查询 所有这个区域的项目 未提交的项目
                        List<Project> needNextProjects = projectService.list(Wrappers.lambdaQuery(Project.class)
                                .eq(Project::getAreaCode, areaCode)
                                .eq(Project::getNewest, Boolean.TRUE)
                                .eq(Project::getStatus, projectStutas));

                        List<String> projectCodes = needNextProjects.stream().map(Project::getProjectCode).collect(Collectors.toList());

                        List<Operation> operations = operationService.list(Wrappers.lambdaQuery(Operation.class)
                                .in(Operation::getProjectCode, projectCodes));

                        Map<String, Operation> operationMap = operations.stream().collect(Collectors.toMap(Operation::getProjectCode, o -> o));
                        Map<String, List<Integer>> noticeMap = Maps.newHashMap();
                        //需要发通知的项目
                        List<Project> needToWaringProjects = needNextProjects.stream()
                                .filter(p -> {
                                    //判断 当状态在 建设中的时候 是不是要初验了
                                    if (ProjectStatusEnum.UNDER_CONSTRUCTION.getCode().equals(projectStutas)) {
                                        List<Long> allVersionProjectId = projectService.getAllVersionProjectId(p);
                                        //如果合同信息提交过了 才是 待初验状态
                                        if (StringUtils.isNotBlank(p.getPreliminaryInspectionMaterials())) {
                                            if (0L == contractService.count(Wrappers.lambdaQuery(Contract.class)
                                                    .in(Contract::getProjectId, allVersionProjectId))) {
                                                return Boolean.FALSE;
                                            }
                                        }
                                    }

                                    //判断 实施信息中 初验和终验的时间
                                    if (!operationMap.containsKey(p.getProjectCode())) {
                                        return Boolean.FALSE;
                                    }
                                    Operation operation = operationMap.get(p.getProjectCode());

                                    List<Integer> noticeTypes = Lists.newArrayList();
                                    if (WarningOperationTypeEnum.CHUYAN.getCode().equals(biz)) {
                                        //初验
                                        if (Objects.nonNull(time) && Duration.between(operation.getInitialInspectionDate(), LocalDateTime.now()).toMinutes() >= time * 60 && //time * 60
                                                Duration.between(operation.getInitialInspectionDate(), LocalDateTime.now()).toMinutes() <= time * 60 + 1) { //time * 60
                                            noticeTypes.add(com.wflow.enums.WarningNoticeTypeEnum.OVER.getCode());
                                        }
                                        if (Objects.nonNull(adventTime) && Duration.between(operation.getInitialInspectionDate(), LocalDateTime.now()).toMinutes() >= adventTime * 60 && //time * 60
                                                Duration.between(operation.getInitialInspectionDate(), LocalDateTime.now()).toMinutes() <= adventTime * 60 + 1) {
                                            noticeTypes.add(com.wflow.enums.WarningNoticeTypeEnum.ADVENT.getCode());
                                        }
                                    } else if (WarningOperationTypeEnum.ZHONGYAN.getCode().equals(biz)) {
                                        //终验
                                        if (Objects.nonNull(time) && Duration.between(operation.getFinalInspectionDate(), LocalDateTime.now()).toMinutes() >= time * 60 && //time * 60
                                                Duration.between(operation.getFinalInspectionDate(), LocalDateTime.now()).toMinutes() <= time * 60 + 1) { //time * 60
                                            noticeTypes.add(com.wflow.enums.WarningNoticeTypeEnum.OVER.getCode());
                                        }
                                        if (Objects.nonNull(adventTime) && Duration.between(operation.getFinalInspectionDate(), LocalDateTime.now()).toMinutes() >= adventTime * 60 && //time * 60
                                                Duration.between(operation.getFinalInspectionDate(), LocalDateTime.now()).toMinutes() <= adventTime * 60 + 1) { //time * 60
                                            noticeTypes.add(com.wflow.enums.WarningNoticeTypeEnum.ADVENT.getCode());
                                        }
                                    }
                                    if (CollUtil.isNotEmpty(noticeTypes)) {
                                        noticeMap.put(p.getProjectCode(), noticeTypes);
                                        //项目 运维预警
                                        projectEarlyWarningService.earlyWarning(p,
                                                WarningRuleTypeEnum.OPERATION_WARNING.getCode(), noticeTypes);
                                        return Boolean.TRUE;
                                    }
                                    return Boolean.FALSE;
                                })
                                .collect(Collectors.toList());

                        if (StringUtils.isBlank(noticeMethod) ||
                                (!noticeMethod.contains(String.valueOf(CommonEnum.ZWDD.getCode())) &&
                                        !noticeMethod.contains(String.valueOf(CommonEnum.MOBILE.getCode())))) {
                            log.info("通知方式为空或者错误!");
                            return;
                        }

                        for (Project needToWaringProject : needToWaringProjects) {
                            //去预警通知
                            List<String> batchEmployees = Lists.newArrayList();

                            if (StringUtils.isBlank(notice)) {
                                String sponsor = needToWaringProject.getSponsor();
                                batchEmployees.add(sponsor);
                            } else {
                                //去预警通知人 1.项目联系人 2.项目负责人 如果都包含 都要发

                                if (notice.contains(WarningNoticeTypeEnum.CONTACT.getCode().toString())) {
                                    String employeeCode = null;
                                    String username = needToWaringProject.getContactName();
                                    String mobile = needToWaringProject.getContactPhone();
                                    if (StringUtils.isNotBlank(username) && StringUtils.isNotBlank(mobile)) {
                                        UserInfo user = userInfoService.getOne(Wrappers.lambdaQuery(UserInfo.class)
                                                .eq(UserInfo::getUsername, username)
                                                .eq(UserInfo::getMobile, mobile)
                                                .last(BizConst.LIMIT_1));
                                        if (Objects.nonNull(user)) {
                                            employeeCode = user.getEmployeeCode();
                                        }
                                        if (Objects.nonNull(employeeCode)) {
                                            batchEmployees.add(employeeCode);
                                        }
                                    }
                                }
                                if (notice.contains(WarningNoticeTypeEnum.RESPONSIBLE.getCode().toString())) {
                                    String employeeCode = null;
                                    String username = needToWaringProject.getResponsibleMan();
                                    String mobile = needToWaringProject.getResponsibleManMobile();
                                    if (StringUtils.isNotBlank(username) && StringUtils.isNotBlank(mobile)) {
                                        UserInfo user = userInfoService.getOne(Wrappers.lambdaQuery(UserInfo.class)
                                                .eq(UserInfo::getUsername, username)
                                                .eq(UserInfo::getMobile, mobile)
                                                .last(BizConst.LIMIT_1));
                                        if (Objects.nonNull(user)) {
                                            employeeCode = user.getEmployeeCode();
                                        }
                                        if (Objects.nonNull(employeeCode)) {
                                            batchEmployees.add(employeeCode);
                                        }
                                    }
                                }
                            }

                            if (noticeMap.containsKey(needToWaringProject.getProjectCode())) {
                                List<Integer> noticeTypes = noticeMap.get(needToWaringProject.getProjectCode());
                                for (Integer noticeType : noticeTypes) {
                                    for (String employeeCode : batchEmployees) {
                                        earlyWarningManage.doEarlyWarning(noticeMethod, Objects.nonNull(noticeType) && noticeType.equals(1) ? noticeContent : adventContent,
                                                adventTime, time, biz,
                                                needToWaringProject.getUpdateOn(), Long.valueOf(employeeCode), needToWaringProject,
                                                WarningRuleTypeEnum.OPERATION_WARNING.getCode(), noticeType, path,
                                                batchEmployees.stream().collect(Collectors.joining(StrPool.COMMA)), null);
                                    }
                                }
                            }

                        }
                    });
                }
            }
        }

        stopWatch.stop();
        log.info("=========== 预警实施超时任务结束 耗时{}s", stopWatch.getTotalTimeSeconds());
    }

    /**
     * 续建项目资金 超时未审批定时器
     *
     * @throws UnknownHostException
     */
    @Scheduled(cron = "20 0/2 * * * ?")
    public void doEarlyWarningRenewalFund() throws UnknownHostException {
        if (!HOST_NAME.equals(InetAddress.getLocalHost().getHostName())) {
            return;
        }

        log.info("=========== 预警续建资金项目 超时任务开始 ========");
        StopWatch stopWatch = new StopWatch();
        stopWatch.start();

        // 1.查询 填报的 预警规则 填报类型的 每个区域 续建项目资金审批
        List<WflowEarlyWarning> warnings = earlyWarningService.list(Wrappers.lambdaQuery(WflowEarlyWarning.class)
                .eq(WflowEarlyWarning::getRuleType, WarningRuleTypeEnum.RENEWAL_FUND.getCode()));
        for (WflowEarlyWarning warning : warnings) {
            //2. 取出rule的数据
            if (!warning.getIsOpen()) {
                log.info(warning.getId() + " 此规则关闭了");
                continue;
            }

            String noticeMethod = warning.getNoticeMethod();
            String noticeContent = warning.getNoticeContent();
            String adventContent = warning.getAdventContent();
            if (StringUtils.isBlank(noticeMethod) ||
                    (!noticeMethod.contains(String.valueOf(CommonEnum.ZWDD.getCode())) &&
                            !noticeMethod.contains(String.valueOf(CommonEnum.MOBILE.getCode())))) {
                log.info("通知方式为空或者错误!");
                return;
            }

            String rule = warning.getRule();
            if (StringUtils.isNotBlank(rule)) {
                JSONArray ruleArray = JSON.parseArray(rule);
                if (CollUtil.isNotEmpty(ruleArray)) {
                    ruleArray.forEach(r -> {
                        JSONObject rJson = JSON.parseObject(JSON.toJSONString(r));
                        Integer time = rJson.getInteger("time");
                        Integer adventTime = rJson.getInteger("adventTime");
                        if ((Objects.isNull(time) && Objects.isNull(adventTime)) && Objects.isNull(adventTime)) {
                            log.info("规则数据 错误 :{}", rJson);
                            return;
                        }

                        //得出 对应待提交的续建项目区域信息
                        String areaCode = warning.getAreaCode();
                        //测试先用分钟
                        //查询 所有这个区域的项目 未提交的项目
                        List<ProjectRenewalFundDeclaration> needNextProjects = renewalFundDeclarationService.list(Wrappers.lambdaQuery(ProjectRenewalFundDeclaration.class)
                                .eq(ProjectRenewalFundDeclaration::getRegionCode, areaCode)
                                .eq(ProjectRenewalFundDeclaration::getDeleted, Boolean.FALSE)
                                .eq(ProjectRenewalFundDeclaration::getApprovalStatus, ProjectRenewalApprovalStatusEnum.PENDING.name()));

                        Map<String, List<Integer>> noticeTypeMap = Maps.newHashMap();
                        //需要发通知的项目
                        List<ProjectRenewalFundDeclaration> needToWaringProjects = needNextProjects.stream()
                                .filter(p -> {
                                    //超时未审批
                                    List<Integer> noticeTypes = Lists.newArrayList();
                                    if (Objects.nonNull(time) && Duration.between(p.getCreateOn(), LocalDateTime.now()).toMinutes() >= time * 60 && //time * 60
                                            Duration.between(p.getCreateOn(), LocalDateTime.now()).toMinutes() <= time * 60 + 1) { //time * 60
                                        noticeTypes.add(com.wflow.enums.WarningNoticeTypeEnum.OVER.getCode());
                                    }
                                    if (Objects.nonNull(adventTime) && Duration.between(p.getCreateOn(), LocalDateTime.now()).toMinutes() >= adventTime * 60 && //time * 60
                                            Duration.between(p.getCreateOn(), LocalDateTime.now()).toMinutes() <= adventTime * 60 + 1) { //time * 60
                                        noticeTypes.add(com.wflow.enums.WarningNoticeTypeEnum.ADVENT.getCode());
                                    }
                                    if (CollUtil.isNotEmpty(noticeTypes)) {
                                        noticeTypeMap.put(p.getProjectCode(), noticeTypes);
                                        //项目 运维预警
                                        projectEarlyWarningService.earlyWarning(p.getProjectCode(),
                                                WarningRuleTypeEnum.OPERATION_WARNING.getCode(), noticeTypes);
                                        return Boolean.TRUE;
                                    }
                                    return Boolean.FALSE;
                                })
                                .collect(Collectors.toList());

                        if (StringUtils.isBlank(noticeMethod) ||
                                (!noticeMethod.contains(String.valueOf(CommonEnum.ZWDD.getCode())) &&
                                        !noticeMethod.contains(String.valueOf(CommonEnum.MOBILE.getCode())))) {
                            log.info("通知方式为空或者错误!");
                            return;
                        }

                        for (ProjectRenewalFundDeclaration needToWaringProject : needToWaringProjects) {
                            String path = "projectStoreManage/renewalProjectAsscess?projectRenewalId=" + needToWaringProject.getId();
                            Project project = projectService.getProjectByCode(needToWaringProject.getProjectCode());
                            //去预警通知 发给区管
                            List<UserInfo> users = roleManage.getUsersByRoleType(RoleEnum.REGION_MANAGER.name());
                            String employeesStr = users.stream().map(UserInfo::getEmployeeCode)
                                    .collect(Collectors.joining(StrPool.COMMA));
                            for (UserInfo user : users) {
                                if (noticeTypeMap.containsKey(needToWaringProject.getProjectCode())) {
                                    List<Integer> noticeTypes = noticeTypeMap.get(needToWaringProject.getProjectCode());
                                    for (Integer noticeType : noticeTypes) {
                                        earlyWarningManage.doEarlyWarning(noticeMethod, Objects.nonNull(noticeType) && noticeType.equals(1) ? noticeContent : adventContent,
                                                adventTime, time, null,
                                                needToWaringProject.getUpdateOn(), Long.valueOf(user.getEmployeeCode()), project,
                                                WarningRuleTypeEnum.RENEWAL_FUND.getCode(), noticeType, path, employeesStr, needToWaringProject.getId().toString());
                                    }
                                }

                            }
                        }
                    });
                }
            }
        }

        stopWatch.stop();
        log.info("=========== 预警续建资金项目 超时任务结束 耗时{}s", stopWatch.getTotalTimeSeconds());
    }
}
