fix: 微信小程序接口对接修改,联调测试问题修复。

This commit is contained in:
tianyongbao
2026-01-14 08:31:55 +08:00
parent a41248e405
commit 15af17fb95
44 changed files with 3887 additions and 52 deletions

View File

@@ -1,5 +1,7 @@
package com.intc.fishery.service;
import com.intc.fishery.domain.dto.PublicDeviceSimpleDto;
import com.intc.fishery.domain.dto.PublicDeviceSwitchBaseData;
import com.intc.fishery.domain.vo.DeviceSwitchVo;
import com.intc.fishery.domain.bo.DeviceSwitchBo;
import com.intc.common.mybatis.core.page.TableDataInfo;
@@ -65,4 +67,36 @@ public interface IDeviceSwitchService {
* @return 是否删除成功
*/
Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid);
/**
* 根据塘口ID查询塘口下的设备及其开关信息
*
* @param pondId 塘口ID
* @return 设备及开关列表
*/
List<PublicDeviceSimpleDto> getPondSwitch(Long pondId);
/**
* 获取单个开关的基础信息
*
* @param switchId 开关ID
* @return 开关基础信息
*/
PublicDeviceSwitchBaseData getOneSwitchInfo(Long switchId);
/**
* 修改开关名称
*
* @param switchId 开关ID
* @param newName 新名称
*/
void changeSwitchName(Long switchId, String newName);
/**
* 设置电流告警开关
*
* @param switchId 开关ID
* @param isOpen 是否开启0-关闭1-开启)
*/
void updateElectricWarnOpen(Long switchId, Integer isOpen);
}

View File

@@ -1,5 +1,9 @@
package com.intc.fishery.service;
import com.intc.fishery.domain.bo.ReqAddLinkedCtrl;
import com.intc.fishery.domain.bo.ReqLinkedCtrlOpen;
import com.intc.fishery.domain.bo.ReqUpdateLinkedCtrl;
import com.intc.fishery.domain.dto.PublicLinkedCtrl;
import com.intc.fishery.domain.vo.LinkedCtrlVo;
import com.intc.fishery.domain.bo.LinkedCtrlBo;
import com.intc.common.mybatis.core.page.TableDataInfo;
@@ -65,4 +69,33 @@ public interface ILinkedCtrlService {
* @return 是否删除成功
*/
Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid);
/**
* 新增联动控制(业务方法)
*
* @param request 新增联动控制请求
*/
void addLinkedCtrl(ReqAddLinkedCtrl request);
/**
* 修改联动控制(业务方法)
*
* @param request 修改联动控制请求
*/
void updateLinkedCtrl(ReqUpdateLinkedCtrl request);
/**
* 根据设备ID查询所有联动控制
*
* @param deviceId 设备ID
* @return 联动控制列表
*/
List<PublicLinkedCtrl> queryAllLinkedCtrl(Long deviceId);
/**
* 设置联动控制开关
*
* @param request 设置开关请求
*/
void setLinkedOpen(ReqLinkedCtrlOpen request);
}

View File

