Explorar o código

规则引擎2.0定时执行逻辑

zhaoxian %!s(int64=3) %!d(string=hai) anos
pai
achega
817fff6548

+ 60 - 7
module-alarm/src/main/java/cn/com/ctop/alarm/modules/service/impl/RuleGroupServiceImpl.java

@@ -53,6 +53,7 @@ import org.springframework.stereotype.Service;
 
 import javax.annotation.Resource;
 import java.math.BigDecimal;
+import java.text.ParseException;
 import java.util.ArrayList;
 import java.util.Date;
 import java.util.HashMap;
@@ -504,8 +505,8 @@ public class RuleGroupServiceImpl extends ServiceImpl<RuleGroupMapper, RuleGroup
             //后续 操作阈值
             String operationThreshold = thresholdJson.getString("operationThreshold");
             //峰值
-            String maxValue = Check.isNull(thresholdJson.getString("maxValue"))?"-1":thresholdJson.getString("maxValue");
-            String minValue = Check.isNull(thresholdJson.getString("minValue"))?"-1":thresholdJson.getString("minValue");
+            String maxValue = Check.isNull(thresholdJson.getString("maxValue")) ? "-1" : thresholdJson.getString("maxValue");
+            String minValue = Check.isNull(thresholdJson.getString("minValue")) ? "-1" : thresholdJson.getString("minValue");
 
             if (type == 1 || type == 3) {
                 Long planId = obj.getLong("planId");//头条
@@ -634,11 +635,12 @@ public class RuleGroupServiceImpl extends ServiceImpl<RuleGroupMapper, RuleGroup
                     }
                     recordEntity = getOperationRecordEntity(accountId, ruleGroup, maxValue, minValue, operate, unitId, null, operationThreshold, oldBudget.toString(), newBudget.toString(), message, "快手");
                     //修改投放时间
-                } else if ("TIME".equals(operate) && !kuaiShouGroup.getScheduleTime().equals(operationThreshold)) {
+                } else if ("TIME".equals(operate)) {
+                    String newScheduleTime = getNewScheduleTime(operationThreshold);
                     JSONObject unitJson = new JSONObject();
                     unitJson.put("advertiser_id", accountId);
                     unitJson.put("unit_id", unitId);
-                    unitJson.put("schedule_time", operationThreshold);
+                    unitJson.put("schedule_time", newScheduleTime);
                     JSONObject jsonObject = kuaiShouUpdateService.updateUnit(token.getAccessToken(), unitJson);
                     Integer code = jsonObject.getInteger("code");
                     String message = "success";
@@ -649,7 +651,7 @@ public class RuleGroupServiceImpl extends ServiceImpl<RuleGroupMapper, RuleGroup
                         message = "fail," + jsonObject.getString("message");
                         log.error("(快手)规则引擎修改投放时间段失败,accountId={},unitId={},{}", accountId, unitId, jsonObject.getString("message"));
                     }
-                    recordEntity = getOperationRecordEntity(accountId, ruleGroup, maxValue, minValue, operate, unitId, null, operationThreshold, kuaiShouGroup.getScheduleTime(), operationThreshold, message, "快手");
+                    recordEntity = getOperationRecordEntity(accountId, ruleGroup, maxValue, minValue, operate, unitId, null, newScheduleTime, kuaiShouGroup.getScheduleTime(), operationThreshold, message, "快手");
                 }
             }
             if (!Check.isNull(recordEntity)) {
@@ -662,6 +664,57 @@ public class RuleGroupServiceImpl extends ServiceImpl<RuleGroupMapper, RuleGroup
     }
 
     /**
+     * 获取新的投放时间
+     */
+    private static String getNewScheduleTime(String operationThreshold) {
+        String ScheduleTime = "";
+        String WeekStr = DateUtils.getWhatDay(new Date().getTime());
+        Integer weekInt = -1;
+        switch (WeekStr) {
+            case "星期一":
+                weekInt = 0;
+                break;
+            case "星期二":
+                weekInt = 1;
+                break;
+            case "星期三":
+                weekInt = 2;
+                break;
+            case "星期四":
+                weekInt = 3;
+                break;
+            case "星期五":
+                weekInt = 4;
+                break;
+            case "星期六":
+                weekInt = 5;
+                break;
+            default:
+                weekInt = 6;
+                break;
+        }
+        //获取当前小时
+        Integer nowHour = -1;
+        try {
+            nowHour = DateUtils.getNowHour();
+        } catch (ParseException e) {
+            e.printStackTrace();
+        }
+        StringBuffer buf = new StringBuffer();
+        for (int i = 0; i < weekInt * 24 + nowHour; i++) {
+            buf.append("1");
+        }
+        Integer invalidTime = Integer.valueOf(operationThreshold);
+        for (int i = 0; i < invalidTime; i++) {
+            buf.append("0");
+        }
+        for (int i = 0; i < 7 * 24 - weekInt * 24 - nowHour - invalidTime; i++) {
+            buf.append("1");
+        }
+        return buf.toString();
+    }
+
+    /**
      * 整合操作记录实体类
      */
     private AlarmRuleOperationRecord getOperationRecordEntity(Long accountId, RuleGroup ruleGroup, String maxValue, String minValue, String indicatorCode, Long unitId, Long planId, String operationThreshold, String oldValue, String newValue, String msg, String mediaType) {
@@ -1189,8 +1242,8 @@ public class RuleGroupServiceImpl extends ServiceImpl<RuleGroupMapper, RuleGroup
                 SendMailUtil.sendMail(mailList, ruleGroup.getGroupName() + "预警", msg);
             } else if ("TEL".equals(sendType)) {
             } else {
-                sendMessageService.sendMessage("1b3deb8258e84df994f1371a51cfc14a", msg);
-//                sendMessageService.sendMessage(user.getString("id"), msg);
+//                sendMessageService.sendMessage("1b3deb8258e84df994f1371a51cfc14a", msg);
+                sendMessageService.sendMessage(user.getString("id"), msg);
                 Thread.sleep(100);
             }
             if (!Check.isNull(msg)) {