From b028bdb644aedbbe1f47e457d74bdea39028d3ff Mon Sep 17 00:00:00 2001 From: sjh Date: Fri, 18 Oct 2024 16:13:27 +0800 Subject: [PATCH] =?UTF-8?q?=E6=96=B0=E5=A2=9E=E6=A8=A1=E5=9E=8B1=EF=BC=9A?= =?UTF-8?q?=E6=A1=88=E4=BB=B6=E8=AD=A6=E6=83=85=E7=96=91=E4=BC=BC=E6=9C=AA?= =?UTF-8?q?=E5=8F=8A=E6=97=B6=E5=8F=97=E7=90=86=E6=A1=88=E4=BB=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pom.xml | 6 ++ .../java/com/biutag/supervision/job/Job.java | 12 ++- .../mapper/ModelClueDataMapper.java | 20 ++++ .../biutag/supervision/pojo/entity/Model.java | 2 + .../supervision/pojo/entity/ModelClue.java | 7 ++ .../pojo/entity/ModelClueRecord.java | 4 +- .../service/BusinessDepartService.java | 7 ++ .../supervision/service/ModelClueService.java | 91 +++++++++++++++++-- .../SupervisionApplicationTests.java | 2 - 9 files changed, 134 insertions(+), 17 deletions(-) create mode 100644 src/main/java/com/biutag/supervision/mapper/ModelClueDataMapper.java diff --git a/pom.xml b/pom.xml index 4bd908d..8b4e47c 100644 --- a/pom.xml +++ b/pom.xml @@ -147,6 +147,12 @@ 1.2.22 + + com.alibaba + fastjson + 2.0.53 + + diff --git a/src/main/java/com/biutag/supervision/job/Job.java b/src/main/java/com/biutag/supervision/job/Job.java index d3070ae..28d13d5 100644 --- a/src/main/java/com/biutag/supervision/job/Job.java +++ b/src/main/java/com/biutag/supervision/job/Job.java @@ -5,10 +5,7 @@ import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper; import com.biutag.supervision.constants.enums.ProcessingStatusEnum; import com.biutag.supervision.pojo.entity.Negative; import com.biutag.supervision.pojo.entity.SupDepart; -import com.biutag.supervision.service.BusinessDepartService; -import com.biutag.supervision.service.BusinessPoliceService; -import com.biutag.supervision.service.NegativeService; -import com.biutag.supervision.service.SupDepartService; +import com.biutag.supervision.service.*; import com.biutag.supervision.util.TimeUtil; import lombok.RequiredArgsConstructor; import org.springframework.scheduling.annotation.Scheduled; @@ -32,6 +29,8 @@ public class Job { private final BusinessDepartService businessDepartService; + private final ModelClueService modelClueService; + // 10分钟 // 更新流程状态 @Scheduled(fixedRate = 600000) @@ -91,4 +90,9 @@ public class Job { businessDepartService.generate(happenTime); businessPoliceService.generate(happenTime); } + + @Scheduled(cron = "0 0 5 * * ?") + public void executeModel() { + modelClueService.generate(7); + } } diff --git a/src/main/java/com/biutag/supervision/mapper/ModelClueDataMapper.java b/src/main/java/com/biutag/supervision/mapper/ModelClueDataMapper.java new file mode 100644 index 0000000..16adac3 --- /dev/null +++ b/src/main/java/com/biutag/supervision/mapper/ModelClueDataMapper.java @@ -0,0 +1,20 @@ +package com.biutag.supervision.mapper; + +import com.baomidou.dynamic.datasource.annotation.DS; +import com.baomidou.mybatisplus.core.mapper.BaseMapper; +import com.biutag.supervision.pojo.entity.ModelClue; +import org.apache.ibatis.annotations.Select; + +import java.util.List; +import java.util.Map; + +@DS("slave") +public interface ModelClueDataMapper extends BaseMapper { + + @Select("${sql}") + List selectDataByDynamicSql(@org.apache.ibatis.annotations.Param("sql") String sql); + + @Select("${newSql}") + List> selectByUniqueKeys(@org.apache.ibatis.annotations.Param("newSql") String newSql); + +} diff --git a/src/main/java/com/biutag/supervision/pojo/entity/Model.java b/src/main/java/com/biutag/supervision/pojo/entity/Model.java index e6b8f56..aa66988 100644 --- a/src/main/java/com/biutag/supervision/pojo/entity/Model.java +++ b/src/main/java/com/biutag/supervision/pojo/entity/Model.java @@ -89,4 +89,6 @@ public class Model { // 模型数据类型 private String modelDataType; + @TableField("model_sql") + private String modelSql; } \ No newline at end of file diff --git a/src/main/java/com/biutag/supervision/pojo/entity/ModelClue.java b/src/main/java/com/biutag/supervision/pojo/entity/ModelClue.java index 985eae4..4c13b6b 100644 --- a/src/main/java/com/biutag/supervision/pojo/entity/ModelClue.java +++ b/src/main/java/com/biutag/supervision/pojo/entity/ModelClue.java @@ -24,12 +24,15 @@ public class ModelClue { @TableField("involve_depart_name") private String involveDepartName; + @TableField("involve_depart_id") private String involveDepartId; // 涉及人员 + @TableField("involve_depart_name") private String involvePoliceName; // 涉及人员警号 + @TableField("involve_police_emp_no") private String involvePoliceEmpNo; // 预警内容 @@ -43,9 +46,13 @@ public class ModelClue { @TableField("create_time") private LocalDateTime createTime; + @TableField("update_time") private LocalDateTime updateTime; // 数据详情 JSON private String data; + @TableField(exist = false) + private String uniqueKey; + } \ No newline at end of file diff --git a/src/main/java/com/biutag/supervision/pojo/entity/ModelClueRecord.java b/src/main/java/com/biutag/supervision/pojo/entity/ModelClueRecord.java index b3ca285..159f355 100644 --- a/src/main/java/com/biutag/supervision/pojo/entity/ModelClueRecord.java +++ b/src/main/java/com/biutag/supervision/pojo/entity/ModelClueRecord.java @@ -5,9 +5,11 @@ import com.baomidou.mybatisplus.annotation.TableField; import com.baomidou.mybatisplus.annotation.TableId; import lombok.Getter; import lombok.Setter; +import lombok.experimental.Accessors; import java.time.LocalDateTime; +@Accessors(chain = true) @Setter @Getter public class ModelClueRecord { @@ -17,7 +19,7 @@ public class ModelClueRecord { private Integer id; // 模型ID - private String modelId; + private Integer modelId; // 条数 @TableField("size") diff --git a/src/main/java/com/biutag/supervision/service/BusinessDepartService.java b/src/main/java/com/biutag/supervision/service/BusinessDepartService.java index 9a1c6ae..edab6d1 100644 --- a/src/main/java/com/biutag/supervision/service/BusinessDepartService.java +++ b/src/main/java/com/biutag/supervision/service/BusinessDepartService.java @@ -35,6 +35,8 @@ public class BusinessDepartService extends ServiceImpl page(BusinessQueryParam businessQueryParam) { // if (businessQueryParam.getDepartName()!=null && businessQueryParam.getDepartName().equals("开始导入今年所有的数据")) { // LocalDate start = LocalDate.parse("2024-05-23"); @@ -56,6 +58,11 @@ public class BusinessDepartService extends ServiceImpl queryWrapper = new QueryWrapper<>(); queryWrapper .like(StrUtil.isNotBlank(businessQueryParam.getDepartName()), "depart_name", businessQueryParam.getDepartName()) diff --git a/src/main/java/com/biutag/supervision/service/ModelClueService.java b/src/main/java/com/biutag/supervision/service/ModelClueService.java index c4523dd..f3d5246 100644 --- a/src/main/java/com/biutag/supervision/service/ModelClueService.java +++ b/src/main/java/com/biutag/supervision/service/ModelClueService.java @@ -2,17 +2,14 @@ package com.biutag.supervision.service; import cn.hutool.core.bean.BeanUtil; import cn.hutool.core.util.StrUtil; +import com.alibaba.fastjson.JSONObject; import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; import com.baomidou.mybatisplus.extension.plugins.pagination.Page; import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; import com.biutag.supervision.constants.enums.*; -import com.biutag.supervision.mapper.ModelClueMapper; -import com.biutag.supervision.mapper.ModelMapper; +import com.biutag.supervision.mapper.*; import com.biutag.supervision.pojo.dto.NegativeDto; -import com.biutag.supervision.pojo.entity.Model; -import com.biutag.supervision.pojo.entity.ModelClue; -import com.biutag.supervision.pojo.entity.ModelClueTask; -import com.biutag.supervision.pojo.entity.SupDepart; +import com.biutag.supervision.pojo.entity.*; import com.biutag.supervision.pojo.model.ModelClueModel; import com.biutag.supervision.pojo.param.ModelClueQueryParam; import lombok.RequiredArgsConstructor; @@ -20,10 +17,9 @@ import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import java.time.LocalDateTime; -import java.util.ArrayList; -import java.util.List; -import java.util.Objects; -import java.util.Set; +import java.util.*; +import java.util.regex.Matcher; +import java.util.regex.Pattern; import java.util.stream.Collectors; @RequiredArgsConstructor @@ -38,6 +34,14 @@ public class ModelClueService extends ServiceImpl { public final SupDepartService departService; + public final ModelClueMapper modelClueMapper; + + public final ModelClueDataMapper modelClueDataMapper; + + public final ModelClueRecordMapper modelClueRecordMapper; + + public final SupDepartMapper supDepartMapper; + public Page page(ModelClueQueryParam param) { LambdaQueryWrapper queryWrapper = new LambdaQueryWrapper() .eq(Objects.nonNull(param.getInvolveDepartId()), ModelClue::getInvolveDepartId, param.getInvolveDepartId()) @@ -130,4 +134,71 @@ public class ModelClueService extends ServiceImpl { return true; } + public void generate(Integer modelId) { + Model model = modelMapper.selectById(modelId); + List modelClues = new ArrayList<>(); + if (!Objects.isNull(model)) { + if (model.getModelSql() != null) { + modelClues.addAll(getModelClueBySql(model.getModelSql())); + } + } + if (!modelClues.isEmpty()) { + for (ModelClue modelClue : modelClues) { + modelClue.setModelId(modelId); + modelClue.setCreateTime(LocalDateTime.now()); + SupDepart supDepart = supDepartMapper.selectOne(new LambdaQueryWrapper().eq(SupDepart::getCode, modelClue.getInvolveDepartId())); + if (supDepart != null && supDepart.getShortName() != null && !supDepart.getShortName().isEmpty()) { + modelClue.setInvolveDepartName(supDepart.getShortName()); + modelClue.setInvolveDepartId(supDepart.getId()); + } + } + modelClueMapper.insert(modelClues); + modelClueRecordMapper.insert(new ModelClueRecord().setModelId(modelId).setSize(modelClues.size()).setCreateTime(LocalDateTime.now()).setState("success")); + } else { + modelClueRecordMapper.insert(new ModelClueRecord().setModelId(modelId).setSize(0).setCreateTime(LocalDateTime.now()).setState("fail").setErrMsg("数据为空")); + } + } + + public List getModelClueBySql(String sql) { + List modelClues = modelClueDataMapper.selectDataByDynamicSql(sql); + List uniqueKeys = modelClues.stream().map(ModelClue::getUniqueKey).filter(k -> k != null && !k.isEmpty()).toList(); + String newSql = generateNewSql(sql, uniqueKeys); + List> allData = modelClueDataMapper.selectByUniqueKeys(newSql); + String originalFieldName = getKeyFieldName(sql); + for (ModelClue modelClue : modelClues) { + for (Map data : allData) { + String keyColumn = (String) data.get(originalFieldName); + if (Objects.equals(keyColumn, modelClue.getUniqueKey())) { + modelClue.setData(JSONObject.toJSONString(data)); + break; + } + } + } + return modelClues; + } + + public static String generateNewSql(String originalSql, List uniqueKeys) { + Pattern tablePattern = Pattern.compile("FROM\\s+(\\w+)", Pattern.CASE_INSENSITIVE); + Matcher tableMatcher = tablePattern.matcher(originalSql); + String tableName = ""; + if (tableMatcher.find()) { + tableName = tableMatcher.group(1); + } + String originalFieldName = getKeyFieldName(originalSql); + return "SELECT * FROM " + tableName + " " + "WHERE " + originalFieldName + " IN " + "(" + uniqueKeys.stream().map(k -> "'" + k + "'").collect(Collectors.joining(",")) + ");"; + } + + private static String getKeyFieldName(String originalSql) { + String patternString = "SELECT\\s+(\\w+)\\s+AS\\s+" + Pattern.quote("uniqueKey"); + Pattern uniqueKeyPattern = Pattern.compile(patternString, Pattern.CASE_INSENSITIVE); + Matcher uniqueKeyMatcher = uniqueKeyPattern.matcher(originalSql); + String originalFieldName = ""; + if (uniqueKeyMatcher.find()) { + originalFieldName = uniqueKeyMatcher.group(1); + } + if (originalFieldName.isEmpty()) { + throw new RuntimeException("未找到唯一键字段名"); + } + return originalFieldName; + } } diff --git a/src/test/java/com/biutag/supervision/SupervisionApplicationTests.java b/src/test/java/com/biutag/supervision/SupervisionApplicationTests.java index 77c70dc..e056ff1 100644 --- a/src/test/java/com/biutag/supervision/SupervisionApplicationTests.java +++ b/src/test/java/com/biutag/supervision/SupervisionApplicationTests.java @@ -5,13 +5,11 @@ import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.scheduling.TaskScheduler; -import org.springframework.scheduling.Trigger; import org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler; import org.springframework.scheduling.config.ScheduledTaskRegistrar; import org.springframework.scheduling.support.CronTrigger; import java.time.LocalDateTime; -import java.util.Date; @SpringBootTest class SupervisionApplicationTests {