@@ -8,8 +8,13 @@ import com.intc.common.mybatis.core.page.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.intc.fishery.domain.dto.PublicDeviceSimpleDto;
import com.intc.fishery.domain.dto.PublicDeviceSwitchSimpleDto;
import com.intc.fishery.domain.dto.PublicDeviceSwitchBaseData;
import com.intc.fishery.domain.dto.PublicPondIdName;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.intc.fishery.domain.bo.DeviceSwitchBo;
import com.intc.fishery.domain.vo.DeviceSwitchVo;
@@ -17,12 +22,13 @@ import com.intc.fishery.domain.DeviceSwitch;
import com.intc.fishery.domain.Device;
import com.intc.fishery.domain.Pond;
import com.intc.fishery.domain.AquUser;
import com.intc.fishery.domain.TimingCtrl;
import com.intc.fishery.mapper.DeviceSwitchMapper;
import com.intc.fishery.mapper.DeviceMapper;
import com.intc.fishery.service.IDeviceSwitchService;
import java.util.List;
import java.util.Map;
import java.util.Collection;
import java.util.*;
import java.util.stream.Collectors;
/**
* 测控一体机开关Service业务层处理
@@ -36,6 +42,7 @@ import java.util.Collection;
public class DeviceSwitchServiceImpl implements IDeviceSwitchService {
private final DeviceSwitchMapper baseMapper;
private final DeviceMapper deviceMapper;
/**
* 查询测控一体机开关
@@ -223,4 +230,221 @@ public class DeviceSwitchServiceImpl implements IDeviceSwitchService {
}
return baseMapper.deleteByIds(ids) > 0;
}
}
/**
* 根据塘口ID查询塘口下的设备及其开关信息
*
* @param pondId 塘口ID
* @return 设备及开关列表
*/
@Override
public List<PublicDeviceSimpleDto> getPondSwitch(Long pondId) {
// 使用连表查询获取该塘口下的所有开关及其关联的设备信息
MPJLambdaWrapper<DeviceSwitch> wrapper = new MPJLambdaWrapper<DeviceSwitch>()
.selectAll(DeviceSwitch.class)
.selectAs(Device::getDeviceName, DeviceSwitchVo::getDeviceName)
.selectAs(Device::getWarnCode, DeviceSwitchVo::getWarnCode)
.selectAs(Device::getDeadTime, "deadTime")
.leftJoin(Device.class, Device::getId, DeviceSwitch::getDeviceId)
.eq(DeviceSwitch::getPondId, pondId)
.orderByAsc(DeviceSwitch::getIndex);
List<DeviceSwitchVo> switchList = baseMapper.selectJoinList(DeviceSwitchVo.class, wrapper);
// 过滤掉已过期的设备,并按设备分组
Map<Long, List<DeviceSwitchVo>> deviceSwitchMap = switchList.stream()
.filter(vo -> !isDeviceDead(vo))
.collect(Collectors.groupingBy(DeviceSwitchVo::getDeviceId));
// 构建返回结果
List<PublicDeviceSimpleDto> result = new ArrayList<>();
for (Map.Entry<Long, List<DeviceSwitchVo>> entry : deviceSwitchMap.entrySet()) {
List<DeviceSwitchVo> switches = entry.getValue();
if (switches.isEmpty()) {
continue;
}
// 从第一个开关记录中获取设备信息
DeviceSwitchVo firstSwitch = switches.get(0);
PublicDeviceSimpleDto deviceDto = new PublicDeviceSimpleDto();
deviceDto.setId(entry.getKey());
deviceDto.setDeviceName(firstSwitch.getDeviceName());
deviceDto.setDeviceType(1); // 测控一体机设备类型设为1Controller
deviceDto.setWarnCode(firstSwitch.getWarnCode());
// 转换开关列表
List<PublicDeviceSwitchSimpleDto> switchDtoList = switches.stream()
.map(this::convertToSwitchDto)
.collect(Collectors.toList());
deviceDto.setListSwitch(switchDtoList);
result.add(deviceDto);
}
return result;
}
/**
* 判断设备是否已过期
*
* @param vo 设备开关信息
* @return true-已过期false-未过期
*/
private boolean isDeviceDead(DeviceSwitchVo vo) {
Date deadTime = vo.getDeadTime();
if (deadTime == null) {
return false;
}
return deadTime.before(new Date());
}
/**
* 将DeviceSwitchVo转换为PublicDeviceSwitchSimpleDto
*
* @param vo 设备开关VO
* @return 设备开关简单DTO
*/
private PublicDeviceSwitchSimpleDto convertToSwitchDto(DeviceSwitchVo vo) {
PublicDeviceSwitchSimpleDto dto = new PublicDeviceSwitchSimpleDto();
dto.setId(vo.getId());
dto.setIndex(vo.getIndex());
dto.setSwitchName(vo.getSwitchName());
dto.setLinkedCtrlId(vo.getLinkedCtrlId() != null ? vo.getLinkedCtrlId() : 0L);
return dto;
}
/**
* 获取单个开关的基础信息
*
* @param switchId 开关ID
* @return 开关基础信息
*/
@Override
public PublicDeviceSwitchBaseData getOneSwitchInfo(Long switchId) {
// 使用连表查询获取开关及其关联的设备和塘口信息
MPJLambdaWrapper<DeviceSwitch> wrapper = new MPJLambdaWrapper<DeviceSwitch>()
.selectAll(DeviceSwitch.class)
.selectAs(Device::getDeviceName, "deviceName")
.selectAs(Device::getInputVoltage, "inputVoltage")
.selectAs(Pond::getPondName, "pondName")
.leftJoin(Device.class, Device::getId, DeviceSwitch::getDeviceId)
.leftJoin(Pond.class, Pond::getId, DeviceSwitch::getPondId)
.eq(DeviceSwitch::getId, switchId);
DeviceSwitchVo vo = baseMapper.selectJoinOne(DeviceSwitchVo.class, wrapper);
if (vo == null) {
return null;
}
// 转换为返回DTO
PublicDeviceSwitchBaseData data = new PublicDeviceSwitchBaseData();
data.setId(vo.getId());
data.setIndex(vo.getIndex());
data.setSwitchName(vo.getSwitchName());
data.setDeviceName(vo.getDeviceName());
data.setDetectElectricValue(vo.getDetectElectricValue());
data.setDetectVoltageValue(vo.getDetectVoltageValue());
data.setConnectVoltageType(vo.getConnectVoltageType());
data.setElectricWarnOpen(vo.getElectricWarnOpen());
data.setRateElectricValue(vo.getRateElectricValue());
// 从连表查询结果中获取inputVoltage
Integer inputVoltage = null;
if (vo.getDeviceId() != null) {
Device device = deviceMapper.selectById(vo.getDeviceId());
if (device != null) {
inputVoltage = device.getInputVoltage();
}
}
data.setInputVoltage(inputVoltage);
// 设置塘口信息
if (vo.getPondId() != null && StringUtils.isNotBlank(vo.getPondName())) {
PublicPondIdName pondInfo = new PublicPondIdName();
pondInfo.setId(vo.getPondId());
pondInfo.setPondName(vo.getPondName());
data.setPondInfo(pondInfo);
}
// 如果接线方式为4需要将电压值乘以1.732
if (data.getConnectVoltageType() != null && data.getConnectVoltageType() == 4 && data.getDetectVoltageValue() != null) {
data.setDetectVoltageValue(data.getDetectVoltageValue() * 1.732);
}
return data;
}
/**
* 修改开关名称
*
* @param switchId 开关ID
* @param newName 新名称
*/
@Override
public void changeSwitchName(Long switchId, String newName) {
// 查询开关信息,包括关联的设备信息
MPJLambdaWrapper<DeviceSwitch> wrapper = new MPJLambdaWrapper<DeviceSwitch>()
.selectAll(DeviceSwitch.class)
.selectAs(Device::getDeviceName, "deviceName")
.selectAs(Device::getSerialNum, "serialNum")
.leftJoin(Device.class, Device::getId, DeviceSwitch::getDeviceId)
.eq(DeviceSwitch::getId, switchId);
DeviceSwitchVo vo = baseMapper.selectJoinOne(DeviceSwitchVo.class, wrapper);
if (vo == null) {
throw new RuntimeException("开关不存在");
}
String oldName = vo.getSwitchName();
// 更新开关名称
DeviceSwitch updateEntity = new DeviceSwitch();
updateEntity.setId(switchId);
updateEntity.setSwitchName(newName);
baseMapper.updateById(updateEntity);
// 记录操作日志
log.info("开关操作:{}({})的开关{}修改名称为:{}",
vo.getDeviceName(), vo.getSerialNum(), oldName, newName);
}
/**
* 设置电流告警开关
*
* @param switchId 开关ID
* @param isOpen 是否开启0-关闭1-开启)
*/
@Override
public void updateElectricWarnOpen(Long switchId, Integer isOpen) {
// 查询开关信息,包括关联的设备信息
MPJLambdaWrapper<DeviceSwitch> wrapper = new MPJLambdaWrapper<DeviceSwitch>()
.selectAll(DeviceSwitch.class)
.selectAs(Device::getDeviceName, "deviceName")
.selectAs(Device::getSerialNum, "serialNum")
.selectAs(Device::getUserId, "userId")
.leftJoin(Device.class, Device::getId, DeviceSwitch::getDeviceId)
.eq(DeviceSwitch::getId, switchId);
DeviceSwitchVo vo = baseMapper.selectJoinOne(DeviceSwitchVo.class, wrapper);
if (vo == null || vo.getUserId() == null) {
throw new RuntimeException("开关不存在");
}
// 如果状态未变化,无需更新
if (vo.getElectricWarnOpen() != null && vo.getElectricWarnOpen().equals(isOpen)) {
return;
}
// 更新电流告警开关状态
DeviceSwitch updateEntity = new DeviceSwitch();
updateEntity.setId(switchId);
updateEntity.setElectricWarnOpen(isOpen);
baseMapper.updateById(updateEntity);
// 记录操作日志
String operation = isOpen == 1 ? "开启" : "关闭";
log.info("开关操作:{}({})的开关{},设置电流告警开关:{}",
vo.getDeviceName(), vo.getSerialNum(), vo.getSwitchName(), operation);
}
}

