Jelajahi Sumber

aps-定时任务调度重排之后预排

sucheng 4 bulan lalu
induk
melakukan
2e288e2e05

+ 2 - 0
cx-aps/cx-aps-common/src/main/java/com/rongwei/bscommon/sys/dao/ApsBlankOrderDao.java

@@ -176,4 +176,6 @@ public interface ApsBlankOrderDao extends BaseMapper<ApsBlankOrderDo> {
     List<ApsProcessOperationProcessEquDo> selectAllNeedSortLenZha(@Param("chongpaiEndDate") DateTime chongpaiEndDate,@Param("tenantId") String tenantId);
 
     List<ApsProcessOperationProcessEquDo> selectAllNeedSortTuiHuo(@Param("chongpaiEndDate") DateTime chongpaiEndDate,@Param("tenantId") String tenantId);
+
+    List<ApsBlankOrderVo> getNeedScheduleList(@Param("tenantId") String tenantId);
 }

+ 82 - 31
cx-aps/cx-aps-common/src/main/java/com/rongwei/bscommon/sys/service/impl/ApsBlankOrderServiceImpl.java

@@ -33,6 +33,8 @@ import com.rongwei.rwcommon.vo.SearchOrderVo;
 import com.rongwei.safecommon.utils.CXCommonUtils;
 import com.rongwei.safecommon.utils.SaveConstans;
 import lombok.extern.log4j.Log4j2;
+import org.redisson.api.RLock;
+import org.redisson.api.RedissonClient;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 import org.springframework.beans.BeanUtils;
@@ -130,6 +132,8 @@ public class ApsBlankOrderServiceImpl extends ServiceImpl<ApsBlankOrderDao, ApsB
     private SysOrganizationService sysOrganizationService;
     @Autowired
     private ApsScheduleLogService apsScheduleLogService;
+    @Autowired
+    private RedissonClient redissonClient;
 
 
     @Override
