diff --git a/README.MD b/README.MD index b418249..4e92a12 100644 --- a/README.MD +++ b/README.MD @@ -1,4 +1,4 @@ -# 奇异值管理api +# 歧义数据管理api ## 目录 ``` diff --git a/src/main/java/com/whdc/config/Knife4jConfiguration.java b/src/main/java/com/whdc/config/Knife4jConfiguration.java index 28f71d4..df9d0e4 100644 --- a/src/main/java/com/whdc/config/Knife4jConfiguration.java +++ b/src/main/java/com/whdc/config/Knife4jConfiguration.java @@ -30,8 +30,8 @@ public class Knife4jConfiguration { private ApiInfo getApiInfoBuilder() { return new ApiInfoBuilder() - .title("奇异值管理api") - .description("# 奇异值管理api RESTful APIs") + .title("歧义数据管理api") + .description("# 歧义数据管理api RESTful APIs") .termsOfServiceUrl("http://219.138.108.99:19000/jszx") .contact(new Contact("湖北纬皓端成", null, null)) .version("1.0") diff --git a/src/main/java/com/whdc/controller/ERuleController.java b/src/main/java/com/whdc/controller/ERuleController.java index b82749a..5830491 100644 --- a/src/main/java/com/whdc/controller/ERuleController.java +++ b/src/main/java/com/whdc/controller/ERuleController.java @@ -1,7 +1,6 @@ package com.whdc.controller; -import com.whdc.model.bean.ItemType; import com.whdc.model.dto.FindRuleDto; import com.whdc.model.entity.ERule; import com.whdc.model.entity.EStationRules; @@ -10,11 +9,11 @@ import com.whdc.model.group.Update; import com.whdc.service.IERuleService; import com.whdc.service.IEStationRulesService; import com.whdc.utils.ResultJson; +import com.whdc.valid.bean.ItemType; import io.swagger.annotations.Api; import io.swagger.annotations.ApiOperation; import lombok.extern.slf4j.Slf4j; import org.apache.commons.collections.CollectionUtils; -import org.apache.commons.lang3.StringUtils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.validation.annotation.Validated; import org.springframework.web.bind.annotation.*; @@ -28,7 +27,7 @@ import java.util.Objects; @Slf4j @Api(tags = "规则管理 - Controller") @RestController -@RequestMapping("/v2/eRule") +@RequestMapping("/eRule") public class ERuleController { @Autowired @@ -68,9 +67,12 @@ public class ERuleController { } if ( - StringUtils.isBlank(model.getDiffMax()) - && StringUtils.isBlank(model.getMin()) - && StringUtils.isBlank(model.getMax()) + Objects.isNull(model.getDiffMax()) + && Objects.isNull(model.getMin()) + && Objects.isNull(model.getMax()) + && Objects.isNull(model.getDuration()) + && Objects.isNull(model.getLagTime()) + && Objects.isNull(model.getLeadingTime()) ){ return ResultJson.error("请设置规则"); } diff --git a/src/main/java/com/whdc/controller/EStationRulesController.java b/src/main/java/com/whdc/controller/EStationRulesController.java index c365616..f4b3d91 100644 --- a/src/main/java/com/whdc/controller/EStationRulesController.java +++ b/src/main/java/com/whdc/controller/EStationRulesController.java @@ -1,6 +1,7 @@ package com.whdc.controller; +import com.whdc.model.dto.ERuleDtoTest; import com.whdc.model.dto.FindStationDto; import com.whdc.model.entity.EStationRules; import com.whdc.model.group.Insert; @@ -8,6 +9,7 @@ import com.whdc.model.group.Update; import com.whdc.service.IERuleService; import com.whdc.service.IEStationRulesService; import com.whdc.utils.ResultJson; +import com.whdc.valid.service.ValidateService; import io.swagger.annotations.Api; import io.swagger.annotations.ApiOperation; import org.apache.commons.collections.CollectionUtils; @@ -23,7 +25,7 @@ import java.util.Objects; */ @Api(tags = "站点规则管理 - Controller") @RestController -@RequestMapping("/v2/eStationRule") +@RequestMapping("/eStationRule") public class EStationRulesController { @Autowired @@ -109,4 +111,13 @@ public class EStationRulesController { return ResultJson.ok(ieStationRulesService.removeById(id,model)); } + @Autowired + private ValidateService validateService; + + @ApiOperation(value = "规则测试 ") + @PostMapping(value = "testRule") + public ResultJson testRule(@RequestBody ERuleDtoTest test) { + return ResultJson.ok(validateService.validate(test.getStr() ,test.getOldStr())); + } + } diff --git a/src/main/java/com/whdc/model/entity/ERule.java b/src/main/java/com/whdc/model/entity/ERule.java index 1cf5441..943a307 100644 --- a/src/main/java/com/whdc/model/entity/ERule.java +++ b/src/main/java/com/whdc/model/entity/ERule.java @@ -15,6 +15,7 @@ import org.apache.commons.lang3.StringUtils; import javax.validation.constraints.Max; import javax.validation.constraints.NotEmpty; import javax.validation.constraints.NotNull; +import java.math.BigDecimal; import java.util.Date; import java.util.Locale; @@ -59,7 +60,7 @@ public class ERule extends Model { */ @ApiParam(value = "规则类型", required = true, example = "") @ApiModelProperty(value = "规则类型", required = true, dataType = "String") - @TableField(value = "ITEM", updateStrategy = FieldStrategy.NOT_EMPTY) + @TableField(value = "ITEM", updateStrategy = FieldStrategy.IGNORED) @NotEmpty(message = "规则类型不能为空", groups = {Insert.class,Update.class}) @Max(50) private String item; @@ -77,34 +78,34 @@ public class ERule extends Model { * 最小值 */ @ApiParam(value = "最小值", required = false, example = "") - @ApiModelProperty(value = "最小值", required = false, dataType = "String") - @TableField(value = "MIN", updateStrategy = FieldStrategy.NOT_EMPTY) + @ApiModelProperty(value = "最小值", required = false, dataType = "Numeric") + @TableField(value = "MIN", updateStrategy = FieldStrategy.IGNORED) @Max(50) - private String min; + private BigDecimal min; /** * 最大值 */ @ApiParam(value = "最大值", required = false, example = "") - @ApiModelProperty(value = "最大值", required = false, dataType = "String") - @TableField(value = "MAX", updateStrategy = FieldStrategy.NOT_EMPTY) + @ApiModelProperty(value = "最大值", required = false, dataType = "Numeric") + @TableField(value = "MAX", updateStrategy = FieldStrategy.IGNORED) @Max(50) - private String max; + private BigDecimal max; /** * 最大值 */ @ApiParam(value = "两条数据之间的最大差值", required = false, example = "") - @ApiModelProperty(value = "两条数据之间的最大差值", required = false, dataType = "String") - @TableField(value = "DIFF_MAX", updateStrategy = FieldStrategy.NOT_EMPTY) - private String diffMax; + @ApiModelProperty(value = "两条数据之间的最大差值", required = false, dataType = "Numeric") + @TableField(value = "DIFF_MAX", updateStrategy = FieldStrategy.IGNORED) + private BigDecimal diffMax; /** * 最大值 */ @ApiParam(value = "时间段, 单位 s", required = false, example = "") - @ApiModelProperty(value = "时间段, 单位 s", required = false, dataType = "Integer") - @TableField(value = "DURATION", updateStrategy = FieldStrategy.NOT_EMPTY) + @ApiModelProperty(value = "时间段, 单位 s", required = false, dataType = "Numeric") + @TableField(value = "DURATION", updateStrategy = FieldStrategy.IGNORED) @Max(10) private Integer duration; @@ -112,8 +113,8 @@ public class ERule extends Model { * 接收超出时间,单位: s ,指接收时间超前范围 */ @ApiParam(value = "接收超前时间,单位: s ,指接收时间超前范围", required = false, example = "") - @ApiModelProperty(value = "接收超前时间,单位: s ,指接收时间超前范围", required = false, dataType = "Integer") - @TableField(value = "LEADING_TIME", updateStrategy = FieldStrategy.NOT_EMPTY) + @ApiModelProperty(value = "接收超前时间,单位: s ,指接收时间超前范围", required = false, dataType = "Numeric") + @TableField(value = "LEADING_TIME", updateStrategy = FieldStrategy.IGNORED) @Max(10) private Integer leadingTime; @@ -121,8 +122,8 @@ public class ERule extends Model { * 接收滞后时间,单位: s ,指接收时间滞后范围 */ @ApiParam(value = "接收滞后时间,单位: s ,指接收时间滞后范围", required = false, example = "") - @ApiModelProperty(value = "接收滞后时间,单位: s ,指接收时间滞后范围", required = false, dataType = "Integer") - @TableField(value = "LAG_TIME", updateStrategy = FieldStrategy.NOT_EMPTY) + @ApiModelProperty(value = "接收滞后时间,单位: s ,指接收时间滞后范围", required = false, dataType = "Numeric") + @TableField(value = "LAG_TIME", updateStrategy = FieldStrategy.IGNORED) @Max(10) private Integer lagTime; diff --git a/src/main/java/com/whdc/valid/bean/IntvType.java b/src/main/java/com/whdc/valid/bean/IntvType.java new file mode 100644 index 0000000..4434f23 --- /dev/null +++ b/src/main/java/com/whdc/valid/bean/IntvType.java @@ -0,0 +1,72 @@ +package com.whdc.valid.bean; + +import com.alibaba.fastjson.JSONObject; +import org.apache.commons.lang3.StringUtils; + +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +/** + * + * @author xusan + * @date 2023年3月22日10:32:54 + * @description: 雨量值时间规则 + */ +public enum IntvType { + + IT001(300, "0.05"), + IT002(300, "0.08"), + IT003(3600, "1"); + + private Integer name; + private String value; + + public Integer getName() { + return name; + } + + public void setName(Integer name) { + this.name = name; + } + + public String getValue() { + return value; + } + + public void setValue(String value) { + this.value = value; + } + + IntvType(Integer name, String value) { + this.name = name; + this.value = value; + } + + public static List list() { + List list = new ArrayList<>(); + IntvType[] values = IntvType.values(); + for (IntvType type : values) { + JSONObject obj = new JSONObject(); + obj.put("name", type.getName()); + obj.put("value", type.getValue()); + list.add(obj); + } + return list; + } + + public static Map map() { + Map map = new HashMap<>(); + IntvType[] values = IntvType.values(); + for (IntvType e : values) { + map.put(e.getValue(), e); + } + return map; + } + + public static IntvType getByValue(String value) { + if (StringUtils.isEmpty(value)) return null; + return map().get(value); + } +} diff --git a/src/main/java/com/whdc/model/bean/ItemType.java b/src/main/java/com/whdc/valid/bean/ItemType.java similarity index 94% rename from src/main/java/com/whdc/model/bean/ItemType.java rename to src/main/java/com/whdc/valid/bean/ItemType.java index 4bf70a2..5eb7ba7 100644 --- a/src/main/java/com/whdc/model/bean/ItemType.java +++ b/src/main/java/com/whdc/valid/bean/ItemType.java @@ -1,4 +1,4 @@ -package com.whdc.model.bean; +package com.whdc.valid.bean; import com.alibaba.fastjson.JSONObject; import org.apache.commons.lang3.StringUtils; @@ -18,7 +18,8 @@ public enum ItemType { YU_LIANG("雨量", "P"), SHUI_WEI("水位", "Z"), - LIU_LIANG("流量", "Q"); + LIU_LIANG("流量", "Q"), + TONG_YONG("通用", "ALL"); private String name; private String value; diff --git a/src/main/java/com/whdc/valid/component/RuleRedisService.java b/src/main/java/com/whdc/valid/component/RuleRedisService.java index 1a37f7e..15b3292 100644 --- a/src/main/java/com/whdc/valid/component/RuleRedisService.java +++ b/src/main/java/com/whdc/valid/component/RuleRedisService.java @@ -10,6 +10,7 @@ import org.springframework.beans.factory.annotation.Autowired; import org.springframework.data.redis.core.RedisTemplate; import org.springframework.stereotype.Component; +import java.util.ArrayList; import java.util.List; import java.util.stream.Collectors; @@ -26,7 +27,9 @@ public class RuleRedisService { public List getRule(String stcd,ValidateMapper validateMapper) { - log.info("获取规则: " + stcd); + List rules = null; + + log.info("获取规则: {}" , stcd); String key = VALIDATE_REDIS_KEY + stcd; @@ -35,29 +38,48 @@ public class RuleRedisService { List json = ruleRedisTemplate.opsForList().range(key, 0, -1); if (CollectionUtils.isNotEmpty(json)) { - return - json.stream() - .filter(StringUtils::isNotBlank) - .map(s -> JSON.toJavaObject(JSON.parseObject(s), ValidateVo.class)) - .collect(Collectors.toList()); + + rules = json.stream() + .filter(StringUtils::isNotBlank) + .map(s -> JSON.toJavaObject(JSON.parseObject(s), ValidateVo.class)) + .collect(Collectors.toList()); + + } - } + }else{ + rules = validateMapper.getRuleByStcd(stcd); - List rules = validateMapper.getRuleByStcd(stcd); + log.info("保存规则: " + rules.size()); + if (CollectionUtils.isEmpty(rules)) { + rules = new ArrayList<>(); - log.info("保存规则: " + rules.size()); + // 添加空号缓存 + rules.add(new ValidateVo()); + } - if (CollectionUtils.isNotEmpty(rules)) { Long aLong = ruleRedisTemplate.opsForList().rightPushAll(key, rules.stream().map(JSON::toJSONString).collect(Collectors.toList())); if (aLong != rules.size()) { log.info("保存规则失败: " + key + "_" + rules.size()); } - } else { - ruleRedisTemplate.opsForList().rightPushAll(key, ""); } + + if (CollectionUtils.isNotEmpty(rules) && JSON.parseObject(JSON.toJSONString(rules.get(0))).isEmpty()){ + rules.remove(0); + } + + + if (CollectionUtils.isEmpty(rules)) { + // 查询通用规则 + return this.getRule("ALL", validateMapper); + + } + + log.info("获取规则条数: {}" , rules.size()); + + return rules; } diff --git a/src/main/java/com/whdc/valid/mapper/ValidateMapper.java b/src/main/java/com/whdc/valid/mapper/ValidateMapper.java index da68d8f..8065e03 100644 --- a/src/main/java/com/whdc/valid/mapper/ValidateMapper.java +++ b/src/main/java/com/whdc/valid/mapper/ValidateMapper.java @@ -17,8 +17,9 @@ public interface ValidateMapper { * @param stcd * @return */ - @Select("SELECT E.ITEM,E.MIN,E.MAX,E.DIFF_MAX,E.DURATION,E.LEADING_TIME,E.LAG_TIME,T.SORT FROM SHZH_IOT.E_STATION_RULES T LEFT JOIN SHZH_IOT.E_RULE E ON T.RULE_ID = E.ID WHERE T.DEL = 1 AND E.DEL = 1 AND T.STCD = #{stcd} ORDER BY T.SORT ASC;") + @Select("SELECT E.ITEM,E.MIN,E.MAX,E.DIFF_MAX,E.DURATION,E.LEADING_TIME,E.LAG_TIME,T.SORT FROM SHZH_IOT.E_STATION_RULES T LEFT JOIN SHZH_IOT.E_RULE E ON T.RULE_ID = E.ID WHERE T.DEL = 1 AND E.DEL = 1 AND T.STCD = #{stcd} ORDER BY T.SORT ASC;") List getRuleByStcd(String stcd); + } diff --git a/src/main/java/com/whdc/valid/model/ValidateDto.java b/src/main/java/com/whdc/valid/model/ValidateDto.java index eaefe1d..ef34d11 100644 --- a/src/main/java/com/whdc/valid/model/ValidateDto.java +++ b/src/main/java/com/whdc/valid/model/ValidateDto.java @@ -25,6 +25,9 @@ public class ValidateDto implements Serializable { @ApiModelProperty(value = "监测时间", dataType = "java.lang.Date") private Date tm; + @ApiModelProperty(value = "累计时间", dataType = "java.lang.Date") + private String intv; + private Data data; @lombok.Data diff --git a/src/main/java/com/whdc/valid/model/ValidateVo.java b/src/main/java/com/whdc/valid/model/ValidateVo.java index c218635..253d610 100644 --- a/src/main/java/com/whdc/valid/model/ValidateVo.java +++ b/src/main/java/com/whdc/valid/model/ValidateVo.java @@ -44,7 +44,7 @@ public class ValidateVo implements Serializable { private BigDecimal max; /** - * 最大值 + * 差值 */ @ApiParam(value = "两条数据之间的最大差值", required = false, example = "") @ApiModelProperty(value = "两条数据之间的最大差值", required = false, dataType = "String") diff --git a/src/main/java/com/whdc/valid/service/ValidateServiceImpl.java b/src/main/java/com/whdc/valid/service/ValidateServiceImpl.java index efbfada..1921fc8 100644 --- a/src/main/java/com/whdc/valid/service/ValidateServiceImpl.java +++ b/src/main/java/com/whdc/valid/service/ValidateServiceImpl.java @@ -3,7 +3,8 @@ package com.whdc.valid.service; import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSONException; import com.alibaba.fastjson.JSONObject; -import com.whdc.model.bean.ItemType; +import com.whdc.valid.bean.IntvType; +import com.whdc.valid.bean.ItemType; import com.whdc.valid.component.RuleRedisService; import com.whdc.valid.mapper.ValidateMapper; import com.whdc.valid.model.ValidateDto; @@ -20,7 +21,7 @@ import java.util.List; import java.util.Objects; import java.util.stream.Collectors; -import static com.whdc.model.bean.ItemType.getByValue; +import static com.whdc.valid.bean.ItemType.getByValue; import static com.whdc.valid.error.MyException.getException; @@ -128,7 +129,7 @@ public class ValidateServiceImpl implements ValidateService { continue; } - if (Objects.isNull(min) && Objects.isNull(max) && Objects.isNull(leadingTime) && Objects.isNull(lagTime)) { + if (Objects.isNull(min) && Objects.isNull(max)) { continue; } @@ -208,6 +209,9 @@ public class ValidateServiceImpl implements ValidateService { prevDto = getValidateDto(prevJson); // 老差值 } + + String intv = dto.getIntv(); + ValidateDto.Data data = dto.getData(); BigDecimal p = data.getP(); BigDecimal z = data.getZ(); @@ -235,6 +239,8 @@ public class ValidateServiceImpl implements ValidateService { for (ValidateVo vo : rule) { + Integer duration = vo.getDuration(); + BigDecimal min = vo.getMin(); BigDecimal max = vo.getMax(); Integer leadingTime = vo.getLeadingTime(); @@ -242,22 +248,23 @@ public class ValidateServiceImpl implements ValidateService { String item = vo.getItem(); ItemType itemType = getByValue(item); - if (Objects.nonNull(min) && Objects.nonNull(max) && Objects.nonNull(leadingTime) && Objects.nonNull(lagTime)) { - if (Objects.nonNull(leadingTime)) { - long time = currentTime + (leadingTime * 1000); - if (tm.getTime() > time) { - log.info("时间大于最大差值时间: {} > {}", tm.getTime(), time); - return false; - } + if (Objects.nonNull(leadingTime)) { + long time = currentTime + (leadingTime * 1000); + if (tm.getTime() > time) { + log.info("时间大于最大差值时间: {} > {}", tm.getTime(), time); + return false; } + } - if (Objects.nonNull(lagTime)) { - long time = currentTime - (lagTime * 1000); - if (tm.getTime() < time) { - log.info("时间小于最小差值时间: {} < {}", tm.getTime(), time); - return false; - } + if (Objects.nonNull(lagTime)) { + long time = currentTime - (lagTime * 1000); + if (tm.getTime() < time) { + log.info("时间小于最小差值时间: {} < {}", tm.getTime(), time); + return false; } + } + + if (Objects.nonNull(min) || Objects.nonNull(max)) { if (StringUtils.isBlank(item)) { @@ -272,8 +279,23 @@ public class ValidateServiceImpl implements ValidateService { switch (itemType) { case YU_LIANG: { // 雨量判断 P - if (!rangeRuleValidate(p, min, max)) { - return false; + + if (StringUtils.isNotBlank(intv) && Objects.nonNull(duration)){ + IntvType intvType = IntvType.getByValue(intv); + + if (Objects.nonNull(intvType)){ + if (Objects.equals(intvType.getName(), duration)) { + if (!rangeRuleValidate(p, min, max)) { + return false; + } + } + } + }else{ + + if (!rangeRuleValidate(p, min, max)) { + return false; + } + } break; @@ -321,7 +343,7 @@ public class ValidateServiceImpl implements ValidateService { Date oldTm = prevDto.getTm(); BigDecimal diffMax = vo.getDiffMax(); - Integer duration = vo.getDuration(); + if (Objects.nonNull(duration)) { @@ -334,6 +356,10 @@ public class ValidateServiceImpl implements ValidateService { continue; } + if (Objects.isNull(diffMax)) { + continue; + } + switch (itemType) { case YU_LIANG: { // 雨量判断 P if (Objects.nonNull(p) && Objects.nonNull(oldP) && Math.abs(p.subtract(oldP).doubleValue()) > diffMax.doubleValue()) { diff --git a/src/main/resources/application.yml b/src/main/resources/application.yml index b62f668..d77e740 100644 --- a/src/main/resources/application.yml +++ b/src/main/resources/application.yml @@ -6,12 +6,12 @@ spring: datasource: type: com.alibaba.druid.pool.DruidDataSource driver-class-name: dm.jdbc.driver.DmDriver -# url: jdbc:dm://127.0.0.1:5236?schema=SYSDBA -# username: SYSDBA -# password: 199999999 - url: jdbc:dm://10.42.6.247:5236?schema=SHZH - username: SHZH - password: Shzh_890 + url: jdbc:dm://127.0.0.1:5236?schema=SYSDBA + username: SYSDBA + password: 199999999 +# url: jdbc:dm://10.42.6.247:5236?schema=SHZH +# username: SHZH +# password: Shzh_890 druid: initialSize: 5 minIdle: 5 @@ -35,17 +35,17 @@ spring: # Redis redis: database: 5 - host: 10.42.6.75 -# host: 127.0.0.1 +# host: 10.42.6.75 + host: 127.0.0.1 port: 6379 -# password: - password: Whdc_890 + password: +# password: Whdc_890 # 数据清洗专用redis redisRules: database: 6 - host: 10.42.6.75 -# host: 127.0.0.1 +# host: 10.42.6.75 + host: 127.0.0.1 port: 6379 # password: Whdc_890 password: