Ver Fonte

优化规则引擎-条件下拉接口,去除租户刷选的逻辑,做到通用;优化当触发条件中触发方式为采集量时,条件对应的开始时间和结束时间的处理,以及次日、次月、次周的处理逻辑;

james há 1 semana atrás
pai
commit
d74f09bde3

+ 52 - 15
service-rule/service-rule-biz/src/main/java/com/usky/rule/jobs/ConsumptionJob.java

@@ -27,6 +27,7 @@ import com.usky.rule.util.RuleEngineUtil;
 //import com.usky.rule.DeviceService;
 import com.usky.rule.service.RuleEngineService;
 import java.math.BigDecimal;
+import java.time.DayOfWeek;
 import java.time.LocalDateTime;
 import java.time.temporal.ChronoUnit;
 import java.util.*;
@@ -156,28 +157,64 @@ public class ConsumptionJob implements Job {
         Integer end = timeRange.getEnd();
         Assert.notNull(start, "start不能为空");
         Assert.notNull(end, "end不能为空");
-        Assert.isTrue(end - start > 0, "结束时间必须大于起始时间");
-        LocalDateTime startTime = null;
-        LocalDateTime endTime = null;
-        switch (TimeTypeEnum.get(timeRange.getType())) {
+        Assert.isTrue(end > start, "结束时间必须大于起始时间");
+
+        LocalDateTime startTime;
+        LocalDateTime endTime;
+
+        TimeTypeEnum typeEnum = TimeTypeEnum.get(timeRange.getType());
+        Assert.notNull(typeEnum, "不支持的时间类型");
+
+        // 老版本传统 switch 格式
+        switch (typeEnum) {
             case HOUR:
-                endTime = now.truncatedTo(ChronoUnit.HOURS);
-                int hour = endTime.getHour();
-                if (hour < start) {
-                    startTime = endTime.minusDays(1L).withHour(start);
+                // 支持跨天小时 22~26
+                LocalDateTime todayZeroHour = now.truncatedTo(ChronoUnit.DAYS);
+                startTime = todayZeroHour.withHour(start);
+
+                if (end >= 24) {
+                    endTime = todayZeroHour.plusDays(1).withHour(end - 24);
                 } else {
-                    startTime = endTime.withHour(start);
+                    endTime = todayZeroHour.withHour(end);
+                }
+
+                if (now.isBefore(startTime)) {
+                    startTime = startTime.minusDays(1);
+                    endTime = endTime.minusDays(1);
                 }
                 break;
             case DAY:
+                // 支持按当月实际天数跨月:start=5, end=33
+                LocalDateTime todayZeroDay = now.truncatedTo(ChronoUnit.DAYS);
+                startTime = todayZeroDay.withDayOfMonth(start);
+                // 结束时间 = 开始时间 + (end-1)天
+                endTime = startTime.plusDays(end - 1);
+
+                if (now.isBefore(startTime)) {
+                    startTime = startTime.minusMonths(1);
+                    endTime = endTime.minusMonths(1);
+                }
+                break;
             case WEEK:
-                endTime = now.truncatedTo(ChronoUnit.DAYS);
-                int day = endTime.getDayOfMonth();
-                if (day < start) {
-                    startTime = endTime.minusMonths(1L).withDayOfMonth(start);
-                } else {
-                    startTime = endTime.withDayOfMonth(start);
+                LocalDateTime todayZeroWeek = now.truncatedTo(ChronoUnit.DAYS);
+
+                // 开始时间:本周 星期start(1=周一)
+                DayOfWeek todayWeek = todayZeroWeek.getDayOfWeek();
+                int weekVal = todayWeek.getValue();
+                startTime = todayZeroWeek.plusDays((long) start - weekVal);
+
+                // 结束时间:从开始时间 直接往后加 (end - start) 天
+                // 支持 end > 7,自动跨周、跨N周
+                endTime = startTime.plusDays(end - start);
+
+                // 如果还没到当前周期 → 取上一周
+                if (now.isBefore(startTime)) {
+                    startTime = startTime.minusWeeks(1);
+                    endTime = endTime.minusWeeks(1);
                 }
+                break;
+            default:
+                throw new IllegalArgumentException("不支持的时间类型: " + typeEnum);
         }
 
         times[0] = startTime;

+ 2 - 2
service-rule/service-rule-biz/src/main/java/com/usky/rule/mapper/RuleEngineConditionMapper.java

@@ -9,6 +9,6 @@ import java.util.List;
  * 规则条件字典,供条件下拉;tenantId 为空时不按租户过滤
  */
 public interface RuleEngineConditionMapper {
-    List<RuleEngineCondition> selectAll(@Param("tenantId") Integer tenantId);
-    List<RuleEngineCondition> selectByType(@Param("type") Integer type, @Param("tenantId") Integer tenantId);
+    List<RuleEngineCondition> selectAll();
+    List<RuleEngineCondition> selectByType(@Param("type") Integer type);
 }

+ 2 - 7
service-rule/service-rule-biz/src/main/java/com/usky/rule/service/impl/RuleEngineConditionServiceImpl.java

@@ -18,14 +18,9 @@ public class RuleEngineConditionServiceImpl implements RuleEngineConditionServic
 
     @Override
     public List<RuleEngineCondition> listConditions(Integer type) {
-        Integer tenantId = null;
-        try {
-            tenantId = SecurityUtils.getTenantId();
-        } catch (Exception ignored) {
-        }
         if (type == null) {
-            return ruleEngineConditionMapper.selectAll(tenantId);
+            return ruleEngineConditionMapper.selectAll();
         }
-        return ruleEngineConditionMapper.selectByType(type, tenantId);
+        return ruleEngineConditionMapper.selectByType(type);
     }
 }

+ 2 - 2
service-rule/service-rule-biz/src/main/java/com/usky/rule/service/impl/RuleEngineServiceImpl.java

@@ -617,13 +617,13 @@ public class RuleEngineServiceImpl extends AbstractCrudService<RuleEngineMapper,
 
     @Override
     public String getName(Long ruleEngineId) {
-        RuleEngine ruleEngine = getBaseMapper().selectById(ruleEngineId, SecurityUtils.getTenantId());
+        RuleEngine ruleEngine = getBaseMapper().selectById(ruleEngineId);
         return ruleEngine == null ? "" : ruleEngine.getName();
     }
 
     @Override
     public String getDetail(Long ruleEngineId) {
-        RuleEngine ruleEngine = getBaseMapper().selectById(ruleEngineId, SecurityUtils.getTenantId());
+        RuleEngine ruleEngine = getBaseMapper().selectById(ruleEngineId);
         return ruleEngine == null ? "" : ruleEngine.getDetail();
     }
 }

+ 4 - 5
service-rule/service-rule-biz/src/main/java/com/usky/rule/util/RuleEngineUtil.java

@@ -90,8 +90,8 @@ public class RuleEngineUtil {
             ruleEngineLog.setTriggerType(triggerType);
             ruleEngineLog.setUpdateTime(now);
             ruleEngineLog.setCreateTime(now);
-            ruleEngineLog.setCreatedBy(SecurityUtils.getUsername());
-            ruleEngineLog.setUpdatedBy(SecurityUtils.getUsername());
+            ruleEngineLog.setCreatedBy("admin");
+            ruleEngineLog.setUpdatedBy("admin");
             ruleEngineLog.setTime(now);
             this.ruleEngineLogService.save(ruleEngineLog);
         }
@@ -178,8 +178,7 @@ public class RuleEngineUtil {
             //String productCode = dmpProductService.getOne(new LambdaQueryWrapper<DmpProduct>().eq(DmpProduct::getId, productId).eq(DmpProduct::getDeleteFlag,0).eq(DmpProduct::getTenantId, SecurityUtils.getTenantId())).getProductCode();
             List<DmpProductCommand> dmpProductCommandList = Optional.ofNullable(
                     dmpProductCommandService.getBaseMapper().selectList(new LambdaQueryWrapper<DmpProductCommand>()
-                            .eq(DmpProductCommand::getProductCode, productCode)
-                            .eq(DmpProductCommand::getTenantId, SecurityUtils.getTenantId())))
+                            .eq(DmpProductCommand::getProductCode, productCode)))
                     .orElse(Collections.emptyList());
             List<DeviceControlAction.Functions> functions = deviceControlAction.getFunctions();
             List<Control> controls = new ArrayList();
@@ -197,7 +196,7 @@ public class RuleEngineUtil {
                 HashMap<String, String> functionMap = new HashMap();
                 functionMap.put(identifier, function.getValue());
                 Control control = new Control();
-                control.setDelaySeconds(function.getDelaySeconds());
+                //control.setDelaySeconds(function.getDelaySeconds());
                 control.setIdentifier(identifier);
                 control.setName(dmpProductCommand != null ? dmpProductCommand.getCommandName() : null);
                 control.setValue(function.getValue());

+ 0 - 4
service-rule/service-rule-biz/src/main/resources/mapper/RuleEngineConditionMapper.xml

@@ -15,15 +15,11 @@
     </resultMap>
     <select id="selectAll" resultMap="Base">
         SELECT * FROM rule_engine_condition
-        <where>
-            <if test="tenantId != null">AND tenant_id = #{tenantId}</if>
-        </where>
         ORDER BY type, id
     </select>
     <select id="selectByType" resultMap="Base">
         SELECT * FROM rule_engine_condition
         <where>
-            <if test="tenantId != null">AND tenant_id = #{tenantId}</if>
             AND type = #{type}
         </where>
         ORDER BY id