@@ -743,21 +747,21 @@ public class ApsBlankOrderServiceImpl extends ServiceImpl<ApsBlankOrderDao, ApsB
     @Override
     public void blankOrderAps(List<ApsBlankOrderVo> apsBlankOrders) {
         apsService.apsSchedule(apsBlankOrders, new ApsTypeVo(), null, null, null);
-        //调度排程
-        //判断是否是 18:00:00-24:00:00/00:00:00-5:00:00
-        // 获取当前时间
-        LocalTime now = LocalTime.now();
-        // 定义时间段
-        LocalTime startTime1 = LocalTime.of(20, 0); // 20:00:00
-        LocalTime endTime1 = LocalTime.of(23, 59, 59); // 23:59:59 (24:00:00 - 1 second)
-        LocalTime startTime2 = LocalTime.MIDNIGHT; // 00:00:00
-        LocalTime endTime2 = LocalTime.of(5, 0); // 05:00:00
-        // 检查当前时间是否在两个时间段之一内
-        boolean isInFirstRange = (now.isAfter(startTime1) || now.equals(startTime1)) && (now.isBefore(endTime1) || now.equals(endTime1));
-        boolean isInSecondRange = (now.isAfter(startTime2) || now.equals(startTime2)) && (now.isBefore(endTime2) || now.equals(endTime2));
-        if (isInFirstRange || isInSecondRange) {
-            apsScheduleRestart(null, null, null, null);
-        }
+//        //调度排程
+//        //判断是否是 18:00:00-24:00:00/00:00:00-5:00:00
+//        // 获取当前时间
+//        LocalTime now = LocalTime.now();
+//        // 定义时间段
+//        LocalTime startTime1 = LocalTime.of(20, 0); // 20:00:00
+//        LocalTime endTime1 = LocalTime.of(23, 59, 59); // 23:59:59 (24:00:00 - 1 second)
+//        LocalTime startTime2 = LocalTime.MIDNIGHT; // 00:00:00
+//        LocalTime endTime2 = LocalTime.of(5, 0); // 05:00:00
+//        // 检查当前时间是否在两个时间段之一内
+//        boolean isInFirstRange = (now.isAfter(startTime1) || now.equals(startTime1)) && (now.isBefore(endTime1) || now.equals(endTime1));
+//        boolean isInSecondRange = (now.isAfter(startTime2) || now.equals(startTime2)) && (now.isBefore(endTime2) || now.equals(endTime2));
+//        if (isInFirstRange || isInSecondRange) {
+//            apsScheduleRestart(null, null, null, null);
+//        }
     }
 
     /**
@@ -4387,42 +4391,76 @@ public class ApsBlankOrderServiceImpl extends ServiceImpl<ApsBlankOrderDao, ApsB
         String bocaiTenantId = "0001651000000015EFDD";
 
         Thread thread = new Thread(() -> {
-            doRestartSchedule(bancaiTenantId);
+            String res = "needWait";
+            while (res.equals("needWait")) {
+                res = doRestartSchedule(bancaiTenantId);
+                try {
+                    Thread.sleep(640000);
+                } catch (InterruptedException e) {
+                    throw new RuntimeException(e);
+                }
+            }
+            if (res.equals("success")) {//调度之后,预排程
+                //调度排程之后,进行预排程
+                //查询待排程的订单
+                List<ApsBlankOrderVo> resBlankList = apsBlankOrderDao.getNeedScheduleList(bancaiTenantId);
+                blankOrderAps(resBlankList);
+            }
         });
         thread.start();
 
         Thread thread1 = new Thread(() -> {
-            doRestartSchedule(bocaiTenantId);
+            String res = "needWait";
+            while (res.equals("needWait")) {
+                res = doRestartSchedule(bocaiTenantId);
+                try {
+                    Thread.sleep(640000);
+                } catch (InterruptedException e) {
+                    throw new RuntimeException(e);
+                }
+            }
+            if (res.equals("success")) {//调度之后,预排程
+                //调度排程之后,进行预排程
+                //查询待排程的订单
+                List<ApsBlankOrderVo> resBlankList = apsBlankOrderDao.getNeedScheduleList(bocaiTenantId);
+                blankOrderAps(resBlankList);
+            }
         });
         thread1.start();
     }
 
-    private void doRestartSchedule(String tenantId) {
+    private String doRestartSchedule(String tenantId) {
         //即将排程日期
         DateTime nowDate = DateUtil.date();
         String time1 = DateUtil.format(nowDate, "yyyy-MM-dd 00:00:00");
-        String time2 = DateUtil.format(nowDate, "yyyy-MM-dd 12:00:00");
-        String time3 = DateUtil.format(nowDate, "yyyy-MM-dd 23:59:59");
+        String time2 = DateUtil.format(nowDate, "yyyy-MM-dd 04:00:00");
+        String time3 = DateUtil.format(nowDate, "yyyy-MM-dd 19:00:00");
+        String time4 = DateUtil.format(nowDate, "yyyy-MM-dd 23:59:59");
         String now = DateUtil.format(nowDate, "yyyy-MM-dd HH:mm:ss");
-        //如果当前时间在0-12点,重排明天的
-        //如果当前时间在12-24点,重排后天的
-        String needScheduleDate = "";
+        //如果当前时间在0-12点,重排今天12点-明天12点
+        //如果当前时间在12-24点,重排明天12点-后天12点
+        String needScheduleStartDate = "";
+        String needScheduleEndDate = "";
         if (now.compareTo(time1) >= 0 && now.compareTo(time2) < 0) {
-            needScheduleDate = DateUtil.format(DateUtil.offsetDay(nowDate, 1), "yyyy-MM-dd 12:00:00");
-        }
-        if (now.compareTo(time2) >= 0 && now.compareTo(time3) <= 0) {
-            needScheduleDate = DateUtil.format(DateUtil.offsetDay(nowDate, 2), "yyyy-MM-dd 12:00:00");
+            needScheduleStartDate = DateUtil.format(nowDate, "yyyy-MM-dd 12:00:00");
+            needScheduleEndDate = DateUtil.format(DateUtil.offsetDay(nowDate, 1), "yyyy-MM-dd 12:00:00");
+        } else if (now.compareTo(time3) >= 0 && now.compareTo(time4) <= 0) {
+            needScheduleStartDate = DateUtil.format(DateUtil.offsetDay(nowDate, 1), "yyyy-MM-dd 12:00:00");
+            needScheduleEndDate = DateUtil.format(DateUtil.offsetDay(nowDate, 2), "yyyy-MM-dd 12:00:00");
+        } else {
+            log.info("{}不在调度时间段中,不调度排程", tenantId);
+            return "notNeedWait";
         }
         //查询日志判断是否已调度排程成功
         int count = apsScheduleLogService.count(new LambdaQueryWrapper<ApsScheduleLogDo>()
                 .eq(ApsScheduleLogDo::getTenantid, tenantId)
                 .eq(ApsScheduleLogDo::getScheduletype, "调度排程")
                 .eq(ApsScheduleLogDo::getSucess, "是")
-                .le(ApsScheduleLogDo::getSchedulestarttime, needScheduleDate)
-                .ge(ApsScheduleLogDo::getScheduleendtime, needScheduleDate));
+                .eq(ApsScheduleLogDo::getSchedulestarttime, needScheduleStartDate)
+                .eq(ApsScheduleLogDo::getScheduleendtime, needScheduleEndDate));
         if (count > 0) {
             log.info("{}已调度排程,无需排程", tenantId);
-            return;
+            return "notNeedWait";
         }
 
         SysUserVo currentUser = new SysUserVo();
@@ -4435,7 +4473,20 @@ public class ApsBlankOrderServiceImpl extends ServiceImpl<ApsBlankOrderDao, ApsB
         currentUser.setOrganizationDoList(organizationVoList);
         CXCommonUtils.getCurrentUserFactoryId(currentUser);
 
-        apsScheduleRestart(null, null, null, currentUser);
+        // 按照所属工厂加锁
+        RLock rLock = redissonClient.getLock(tenantId);
+        boolean isLocked = rLock.isLocked();
+        // 锁是否被占用
+        if (isLocked) {
+            return "needWait";
+        }
+
+        try {
+            apsScheduleRestart(null, needScheduleStartDate, needScheduleEndDate, currentUser);
+        } catch (Exception e) {
+            return "success";
+        }
+        return "success";
     }
 
     //去除后续的冲突

+ 14 - 0
cx-aps/cx-aps-common/src/main/resources/mybatis/ApsBlankOrderDao.xml

@@ -564,4 +564,18 @@
           AND apo.PROCESS IN ('中退','成退','小卷成退')
           AND apope.PLANENDDATE is not null
     </select>
+    <select id="getNeedScheduleList" resultType="com.rongwei.bsentity.vo.ApsBlankOrderVo">
+        SELECT
+            abo.*,
+            apo.DELIVERYTIME AS 'deliverytime'
+        FROM
+            aps_blank_order abo
+                LEFT JOIN aps_production_order apo ON abo.PRODUCTIONORDERID = apo.ID
+        WHERE
+            abo.DELETED = '0'
+          AND apo.DELETED = '0'
+          AND abo.ISSTAG='否'
+          AND abo.SCHEDULINGSTATUS = '10'
+          AND abo.TENANTID = #{tenantId}
+    </select>
 </mapper>

+ 2 - 2
cx-aps/cx-aps-server/src/main/java/com/rongwei/bsserver/controller/ApsBlankOrderController.java

@@ -75,9 +75,9 @@ public class ApsBlankOrderController {
         return R.ok();
     }
     /**
-     * 定时任务自动调度,每天1点和22点执行
+     * 定时任务自动调度,每天19点执行
      */
-    @Scheduled(cron = "0 0 1,22 * * *")
+    @Scheduled(cron = "0 0 19 * * *")
     @GetMapping("/apsScheduleRestart2")
     public R apsScheduleRestart2() {
         apsBlankOrderService.apsScheduleRestart2();