View File

@@ -1,5 +1,7 @@
package com.intc.fishery.service.impl;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.intc.common.core.exception.ServiceException;
import com.intc.common.core.utils.MapstructUtils;
import com.intc.common.core.utils.StringUtils;
import com.intc.common.mybatis.core.page.TableDataInfo;
@@ -9,7 +11,14 @@ import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import com.intc.fishery.domain.*;
import com.intc.fishery.domain.bo.ReqAddLinkedCtrl;
import com.intc.fishery.domain.bo.ReqLinkedCtrlOpen;
import com.intc.fishery.domain.bo.ReqUpdateLinkedCtrl;
import com.intc.fishery.domain.dto.PublicDeviceSwitchSimple;
import com.intc.fishery.domain.dto.PublicLinkedCtrl;
import com.intc.fishery.domain.vo.DeviceSwitchVo;
import com.intc.fishery.mapper.DeviceMapper;
import com.intc.fishery.mapper.DeviceSwitchMapper;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
@@ -17,10 +26,10 @@ import com.intc.fishery.domain.bo.LinkedCtrlBo;
import com.intc.fishery.domain.vo.LinkedCtrlVo;
import com.intc.fishery.mapper.LinkedCtrlMapper;
import com.intc.fishery.service.ILinkedCtrlService;
import org.springframework.transaction.annotation.Transactional;
import java.util.List;
import java.util.Map;
import java.util.Collection;
import java.util.*;
import java.util.stream.Collectors;
/**
* 溶解氧联动控制Service业务层处理
@@ -34,6 +43,8 @@ import java.util.Collection;
public class LinkedCtrlServiceImpl implements ILinkedCtrlService {
private final LinkedCtrlMapper baseMapper;
private final DeviceMapper deviceMapper;
private final DeviceSwitchMapper deviceSwitchMapper;
/**
* 查询溶解氧联动控制
@@ -191,10 +202,351 @@ public class LinkedCtrlServiceImpl implements ILinkedCtrlService {
* @return 是否删除成功
*/
@Override
@Transactional(rollbackFor = Exception.class)
public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
if(isValid){
//TODO 做一些业务上的校验,判断是否需要校验
}
// 删除联动控制前,先解除关联的开关绑定
deviceSwitchMapper.update(null,
Wrappers.lambdaUpdate(DeviceSwitch.class)
.set(DeviceSwitch::getLinkedCtrlId, null)
.in(DeviceSwitch::getLinkedCtrlId, ids));
// 删除联动控制记录
return baseMapper.deleteByIds(ids) > 0;
}
/**
* 新增联动控制(业务方法)
*
* @param request 新增联动控制请求
*/
@Override
@Transactional(rollbackFor = Exception.class)
public void addLinkedCtrl(ReqAddLinkedCtrl request) {
// 1. 检查该设备的联动控制数量是否超过3个
long count = baseMapper.selectCount(Wrappers.lambdaQuery(LinkedCtrl.class)
.eq(LinkedCtrl::getDeviceId, request.getDeviceId()));
if (count >= 3) {
throw new ServiceException("联动控制数量不得超过3个");
}
// 2. 查询设备信息
Device device = deviceMapper.selectOne(Wrappers.lambdaQuery(Device.class)
.eq(Device::getId, request.getDeviceId())
.select(Device::getId, Device::getUserId, Device::getPondId,
Device::getDeviceName, Device::getSerialNum, Device::getWarnCode, Device::getDeadTime));
if (device == null) {
throw new ServiceException("设备不存在");
}
// 3. 检查设备是否过期
if (isDeviceDead(device)) {
throw new ServiceException("设备已过期");
}
// 4. 检查设备是否已绑定塘口
if (device.getPondId() == null) {
throw new ServiceException("设备未绑定塘口");
}
Long pondId = device.getPondId();
// 5. 验证开关是否属于该塘口
long matchCount = deviceSwitchMapper.selectCount(Wrappers.lambdaQuery(DeviceSwitch.class)
.eq(DeviceSwitch::getPondId, pondId)
.in(DeviceSwitch::getId, request.getListSwitchId()));
if (matchCount != request.getListSwitchId().size()) {
throw new ServiceException("开关不属于该塘口");
}
// 6. 创建联动控制记录
LinkedCtrl linkedCtrl = new LinkedCtrl();
linkedCtrl.setDeviceId(request.getDeviceId());
linkedCtrl.setOxyUpperOpen(0); // 默认关闭
linkedCtrl.setOxyUpperValue(10.0);
linkedCtrl.setIsOxyUpperTrigger(0);
linkedCtrl.setOxyLowerOpen(0); // 默认关闭
linkedCtrl.setOxyLowerValue(5.0);
int inserted = baseMapper.insert(linkedCtrl);
if (inserted == 0) {
throw new ServiceException("新增联动控制失败");
}
Long linkedCtrlId = linkedCtrl.getId();
// 7. 更新开关的linkedCtrlId
int updated = deviceSwitchMapper.update(null,
Wrappers.lambdaUpdate(DeviceSwitch.class)
.set(DeviceSwitch::getLinkedCtrlId, linkedCtrlId)
.in(DeviceSwitch::getId, request.getListSwitchId()));
if (updated == 0) {
throw new ServiceException("请选择联动控制开关");
}
log.info("设备{}({})新增联动控制", device.getDeviceName(), device.getSerialNum());
}
/**
* 修改联动控制(业务方法)
*
* @param request 修改联动控制请求
*/
@Override
@Transactional(rollbackFor = Exception.class)
public void updateLinkedCtrl(ReqUpdateLinkedCtrl request) {
// 1. 验证溶解氧上下限值
if (request.getOxyWarnUpper() < request.getOxyWarnLower() + 1.0) {
throw new ServiceException("溶解氧上限值必须大于下限值1以上");
}
// 2. 查询联动控制信息(包括设备信息)
MPJLambdaWrapper<LinkedCtrl> wrapper = new MPJLambdaWrapper<LinkedCtrl>()
.selectAll(LinkedCtrl.class)
.selectAs(Device::getUserId, LinkedCtrlVo::getUserId)
.selectAs(Device::getPondId, LinkedCtrlVo::getPondId)
.selectAs(Device::getDeviceName, LinkedCtrlVo::getDeviceName)
.selectAs(Device::getSerialNum, LinkedCtrlVo::getSerialNum)
.selectAs(Device::getWarnCode, LinkedCtrlVo::getWarnCode)
.selectAs(Device::getDeadTime, LinkedCtrlVo::getDeadTime)
.leftJoin(Device.class, Device::getId, LinkedCtrl::getDeviceId)
.eq(LinkedCtrl::getId, request.getId());
LinkedCtrlVo linkedCtrlVo = baseMapper.selectJoinOne(LinkedCtrlVo.class, wrapper);
if (linkedCtrlVo == null) {
throw new ServiceException("联动控制不存在");
}
// 3. 查询设备信息用于过期检查
Device device = deviceMapper.selectOne(Wrappers.lambdaQuery(Device.class)
.eq(Device::getId, linkedCtrlVo.getDeviceId())
.select(Device::getId, Device::getDeadTime, Device::getPondId,
Device::getDeviceName, Device::getSerialNum));
if (device == null) {
throw new ServiceException("设备不存在");
}
// 4. 检查设备是否过期
if (isDeviceDead(device)) {
throw new ServiceException("设备已过期");
}
// 5. 验证开关是否属于该塘口
long matchCount = deviceSwitchMapper.selectCount(Wrappers.lambdaQuery(DeviceSwitch.class)
.eq(DeviceSwitch::getPondId, device.getPondId())
.in(DeviceSwitch::getId, request.getListSwitchId()));
if (matchCount != request.getListSwitchId().size()) {
throw new ServiceException("开关不属于该塘口");
}
// 6. 查询当前联动控制已绑定的开关
List<DeviceSwitch> currentSwitches = deviceSwitchMapper.selectList(
Wrappers.lambdaQuery(DeviceSwitch.class)
.eq(DeviceSwitch::getLinkedCtrlId, request.getId())
.select(DeviceSwitch::getId));
List<Long> currentSwitchIds = currentSwitches.stream()
.map(DeviceSwitch::getId)
.collect(Collectors.toList());
// 找出需要移除绑定的开关(在旧列表中但不在新列表中)
List<Long> switchesToRemove = currentSwitchIds.stream()
.filter(id -> !request.getListSwitchId().contains(id))
.collect(Collectors.toList());
// 7. 更新联动控制的溶解氧上下限值
baseMapper.update(null,
Wrappers.lambdaUpdate(LinkedCtrl.class)
.set(LinkedCtrl::getOxyUpperValue, request.getOxyWarnUpper())
.set(LinkedCtrl::getOxyLowerValue, request.getOxyWarnLower())
.eq(LinkedCtrl::getId, request.getId()));
// 8. 更新新开关的linkedCtrlId
deviceSwitchMapper.update(null,
Wrappers.lambdaUpdate(DeviceSwitch.class)
.set(DeviceSwitch::getLinkedCtrlId, request.getId())
.in(DeviceSwitch::getId, request.getListSwitchId()));
// 9. 移除旧开关的linkedCtrlId
if (!switchesToRemove.isEmpty()) {
deviceSwitchMapper.update(null,
Wrappers.lambdaUpdate(DeviceSwitch.class)
.set(DeviceSwitch::getLinkedCtrlId, null)
.in(DeviceSwitch::getId, switchesToRemove));
}
log.info("设备{}({})修改联动控制", device.getDeviceName(), device.getSerialNum());
}
/**
* 判断设备是否已过期
*
* @param device 设备信息
* @return true-已过期false-未过期
*/
private boolean isDeviceDead(Device device) {
Date deadTime = device.getDeadTime();
if (deadTime == null) {
return false;
}
return deadTime.before(new Date());
}
/**
* 根据设备ID查询所有联动控制
*
* @param deviceId 设备ID
* @return 联动控制列表
*/
@Override
public List<PublicLinkedCtrl> queryAllLinkedCtrl(Long deviceId) {
// 1. 查询该设备下的所有联动控制
List<LinkedCtrl> linkedCtrls = baseMapper.selectList(
Wrappers.lambdaQuery(LinkedCtrl.class)
.eq(LinkedCtrl::getDeviceId, deviceId)
.orderByAsc(LinkedCtrl::getId));
if (linkedCtrls.isEmpty()) {
return new ArrayList<>();
}
// 2. 收集所有联动控制ID
List<Long> linkedCtrlIds = linkedCtrls.stream()
.map(LinkedCtrl::getId)
.collect(Collectors.toList());
// 3. 查询与这些联动控制相关的开关
List<DeviceSwitch> switches = deviceSwitchMapper.selectList(
Wrappers.lambdaQuery(DeviceSwitch.class)
.in(DeviceSwitch::getLinkedCtrlId, linkedCtrlIds)
.orderByAsc(DeviceSwitch::getIndex));
// 4. 按linkedCtrlId分组开关
Map<Long, List<DeviceSwitch>> switchMap = switches.stream()
.collect(Collectors.groupingBy(DeviceSwitch::getLinkedCtrlId));
// 5. 构建返回结果
List<PublicLinkedCtrl> result = new ArrayList<>();
for (LinkedCtrl linkedCtrl : linkedCtrls) {
PublicLinkedCtrl dto = new PublicLinkedCtrl();
dto.setId(linkedCtrl.getId());
dto.setDeviceId(linkedCtrl.getDeviceId());
dto.setOxyUpperOpen(linkedCtrl.getOxyUpperOpen());
dto.setOxyUpperValue(linkedCtrl.getOxyUpperValue());
dto.setOxyLowerOpen(linkedCtrl.getOxyLowerOpen());
dto.setOxyLowerValue(linkedCtrl.getOxyLowerValue());
// 转换开关列表
List<DeviceSwitch> linkedSwitches = switchMap.getOrDefault(linkedCtrl.getId(), new ArrayList<>());
List<PublicDeviceSwitchSimple> switchDtos = linkedSwitches.stream()
.map(this::convertToSwitchSimple)
.collect(Collectors.toList());
dto.setListSwitch(switchDtos);
result.add(dto);
}
return result;
}
/**
* 将DeviceSwitch转换为PublicDeviceSwitchSimple
*
* @param deviceSwitch 设备开关
* @return 设备开关简单DTO
*/
private PublicDeviceSwitchSimple convertToSwitchSimple(DeviceSwitch deviceSwitch) {
PublicDeviceSwitchSimple dto = new PublicDeviceSwitchSimple();
dto.setId(deviceSwitch.getId());
dto.setIndex(deviceSwitch.getIndex());
dto.setSwitchName(deviceSwitch.getSwitchName());
return dto;
}
/**
* 设置联动控制开关
*
* @param request 设置开关请求
*/
@Override
@Transactional(rollbackFor = Exception.class)
public void setLinkedOpen(ReqLinkedCtrlOpen request) {
// 1. 查询联动控制信息(包括设备信息)
MPJLambdaWrapper<LinkedCtrl> wrapper = new MPJLambdaWrapper<LinkedCtrl>()
.selectAll(LinkedCtrl.class)
.selectAs(Device::getUserId, LinkedCtrlVo::getUserId)
.selectAs(Device::getPondId, LinkedCtrlVo::getPondId)
.selectAs(Device::getDeviceName, LinkedCtrlVo::getDeviceName)
.selectAs(Device::getSerialNum, LinkedCtrlVo::getSerialNum)
.selectAs(Device::getWarnCode, LinkedCtrlVo::getWarnCode)
.selectAs(Device::getDeadTime, LinkedCtrlVo::getDeadTime)
.leftJoin(Device.class, Device::getId, LinkedCtrl::getDeviceId)
.eq(LinkedCtrl::getId, request.getId());
LinkedCtrlVo linkedCtrlVo = baseMapper.selectJoinOne(LinkedCtrlVo.class, wrapper);
if (linkedCtrlVo == null) {
throw new ServiceException("联动控制不存在");
}
// 2. 查询设备信息用于过期检查
Device device = deviceMapper.selectOne(Wrappers.lambdaQuery(Device.class)
.eq(Device::getId, linkedCtrlVo.getDeviceId())
.select(Device::getId, Device::getDeadTime, Device::getDeviceName, Device::getSerialNum));
if (device == null) {
throw new ServiceException("设备不存在");
}
// 3. 检查设备是否过期
if (isDeviceDead(device)) {
throw new ServiceException("设备已过期");
}
// 4. 根据开关类型设置对应的开关
if (request.getOpenType() == 1) {
// 上限开关
if (linkedCtrlVo.getOxyUpperOpen().equals(request.getIsOpen())) {
// 状态未变化,直接返回
return;
}
// 更新上限开关状态和触发标志
baseMapper.update(null,
Wrappers.lambdaUpdate(LinkedCtrl.class)
.set(LinkedCtrl::getOxyUpperOpen, request.getIsOpen())
.set(LinkedCtrl::getIsOxyUpperTrigger, 0) // 重置触发标志
.eq(LinkedCtrl::getId, request.getId()));
String op = request.getIsOpen() == 1 ? "开启" : "关闭";
log.info("设备{}({})溶解氧上限联动控制:{}",
device.getDeviceName(), device.getSerialNum(), op);
} else if (request.getOpenType() == 2) {
// 下限开关
if (linkedCtrlVo.getOxyLowerOpen().equals(request.getIsOpen())) {
// 状态未变化,直接返回
return;
}
// 更新下限开关状态
baseMapper.update(null,
Wrappers.lambdaUpdate(LinkedCtrl.class)
.set(LinkedCtrl::getOxyLowerOpen, request.getIsOpen())
.eq(LinkedCtrl::getId, request.getId()));
String op = request.getIsOpen() == 1 ? "开启" : "关闭";
log.info("设备{}({})溶解氧下限联动控制:{}",
device.getDeviceName(), device.getSerialNum(), op);
} else {
throw new ServiceException("无效的开关类型");
}
}
}