Browse Source

运维接口: 监控调阅接口代码提交

jingyuanchao 1 year ago
parent
commit
fb6373a318

+ 6 - 7
soc-common/soc-common-core/src/main/java/com/xunmei/common/core/domain/retrieval/domain/CoreMonitoringRetrievalTask.java

@@ -1,19 +1,18 @@
 package com.xunmei.common.core.domain.retrieval.domain;
 
-import java.util.Date;
-
+import com.baomidou.mybatisplus.annotation.TableName;
 import com.fasterxml.jackson.annotation.JsonFormat;
 import com.fasterxml.jackson.databind.annotation.JsonSerialize;
 import com.fasterxml.jackson.databind.ser.std.ToStringSerializer;
-import org.apache.commons.lang3.builder.ToStringBuilder;
-import org.apache.commons.lang3.builder.ToStringStyle;
-import com.baomidou.mybatisplus.annotation.TableName;
 import io.swagger.annotations.ApiModel;
 import io.swagger.annotations.ApiModelProperty;
 import lombok.Data;
 import lombok.EqualsAndHashCode;
 import lombok.experimental.Accessors;
-import com.xunmei.common.core.web.domain.BaseEntity;
+import org.apache.commons.lang3.builder.ToStringBuilder;
+import org.apache.commons.lang3.builder.ToStringStyle;
+
+import java.util.Date;
 
 /**
  * 监控调阅任务对象 core_monitoring_retrieval_task
@@ -26,7 +25,7 @@ import com.xunmei.common.core.web.domain.BaseEntity;
 @Accessors(chain = true)
 @TableName("core_monitoring_retrieval_task")
 @ApiModel(value = "CoreMonitoringRetrievalTask对象", description = "监控调阅任务")
-public class CoreMonitoringRetrievalTask extends BaseEntity {
+public class CoreMonitoringRetrievalTask {
     private static final long serialVersionUID = 1L;
 
     /**

+ 2 - 2
soc-modules/soc-modules-core/src/main/java/com/xunmei/core/access/service/impl/CoreMonitoringTaskRegistrationServiceImpl.java

@@ -143,8 +143,8 @@ private ITMonitoringRetrievalPlanService itMonitoringRetrievalPlanService;
         CoreMonitoringRetrievalTask coreMonitoringRetrievalTask = coreMonitoringRetrievalTaskMapper.selectCoreMonitoringRetrievalTaskById(coreMonitoringTaskRegistration.getTaskId());
         coreMonitoringRetrievalTask.setStatus(TWO);
         coreMonitoringRetrievalTask.setEndTime(DateUtils.getNowDate());
-        coreMonitoringRetrievalTask.setUpdateBy(SecurityUtils.getUsername());
-        coreMonitoringRetrievalTask.setUpdateTime(DateUtils.getNowDate());
+        /*coreMonitoringRetrievalTask.setUpdateBy(SecurityUtils.getUsername());
+        coreMonitoringRetrievalTask.setUpdateTime(DateUtils.getNowDate());*/
         coreMonitoringRetrievalTask.setSubmitTime(DateUtils.getNowDate());
         coreMonitoringRetrievalTask.setRetrievalUser(SecurityUtils.getUserId());
         coreMonitoringRetrievalTask.setSignImg(coreMonitoringTaskRegistration.getSignImgUrl());

+ 225 - 111
soc-modules/soc-modules-core/src/main/java/com/xunmei/core/access/service/impl/TMonitoringRetrievalPlanServiceImpl.java

@@ -4,20 +4,27 @@ package com.xunmei.core.access.service.impl;
 import cn.hutool.core.map.MapUtil;
 import cn.hutool.core.util.ObjectUtil;
 import com.alibaba.nacos.common.utils.CollectionUtils;
+import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
 import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
 import com.baomidou.mybatisplus.core.toolkit.IdWorker;
 import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
 import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
+import com.google.common.collect.Lists;
+import com.google.common.collect.Sets;
+import com.xunmei.common.core.constant.CacheConstants;
 import com.xunmei.common.core.constant.DictConstants;
 import com.xunmei.common.core.constant.ErrorMsgConstants;
 import com.xunmei.common.core.constant.SecurityConstants;
 import com.xunmei.common.core.domain.registerbook.domain.CoreRegisterBookPdf;
 import com.xunmei.common.core.domain.retrieval.domain.CoreMonitoringRetrievalTask;
 import com.xunmei.common.core.enums.OrgTypeEnum;
+import com.xunmei.common.core.enums.PlanStatus;
 import com.xunmei.common.core.enums.RegisterBookType;
+import com.xunmei.common.core.enums.drill.DrillPlanStatus;
 import com.xunmei.common.core.exception.ServiceException;
 import com.xunmei.common.core.exception.SystemException;
 import com.xunmei.common.core.thread.ThreadPoolConfig;
+import com.xunmei.common.core.util.BeanHelper;
 import com.xunmei.common.core.utils.DateUtils;
 import com.xunmei.common.core.utils.StringUtils;
 import com.xunmei.common.core.web.page.TableDataInfo;
@@ -43,6 +50,7 @@ import com.xunmei.system.api.*;
 import com.xunmei.system.api.domain.*;
 import com.xunmei.system.api.enums.BusinessPlanType;
 import com.xunmei.system.api.function.RemoteCallHandlerExecutor;
+import com.xunmei.system.api.vo.SysOrgVO;
 import io.netty.util.internal.StringUtil;
 import org.springframework.beans.BeanUtils;
 import org.springframework.beans.factory.annotation.Autowired;
@@ -59,13 +67,12 @@ import java.io.File;
 import java.text.ParseException;
 import java.text.SimpleDateFormat;
 import java.time.LocalDate;
-import java.util.ArrayList;
-import java.util.Calendar;
-import java.util.Date;
-import java.util.List;
+import java.util.*;
 import java.util.concurrent.CompletableFuture;
 import java.util.stream.Collectors;
 
