完整版
This commit is contained in:
parent
c30da6fcc3
commit
e14db87753
@ -3,6 +3,7 @@ package com.shgx.web.controller.dryingroom;
|
||||
import java.util.List;
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
|
||||
import com.shgx.dryingroom.collect.AutoCollectService;
|
||||
import com.shgx.dryingroom.web.domain.DryEquipmentInfo;
|
||||
import com.shgx.dryingroom.web.domain.dto.ParamThresholdDTO;
|
||||
import com.shgx.dryingroom.web.service.IDryEquipmentInfoService;
|
||||
@ -40,6 +41,9 @@ public class DryEquipmentInfoController extends BaseController
|
||||
@Autowired
|
||||
private IDryEquipmentInfoService dryEquipmentInfoService;
|
||||
|
||||
@Autowired
|
||||
private AutoCollectService autoCollectService;
|
||||
|
||||
/**
|
||||
* 查询设备信息列表
|
||||
*/
|
||||
@ -83,7 +87,9 @@ public class DryEquipmentInfoController extends BaseController
|
||||
@PostMapping
|
||||
public AjaxResult add(@RequestBody DryEquipmentInfo dryEquipmentInfo)
|
||||
{
|
||||
return toAjax(dryEquipmentInfoService.insertDryEquipmentInfo(dryEquipmentInfo));
|
||||
int result = dryEquipmentInfoService.insertDryEquipmentInfo(dryEquipmentInfo);
|
||||
autoCollectService.manualPushMqttDataWithStatusNormal();
|
||||
return toAjax(result);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -94,7 +100,9 @@ public class DryEquipmentInfoController extends BaseController
|
||||
@PutMapping
|
||||
public AjaxResult edit(@RequestBody DryEquipmentInfo dryEquipmentInfo)
|
||||
{
|
||||
return toAjax(dryEquipmentInfoService.updateDryEquipmentInfo(dryEquipmentInfo));
|
||||
int result = dryEquipmentInfoService.updateDryEquipmentInfo(dryEquipmentInfo);
|
||||
autoCollectService.manualPushMqttDataWithStatusNormal();
|
||||
return toAjax(result);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@ -57,7 +57,6 @@ public class DryEquipmentScreenController extends BaseController {
|
||||
* @return
|
||||
*/
|
||||
@Anonymous
|
||||
@PermitAll
|
||||
@PostMapping("/closeAlarm")
|
||||
public AjaxResult closeAlarm(){
|
||||
// MqttConsole c = new MqttConsole();
|
||||
|
||||
@ -295,7 +295,7 @@ public class AlarmLightService {
|
||||
com.alibaba.fastjson2.JSONObject json = com.alibaba.fastjson2.JSONObject.parseObject(payload);
|
||||
// 仅处理“equipment_alarm”类型的指令
|
||||
if (!"equipment_alarm".equals(json.getString("type"))) {
|
||||
log.debug("忽略非报警类型的MQTT指令:type={}", json.getString("type"));
|
||||
// log.debug("忽略非报警类型的MQTT指令:type={}", json.getString("type"));
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
File diff suppressed because it is too large
Load Diff
@ -0,0 +1,79 @@
|
||||
package com.shgx.dryingroom.collect;
|
||||
|
||||
import com.shgx.dryingroom.collect.AutoCollectService;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import javax.annotation.PostConstruct;
|
||||
import java.util.concurrent.atomic.AtomicInteger;
|
||||
|
||||
/**
|
||||
* 缓存同步管理服务
|
||||
*/
|
||||
@Slf4j
|
||||
@Service
|
||||
public class CacheSyncService {
|
||||
|
||||
@Autowired
|
||||
private AutoCollectService autoCollectService;
|
||||
|
||||
// 缓存同步计数器
|
||||
private final AtomicInteger syncCounter = new AtomicInteger(0);
|
||||
|
||||
/**
|
||||
* 初始化
|
||||
*/
|
||||
@PostConstruct
|
||||
public void init() {
|
||||
log.info("缓存同步服务初始化完成");
|
||||
}
|
||||
|
||||
/**
|
||||
* 通知缓存更新
|
||||
* @param cacheType 缓存类型:EQUIPMENT, RULE, ALL
|
||||
*/
|
||||
public void notifyCacheUpdate(String cacheType) {
|
||||
int count = syncCounter.incrementAndGet();
|
||||
log.info("收到缓存更新通知[{}],类型:{},累计通知次数:{}", count, cacheType, count);
|
||||
|
||||
try {
|
||||
switch (cacheType.toUpperCase()) {
|
||||
case "EQUIPMENT":
|
||||
autoCollectService.refreshEquipmentCache();
|
||||
break;
|
||||
case "RULE":
|
||||
autoCollectService.refreshRuleCache();
|
||||
break;
|
||||
case "ALL":
|
||||
autoCollectService.refreshEquipmentCache();
|
||||
autoCollectService.refreshRuleCache();
|
||||
break;
|
||||
default:
|
||||
log.warn("未知的缓存类型:{},默认刷新全部", cacheType);
|
||||
autoCollectService.refreshEquipmentCache();
|
||||
autoCollectService.refreshRuleCache();
|
||||
}
|
||||
|
||||
log.info("缓存更新完成[{}]", count);
|
||||
|
||||
} catch (Exception e) {
|
||||
log.error("缓存更新失败[{}]", count, e);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 通知缓存更新并立即推送
|
||||
*/
|
||||
public void notifyCacheUpdateAndPush(String cacheType) {
|
||||
notifyCacheUpdate(cacheType);
|
||||
autoCollectService.triggerImmediatePush();
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取同步统计
|
||||
*/
|
||||
public int getSyncCount() {
|
||||
return syncCounter.get();
|
||||
}
|
||||
}
|
||||
@ -75,8 +75,8 @@ public class MqttDataHandler implements Consumer<MqttMessage> {
|
||||
// 3. 存储到Redis(设备ID-温度参数)
|
||||
String redisKey = REDIS_EQUIP_PREFIX + formatEquipId + ":" + PARAM_TEMP;
|
||||
redisTemplate.opsForValue().set(redisKey, new EquipmentData(time, paramValue, PARAM_TEMP, formatEquipId));
|
||||
log.info("✅ 存储设备数据 - 设备:{},参数:{},值:{},Key:{}",
|
||||
formatEquipId, PARAM_TEMP, paramValue, redisKey);
|
||||
// log.info("✅ 存储设备数据 - 设备:{},参数:{},值:{},Key:{}",
|
||||
// formatEquipId, PARAM_TEMP, paramValue, redisKey);
|
||||
}
|
||||
} catch (Exception e) {
|
||||
log.error("❌ 处理MQTT设备数据失败:{}", e.getMessage(), e);
|
||||
|
||||
@ -108,7 +108,7 @@ public class MqttService implements MqttCallback {
|
||||
@Override
|
||||
public void messageArrived(String topic, MqttMessage message) throws Exception {
|
||||
String payload = new String(message.getPayload());
|
||||
log.info("📨 收到消息 - 主题: {}, QoS: {}, 内容: {}", topic, message.getQos(), payload);
|
||||
// log.info("📨 收到消息 - 主题: {}, QoS: {}, 内容: {}", topic, message.getQos(), payload);
|
||||
|
||||
// 控制台显示
|
||||
System.out.println("\n🎯 收到客户端消息:");
|
||||
|
||||
@ -6,11 +6,11 @@ import java.util.List;
|
||||
|
||||
import com.baomidou.dynamic.datasource.annotation.DS;
|
||||
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
||||
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
|
||||
import com.shgx.common.utils.DateUtils;
|
||||
import com.shgx.common.utils.SecurityUtils;
|
||||
import com.shgx.dryingroom.alarm.AlarmLightService;
|
||||
import com.shgx.dryingroom.collect.AutoCollectService;
|
||||
import com.shgx.dryingroom.collect.CacheSyncService;
|
||||
import com.shgx.dryingroom.web.domain.DryEquipmentParamRelation;
|
||||
import com.shgx.dryingroom.web.domain.DryParamThreshold;
|
||||
import com.shgx.dryingroom.web.domain.dto.ParamThresholdDTO;
|
||||
@ -47,6 +47,10 @@ public class DryEquipmentInfoServiceImpl implements IDryEquipmentInfoService {
|
||||
@Autowired
|
||||
private AutoCollectService autoCollectService;
|
||||
|
||||
// 新增:缓存同步服务
|
||||
@Autowired
|
||||
private CacheSyncService cacheSyncService;
|
||||
|
||||
/**
|
||||
* 查询设备信息
|
||||
*
|
||||
@ -103,9 +107,19 @@ public class DryEquipmentInfoServiceImpl implements IDryEquipmentInfoService {
|
||||
dryEquipmentInfo.setCreateBy(SecurityUtils.getUsername());
|
||||
dryEquipmentInfo.setCreateTime(DateUtils.getNowDate());
|
||||
|
||||
autoCollectService.loadRuleCache();
|
||||
// 新增:先刷新缓存,再插入数据
|
||||
autoCollectService.refreshEquipmentCache();
|
||||
if (dryEquipmentInfo.getRuleCode() != null && !dryEquipmentInfo.getRuleCode().trim().isEmpty()) {
|
||||
autoCollectService.refreshRuleCache();
|
||||
}
|
||||
|
||||
return dryEquipmentInfoMapper.insert(dryEquipmentInfo);
|
||||
// 插入数据
|
||||
int result = dryEquipmentInfoMapper.insert(dryEquipmentInfo);
|
||||
|
||||
// 新增:立即推送数据到前端
|
||||
autoCollectService.triggerImmediatePush();
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
@ -116,6 +130,11 @@ public class DryEquipmentInfoServiceImpl implements IDryEquipmentInfoService {
|
||||
*/
|
||||
@Override
|
||||
public int updateDryEquipmentInfo(DryEquipmentInfo dryEquipmentInfo) {
|
||||
// 先查询旧的设备信息,用于判断规则是否变更
|
||||
DryEquipmentInfo oldInfo = dryEquipmentInfoMapper.selectById(dryEquipmentInfo.getId());
|
||||
String oldRuleCode = oldInfo != null ? oldInfo.getRuleCode() : null;
|
||||
String newRuleCode = dryEquipmentInfo.getRuleCode();
|
||||
|
||||
// 查找对应规则
|
||||
LambdaQueryWrapper<DryParamThreshold> paramWrapper = new LambdaQueryWrapper<>();
|
||||
paramWrapper.eq(DryParamThreshold::getRuleCode, dryEquipmentInfo.getRuleCode());
|
||||
@ -125,34 +144,47 @@ public class DryEquipmentInfoServiceImpl implements IDryEquipmentInfoService {
|
||||
dryEquipmentInfo.setRuleName(param.getRuleName());
|
||||
dryEquipmentInfo.setSetTem(param.getMaxValue());
|
||||
|
||||
// 更新设备规则关系表
|
||||
boolean isRelationExist = false; // 标记:是否已存在匹配记录
|
||||
LambdaQueryWrapper<DryEquipmentParamRelation> relationWrapper = new LambdaQueryWrapper<>();
|
||||
relationWrapper.eq(DryEquipmentParamRelation::getEquipmentCode, dryEquipmentInfo.getEquipmentCode());
|
||||
List<DryEquipmentParamRelation> relationList = relationMapper.selectList(relationWrapper);
|
||||
for (DryEquipmentParamRelation relation : relationList) {
|
||||
if (relation.getRuleCode().equals(dryEquipmentInfo.getRuleCode())) {
|
||||
isRelationExist = true; // 标记为已存在
|
||||
break;
|
||||
}
|
||||
// 更新设备规则关系表
|
||||
boolean isRelationExist = false; // 标记:是否已存在匹配记录
|
||||
LambdaQueryWrapper<DryEquipmentParamRelation> relationWrapper = new LambdaQueryWrapper<>();
|
||||
relationWrapper.eq(DryEquipmentParamRelation::getEquipmentCode, dryEquipmentInfo.getEquipmentCode());
|
||||
List<DryEquipmentParamRelation> relationList = relationMapper.selectList(relationWrapper);
|
||||
for (DryEquipmentParamRelation relation : relationList) {
|
||||
if (relation.getRuleCode().equals(dryEquipmentInfo.getRuleCode())) {
|
||||
isRelationExist = true; // 标记为已存在
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
// 仅当不存在时,才执行插入操作
|
||||
if (!isRelationExist) {
|
||||
DryEquipmentParamRelation relation = new DryEquipmentParamRelation();
|
||||
relation.setEquipmentCode(dryEquipmentInfo.getEquipmentCode());
|
||||
relation.setEquipmentName(dryEquipmentInfo.getEquipmentName());
|
||||
relation.setRuleCode(dryEquipmentInfo.getRuleCode());
|
||||
relation.setRuleName(dryEquipmentInfo.getRuleName());
|
||||
relationMapper.insert(relation);
|
||||
}
|
||||
|
||||
autoCollectService.loadRuleCache();
|
||||
// 仅当不存在时,才执行插入操作
|
||||
if (!isRelationExist) {
|
||||
DryEquipmentParamRelation relation = new DryEquipmentParamRelation();
|
||||
relation.setEquipmentCode(dryEquipmentInfo.getEquipmentCode());
|
||||
relation.setEquipmentName(dryEquipmentInfo.getEquipmentName());
|
||||
relation.setRuleCode(dryEquipmentInfo.getRuleCode());
|
||||
relation.setRuleName(dryEquipmentInfo.getRuleName());
|
||||
relationMapper.insert(relation);
|
||||
}
|
||||
|
||||
dryEquipmentInfo.setUpdateBy(SecurityUtils.getUsername());
|
||||
dryEquipmentInfo.setUpdateTime(DateUtils.getNowDate());
|
||||
|
||||
return dryEquipmentInfoMapper.updateById(dryEquipmentInfo);
|
||||
// 更新数据库
|
||||
int result = dryEquipmentInfoMapper.updateById(dryEquipmentInfo);
|
||||
|
||||
// 新增:判断规则是否变更,更新相应缓存
|
||||
boolean ruleChanged = (oldRuleCode == null && newRuleCode != null) ||
|
||||
(oldRuleCode != null && !oldRuleCode.equals(newRuleCode));
|
||||
|
||||
if (ruleChanged) {
|
||||
// 规则变更,刷新所有缓存
|
||||
cacheSyncService.notifyCacheUpdateAndPush("ALL");
|
||||
} else {
|
||||
// 只刷新设备缓存
|
||||
cacheSyncService.notifyCacheUpdateAndPush("EQUIPMENT");
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
@ -165,7 +197,13 @@ public class DryEquipmentInfoServiceImpl implements IDryEquipmentInfoService {
|
||||
public int deleteDryEquipmentInfoByIds(Long[] ids) {
|
||||
List<Long> idList = Arrays.asList(ids);
|
||||
|
||||
return dryEquipmentInfoMapper.deleteByIds(idList);
|
||||
// 执行删除
|
||||
int result = dryEquipmentInfoMapper.deleteByIds(idList);
|
||||
|
||||
// 新增:刷新缓存并推送
|
||||
cacheSyncService.notifyCacheUpdateAndPush("ALL");
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
@ -176,7 +214,13 @@ public class DryEquipmentInfoServiceImpl implements IDryEquipmentInfoService {
|
||||
*/
|
||||
@Override
|
||||
public int deleteDryEquipmentInfoById(Long id) {
|
||||
return dryEquipmentInfoMapper.deleteById(id);
|
||||
// 执行删除
|
||||
int result = dryEquipmentInfoMapper.deleteById(id);
|
||||
|
||||
// 新增:刷新缓存并推送
|
||||
cacheSyncService.notifyCacheUpdateAndPush("ALL");
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
@ -198,7 +242,14 @@ public class DryEquipmentInfoServiceImpl implements IDryEquipmentInfoService {
|
||||
alarmLightService.triggerAllAlarmsByEquipment(equipmentInfo.getEquipmentCode());
|
||||
}
|
||||
}
|
||||
return dryEquipmentInfoMapper.updateById(info);
|
||||
|
||||
// 更新数据库
|
||||
int result = dryEquipmentInfoMapper.updateById(info);
|
||||
|
||||
// 新增:刷新缓存并立即推送
|
||||
cacheSyncService.notifyCacheUpdateAndPush("EQUIPMENT");
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
@ -218,4 +269,4 @@ public class DryEquipmentInfoServiceImpl implements IDryEquipmentInfoService {
|
||||
}
|
||||
return result;
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -14,7 +14,9 @@ import com.shgx.common.utils.DateUtils;
|
||||
import com.shgx.common.utils.SecurityUtils;
|
||||
import com.shgx.common.utils.StringUtils;
|
||||
import com.shgx.common.utils.uuid.UUID;
|
||||
|
||||
import com.shgx.dryingroom.collect.AutoCollectService;
|
||||
import com.shgx.dryingroom.collect.CacheSyncService;
|
||||
import com.shgx.dryingroom.web.domain.DryEquipmentInfo;
|
||||
import com.shgx.dryingroom.web.domain.DryEquipmentParamRelation;
|
||||
import com.shgx.dryingroom.web.mapper.DryEquipmentInfoMapper;
|
||||
@ -36,7 +38,7 @@ import static org.apache.naming.SelectorContext.prefix;
|
||||
|
||||
/**
|
||||
* 参数阈值Service业务层处理
|
||||
*
|
||||
*
|
||||
* @author shgx
|
||||
* @date 2025-10-16
|
||||
*/
|
||||
@ -56,6 +58,10 @@ public class DryParamThresholdServiceImpl implements IDryParamThresholdService
|
||||
@Autowired
|
||||
private DryEquipmentInfoMapper infoMapper;
|
||||
|
||||
// 新增:缓存同步服务
|
||||
@Autowired
|
||||
private CacheSyncService cacheSyncService;
|
||||
|
||||
/**
|
||||
* 查询参数阈值
|
||||
*
|
||||
@ -92,6 +98,7 @@ public class DryParamThresholdServiceImpl implements IDryParamThresholdService
|
||||
* @return 结果
|
||||
*/
|
||||
@Override
|
||||
@Transactional(rollbackFor = Exception.class)
|
||||
public int insertDryParamThreshold(DryParamThreshold dryParamThreshold)
|
||||
{
|
||||
// 校验 ruleName 是否已存在于数据库
|
||||
@ -114,10 +121,13 @@ public class DryParamThresholdServiceImpl implements IDryParamThresholdService
|
||||
dryParamThreshold.setCreateBy(SecurityUtils.getUsername());
|
||||
dryParamThreshold.setCreateTime(DateUtils.getNowDate());
|
||||
|
||||
// 重新加载缓存
|
||||
autoCollectService.loadRuleCache();
|
||||
// 新增数据
|
||||
int result = dryParamThresholdMapper.insert(dryParamThreshold);
|
||||
|
||||
return dryParamThresholdMapper.insert(dryParamThreshold);
|
||||
// 新增:同步刷新规则缓存并立即推送
|
||||
cacheSyncService.notifyCacheUpdateAndPush("RULE");
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
@ -127,23 +137,60 @@ public class DryParamThresholdServiceImpl implements IDryParamThresholdService
|
||||
* @return 结果
|
||||
*/
|
||||
@Override
|
||||
@Transactional(rollbackFor = Exception.class)
|
||||
public int updateDryParamThreshold(DryParamThreshold dryParamThreshold)
|
||||
{
|
||||
// 先查询旧的规则信息,用于判断是否需要刷新设备缓存
|
||||
DryParamThreshold oldThreshold = dryParamThresholdMapper.selectById(dryParamThreshold.getId());
|
||||
String oldRuleCode = oldThreshold != null ? oldThreshold.getRuleCode() : null;
|
||||
String newRuleCode = dryParamThreshold.getRuleCode();
|
||||
|
||||
// 记录是否启停状态变更
|
||||
boolean enabledStatusChanged = oldThreshold != null &&
|
||||
!StringUtils.equals(oldThreshold.getIsEnabled(), dryParamThreshold.getIsEnabled());
|
||||
|
||||
// 记录阈值是否变更
|
||||
boolean thresholdChanged = oldThreshold != null &&
|
||||
(!StringUtils.equals(oldThreshold.getMaxValue(), dryParamThreshold.getMaxValue()) ||
|
||||
!StringUtils.equals(oldThreshold.getMinValue(), dryParamThreshold.getMinValue()));
|
||||
|
||||
dryParamThreshold.setUpdateBy(SecurityUtils.getUsername());
|
||||
dryParamThreshold.setUpdateTime(DateUtils.getNowDate());
|
||||
|
||||
// 重新加载缓存
|
||||
autoCollectService.loadRuleCache();
|
||||
// 更新数据
|
||||
int result = dryParamThresholdMapper.updateById(dryParamThreshold);
|
||||
|
||||
return dryParamThresholdMapper.updateById(dryParamThreshold);
|
||||
LambdaQueryWrapper<DryEquipmentInfo> queryWrapper = new LambdaQueryWrapper<>();
|
||||
queryWrapper.eq(DryEquipmentInfo::getRuleCode, dryParamThreshold.getRuleCode());
|
||||
List<DryEquipmentInfo> infoList = infoMapper.selectList(queryWrapper);
|
||||
if (!infoList.isEmpty()) {
|
||||
for (DryEquipmentInfo info : infoList) {
|
||||
info.setRuleName(dryParamThreshold.getRuleName());
|
||||
info.setSetTem(dryParamThreshold.getMaxValue());
|
||||
infoMapper.updateById(info);
|
||||
}
|
||||
}
|
||||
|
||||
// 新增:根据变更情况同步缓存
|
||||
if (result > 0) {
|
||||
if (enabledStatusChanged || thresholdChanged) {
|
||||
// 启停状态或阈值变更,刷新所有缓存并推送
|
||||
cacheSyncService.notifyCacheUpdateAndPush("ALL");
|
||||
} else {
|
||||
// 其他信息变更,只刷新规则缓存并推送
|
||||
cacheSyncService.notifyCacheUpdateAndPush("RULE");
|
||||
}
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* 批量删除参数阈值
|
||||
* 批量删除参数阈值(按照单独删除的完整逻辑逐个处理)
|
||||
* 每个规则ID都执行完整的级联置空和关联删除流程
|
||||
*
|
||||
* @param ids 需要删除的参数阈值主键
|
||||
* @return 结果
|
||||
* @return 成功删除的记录数
|
||||
*/
|
||||
@Override
|
||||
@Transactional(rollbackFor = Exception.class)
|
||||
@ -152,32 +199,83 @@ public class DryParamThresholdServiceImpl implements IDryParamThresholdService
|
||||
if (ids == null || ids.length == 0) {
|
||||
return 0;
|
||||
}
|
||||
List<Long> idList = Arrays.asList(ids);
|
||||
|
||||
// 2. 根据规则ID批量查询DryParamThreshold,获取对应的ruleCode列表
|
||||
LambdaQueryWrapper<DryParamThreshold> thresholdQueryWrapper = new LambdaQueryWrapper<>();
|
||||
thresholdQueryWrapper.in(DryParamThreshold::getId, idList)
|
||||
.select(DryParamThreshold::getRuleCode);
|
||||
List<DryParamThreshold> thresholdList = dryParamThresholdMapper.selectList(thresholdQueryWrapper);
|
||||
int totalDeleteCount = 0;
|
||||
|
||||
// 3. 提取ruleCode列表
|
||||
List<String> ruleCodeList = thresholdList.stream()
|
||||
.map(DryParamThreshold::getRuleCode)
|
||||
.filter(ruleCode -> ruleCode != null && !ruleCode.isEmpty())
|
||||
.distinct()
|
||||
.collect(Collectors.toList());
|
||||
// 记录所有被删除的规则编码,用于设备缓存更新
|
||||
List<String> deletedRuleCodes = new java.util.ArrayList<>();
|
||||
|
||||
// 4. 级联删除DryEquipmentParamRelation中匹配ruleCode的所有数据
|
||||
if (!CollectionUtils.isEmpty(ruleCodeList)) {
|
||||
LambdaQueryWrapper<DryEquipmentParamRelation> relationQueryWrapper = new LambdaQueryWrapper<>();
|
||||
relationQueryWrapper.in(DryEquipmentParamRelation::getRuleCode, ruleCodeList);
|
||||
relationMapper.delete(relationQueryWrapper);
|
||||
// 2. 遍历每个ID,按照单独删除的逻辑处理
|
||||
for (Long id : ids) {
|
||||
if (id == null) {
|
||||
continue;
|
||||
}
|
||||
|
||||
// 3. 根据ID查询对应的DryParamThreshold,获取ruleCode
|
||||
LambdaQueryWrapper<DryParamThreshold> thresholdQueryWrapper = new LambdaQueryWrapper<>();
|
||||
thresholdQueryWrapper.eq(DryParamThreshold::getId, id)
|
||||
.select(DryParamThreshold::getRuleCode);
|
||||
DryParamThreshold threshold = dryParamThresholdMapper.selectOne(thresholdQueryWrapper);
|
||||
|
||||
if (threshold == null) {
|
||||
continue;
|
||||
}
|
||||
|
||||
String ruleCode = threshold.getRuleCode();
|
||||
|
||||
// 记录被删除的规则编码
|
||||
if (StringUtils.hasText(ruleCode)) {
|
||||
deletedRuleCodes.add(ruleCode);
|
||||
}
|
||||
|
||||
// 4. 如果存在ruleCode,则处理该规则的级联删除和置空逻辑
|
||||
if (StringUtils.hasText(ruleCode)) {
|
||||
// 4.1 查询relation表中该ruleCode绑定的所有equipmentCode
|
||||
LambdaQueryWrapper<DryEquipmentParamRelation> relationQueryWrapper = new LambdaQueryWrapper<>();
|
||||
relationQueryWrapper.eq(DryEquipmentParamRelation::getRuleCode, ruleCode)
|
||||
.select(DryEquipmentParamRelation::getEquipmentCode);
|
||||
List<String> equipmentCodeList = relationMapper.selectObjs(relationQueryWrapper)
|
||||
.stream()
|
||||
.filter(Objects::nonNull)
|
||||
.map(Object::toString)
|
||||
.distinct()
|
||||
.collect(Collectors.toList());
|
||||
|
||||
// 4.2 批量置空这些equipmentCode对应的ruleCode(DryEquipmentInfo表)
|
||||
if (!CollectionUtils.isEmpty(equipmentCodeList)) {
|
||||
LambdaUpdateWrapper<DryEquipmentInfo> infoUpdateWrapper = new LambdaUpdateWrapper<>();
|
||||
infoUpdateWrapper.in(DryEquipmentInfo::getEquipmentCode, equipmentCodeList)
|
||||
.set(DryEquipmentInfo::getRuleCode, null)
|
||||
.set(DryEquipmentInfo::getRuleName, null)
|
||||
.set(DryEquipmentInfo::getSetTem, "0");
|
||||
infoMapper.update(null, infoUpdateWrapper);
|
||||
}
|
||||
|
||||
// 4.3 级联删除DryEquipmentParamRelation中该ruleCode的所有关联数据
|
||||
LambdaQueryWrapper<DryEquipmentParamRelation> deleteWrapper = new LambdaQueryWrapper<>();
|
||||
deleteWrapper.eq(DryEquipmentParamRelation::getRuleCode, ruleCode);
|
||||
relationMapper.delete(deleteWrapper);
|
||||
}
|
||||
|
||||
// 5. 删除DryParamThreshold主表数据
|
||||
int deleteCount = dryParamThresholdMapper.deleteById(id);
|
||||
if (deleteCount > 0) {
|
||||
totalDeleteCount++;
|
||||
}
|
||||
}
|
||||
|
||||
// 5. 删除DryParamThreshold表中指定ID的规则数据
|
||||
int deleteCount = dryParamThresholdMapper.deleteByIds(idList);
|
||||
// 6. 处理完成后刷新缓存并推送
|
||||
if (totalDeleteCount > 0) {
|
||||
// 如果有设备关联的规则被删除,需要刷新所有缓存
|
||||
if (!deletedRuleCodes.isEmpty()) {
|
||||
cacheSyncService.notifyCacheUpdateAndPush("ALL");
|
||||
} else {
|
||||
// 只刷新规则缓存
|
||||
cacheSyncService.notifyCacheUpdateAndPush("RULE");
|
||||
}
|
||||
}
|
||||
|
||||
return deleteCount;
|
||||
return totalDeleteCount;
|
||||
}
|
||||
|
||||
/**
|
||||
@ -202,6 +300,8 @@ public class DryParamThresholdServiceImpl implements IDryParamThresholdService
|
||||
}
|
||||
String ruleCode = threshold.getRuleCode();
|
||||
|
||||
boolean hasEquipmentRelation = false;
|
||||
|
||||
// 3. 核心逻辑:处理设备关联的ruleCode置空
|
||||
if (StringUtils.hasText(ruleCode)) {
|
||||
// 3.1 查询relation表中该ruleCode绑定的所有equipmentCode
|
||||
@ -218,10 +318,12 @@ public class DryParamThresholdServiceImpl implements IDryParamThresholdService
|
||||
|
||||
// 3.2 批量置空这些equipmentCode对应的ruleCode(DryEquipmentInfo表)
|
||||
if (!equipmentCodeList.isEmpty()) {
|
||||
hasEquipmentRelation = true;
|
||||
LambdaUpdateWrapper<DryEquipmentInfo> infoUpdateWrapper = new LambdaUpdateWrapper<>();
|
||||
infoUpdateWrapper.in(DryEquipmentInfo::getEquipmentCode, equipmentCodeList) // 匹配所有关联设备
|
||||
.set(DryEquipmentInfo::getRuleCode, null) // 将ruleCode置为null
|
||||
.set(DryEquipmentInfo::getRuleName, null);
|
||||
.set(DryEquipmentInfo::getRuleName, null)
|
||||
.set(DryEquipmentInfo::getSetTem, "0");
|
||||
int updateCount = infoMapper.update(null, infoUpdateWrapper);
|
||||
}
|
||||
|
||||
@ -234,7 +336,16 @@ public class DryParamThresholdServiceImpl implements IDryParamThresholdService
|
||||
// 4. 删除DryParamThreshold主表数据
|
||||
int mainDeleteCount = dryParamThresholdMapper.deleteById(id);
|
||||
|
||||
autoCollectService.loadRuleCache();
|
||||
// 5. 刷新缓存并推送
|
||||
if (mainDeleteCount > 0) {
|
||||
if (hasEquipmentRelation) {
|
||||
// 如果有关联设备,刷新所有缓存
|
||||
cacheSyncService.notifyCacheUpdateAndPush("ALL");
|
||||
} else {
|
||||
// 只刷新规则缓存
|
||||
cacheSyncService.notifyCacheUpdateAndPush("RULE");
|
||||
}
|
||||
}
|
||||
|
||||
return mainDeleteCount;
|
||||
}
|
||||
@ -325,7 +436,7 @@ public class DryParamThresholdServiceImpl implements IDryParamThresholdService
|
||||
return "";
|
||||
}
|
||||
|
||||
// 配置拼音格式:无音调(如“干”→“gan”,而非“gan1”)
|
||||
// 配置拼音格式:无音调(如"干"→"gan",而非"gan1")
|
||||
HanyuPinyinOutputFormat format = new HanyuPinyinOutputFormat();
|
||||
format.setToneType(HanyuPinyinToneType.WITHOUT_TONE);
|
||||
|
||||
@ -337,7 +448,7 @@ public class DryParamThresholdServiceImpl implements IDryParamThresholdService
|
||||
// 获取该汉字的所有拼音(多音字取第一个)
|
||||
String[] pinyinArray = PinyinHelper.toHanyuPinyinStringArray(c, format);
|
||||
if (pinyinArray != null && pinyinArray.length > 0) {
|
||||
pinyin.append(pinyinArray[0]); // 取第一个拼音(如“行”→“xing”)
|
||||
pinyin.append(pinyinArray[0]); // 取第一个拼音(如"行"→"xing")
|
||||
} else {
|
||||
pinyin.append("unknown"); // 生僻字兜底
|
||||
}
|
||||
@ -355,4 +466,4 @@ public class DryParamThresholdServiceImpl implements IDryParamThresholdService
|
||||
|
||||
return pinyin.toString();
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -55,14 +55,12 @@ public class ResourcesConfig implements WebMvcConfigurer
|
||||
public CorsFilter corsFilter()
|
||||
{
|
||||
CorsConfiguration config = new CorsConfiguration();
|
||||
config.addAllowedOrigin("*"); // 允许所有来源
|
||||
// 设置访问源地址
|
||||
config.addAllowedOriginPattern("*");
|
||||
// 设置访问源请求头
|
||||
config.addAllowedHeader("*");
|
||||
// 设置访问源请求方法
|
||||
config.addAllowedMethod("*");
|
||||
config.setAllowCredentials(false);
|
||||
// 有效期 1800秒
|
||||
config.setMaxAge(1800L);
|
||||
// 添加映射路径,拦截一切请求
|
||||
|
||||
@ -97,7 +97,6 @@ public class SecurityConfig
|
||||
protected SecurityFilterChain filterChain(HttpSecurity httpSecurity) throws Exception
|
||||
{
|
||||
return httpSecurity
|
||||
.cors().and()
|
||||
// CSRF禁用,因为不使用session
|
||||
.csrf(csrf -> csrf.disable())
|
||||
// 禁用HTTP响应标头
|
||||
@ -113,8 +112,7 @@ public class SecurityConfig
|
||||
permitAllUrl.getUrls().forEach(url -> requests.antMatchers(url).permitAll());
|
||||
// 对于登录login 注册register 验证码captchaImage 允许匿名访问
|
||||
requests.antMatchers("/login", "/register", "/captchaImage").permitAll()
|
||||
.antMatchers("/dry/screen/closeAlarm").permitAll()
|
||||
// 静态资源,可匿名访问
|
||||
// 静态资源,可匿名访问
|
||||
.antMatchers(HttpMethod.GET, "/", "/*.html", "/**/*.html", "/**/*.css", "/**/*.js", "/profile/**").permitAll()
|
||||
.antMatchers("/swagger-ui.html", "/swagger-resources/**", "/webjars/**", "/*/api-docs", "/druid/**").permitAll()
|
||||
// 除上面外的所有请求全部需要鉴权认证
|
||||
|
||||
@ -31,14 +31,6 @@ public class JwtAuthenticationTokenFilter extends OncePerRequestFilter
|
||||
protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain chain)
|
||||
throws ServletException, IOException
|
||||
{
|
||||
String requestURI = request.getRequestURI();
|
||||
|
||||
// 如果是需要放行的接口,直接跳过Token验证
|
||||
if ("/dry/screen/closeAlarm".equals(requestURI)) {
|
||||
chain.doFilter(request, response);
|
||||
return;
|
||||
}
|
||||
|
||||
LoginUser loginUser = tokenService.getLoginUser(request);
|
||||
if (StringUtils.isNotNull(loginUser) && StringUtils.isNull(SecurityUtils.getAuthentication()))
|
||||
{
|
||||
@ -49,4 +41,4 @@ public class JwtAuthenticationTokenFilter extends OncePerRequestFilter
|
||||
}
|
||||
chain.doFilter(request, response);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user