Browse Source

新增模型1:案件警情疑似未及时受理案件

main
sjh 1 year ago
parent
commit
b028bdb644
  1. 6
      pom.xml
  2. 12
      src/main/java/com/biutag/supervision/job/Job.java
  3. 20
      src/main/java/com/biutag/supervision/mapper/ModelClueDataMapper.java
  4. 2
      src/main/java/com/biutag/supervision/pojo/entity/Model.java
  5. 7
      src/main/java/com/biutag/supervision/pojo/entity/ModelClue.java
  6. 4
      src/main/java/com/biutag/supervision/pojo/entity/ModelClueRecord.java
  7. 7
      src/main/java/com/biutag/supervision/service/BusinessDepartService.java
  8. 91
      src/main/java/com/biutag/supervision/service/ModelClueService.java
  9. 2
      src/test/java/com/biutag/supervision/SupervisionApplicationTests.java

6
pom.xml

@ -147,6 +147,12 @@
<version>1.2.22</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>2.0.53</version>
</dependency>
</dependencies>
<build>

12
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);
}
}

20
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<ModelClue> {
@Select("${sql}")
List<ModelClue> selectDataByDynamicSql(@org.apache.ibatis.annotations.Param("sql") String sql);
@Select("${newSql}")
List<Map<String, Object>> selectByUniqueKeys(@org.apache.ibatis.annotations.Param("newSql") String newSql);
}

2
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;
}

7
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;
}

4
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")

7
src/main/java/com/biutag/supervision/service/BusinessDepartService.java

@ -35,6 +35,8 @@ public class BusinessDepartService extends ServiceImpl<BusinessDepartMapper, Bus
private final SupDepartMapper supDepartMapper;
private final ModelClueService modelClueService;
public Page<BusinessPoliceModel> 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<BusinessDepartMapper, Bus
// happenTime.add(end);
// generate(happenTime);
// }
if (businessQueryParam.getDepartName() != null && businessQueryParam.getDepartName().equals("运行测试")) {
modelClueService.generate(7);
System.out.println("执行完毕");
return null;
}
QueryWrapper<BusinessPoliceModel> queryWrapper = new QueryWrapper<>();
queryWrapper
.like(StrUtil.isNotBlank(businessQueryParam.getDepartName()), "depart_name", businessQueryParam.getDepartName())

91
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<ModelClueMapper, ModelClue> {
public final SupDepartService departService;
public final ModelClueMapper modelClueMapper;
public final ModelClueDataMapper modelClueDataMapper;
public final ModelClueRecordMapper modelClueRecordMapper;
public final SupDepartMapper supDepartMapper;
public Page<ModelClueModel> page(ModelClueQueryParam param) {
LambdaQueryWrapper<ModelClue> queryWrapper = new LambdaQueryWrapper<ModelClue>()
.eq(Objects.nonNull(param.getInvolveDepartId()), ModelClue::getInvolveDepartId, param.getInvolveDepartId())
@ -130,4 +134,71 @@ public class ModelClueService extends ServiceImpl<ModelClueMapper, ModelClue> {
return true;
}
public void generate(Integer modelId) {
Model model = modelMapper.selectById(modelId);
List<ModelClue> 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<SupDepart>().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<ModelClue> getModelClueBySql(String sql) {
List<ModelClue> modelClues = modelClueDataMapper.selectDataByDynamicSql(sql);
List<String> uniqueKeys = modelClues.stream().map(ModelClue::getUniqueKey).filter(k -> k != null && !k.isEmpty()).toList();
String newSql = generateNewSql(sql, uniqueKeys);
List<Map<String, Object>> allData = modelClueDataMapper.selectByUniqueKeys(newSql);
String originalFieldName = getKeyFieldName(sql);
for (ModelClue modelClue : modelClues) {
for (Map<String, Object> 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<String> 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;
}
}

2
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 {

Loading…
Cancel
Save