Browse Source

fix: 模型逻辑调整

master
wxc 3 months ago
parent
commit
e9bba5e1c2
  1. 12
      src/main/java/com/biutag/supervisiondata/mapper/mine/AlarmNotificationMapper.java
  2. 155
      src/main/java/com/biutag/supervisiondata/pojo/entity/mine/AlarmNotification.java
  3. 2
      src/main/java/com/biutag/supervisiondata/pojo/entity/mine/Model.java
  4. 137
      src/main/java/com/biutag/supervisiondata/service/impl/ModelClueServiceImpl.java
  5. 24
      src/main/java/com/biutag/supervisiondata/task/TaskService.java

12
src/main/java/com/biutag/supervisiondata/mapper/mine/AlarmNotificationMapper.java

@ -0,0 +1,12 @@
package com.biutag.supervisiondata.mapper.mine;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.biutag.supervisiondata.pojo.entity.mine.AlarmNotification;
/**
* @author wxc
* @date 2025/10/22
*/
public interface AlarmNotificationMapper extends BaseMapper<AlarmNotification> {
}

155
src/main/java/com/biutag/supervisiondata/pojo/entity/mine/AlarmNotification.java

@ -0,0 +1,155 @@
package com.biutag.supervisiondata.pojo.entity.mine;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import com.fasterxml.jackson.annotation.JsonFormat;
import lombok.Data;
import java.io.Serializable;
import java.time.LocalDateTime;
/**
* 预警通知
* @author kami on 2024-11-16 10:51:18
* @version 0.0.1
* @since 1.8
*/
@Data
@TableName("`alarm_notification`")
public class AlarmNotification implements Serializable {
/**
* 主键
*/
@TableId(value = "id", type = IdType.AUTO)
private Integer id;
/**
* 预警时间
*/
@TableField("`alarm_time`")
@JsonFormat(pattern = "yyyy-MM-dd HH:mm" , timezone = "GMT+8")
LocalDateTime alarmTime;
@TableField("`alarm_type_id`")
Long alarmTypeId;
/**
* 预警类型
*/
@TableField("`alarm_type`")
String alarmType;
/**
* 被通知单位ID
*/
@TableField("`notification_depart_id`")
String notificationDepartId;
/**
* 被通知单位机构名称
*/
@TableField("`notification_depart_name`")
String notificationDepartName;
/**
* 通知内容
*/
@TableField("`alarm_content`")
String alarmContent;
/**
* 回复状态0未回复1已回复
*/
@TableField("`reply_state`")
Integer replyState;
/**
* 回复情况内容
*/
@TableField("`reply_result_content`")
String replyResultContent;
/**
* 线索id
*/
@TableField("`clue_id`")
Integer clueId;
/**
* 民警证件号
*/
String policeIdCode;
/**
* 民警姓名
*/
String policeName;
/**
* 警号
*/
String policeNo;
/**
* 创建时间
*/
@TableField("`crt_time`")
LocalDateTime crtTime;
/**
* 创建用户
*/
@TableField("`crt_user`")
String crtUser;
/**
* 创建单位
*/
@TableField("`crt_depart_id`")
String crtDepartId;
/**
* 创建人名称
*/
@TableField("`crt_name`")
String crtName;
/**
* 创建ip
*/
@TableField("`crt_host`")
String crtHost;
/**
* 创建用户
*/
@TableField("`upd_user`")
String updUser;
/**
* 创建单位
*/
@TableField("`upd_depart_id`")
String updDepartId;
/**
* 创建人名称
*/
@TableField("`upd_name`")
String updName;
/**
* 创建ip
*/
@TableField("`upd_host`")
String updHost;
/**
* 修改时间
*/
@TableField("`upd_time`")
LocalDateTime updTime;
Integer del;
// 是否要求回复
private Boolean requestReply;
// 通知回复时限
private Integer replyLimit;
private Integer modelId;
}

2
src/main/java/com/biutag/supervisiondata/pojo/entity/mine/Model.java