+import static java.util.stream.Collectors.toList;
+
 
 /**
  * 监控调阅计划Service业务层处理
@@ -112,6 +119,7 @@ public class TMonitoringRetrievalPlanServiceImpl extends ServiceImpl<TMonitoring
     private ThreadPoolTaskExecutor threadPoolTaskExecutor;
     @Autowired
     private RemoteDictDataService dictDataService;
+
     @Override
     public TableDataInfo selectPage(TMonitoringRetrievalPlan tMonitoringRetrievalPlan) {
         //未删除
@@ -596,7 +604,7 @@ public class TMonitoringRetrievalPlanServiceImpl extends ServiceImpl<TMonitoring
 
 
         final CompletableFuture<List<TMonitoringRetrievalPlan>> planListFuture = CompletableFuture.supplyAsync(() -> {
-           return getNeedCreateTaskPlans(loginUserOrg, publishPlan);
+            return getNeedCreateTaskPlans(loginUserOrg, publishPlan);
         }, threadPoolTaskExecutor);
 
         planListFuture.thenAcceptAsync((result) -> {
@@ -613,8 +621,7 @@ public class TMonitoringRetrievalPlanServiceImpl extends ServiceImpl<TMonitoring
         return i;
     }
 
-    private List<TMonitoringRetrievalPlan> getNeedCreateTaskPlans(SysOrg loginUserOrg,TMonitoringRetrievalPlan publishPlan)
-    {
+    private List<TMonitoringRetrievalPlan> getNeedCreateTaskPlans(SysOrg loginUserOrg, TMonitoringRetrievalPlan publishPlan) {
         List<TMonitoringRetrievalPlan> tempPlans = new ArrayList<>();
         SysOrg publishPlanOrg = remoteOrgService.selectSysOrgById(publishPlan.getOrgId(), SecurityConstants.INNER);
         //如果登录当前是省联社 办事处
@@ -630,7 +637,7 @@ public class TMonitoringRetrievalPlanServiceImpl extends ServiceImpl<TMonitoring
                     tm.setPlanStatus(1);
                     tm.setIsDistribute("1");
                     baseMapper.updateById(tm);
-                    setPlanStartEndTime(publishPlan,tm);
+                    setPlanStartEndTime(publishPlan, tm);
                     tempPlans.add(tm);
 //                    if (publishPlan.getOrgType().equals("3")) {
 //                        setPlanStartEndTime(publishPlan,tm);
@@ -649,8 +656,7 @@ public class TMonitoringRetrievalPlanServiceImpl extends ServiceImpl<TMonitoring
 //                        }
 //                    }
                 }
-            }
-            else {
+            } else {
                 // 计划机构类型 是 省联社或者办事处及行社以下的机构类型 才创建子计划
                 if (!checkOrgType(Integer.parseInt(publishPlan.getOrgType()))) {
                     // 查询当前计划的具体执行机构
@@ -658,18 +664,17 @@ public class TMonitoringRetrievalPlanServiceImpl extends ServiceImpl<TMonitoring
 
                     List<Long> planExecOrgIds = planExecOrgList.stream().map(x -> x.getOrgId()).collect(Collectors.toList());
                     // 根据机构类型和具体调阅机构 获取需创建子计划的机构
-                    List<SysOrg> subPlanOrglist =getPlanOrgListByOrgTypeAndExecOrgs(planExecOrgIds,Integer.parseInt(publishPlan.getOrgType()), publishPlanOrg.getPath());
+                    List<SysOrg> subPlanOrglist = getPlanOrgListByOrgTypeAndExecOrgs(planExecOrgIds, Integer.parseInt(publishPlan.getOrgType()), publishPlanOrg.getPath());
 
-                    List<SysOrg> execOrglist=new ArrayList<>();
-                    if(ObjectUtil.isNotEmpty(planExecOrgList))
-                    {
-                        execOrglist= RemoteCallHandlerExecutor.executeRemoteCall(() ->
-                                        remoteOrgService.selectOrgByIdList(planExecOrgList.stream().map(x->x.getOrgId()).collect(Collectors.toList()), SecurityConstants.INNER),
+                    List<SysOrg> execOrglist = new ArrayList<>();
+                    if (ObjectUtil.isNotEmpty(planExecOrgList)) {
+                        execOrglist = RemoteCallHandlerExecutor.executeRemoteCall(() ->
+                                        remoteOrgService.selectOrgByIdList(planExecOrgList.stream().map(x -> x.getOrgId()).collect(Collectors.toList()), SecurityConstants.INNER),
                                 ErrorMsgConstants.QUERY_ORG_DATA_ERROR);
                     }
 
-                    List<TMonitoringRetrievalPlan> needAddSubPlans=new ArrayList<>();
-                    List<TMonitoringRetrievalPlanToExecOrg> needAddSubPlanExecOrgList=new ArrayList<>();
+                    List<TMonitoringRetrievalPlan> needAddSubPlans = new ArrayList<>();
+                    List<TMonitoringRetrievalPlanToExecOrg> needAddSubPlanExecOrgList = new ArrayList<>();
                     for (SysOrg subPlanOrg : subPlanOrglist) {
                         TMonitoringRetrievalPlan tMonitoringRetrievalPlan1 = new TMonitoringRetrievalPlan();
                         BeanUtils.copyProperties(publishPlan, tMonitoringRetrievalPlan1);
@@ -684,8 +689,8 @@ public class TMonitoringRetrievalPlanServiceImpl extends ServiceImpl<TMonitoring
                         tMonitoringRetrievalPlan1.setOrgName(publishPlanOrg.getShortName());
 //                        insertTMonitoringRetrievalPlan(tMonitoringRetrievalPlan1);
                         needAddSubPlans.add(tMonitoringRetrievalPlan1);
-                        if(ObjectUtil.isNotEmpty(planExecOrgList)){
-                            execOrglist.stream().filter(x -> x.getPath().contains(subPlanOrg.getPath())).forEach(x->{
+                        if (ObjectUtil.isNotEmpty(planExecOrgList)) {
+                            execOrglist.stream().filter(x -> x.getPath().contains(subPlanOrg.getPath())).forEach(x -> {
                                 TMonitoringRetrievalPlanToExecOrg tempExecOrg = new TMonitoringRetrievalPlanToExecOrg();
                                 tempExecOrg.setOrgId(x.getId());
                                 tempExecOrg.setPlanId(tMonitoringRetrievalPlan1.getId());
@@ -694,22 +699,20 @@ public class TMonitoringRetrievalPlanServiceImpl extends ServiceImpl<TMonitoring
                         }
                     }
                     //保存子计划和子计划具体执行机构
-                    if(ObjectUtil.isNotEmpty(needAddSubPlans))
-                    {
+                    if (ObjectUtil.isNotEmpty(needAddSubPlans)) {
                         this.saveBatch(needAddSubPlans);
                         itMonitoringRetrievalPlanToExecOrgService.saveBatch(needAddSubPlanExecOrgList);
-                        needAddSubPlans.stream().forEach(plan->{
-                            setPlanStartEndTime(plan,plan);
+                        needAddSubPlans.stream().forEach(plan -> {
+                            setPlanStartEndTime(plan, plan);
                         });
                         tempPlans.addAll(needAddSubPlans);
                     }
-                }
-                else{
+                } else {
                     publishPlan.setCreateType(1);
                     publishPlan.setUpdateBy(SecurityUtils.getUsername());
                     publishPlan.setUpdateTime(DateUtils.getNowDate());
                     baseMapper.updateById(publishPlan);
-                    setPlanStartEndTime(publishPlan,publishPlan);
+                    setPlanStartEndTime(publishPlan, publishPlan);
                     tempPlans.add(publishPlan);
                 }
 //                if (publishPlan.getOrgType().equals("3")) {
@@ -733,12 +736,11 @@ public class TMonitoringRetrievalPlanServiceImpl extends ServiceImpl<TMonitoring
 //                    }
 //                }
             }
-        }
-        else {
+        } else {
             publishPlan.setCreateType(1);
             publishPlan.setIsDistribute("1");
             baseMapper.updateById(publishPlan);
-            setPlanStartEndTime(publishPlan,publishPlan);
+            setPlanStartEndTime(publishPlan, publishPlan);
             tempPlans.add(publishPlan);
 //            SysOrg sysOrg = new SysOrg();
 //            sysOrg.setPath(loginUserOrg.getPath());
@@ -752,22 +754,20 @@ public class TMonitoringRetrievalPlanServiceImpl extends ServiceImpl<TMonitoring
         return tempPlans;
     }
 
-    private List<TMonitoringRetrievalPlan> buildSubPlans(TMonitoringRetrievalPlan parentPlan,List<Long> parentPlanExecOrgIds, SysOrg parentPlanOrg)
-    {
+    private List<TMonitoringRetrievalPlan> buildSubPlans(TMonitoringRetrievalPlan parentPlan, List<Long> parentPlanExecOrgIds, SysOrg parentPlanOrg) {
         List<TMonitoringRetrievalPlan> subPlans = new ArrayList<>();
         // 根据机构类型和具体调阅机构 获取需创建子计划的机构
-        List<SysOrg> hsOrglist =getPlanOrgListByOrgTypeAndExecOrgs(parentPlanExecOrgIds,3,parentPlanOrg.getPath());
+        List<SysOrg> hsOrglist = getPlanOrgListByOrgTypeAndExecOrgs(parentPlanExecOrgIds, 3, parentPlanOrg.getPath());
 
-        List<SysOrg> execOrglist=new ArrayList<>();
-        if(ObjectUtil.isNotEmpty(parentPlanExecOrgIds))
-        {
-            execOrglist= RemoteCallHandlerExecutor.executeRemoteCall(() ->
+        List<SysOrg> execOrglist = new ArrayList<>();
+        if (ObjectUtil.isNotEmpty(parentPlanExecOrgIds)) {
+            execOrglist = RemoteCallHandlerExecutor.executeRemoteCall(() ->
                             remoteOrgService.selectOrgByIdList(parentPlanExecOrgIds, SecurityConstants.INNER),
                     ErrorMsgConstants.QUERY_ORG_DATA_ERROR);
         }
 
-        List<TMonitoringRetrievalPlan> needAddSubPlans=new ArrayList<>();
-        List<TMonitoringRetrievalPlanToExecOrg> needAddSubPlanExecOrgList=new ArrayList<>();
+        List<TMonitoringRetrievalPlan> needAddSubPlans = new ArrayList<>();
+        List<TMonitoringRetrievalPlanToExecOrg> needAddSubPlanExecOrgList = new ArrayList<>();
         for (SysOrg l : hsOrglist) {
             TMonitoringRetrievalPlan tMonitoringRetrievalPlan1 = new TMonitoringRetrievalPlan();
             BeanUtils.copyProperties(parentPlan, tMonitoringRetrievalPlan1);
@@ -783,8 +783,8 @@ public class TMonitoringRetrievalPlanServiceImpl extends ServiceImpl<TMonitoring
                 tMonitoringRetrievalPlan1.setOrgName(parentPlanOrg.getShortName());
 //                        insertTMonitoringRetrievalPlan(tMonitoringRetrievalPlan1);
                 needAddSubPlans.add(tMonitoringRetrievalPlan1);
-                if(ObjectUtil.isNotEmpty(parentPlanExecOrgIds)){
-                    execOrglist.stream().filter(x -> x.getPath().contains(l.getPath())).forEach(x->{
+                if (ObjectUtil.isNotEmpty(parentPlanExecOrgIds)) {
+                    execOrglist.stream().filter(x -> x.getPath().contains(l.getPath())).forEach(x -> {
                         TMonitoringRetrievalPlanToExecOrg tempExecOrg = new TMonitoringRetrievalPlanToExecOrg();
                         tempExecOrg.setOrgId(x.getId());
                         tempExecOrg.setPlanId(tMonitoringRetrievalPlan1.getId());
@@ -798,34 +798,31 @@ public class TMonitoringRetrievalPlanServiceImpl extends ServiceImpl<TMonitoring
                 parentPlan.setUpdateTime(DateUtils.getNowDate());
                 baseMapper.updateById(parentPlan);
 
-                setPlanStartEndTime(parentPlan,tMonitoringRetrievalPlan1);
+                setPlanStartEndTime(parentPlan, tMonitoringRetrievalPlan1);
                 subPlans.add(tMonitoringRetrievalPlan1);
             } else {
-                List<SysOrg> sysOrgs=getExecOrgList(parentPlanExecOrgIds,Integer.valueOf(parentPlan.getOrgType()),l.getPath());
+                List<SysOrg> sysOrgs = getExecOrgList(parentPlanExecOrgIds, Integer.valueOf(parentPlan.getOrgType()), l.getPath());
                 for (SysOrg s : sysOrgs) {
-                    TMonitoringRetrievalPlan plan = createTempPlan(parentPlan,tMonitoringRetrievalPlan1.getId(),s);
+                    TMonitoringRetrievalPlan plan = createTempPlan(parentPlan, tMonitoringRetrievalPlan1.getId(), s);
                     subPlans.add(plan);
                 }
             }
         }
-        return  subPlans;
+        return subPlans;
     }
 
-    private List<TMonitoringRetrievalPlanToExecOrg> buildPlanExecOrgs(List<TMonitoringRetrievalPlan> plans , List<SysOrg> execOrglist)
-    {
-        if(ObjectUtil.isEmpty(plans))
-        {
+    private List<TMonitoringRetrievalPlanToExecOrg> buildPlanExecOrgs(List<TMonitoringRetrievalPlan> plans, List<SysOrg> execOrglist) {
+        if (ObjectUtil.isEmpty(plans)) {
             return new ArrayList<>();
         }
 
-        if(ObjectUtil.isEmpty(execOrglist))
-        {
+        if (ObjectUtil.isEmpty(execOrglist)) {
             return new ArrayList<>();
         }
-        List<TMonitoringRetrievalPlanToExecOrg> result=new ArrayList<>();
+        List<TMonitoringRetrievalPlanToExecOrg> result = new ArrayList<>();
 
-        plans.stream().forEach(x->{
-            execOrglist.stream().filter(eOrg -> eOrg.getPath().contains(x.getOrgPath())).forEach(y->{
+        plans.stream().forEach(x -> {
+            execOrglist.stream().filter(eOrg -> eOrg.getPath().contains(x.getOrgPath())).forEach(y -> {
                 TMonitoringRetrievalPlanToExecOrg tempExecOrg = new TMonitoringRetrievalPlanToExecOrg();
                 tempExecOrg.setOrgId(y.getId());
                 tempExecOrg.setPlanId(x.getId());
@@ -848,18 +845,17 @@ public class TMonitoringRetrievalPlanServiceImpl extends ServiceImpl<TMonitoring
     }
 
     /**
-     *  根据机构类型和具体调阅机构 获取需创建计划的行社机构
+     * 根据机构类型和具体调阅机构 获取需创建计划的行社机构
+     *
      * @param execOrgIds
      * @param orgType
      * @param belongOrgPath
      * @return
      */
-    public List<SysOrg> getPlanOrgListByOrgTypeAndExecOrgs(List<Long> execOrgIds,Integer orgType, String belongOrgPath) {
-        if(ObjectUtil.isNotEmpty(execOrgIds))
-        {
+    public List<SysOrg> getPlanOrgListByOrgTypeAndExecOrgs(List<Long> execOrgIds, Integer orgType, String belongOrgPath) {
+        if (ObjectUtil.isNotEmpty(execOrgIds)) {
             //省联社/办事处/行社
-            if(checkOrgType(orgType))
-            {
+            if (checkOrgType(orgType)) {
                 return RemoteCallHandlerExecutor.executeRemoteCall(() ->
                                 remoteOrgService.selectOrgByIdList(execOrgIds, SecurityConstants.INNER),
                         ErrorMsgConstants.QUERY_ORG_DATA_ERROR);
@@ -868,19 +864,15 @@ public class TMonitoringRetrievalPlanServiceImpl extends ServiceImpl<TMonitoring
             else {
                 return RemoteCallHandlerExecutor.executeRemoteCall(() -> remoteOrgService.selectParentHs(execOrgIds, SecurityConstants.INNER), ErrorMsgConstants.QUERY_ORG_DATA_ERROR);
             }
-        }
-        else {
+        } else {
             //省联社/办事处/行社
-            if(checkOrgType(orgType))
-            {
+            if (checkOrgType(orgType)) {
                 SysOrg org = new SysOrg();
                 org.setType(orgType);
                 org.setPath(belongOrgPath);
                 //查询符合的机构,需求是先把行社加入
                 return remoteOrgService.listByParentIdAndType(org, SecurityConstants.INNER);
-            }
-            else
-            {
+            } else {
                 SysOrg org = new SysOrg();
                 org.setType(3);
                 org.setPath(belongOrgPath);
@@ -901,8 +893,7 @@ public class TMonitoringRetrievalPlanServiceImpl extends ServiceImpl<TMonitoring
     }
 
     // 准备生成任务需要的临时计划
-    private TMonitoringRetrievalPlan createTempPlan(TMonitoringRetrievalPlan tMonitoringRetrievalPlan,Long planId,SysOrg org)
-    {
+    private TMonitoringRetrievalPlan createTempPlan(TMonitoringRetrievalPlan tMonitoringRetrievalPlan, Long planId, SysOrg org) {
         TMonitoringRetrievalPlan plan = new TMonitoringRetrievalPlan();
         BeanUtils.copyProperties(tMonitoringRetrievalPlan, plan);
         plan.setPlanName(tMonitoringRetrievalPlan.getPlanName());
@@ -959,12 +950,11 @@ public class TMonitoringRetrievalPlanServiceImpl extends ServiceImpl<TMonitoring
 //            Date time = cal.getTime();
 //            plan.setEndTime(time);
 //        }
-        setPlanStartEndTime(tMonitoringRetrievalPlan,plan);
+        setPlanStartEndTime(tMonitoringRetrievalPlan, plan);
         return plan;
     }
 
-    private void setPlanStartEndTime(TMonitoringRetrievalPlan tMonitoringRetrievalPlan,TMonitoringRetrievalPlan tempPlan)
-    {
+    private void setPlanStartEndTime(TMonitoringRetrievalPlan tMonitoringRetrievalPlan, TMonitoringRetrievalPlan tempPlan) {
         if (!tMonitoringRetrievalPlan.getPlanCycle().equals(ZERO.toString()) &&
                 tMonitoringRetrievalPlan.getImmediately().equals(Boolean.TRUE)) {
             tempPlan.setStartTime(new Date());
@@ -1001,19 +991,18 @@ public class TMonitoringRetrievalPlanServiceImpl extends ServiceImpl<TMonitoring
         }
     }
 
-    private void checkPlanIsCreatingTask(Long planId,String msg)
-    {
-        String redisKey="Monitoring_Creating_Task" + planId;
+    private void checkPlanIsCreatingTask(Long planId, String msg) {
+        String redisKey = "Monitoring_Creating_Task" + planId;
         //仅当hashKey不存在时才设置
         Boolean hasKey = RedisUtils.hasKey(redisKey);
         if (hasKey) {
-            throw new ServiceException("该任务正在生成中,现在不能被"+msg+",请稍后再试。");
+            throw new ServiceException("该任务正在生成中,现在不能被" + msg + ",请稍后再试。");
         }
     }
 
     @Override
     public int withdraw(Long id) {
-        checkPlanIsCreatingTask(id,"撤回");
+        checkPlanIsCreatingTask(id, "撤回");
         TMonitoringRetrievalPlan plan = baseMapper.selectById(id);
         QueryWrapper queryWrapper = new QueryWrapper();
         queryWrapper.eq(PARENT_ID, id);
@@ -1225,16 +1214,17 @@ public class TMonitoringRetrievalPlanServiceImpl extends ServiceImpl<TMonitoring
         if (ObjectUtil.equal(tMonitoringRetrievalPlanDto.getPlanCycle(), 0) && ObjectUtil.hasEmpty(tMonitoringRetrievalPlanDto.getStartTime(), tMonitoringRetrievalPlanDto.getEndTime())) {
             throw new RuntimeException("当计划为无周期的时候,请填入开始时间和结束时间!");
         }
-        TMonitoringRetrievalPlan tMonitoringRetrievalPlan =new TMonitoringRetrievalPlan();
+        TMonitoringRetrievalPlan tMonitoringRetrievalPlan = new TMonitoringRetrievalPlan();
         BeanUtils.copyProperties(tMonitoringRetrievalPlanDto, tMonitoringRetrievalPlan);
-        int result= insertTMonitoringRetrievalPlan(tMonitoringRetrievalPlan);
+        int result = insertTMonitoringRetrievalPlan(tMonitoringRetrievalPlan);
         batchSavePlanToExecOrg(tMonitoringRetrievalPlanDto.getPlanExecOrgIdList(), tMonitoringRetrievalPlan.getId());
         return result;
     }
 
     public void batchSavePlanToExecOrg(List<Long> orgIdList, Long planId) {
-        if(ObjectUtil.isEmpty(orgIdList))
-        { return;}
+        if (ObjectUtil.isEmpty(orgIdList)) {
+            return;
+        }
         List<TMonitoringRetrievalPlanToExecOrg> planOrgList = new ArrayList();
         for (Long orgId : orgIdList) {
             TMonitoringRetrievalPlanToExecOrg org = new TMonitoringRetrievalPlanToExecOrg();
@@ -1363,34 +1353,31 @@ public class TMonitoringRetrievalPlanServiceImpl extends ServiceImpl<TMonitoring
     @Transactional
     @Override
     public int updateTMonitoringRetrievalPlan(TMonitoringRetrievalPlanDto tMonitoringRetrievalPlanDto) throws ParseException {
-        Integer result=1;
+        Integer result = 1;
         try {
-            TMonitoringRetrievalPlan newPlan =new TMonitoringRetrievalPlan();
+            TMonitoringRetrievalPlan newPlan = new TMonitoringRetrievalPlan();
             BeanUtils.copyProperties(tMonitoringRetrievalPlanDto, newPlan);
 
             boolean isComplete = getIsComplete(newPlan.getId());
             TMonitoringRetrievalPlan oldPlan = baseMapper.selectById(newPlan.getId());
 
-            if(!ObjectUtil.equal(newPlan.getRoleId(),oldPlan.getRoleId()))
-            {
+            if (!ObjectUtil.equal(newPlan.getRoleId(), oldPlan.getRoleId())) {
                 SysRole sysRole = remoteRoleService.getRoleById(newPlan.getRoleId(), SecurityConstants.INNER);
                 newPlan.setRoleName(sysRole.getRoleName());
             }
             BeanUtils.copyProperties(newPlan, oldPlan);
 
             // 草稿状态 下的计划不存在任务和子计划  直接修改
-            if(oldPlan.getPlanStatus()==0)
-            {
+            if (oldPlan.getPlanStatus() == 0) {
                 // 先删除具体调阅机构,再重新新增具体调阅机构
                 tMonitoringRetrievalPlanToExecOrgMapper.deleteByMap(MapUtil.of("plan_id", oldPlan.getId()));
                 batchSavePlanToExecOrg(tMonitoringRetrievalPlanDto.getPlanExecOrgIdList(), oldPlan.getId());
                 return tMonitoringRetrievalPlanMapper.updateById(oldPlan);
             }
             // 使用中,
-            else if(oldPlan.getPlanStatus()==1)
-            {
+            else if (oldPlan.getPlanStatus() == 1) {
                 // 但是任务都未完成,修改计划和所有子计划, 删除现有任务,根据是否立即生效 判断是否立即生成任务
-                if(!isComplete){
+                if (!isComplete) {
 
                     tMonitoringRetrievalPlanToExecOrgMapper.deleteByMap(MapUtil.of("plan_id", oldPlan.getId()));
                     batchSavePlanToExecOrg(tMonitoringRetrievalPlanDto.getPlanExecOrgIdList(), oldPlan.getId());
@@ -1401,42 +1388,36 @@ public class TMonitoringRetrievalPlanServiceImpl extends ServiceImpl<TMonitoring
                     tMonitoringRetrievalPlanMapper.delete(queryWrapper);
 
                     // 编辑的非行社创建计划,需处理子计划
-                    if(oldPlan.getOrgType()!="3")
-                    {
+                    if (oldPlan.getOrgType() != "3") {
                         iCoreMonitoringRetrievalTaskService.updateComplete(!isComplete, newPlan, oldPlan);
                     }
                     return result;
-                }
-                else // 任务中存在已完成的任务,只能修改任务名称
+                } else // 任务中存在已完成的任务,只能修改任务名称
                 {
                     tMonitoringRetrievalPlanMapper.updateById(oldPlan);
                     QueryWrapper queryWrapper = new QueryWrapper();
                     queryWrapper.eq(PARENT_ID, oldPlan.getId());
                     List<TMonitoringRetrievalPlan> subPlanList = tMonitoringRetrievalPlanMapper.selectList(queryWrapper);
-                    if(ObjectUtil.isNotEmpty(subPlanList))
-                    {
-                        subPlanList.forEach(x->x.setPlanName(oldPlan.getPlanName()));
+                    if (ObjectUtil.isNotEmpty(subPlanList)) {
+                        subPlanList.forEach(x -> x.setPlanName(oldPlan.getPlanName()));
                         this.saveOrUpdateBatch(subPlanList);
                     }
                     return result;
                 }
             }
             // 停用状态 肯定存在已完成的任务, 只能修改任务名称
-            else if(oldPlan.getPlanStatus()==2)
-            {
+            else if (oldPlan.getPlanStatus() == 2) {
                 tMonitoringRetrievalPlanMapper.updateById(oldPlan);
                 QueryWrapper queryWrapper = new QueryWrapper();
                 queryWrapper.eq(PARENT_ID, oldPlan.getId());
                 List<TMonitoringRetrievalPlan> subPlanList = tMonitoringRetrievalPlanMapper.selectList(queryWrapper);
-                if(ObjectUtil.isNotEmpty(subPlanList))
-                {
-                    subPlanList.forEach(x->x.setPlanName(oldPlan.getPlanName()));
+                if (ObjectUtil.isNotEmpty(subPlanList)) {
+                    subPlanList.forEach(x -> x.setPlanName(oldPlan.getPlanName()));
                     this.saveOrUpdateBatch(subPlanList);
                 }
                 return result;
             }
-        }
-        catch (Exception e) {
+        } catch (Exception e) {
             e.getMessage();
             return 0;
         }
@@ -1444,7 +1425,7 @@ public class TMonitoringRetrievalPlanServiceImpl extends ServiceImpl<TMonitoring
     }
 
     // 根据计划Id清除计划下的所有任务
-    private void clearTaskByPlanId(Long id){
+    private void clearTaskByPlanId(Long id) {
         TMonitoringRetrievalPlan plan = baseMapper.selectById(id);
         QueryWrapper queryWrapper = new QueryWrapper();
         queryWrapper.eq(PARENT_ID, id);
@@ -1454,8 +1435,7 @@ public class TMonitoringRetrievalPlanServiceImpl extends ServiceImpl<TMonitoring
         if (CollectionUtils.isNotEmpty(list1)) {
             planIds = list1.stream().map(TMonitoringRetrievalPlan::getId).collect(Collectors.toList());
             coreMonitoringRetrievalTaskBuilderService.deleteAllTaskByPlanIds(planIds);
-        }
-        else if (plan.getPlanCycle().equals("0")) {
+        } else if (plan.getPlanCycle().equals("0")) {
             planIds.add(id);
             coreMonitoringRetrievalTaskBuilderService.deleteAllTaskByPlanIds(planIds);
         }
@@ -1476,7 +1456,7 @@ public class TMonitoringRetrievalPlanServiceImpl extends ServiceImpl<TMonitoring
     public int deleteTMonitoringRetrievalPlanByIds(Long[] ids) {
         int i = 0;
         for (Long id : ids) {
-            checkPlanIsCreatingTask(id,"删除");
+            checkPlanIsCreatingTask(id, "删除");
 
             TMonitoringRetrievalPlan tMonitoringRetrievalPlan = new TMonitoringRetrievalPlan();
             tMonitoringRetrievalPlan.setIsDeleted(2L);
@@ -1602,8 +1582,8 @@ public class TMonitoringRetrievalPlanServiceImpl extends ServiceImpl<TMonitoring
         List<SysDictData> sysDictData = dictDataService.selectAllDictByeTypeFormDB(DictConstants.CORE_REGISTRATION_PROJECT, SecurityConstants.INNER);
         for (AccessDataVo vo : accessDataVos) {
             vo.setProject(DictUtils.getDictLabel(DictConstants.CORE_REGISTRATION_PROJECT, Integer.parseInt(vo.getProject())));
-            if (ObjectUtil.isEmpty(vo.getProject())){
-                sysDictData.stream().filter(d->d.getDictValue().equals(vo.getProject())).findFirst().ifPresent(d->vo.setProject(d.getDictLabel()));
+            if (ObjectUtil.isEmpty(vo.getProject())) {
+                sysDictData.stream().filter(d -> d.getDictValue().equals(vo.getProject())).findFirst().ifPresent(d -> vo.setProject(d.getDictLabel()));
             }
         }
         accessPdf.setDataVos(accessDataVos);
@@ -1634,11 +1614,145 @@ public class TMonitoringRetrievalPlanServiceImpl extends ServiceImpl<TMonitoring
 
     @Override
     public void compensate(CompensateDto compensateDto) {
+        TMonitoringRetrievalPlan retrievalPlan = getById(compensateDto.getPlanId());
+
+        if (ObjectUtil.isNull(retrievalPlan)) {
+            throw new ServiceException("计划不存在");
+        }
+
+        if (ObjectUtil.notEqual(retrievalPlan.getPlanStatus(), PlanStatus.USING.getCode())) {
+            throw new ServiceException("计划尚未下发或已停用");
+        }
+
+        if (ObjectUtil.equal(retrievalPlan.getIsDeleted(), "1")) {
+            throw new ServiceException("计划状态异常");
+        }
+
+        List<SysOrgVO> orgCacheList = RedisUtils.getCacheList(CacheConstants.ORG_CACHE_LIST_KEY);
+        //推断计划 创建任务
+        deducePlanAndBuildTask(retrievalPlan, compensateDto.getOrgIdList(), orgCacheList);
+    }
+
+    private static final Set<OrgTypeEnum> INVALID_ORG_TYPES = Sets.newHashSet(OrgTypeEnum.SHEGN_LIAN_SHE, OrgTypeEnum.BAN_SHI_CHU, OrgTypeEnum.DIQU_HANG_SHE);
 
+    private void deducePlanAndBuildTask(TMonitoringRetrievalPlan retrievalPlan, List<Long> orgIdList, List<SysOrgVO> orgCacheList) {
+        List<SysOrgVO> orgList = orgCacheList.stream().filter(org -> orgIdList.contains(org.getId())).collect(toList());
+        if (orgList.isEmpty()) {
+            throw new ServiceException("未查询到orgIdList中指定的机构信息");
+        }
+        List<Long> idList = orgList.stream().map(SysOrgVO::getId).collect(toList());
+        List<Integer> typeList = orgList.stream().map(SysOrgVO::getType).distinct().collect(toList());
+        if (typeList.size() > 1) {
+            throw new ServiceException("orgIdList中指定的机构信息存在多种机构类型");
+        }
+
+        OrgTypeEnum orgTypeEnum = OrgTypeEnum.getOrgTypeEnum(typeList.get(0));
+
+        // 省联社创建的计划
+        if (checkIsTopOrgCreatePlan(retrievalPlan, orgCacheList)) {
+            List<TMonitoringRetrievalPlan> allHsPlanList = getHsPlanList(retrievalPlan);
+            if (ObjectUtil.isEmpty(allHsPlanList)) {
+                return;
+            }
+
+            Map<String, Object> map = getHsOrg(orgCacheList, orgList, orgTypeEnum);
+            List<Long> hsPlanIdList = allHsPlanList.stream().map(TMonitoringRetrievalPlan::getId).collect(toList());
+            List<Long> hsOrgIdList = ((List<SysOrgVO>) map.get("list")).stream().map(SysOrgVO::getId).collect(toList());
+            if (ObjectUtil.hasEmpty(hsPlanIdList, hsOrgIdList)) {
+                return;
+            }
+            List<TMonitoringRetrievalPlan> reallyPlanList = findReallyPlanIdList(hsPlanIdList, hsOrgIdList);
+            boolean underHsOrgType = (boolean) map.get("underHsOrgType");
+            create(reallyPlanList, underHsOrgType ? idList : null);
+            return;
+        }
+        // 行社自建计划
+        if (INVALID_ORG_TYPES.contains(orgTypeEnum)) {
+            throw new ServiceException("orgIdList中传递机构信息错误!与计划信息不匹配");
+        }
+
+        List<TMonitoringRetrievalPlan> plans = Lists.newArrayList(retrievalPlan);
+        if (OrgTypeEnum.HANG_SHE.equals(orgTypeEnum)) {
+            create(plans, null);
+        } else {
+            //其实这里如果真是传入了行社以下的机构,那么大概率这里的reallyPlanIdList 计划id也只会有一个元素
+            create(plans, idList);
+        }
+    }
+
+    private void create(List<TMonitoringRetrievalPlan> planList, List<Long> idList) {
+        run(() -> coreMonitoringRetrievalTaskBuilderService.createAndSaveTask(planList, LocalDate.now(), idList), threadPoolTaskExecutor);
+    }
+
+    private List<TMonitoringRetrievalPlan> findReallyPlanIdList(List<Long> planIdList, List<Long> orgIdList) {
+
+        LambdaQueryWrapper<TMonitoringRetrievalPlan> wrapper = new LambdaQueryWrapper<>();
+        wrapper.in(TMonitoringRetrievalPlan::getId, planIdList);
+        wrapper.in(TMonitoringRetrievalPlan::getOrgId, orgIdList);
+        return baseMapper.selectList(wrapper);
+    }
+
+    private boolean checkIsTopOrgCreatePlan(TMonitoringRetrievalPlan retrievalPlan, List<SysOrgVO> orgCacheList) {
+        Long orgId = retrievalPlan.getOrgId();
+        SysOrgVO org = orgCacheList.stream().filter(o -> o.getId().equals(orgId)).findFirst().orElse(null);
+        if (org == null) {
+            throw new ServiceException("未查询到计划所属机构信息");
+        }
+        if (ObjectUtil.equal(org.getType(), OrgTypeEnum.SHEGN_LIAN_SHE.getCode())) {
+            return true;
+        }
+        if (ObjectUtil.equal(org.getType(), OrgTypeEnum.HANG_SHE.getCode())) {
+            return false;
+        }
+        throw new ServiceException("当前仅支持省联社/行社所创建的计划!");
+    }
+
+
+    private List<TMonitoringRetrievalPlan> getHsPlanList(TMonitoringRetrievalPlan drillPlan) {
+        LambdaQueryWrapper<TMonitoringRetrievalPlan> wrapper = new LambdaQueryWrapper<>();
+        wrapper.eq(TMonitoringRetrievalPlan::getPlanStatus, DrillPlanStatus.USING.getCode());
+        wrapper.eq(TMonitoringRetrievalPlan::getIsDeleted, 0L);
+        wrapper.eq(TMonitoringRetrievalPlan::getParentId, drillPlan.getId());
+        return baseMapper.selectList(wrapper);
+    }
+
+    private Map<String, Object> getHsOrg(List<SysOrgVO> orgCacheList, List<SysOrgVO> needBuildTaskOrgList, OrgTypeEnum needBuildTaskOrgTypeEnum) {
+        List<SysOrgVO> list = new ArrayList<>();
+        boolean underHsOrgType = false;
+        switch (needBuildTaskOrgTypeEnum) {
+            //如果是省联社/办事处/地区行社 那么找到下级的行社
+            case SHEGN_LIAN_SHE:
+            case BAN_SHI_CHU:
+            case DIQU_HANG_SHE:
+                for (SysOrgVO sysOrgVO : needBuildTaskOrgList) {
+                    String path = sysOrgVO.getPath();
+                    List<SysOrgVO> collect = orgCacheList
+                            .stream()
+                            .filter(org -> ObjectUtil.isAllNotEmpty(org.getPath(), org.getType()))
+                            .filter(org -> org.getPath().contains(path))
+                            .filter(org -> org.getType().equals(OrgTypeEnum.HANG_SHE.getCode()))
+                            .collect(toList());
+                    list.addAll(collect);
+                }
+                break;
+            case HANG_SHE: //如果传进来的就是行社 那么直接返回
+                list = needBuildTaskOrgList;
+                break;
+            default:    //如果是行社以下机构类型,那么找到上级的行社机构返回
+                List<Long> ids = needBuildTaskOrgList.stream().map(SysOrgVO::getId).collect(toList());
+                List<SysOrg> sysOrgs = remoteOrgService.selectParentHs(ids, SecurityConstants.INNER);
+                list = BeanHelper.copyProperties(sysOrgs, SysOrgVO.class);
+                underHsOrgType = true;
+        }
+
+        Map<String, Object> map = new HashMap<>();
+        map.put("list", list);
+        map.put("underHsOrgType", underHsOrgType);
+        return map;
     }
 
     @Override
     public void run(Runnable runnable, ThreadPoolTaskExecutor threadPoolTaskExecutor) {
-
+        CompletableFuture.runAsync(runnable, threadPoolTaskExecutor);
     }
 }

+ 2 - 9
soc-modules/soc-modules-core/src/main/java/com/xunmei/core/retrieval/service/ICoreMonitoringRetrievalTaskBuilderService.java

@@ -1,20 +1,11 @@
 package com.xunmei.core.retrieval.service;
 
-import cn.hutool.core.date.DateTime;
 import com.baomidou.mybatisplus.extension.service.IService;
-import com.xunmei.common.core.domain.panel.dto.PanelListDto;
-import com.xunmei.common.core.domain.panel.vo.PanelListVo;
 import com.xunmei.common.core.domain.retrieval.domain.CoreMonitoringRetrievalTask;
-import com.xunmei.common.core.domain.retrieval.dto.RetrievalTaskPageDto;
 import com.xunmei.common.core.domain.retrieval.vo.CoreMonitoringRetrievalTaskOneVo;
-import com.xunmei.common.core.domain.safetyindex.dto.SafeScoreSourceDto;
 import com.xunmei.common.core.event.WorkTimeChangeEvent;
-import com.xunmei.common.core.web.page.TableDataInfo;
 import com.xunmei.core.access.domain.TMonitoringRetrievalPlan;
-import com.xunmei.core.access.dto.CoreMonitoringTaskRegistrationDTO;
 
-import javax.servlet.http.HttpServletResponse;
-import java.io.IOException;
 import java.time.LocalDate;
 import java.util.Date;
 import java.util.List;
@@ -169,4 +160,6 @@ public interface ICoreMonitoringRetrievalTaskBuilderService extends IService<Cor
 
 
     void taskChangeForTimeWorkChange(WorkTimeChangeEvent event);
+
+    void createAndSaveTask(List<TMonitoringRetrievalPlan> planList,LocalDate taskTime,List<Long> needBuildTaskOrgIdList);
 }

+ 57 - 33
soc-modules/soc-modules-core/src/main/java/com/xunmei/core/retrieval/service/impl/CoreMonitoringRetrievalTaskBuilderServiceImpl.java

@@ -6,6 +6,7 @@ import cn.hutool.core.date.DateTime;
 import cn.hutool.core.date.DateUtil;
 import cn.hutool.core.map.MapUtil;
 import cn.hutool.core.util.ObjectUtil;
+import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
 import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
 import com.baomidou.mybatisplus.core.toolkit.IdWorker;
 import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
@@ -14,10 +15,11 @@ import com.xunmei.common.core.constant.ErrorMsgConstants;
 import com.xunmei.common.core.constant.SecurityConstants;
 import com.xunmei.common.core.domain.DateRange;
 import com.xunmei.common.core.domain.retrieval.domain.CoreMonitoringRetrievalTask;
-import com.xunmei.common.core.domain.retrieval.vo.*;
+import com.xunmei.common.core.domain.retrieval.vo.CoreMonitoringRetrievalTaskOneVo;
+import com.xunmei.common.core.domain.retrieval.vo.KeyValueVo;
+import com.xunmei.common.core.domain.retrieval.vo.TaskDataVo;
 import com.xunmei.common.core.domain.worktime.domain.SysWorkTime;
 import com.xunmei.common.core.domain.worktime.dto.WorkTimeDto;
-import com.xunmei.common.core.enums.OrgTypeEnum;
 import com.xunmei.common.core.enums.RetrievalTaskCycle;
 import com.xunmei.common.core.enums.edu.EduTrainingPlanCycleEnum;
 import com.xunmei.common.core.event.WorkTimeChangeEvent;
@@ -27,7 +29,6 @@ import com.xunmei.common.redis.utils.RedisUtils;
 import com.xunmei.common.security.utils.SecurityUtils;
 import com.xunmei.core.TaskCreatingServiceImplBase;
 import com.xunmei.core.access.domain.TMonitoringRetrievalPlan;
-import com.xunmei.core.access.domain.TMonitoringRetrievalPlanToExecOrg;
 import com.xunmei.core.access.mapper.CoreMonitoringTaskRegistrationMapper;
 import com.xunmei.core.access.mapper.TMonitoringRetrievalPlanToExecOrgMapper;
 import com.xunmei.core.access.service.ITMonitoringRetrievalPlanService;
@@ -84,7 +85,7 @@ public class CoreMonitoringRetrievalTaskBuilderServiceImpl extends ServiceImpl<C
      */
     @Override
     public void noCycleTask(List<TMonitoringRetrievalPlan> planList) {
-        createAndSaveTask(planList);
+        createAndSaveTask(planList, null, new ArrayList<>());
     }
 
     @Async
@@ -92,15 +93,15 @@ public class CoreMonitoringRetrievalTaskBuilderServiceImpl extends ServiceImpl<C
      * 无周期任务生成
      */
     @Override
-    public void noCycleTask(List<TMonitoringRetrievalPlan> planList,Long parentPlanId) {
+    public void noCycleTask(List<TMonitoringRetrievalPlan> planList, Long parentPlanId) {
         // 生成任务为异步方法,新增一个任务正在生成的标识,用于删除计划时的判断
-        String redisKey="Monitoring_Creating_Task" + parentPlanId;
+        String redisKey = "Monitoring_Creating_Task" + parentPlanId;
         RedisUtils.deleteObject(redisKey);
         Boolean hasKey = RedisUtils.hasKey(redisKey);
         if (!hasKey) {
-            RedisUtils.setCacheObject(redisKey, parentPlanId, Duration.ofMillis(1000 * 60*5));
+            RedisUtils.setCacheObject(redisKey, parentPlanId, Duration.ofMillis(1000 * 60 * 5));
         }
-        createAndSaveTask(planList);
+        createAndSaveTask(planList, null, new ArrayList<>());
         RedisUtils.deleteObject(redisKey);
     }
 
@@ -117,32 +118,35 @@ public class CoreMonitoringRetrievalTaskBuilderServiceImpl extends ServiceImpl<C
     }
 
 
-
-    private void createAndSaveTask(List<TMonitoringRetrievalPlan> planList)
-    {
-         List<CoreMonitoringRetrievalTask> allTask = createTask(planList, null);
-        this.saveBatch(allTask);
+    @Override
+    public void createAndSaveTask(List<TMonitoringRetrievalPlan> planList, LocalDate taskTime, List<Long> needBuildTaskOrgIdList) {
+        List<CoreMonitoringRetrievalTask> allTask = createTask(planList, taskTime, needBuildTaskOrgIdList);
+        if (ObjectUtil.isNotEmpty(allTask)){
+            this.saveBatch(allTask);
+        }
     }
 
-    private List<CoreMonitoringRetrievalTask> createTask(List<TMonitoringRetrievalPlan> planList, LocalDate taskTime)
-    {
+
+    private List<CoreMonitoringRetrievalTask> createTask(List<TMonitoringRetrievalPlan> planList, LocalDate taskTime, List<Long> needBuildTaskOrgIdList) {
         List<CoreMonitoringRetrievalTask> taskList = new ArrayList<>();
-        if(ObjectUtil.isNull(taskTime)){
+        if (ObjectUtil.isNull(taskTime)) {
             taskTime = planList.get(0).getStartTime().toInstant().atZone(ZoneId.systemDefault()).toLocalDate();
         }
-        LocalDate taskTime1=taskTime;
+        LocalDate taskTime1 = taskTime;
 
         for (TMonitoringRetrievalPlan plan : planList) {
 
-            List<Long> execOrgIds = tMonitoringRetrievalPlanToExecOrgMapper.selectByMap(MapUtil.of("plan_id", plan.getId())).stream().map(x->x.getOrgId()).collect(Collectors.toList());
-
+            List<Long> execOrgIds = tMonitoringRetrievalPlanToExecOrgMapper.selectByMap(MapUtil.of("plan_id", plan.getId())).stream().map(x -> x.getOrgId()).collect(Collectors.toList());
+            //运维接口传递此参数:needBuildTaskOrgIdList,说明指定了需要生成任务的机构信息
+            if (ObjectUtil.isNotEmpty(needBuildTaskOrgIdList)) {
+                execOrgIds = needBuildTaskOrgIdList;
+            }
             final List<SysOrg> execOrgList = getExecOrgList(execOrgIds, Integer.parseInt(plan.getOrgType()), plan.getOrgPath());
-
             //获取计划关联的机构
 //            List<SysOrg> listByType = orgList.stream().filter(r -> r.getId().equals(plan.getOrgId()) && r.getDeleted() == 0 && r.getIsLock().equals(0))
 //                    .collect(Collectors.toList());
             if (CollectionUtil.isNotEmpty(execOrgList)) {
-                execOrgList.stream().forEach(org->{
+                execOrgList.stream().forEach(org -> {
                     List<CoreMonitoringRetrievalTask> list = this.buildCoreMonitoringRetrievalTask(plan, taskTime1, org);
                     if (CollectionUtil.isNotEmpty(list)) {
                         taskList.addAll(list);
@@ -178,6 +182,7 @@ public class CoreMonitoringRetrievalTaskBuilderServiceImpl extends ServiceImpl<C
         }
         return listAll;
     }
+
     /**
      * 每天生成日任务 如果当天是周一还需生成周任务
      *
@@ -196,7 +201,7 @@ public class CoreMonitoringRetrievalTaskBuilderServiceImpl extends ServiceImpl<C
         //是周一生成周任务
         if (taskTime.getDayOfWeek() == DayOfWeek.MONDAY) {
             List<TMonitoringRetrievalPlan> buildTaskWeekPlanList = getBuildTaskPlanList(RetrievalTaskCycle.WEEKCYCLE.getCode());
-            List<CoreMonitoringRetrievalTask> listWeek =  createTask(buildTaskWeekPlanList,taskTime);
+            List<CoreMonitoringRetrievalTask> listWeek = createTask(buildTaskWeekPlanList, taskTime, new ArrayList<>());
 //            List<CoreMonitoringRetrievalTask> listWeek = this.buildCoreMonitoringRetrievalTask(RetrievalTaskCycle.WEEKCYCLE.getCode(), taskTime, orgList);
             if (CollectionUtil.isNotEmpty(listWeek)) {
                 list.addAll(listWeek);
@@ -205,7 +210,7 @@ public class CoreMonitoringRetrievalTaskBuilderServiceImpl extends ServiceImpl<C
         //生成每日任务
 //        List<CoreMonitoringRetrievalTask> listDay = this.buildCoreMonitoringRetrievalTask(RetrievalTaskCycle.DAYCYCLE.getCode(), taskTime, orgList);
         List<TMonitoringRetrievalPlan> buildTaskDayPlanList = getBuildTaskPlanList(RetrievalTaskCycle.DAYCYCLE.getCode());
-        List<CoreMonitoringRetrievalTask> listDay =  createTask(buildTaskDayPlanList,taskTime);
+        List<CoreMonitoringRetrievalTask> listDay = createTask(buildTaskDayPlanList, taskTime, new ArrayList<>());
         if (CollectionUtil.isNotEmpty(listDay)) {
             list.addAll(listDay);
         }
@@ -238,21 +243,21 @@ public class CoreMonitoringRetrievalTaskBuilderServiceImpl extends ServiceImpl<C
             //季度任务
 //            List<CoreMonitoringRetrievalTask> listQuarter = this.buildCoreMonitoringRetrievalTask(RetrievalTaskCycle.QUARTERCYCLE.getCode(), taskTime, orgList);
             List<TMonitoringRetrievalPlan> buildTaskQuarterPlanList = getBuildTaskPlanList(RetrievalTaskCycle.QUARTERCYCLE.getCode());
-            List<CoreMonitoringRetrievalTask> listQuarter =  createTask(buildTaskQuarterPlanList,taskTime);
+            List<CoreMonitoringRetrievalTask> listQuarter = createTask(buildTaskQuarterPlanList, taskTime, new ArrayList<>());
             if (CollectionUtil.isNotEmpty(listQuarter)) {
                 list.addAll(listQuarter);
             }
             //半年任务
 //            List<CoreMonitoringRetrievalTask> listHalfYear = this.buildCoreMonitoringRetrievalTask(RetrievalTaskCycle.HALFYEARCYCLE.getCode(), taskTime, orgList);
             List<TMonitoringRetrievalPlan> buildTaskHalfYearPlanList = getBuildTaskPlanList(RetrievalTaskCycle.HALFYEARCYCLE.getCode());
-            List<CoreMonitoringRetrievalTask> listHalfYear =  createTask(buildTaskHalfYearPlanList,taskTime);
+            List<CoreMonitoringRetrievalTask> listHalfYear = createTask(buildTaskHalfYearPlanList, taskTime, new ArrayList<>());
             if (CollectionUtil.isNotEmpty(listHalfYear)) {
                 list.addAll(listHalfYear);
             }
             //年任务
 //            List<CoreMonitoringRetrievalTask> listYear = this.buildCoreMonitoringRetrievalTask(RetrievalTaskCycle.YEARCYCLE.getCode(), taskTime, orgList);
             List<TMonitoringRetrievalPlan> buildTaskYearPlanList = getBuildTaskPlanList(RetrievalTaskCycle.YEARCYCLE.getCode());
-            List<CoreMonitoringRetrievalTask> listYear =  createTask(buildTaskYearPlanList,taskTime);
+            List<CoreMonitoringRetrievalTask> listYear = createTask(buildTaskYearPlanList, taskTime, new ArrayList<>());
             if (CollectionUtil.isNotEmpty(listYear)) {
                 list.addAll(listYear);
             }
@@ -262,7 +267,7 @@ public class CoreMonitoringRetrievalTaskBuilderServiceImpl extends ServiceImpl<C
             //季度任务
 //            List<CoreMonitoringRetrievalTask> listQuarter = this.buildCoreMonitoringRetrievalTask(RetrievalTaskCycle.QUARTERCYCLE.getCode(), taskTime, orgList);
             List<TMonitoringRetrievalPlan> buildTaskQuarterPlanList = getBuildTaskPlanList(RetrievalTaskCycle.QUARTERCYCLE.getCode());
-            List<CoreMonitoringRetrievalTask> listQuarter =  createTask(buildTaskQuarterPlanList,taskTime);
+            List<CoreMonitoringRetrievalTask> listQuarter = createTask(buildTaskQuarterPlanList, taskTime, new ArrayList<>());
             if (CollectionUtil.isNotEmpty(listQuarter)) {
                 list.addAll(listQuarter);
             }
@@ -272,14 +277,14 @@ public class CoreMonitoringRetrievalTaskBuilderServiceImpl extends ServiceImpl<C
             //季度任务
 //            List<CoreMonitoringRetrievalTask> listQuarter = this.buildCoreMonitoringRetrievalTask(RetrievalTaskCycle.QUARTERCYCLE.getCode(), taskTime, orgList);
             List<TMonitoringRetrievalPlan> buildTaskQuarterPlanList = getBuildTaskPlanList(RetrievalTaskCycle.QUARTERCYCLE.getCode());
-            List<CoreMonitoringRetrievalTask> listQuarter =  createTask(buildTaskQuarterPlanList,taskTime);
+            List<CoreMonitoringRetrievalTask> listQuarter = createTask(buildTaskQuarterPlanList, taskTime, new ArrayList<>());
             if (CollectionUtil.isNotEmpty(listQuarter)) {
                 list.addAll(listQuarter);
             }
             //半年任务
 //            List<CoreMonitoringRetrievalTask> listHalfYear = this.buildCoreMonitoringRetrievalTask(RetrievalTaskCycle.HALFYEARCYCLE.getCode(), taskTime, orgList);
             List<TMonitoringRetrievalPlan> buildTaskHalfYearPlanList = getBuildTaskPlanList(RetrievalTaskCycle.HALFYEARCYCLE.getCode());
-            List<CoreMonitoringRetrievalTask> listHalfYear =  createTask(buildTaskHalfYearPlanList,taskTime);
+            List<CoreMonitoringRetrievalTask> listHalfYear = createTask(buildTaskHalfYearPlanList, taskTime, new ArrayList<>());
             if (CollectionUtil.isNotEmpty(listHalfYear)) {
                 list.addAll(listHalfYear);
             }
@@ -287,7 +292,7 @@ public class CoreMonitoringRetrievalTaskBuilderServiceImpl extends ServiceImpl<C
         //所有日期都要生成月任务
 //        List<CoreMonitoringRetrievalTask> listMonth = this.buildCoreMonitoringRetrievalTask(RetrievalTaskCycle.MONTHCYCLE.getCode(), taskTime, orgList);
         List<TMonitoringRetrievalPlan> buildTaskMonthPlanList = getBuildTaskPlanList(RetrievalTaskCycle.HALFYEARCYCLE.getCode());
-        List<CoreMonitoringRetrievalTask> listMonth =  createTask(buildTaskMonthPlanList,taskTime);
+        List<CoreMonitoringRetrievalTask> listMonth = createTask(buildTaskMonthPlanList, taskTime, new ArrayList<>());
         if (CollectionUtil.isNotEmpty(listMonth)) {
             list.addAll(listMonth);
         }
@@ -403,7 +408,7 @@ public class CoreMonitoringRetrievalTaskBuilderServiceImpl extends ServiceImpl<C
         coreMonitoringRetrievalTaskMapper.updateDeleteTaskByPlanId(planId, isDeleted);
     }
 
-    private List<TMonitoringRetrievalPlan> getBuildTaskPlanList(Integer cycle){
+    private List<TMonitoringRetrievalPlan> getBuildTaskPlanList(Integer cycle) {
         //获取计划
         QueryWrapper<TMonitoringRetrievalPlan> queryWrapper = new QueryWrapper<>();
         queryWrapper.lambda().eq(TMonitoringRetrievalPlan::getPlanCycle, cycle);
@@ -436,7 +441,7 @@ public class CoreMonitoringRetrievalTaskBuilderServiceImpl extends ServiceImpl<C
             sysOrg.setType(Integer.valueOf(p.getOrgType()));
             List<SysOrg> sysOrgs = remoteOrgService.listByParentIdAndType(sysOrg, SecurityConstants.INNER);*/
             List<SysOrg> sysOrgs = orgList.stream().
-                    filter(s -> (ObjectUtil.equal(s.getIsLock(),0)  && ObjectUtil.equal(s.getDeleted(),0) && s.getPath() != null && s.getPath().contains(p.getOrgPath())) && (s.getType() != null && s.getType().toString().equals(p.getOrgType())))
+                    filter(s -> (ObjectUtil.equal(s.getIsLock(), 0) && ObjectUtil.equal(s.getDeleted(), 0) && s.getPath() != null && s.getPath().contains(p.getOrgPath())) && (s.getType() != null && s.getType().toString().equals(p.getOrgType())))
                     .collect(Collectors.toList());
             for (SysOrg s : sysOrgs) {
                 TMonitoringRetrievalPlan plan = new TMonitoringRetrievalPlan();
@@ -525,9 +530,28 @@ public class CoreMonitoringRetrievalTaskBuilderServiceImpl extends ServiceImpl<C
         for (Map.Entry<Short, DateRange> entry : dateRanges.entrySet()) {
             Integer key = entry.getKey().intValue();
             DateRange dateRange1 = entry.getValue();
+            LambdaQueryWrapper<CoreMonitoringRetrievalTask> queryWrapper = new LambdaQueryWrapper<>();
+            queryWrapper
+                    .eq(CoreMonitoringRetrievalTask::getPlanId, plan.getId())
+                    .eq(CoreMonitoringRetrievalTask::getOrgId, sysOrg.getId())
+                    .ge(CoreMonitoringRetrievalTask::getPlanStartTime, dateRange.getStartTime())
+                    .le(CoreMonitoringRetrievalTask::getPlanEndTime, dateRange.getEndTime());
+            List<CoreMonitoringRetrievalTask> exitTaskList = this.list(queryWrapper);
             CoreMonitoringRetrievalTask task = this.buildCoreMonitoringRetrievalTask(batchNum, key, plan, sysOrg,
                     dateRange1, ymd, dateRanges.size());
+            Optional<CoreMonitoringRetrievalTask> optional = exitTaskList.stream()
+                    .filter(t -> t.getPlanStartTime().equals(dateRange1.getStartTime()))
+                    .filter(t -> t.getPlanEndTime().equals(dateRange1.getEndTime()))
+                    .filter(t -> t.getOrgId().equals(sysOrg.getId()))
+                    .filter(t -> t.getPlanId().equals(plan.getId()))
+                    .findAny();
+            if (optional.isPresent()){
+                //说明已经存在相同的任务了,
+               continue;
+            }
             taskList.add(task);
+
+
         }
         return taskList;
 
@@ -582,7 +606,7 @@ public class CoreMonitoringRetrievalTaskBuilderServiceImpl extends ServiceImpl<C
         task.setYmdDay(ymd.getDay());
         task.setRoleId(plan.getRoleId());
         task.setStatus("0");
-        task.setCreateTime(DateTime.now());
+       // task.setCreateTime(DateTime.now());
         task.setIsDeleted(0);
         return task;
     }