@ -86,7 +86,7 @@ public class Model {
// 建模方式 // 建模方式
private String modelingMethod; private String modelingMethod;
// 模型数据类型 // 预警类型
private String modelDataType; private String modelDataType;
@TableField("model_sql") @TableField("model_sql")

137
src/main/java/com/biutag/supervisiondata/service/impl/ModelClueServiceImpl.java

@ -1,5 +1,7 @@
package com.biutag.supervisiondata.service.impl; package com.biutag.supervisiondata.service.impl;
import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson2.JSON; import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONObject; import com.alibaba.fastjson2.JSONObject;
import com.baomidou.mybatisplus.core.conditions.Wrapper; import com.baomidou.mybatisplus.core.conditions.Wrapper;
@ -8,6 +10,7 @@ import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper; import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers; import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.biutag.supervisiondata.mapper.dwd.ModelClueDataMapper; import com.biutag.supervisiondata.mapper.dwd.ModelClueDataMapper;
import com.biutag.supervisiondata.mapper.mine.AlarmNotificationMapper;
import com.biutag.supervisiondata.mapper.mine.ModelClueMapper; import com.biutag.supervisiondata.mapper.mine.ModelClueMapper;
import com.biutag.supervisiondata.mapper.wdpc2.ModelClueWdpc2Mapper; import com.biutag.supervisiondata.mapper.wdpc2.ModelClueWdpc2Mapper;
import com.biutag.supervisiondata.pojo.entity.mine.*; import com.biutag.supervisiondata.pojo.entity.mine.*;
@ -59,11 +62,16 @@ public class ModelClueServiceImpl implements ModelClueService {
private final ModelClueDataDMRepository modelClueDataDMRepository; private final ModelClueDataDMRepository modelClueDataDMRepository;
private final AlarmNotificationMapper alarmNotificationMapper;
private final SupDepartRepository departRepository;
public void generate(Integer modelId) { public void generate(Integer modelId) {
if (modelId == 1300) { if (modelId == 1300) {
updateDepart(); updateDepart();
return; return;
} }
try {
Model model = modelRepository.getById(modelId); Model model = modelRepository.getById(modelId);
List<ModelClue> modelClues = new ArrayList<>(); List<ModelClue> modelClues = new ArrayList<>();
if (!Objects.isNull(model) && model.getModelingMethod().equals("2")) { if (!Objects.isNull(model) && model.getModelingMethod().equals("2")) {
@ -71,27 +79,116 @@ public class ModelClueServiceImpl implements ModelClueService {
} else if (!Objects.isNull(model) && model.getModelSql() != null) { } else if (!Objects.isNull(model) && model.getModelSql() != null) {
modelClues.addAll(getModelClueBySql(model.getModelSql(), modelId, null)); modelClues.addAll(getModelClueBySql(model.getModelSql(), modelId, null));
} }
System.out.println("模型结果1数量:" + modelClues.size()); log.info("模型线索数量:" + modelClues.size());
// 过滤掉单位为空的数据
modelClues = modelClues.stream().filter(item -> StrUtil.isNotBlank(item.getInvolveDepartId())).toList();
modelClues.forEach(item -> {
item.setModelId(modelId);
item.setCreateTime(LocalDateTime.now());
});
log.info("模型线索数量(过滤单位为空的数据):" + modelClues.size());
int size;
log.info("模型的预警类型为 {}", model.getModelDataType());
// 提醒通知
if ("2".equals(model.getModelDataType())) {
// 保存提醒通知
size = addAlarmNotification(modelClues, model);
}
// 模型线索
else {
if (!modelClues.isEmpty()) { if (!modelClues.isEmpty()) {
List<ModelClue> result = new ArrayList<>(); modelClueMapper.insert(modelClues);
for (ModelClue modelClue : modelClues) {
modelClue.setModelId(modelId);
modelClue.setCreateTime(LocalDateTime.now());
if (modelClue.getInvolveDepartName() != null && !modelClue.getInvolveDepartName().isEmpty()) {
result.add(modelClue);
}
} }
System.out.println("模型结果2数量:" + modelClues.size()); size = modelClues.size();
modelClueMapper.insert(result);
modelClueRecordRepository.getBaseMapper().insert(new ModelClueRecord().setModelId(modelId).setSize(result.size()).setCreateTime(LocalDateTime.now()).setState("success"));
} else {
modelClueRecordRepository.getBaseMapper().insert(new ModelClueRecord().setModelId(modelId).setSize(0).setCreateTime(LocalDateTime.now()).setState("fail").setErrMsg("数据为空"));
} }
if (Objects.nonNull(model)) { log.info("模型结果数据 入库数量:" + size);
modelClueRecordRepository.getBaseMapper().insert(new ModelClueRecord().setModelId(modelId).setSize(size).setCreateTime(LocalDateTime.now()).setState("success"));
model.setLatestRunTime(LocalDateTime.now()); model.setLatestRunTime(LocalDateTime.now());
modelRepository.updateById(model); modelRepository.updateById(model);
} catch (RuntimeException e) {
log.error(e.getMessage(), e);
modelClueRecordRepository.getBaseMapper().insert(new ModelClueRecord().setModelId(modelId).setSize(0).setCreateTime(LocalDateTime.now()).setState("fail").setErrMsg(e.getMessage()));
}
} }
public int addAlarmNotification(List<ModelClue> modelClues, Model model) {
if (modelClues.isEmpty()) {
return 0;
}
List<AlarmNotification> list = new ArrayList<>();
for (ModelClue item : modelClues) {
AlarmNotification notification = new AlarmNotification();
notification.setAlarmTime(LocalDateTime.now());
notification.setAlarmTypeId(3L);
notification.setAlarmType("提醒通知");
notification.setNotificationDepartId(item.getInvolveDepartId());
notification.setNotificationDepartName(item.getInvolveDepartName());
notification.setAlarmContent(item.getThingDesc());
notification.setReplyState(0);
notification.setModelId(model.getId());
notification.setDel(0);
list.add(notification);
}
log.info("模型的提醒通知单位类型为 {}", model.getHandleDepartType());
if ("1".equals(model.getHandleDepartType())) {
alarmNotificationMapper.insert(list);
return list.size();
}
if ("2".equals(model.getHandleDepartType())) {
SupDepart depart = departRepository.getById(model.getHandleDepartId());
list.forEach(item -> {
item.setNotificationDepartId(depart.getId());
item.setNotificationDepartName(depart.getShortName());
});
alarmNotificationMapper.insert(list);
return list.size();
}
// 问题涉及单位+指定单位
if ("3".equals(model.getHandleDepartType())) {
alarmNotificationMapper.insert(list);
int size = list.size();
for (AlarmNotification notification : list) {
SupDepart depart = departRepository.getById(notification.getNotificationDepartId());
// 法制 17、督审 18、警保 19
List<Integer> groups = List.of(17, 18, 19);
if (depart.getLevel() == 2) {
List<SupDepart> departs = departRepository.list(new LambdaQueryWrapper<SupDepart>()
.eq(SupDepart::getPid, notification.getId())
.in(SupDepart::getStatisticsGroupId, groups));
if (!departs.isEmpty()) {
for (SupDepart d : departs) {
AlarmNotification n = new AlarmNotification();
BeanUtil.copyProperties(notification, n);
n.setId(null);
n.setNotificationDepartId(d.getId());
n.setNotificationDepartName(d.getShortName());
alarmNotificationMapper.insert(n);
size += 1;
}
}
}
if (depart.getLevel() == 3) {
List<SupDepart> departs = departRepository.list(new LambdaQueryWrapper<SupDepart>()
.eq(SupDepart::getPid, depart.getPid())
.in(SupDepart::getStatisticsGroupId, groups));
if (!departs.isEmpty()) {
for (SupDepart d : departs) {
AlarmNotification n = new AlarmNotification();
BeanUtil.copyProperties(notification, n);
n.setId(null);
n.setNotificationDepartId(d.getId());
n.setNotificationDepartName(d.getShortName());
alarmNotificationMapper.insert(n);
size += 1;
}
}
}
return size;
}
}
return 0;
} }
public void generateCustomModels() { public void generateCustomModels() {
@ -176,7 +273,7 @@ public class ModelClueServiceImpl implements ModelClueService {
oldUniqueKeys = modelClueRepository.listObjs(new LambdaQueryWrapper<ModelClue>().select(ModelClue::getUniqueKey).ne(ModelClue::getDistributionState, "2").eq(ModelClue::getModelId, modelId), String::valueOf); oldUniqueKeys = modelClueRepository.listObjs(new LambdaQueryWrapper<ModelClue>().select(ModelClue::getUniqueKey).ne(ModelClue::getDistributionState, "2").eq(ModelClue::getModelId, modelId), String::valueOf);
List<String> changeStatusIds = oldUniqueKeys.stream().filter(item -> !uniqueKeys.contains(item)).toList(); List<String> changeStatusIds = oldUniqueKeys.stream().filter(item -> !uniqueKeys.contains(item)).toList();
if (!changeStatusIds.isEmpty()) { if (!changeStatusIds.isEmpty()) {
modelClueMapper.update(null, new LambdaUpdateWrapper<ModelClue>().in(ModelClue::getUniqueKey, changeStatusIds).set(ModelClue::getDistributionState, "2")); //modelClueMapper.update(null, new LambdaUpdateWrapper<ModelClue>().in(ModelClue::getUniqueKey, changeStatusIds).set(ModelClue::getDistributionState, "2"));
} }
} }
List<String> needsInsertIds = new ArrayList<>(); List<String> needsInsertIds = new ArrayList<>();
@ -246,6 +343,10 @@ public class ModelClueServiceImpl implements ModelClueService {
} }
} }
thingDescGeneration = String.join("", thingDescGenerationSplit); thingDescGeneration = String.join("", thingDescGenerationSplit);
// 如果已存在,则跳过
if (modelClueMapper.exists(new LambdaQueryWrapper<ModelClue>().eq(ModelClue::getThingDesc, thingDescGeneration))) {
continue;
}
SupExternalDepart supExternalDepart = supExternalDepartRepository.getOne(new LambdaQueryWrapper<SupExternalDepart>().eq(SupExternalDepart::getExternalId, Optional.ofNullable(map.get(modelGeneration.getInvolveDepartId())) SupExternalDepart supExternalDepart = supExternalDepartRepository.getOne(new LambdaQueryWrapper<SupExternalDepart>().eq(SupExternalDepart::getExternalId, Optional.ofNullable(map.get(modelGeneration.getInvolveDepartId()))
.map(Object::toString).orElse("")).eq(SupExternalDepart::getSource, model.getClueDepartSource()).last("LIMIT 1")); .map(Object::toString).orElse("")).eq(SupExternalDepart::getSource, model.getClueDepartSource()).last("LIMIT 1"));
String involveDepartName = ""; String involveDepartName = "";
@ -281,7 +382,7 @@ public class ModelClueServiceImpl implements ModelClueService {
List<String> oldUniqueKeys = modelClueRepository.listObjs(new LambdaQueryWrapper<ModelClue>().select(ModelClue::getUniqueKey).ne(ModelClue::getDistributionState, "2").eq(ModelClue::getModelId, modelId), String::valueOf); List<String> oldUniqueKeys = modelClueRepository.listObjs(new LambdaQueryWrapper<ModelClue>().select(ModelClue::getUniqueKey).ne(ModelClue::getDistributionState, "2").eq(ModelClue::getModelId, modelId), String::valueOf);
List<String> changeStatusIds = oldUniqueKeys.stream().filter(item -> !uniqueKeys.contains(item)).toList(); List<String> changeStatusIds = oldUniqueKeys.stream().filter(item -> !uniqueKeys.contains(item)).toList();
if (!changeStatusIds.isEmpty()) { if (!changeStatusIds.isEmpty()) {
modelClueMapper.update(null, new LambdaUpdateWrapper<ModelClue>().in(ModelClue::getUniqueKey, changeStatusIds).set(ModelClue::getDistributionState, "2")); //modelClueMapper.update(null, new LambdaUpdateWrapper<ModelClue>().in(ModelClue::getUniqueKey, changeStatusIds).set(ModelClue::getDistributionState, "2"));
} }
List<String> needsInsertIds = new ArrayList<>(); List<String> needsInsertIds = new ArrayList<>();
for (String uniqueKey : uniqueKeys) { for (String uniqueKey : uniqueKeys) {
@ -913,7 +1014,7 @@ public class ModelClueServiceImpl implements ModelClueService {
private void getModelCluesForModel20(String param, List<ModelClue> modelClues) { private void getModelCluesForModel20(String param, List<ModelClue> modelClues) {
System.out.println("模型20开始查询数据库"); System.out.println("模型20开始查询数据库");
// 定时任务 // 定时任务
String sql = "select * from dwd_qb_nbgl_lxqtscsygxjl where STR_TO_DATE(tjrq, '%Y-%m-%d') = (CURDATE() - INTERVAL 1 DAY);"; String sql = "select * from dwd_qb_nbgl_lxqtscsygxjl where STR_TO_DATE(tjrq, '%Y-%m-%d') = (CURDATE() - INTERVAL 3 DAY);";
List<Map<String, Object>> sqlList = modelClueDataMapper.selectByUniqueKeys(sql); List<Map<String, Object>> sqlList = modelClueDataMapper.selectByUniqueKeys(sql);
System.out.println("模型20查询数据库完毕,结果数量:" + sqlList.size()); System.out.println("模型20查询数据库完毕,结果数量:" + sqlList.size());
if (sqlList.isEmpty()) { if (sqlList.isEmpty()) {
@ -943,7 +1044,7 @@ public class ModelClueServiceImpl implements ModelClueService {
private void getModelCluesForModel22(String param, List<ModelClue> modelClues) { private void getModelCluesForModel22(String param, List<ModelClue> modelClues) {
System.out.println("模型22开始查询数据库"); System.out.println("模型22开始查询数据库");
// 定时任务 // 定时任务
String sql = "select * from \"JSDZ_4GDB\".\"v_anomaly_statistics\" where \"CREATETIME\" = \"" + getYesterdayString() + "\""; String sql = "select * from \"JSDZ_4GDB\".\"v_anomaly_statistics\" where CREATETIME = '" + getYesterdayString() + "'";
List<Map<String, Object>> sqlList = modelClueDataDMRepository.selectByUniqueKeys(sql); List<Map<String, Object>> sqlList = modelClueDataDMRepository.selectByUniqueKeys(sql);
System.out.println("模型22查询数据库完毕,结果数量:" + sqlList.size()); System.out.println("模型22查询数据库完毕,结果数量:" + sqlList.size());
if (sqlList.isEmpty()) { if (sqlList.isEmpty()) {

24
src/main/java/com/biutag/supervisiondata/task/TaskService.java

@ -161,6 +161,13 @@ public class TaskService {
log.info("模型3执行完毕"); log.info("模型3执行完毕");
} }
@Scheduled(cron = "0 30 3 * * *")
public void runModel7() {
log.info("开始执行模型7");
modelClueService.generate(7);
log.info("模型7执行完毕");
}
@Scheduled(cron = "0 31 3 * * *") @Scheduled(cron = "0 31 3 * * *")
public void runModel10() { public void runModel10() {
log.info("开始执行模型10"); log.info("开始执行模型10");
@ -176,27 +183,34 @@ public class TaskService {
} }
@Scheduled(cron = "0 33 3 * * *") @Scheduled(cron = "0 33 3 * * *")
public void runModel16() {
log.info("开始执行模型16");
modelClueService.generate(15);
log.info("模型16执行完毕");
}
@Scheduled(cron = "0 34 3 * * *")
public void runModel17() { public void runModel17() {
log.info("开始执行模型17"); log.info("开始执行模型17");
modelClueService.generate(17); modelClueService.generate(17);
log.info("模型17执行完毕"); log.info("模型17执行完毕");
} }
@Scheduled(cron = "0 34 3 * * *") @Scheduled(cron = "0 35 3 * * *")
public void runModel19() { public void runModel19() {
log.info("开始执行模型19"); log.info("开始执行模型19");
modelClueService.generate(19); modelClueService.generate(19);
log.info("模型19执行完毕"); log.info("模型19执行完毕");
} }
@Scheduled(cron = "0 35 3 * * *") @Scheduled(cron = "0 36 3 * * *")
public void runModel20() { public void runModel20() {
log.info("开始执行模型20"); log.info("开始执行模型20");
modelClueService.generate(20); modelClueService.generate(20);
log.info("模型20执行完毕"); log.info("模型20执行完毕");
} }
@Scheduled(cron = "0 36 3 * * *") @Scheduled(cron = "0 37 3 * * *")
public void runModel22To29() { public void runModel22To29() {
for (int i = 22; i <= 29; i++) { for (int i = 22; i <= 29; i++) {
log.info("开始执行模型{}", i); log.info("开始执行模型{}", i);
@ -244,9 +258,9 @@ public class TaskService {
} }
/** /**
* 4点更新自建模型 * 每天8点30更新自建模型
*/ */
@Scheduled(cron = "0 0 4 * * ?") @Scheduled(cron = "0 30 8 * * ?")
public void runCustomModels() { public void runCustomModels() {
log.info("runCustomModels-------------------"); log.info("runCustomModels-------------------");
modelClueService.generateCustomModels(); modelClueService.generateCustomModels();

Loading…
Cancel
Save