fix: 绑定解绑记录,新增接口。
This commit is contained in:
@@ -11,6 +11,7 @@ import com.intc.common.mybatis.core.page.PageQuery;
|
||||
import com.intc.common.mybatis.core.page.TableDataInfo;
|
||||
import com.intc.fishery.domain.AquUser;
|
||||
import com.intc.fishery.domain.Device;
|
||||
import com.intc.fishery.domain.DeviceBindRecord;
|
||||
import com.intc.fishery.domain.DeviceCorrectRecord;
|
||||
import com.intc.fishery.domain.DeviceSwitch;
|
||||
import com.intc.fishery.domain.LinkedCtrl;
|
||||
@@ -18,6 +19,7 @@ import com.intc.fishery.domain.Pond;
|
||||
import com.intc.fishery.domain.TimingCtrl;
|
||||
import com.intc.fishery.domain.bo.DeviceBo;
|
||||
import com.intc.fishery.domain.vo.DeviceVo;
|
||||
import com.intc.fishery.mapper.DeviceBindRecordMapper;
|
||||
import com.intc.fishery.mapper.DeviceCorrectRecordMapper;
|
||||
import com.intc.fishery.mapper.DeviceMapper;
|
||||
import com.intc.fishery.mapper.DeviceSwitchMapper;
|
||||
@@ -45,6 +47,7 @@ import lombok.extern.slf4j.Slf4j;
|
||||
public class DeviceServiceImpl implements IDeviceService {
|
||||
|
||||
private final DeviceMapper baseMapper;
|
||||
private final DeviceBindRecordMapper deviceBindRecordMapper;
|
||||
private final DeviceCorrectRecordMapper deviceCorrectRecordMapper;
|
||||
private final DeviceSwitchMapper deviceSwitchMapper;
|
||||
private final LinkedCtrlMapper linkedCtrlMapper;
|
||||
@@ -291,7 +294,33 @@ public class DeviceServiceImpl implements IDeviceService {
|
||||
.in(LinkedCtrl::getDeviceId, ids)
|
||||
);
|
||||
|
||||
// 8. 最后删除设备
|
||||
// 8. 查询要删除的设备信息,添加解绑记录
|
||||
List<Device> devicesToDelete = baseMapper.selectList(
|
||||
new LambdaQueryWrapper<Device>()
|
||||
.in(Device::getId, ids)
|
||||
.select(Device::getId, Device::getUserId, Device::getIotId,
|
||||
Device::getDeviceType, Device::getSerialNum)
|
||||
);
|
||||
|
||||
// 为每个设备添加解绑记录
|
||||
for (Device device : devicesToDelete) {
|
||||
try {
|
||||
DeviceBindRecord unbindRecord = new DeviceBindRecord();
|
||||
unbindRecord.setUserId(device.getUserId());
|
||||
unbindRecord.setIsBind(0); // 0表示解绑
|
||||
unbindRecord.setIotId(device.getIotId());
|
||||
unbindRecord.setDeviceType(device.getDeviceType() != null ? Long.valueOf(device.getDeviceType()) : null);
|
||||
unbindRecord.setSerialNum(device.getSerialNum());
|
||||
deviceBindRecordMapper.insert(unbindRecord);
|
||||
log.info("添加设备解绑记录: deviceId={}, userId={}, serialNum={}",
|
||||
device.getId(), device.getUserId(), device.getSerialNum());
|
||||
} catch (Exception e) {
|
||||
log.error("添加设备解绑记录失败: deviceId={}, error={}", device.getId(), e.getMessage(), e);
|
||||
// 不影响删除流程,继续执行
|
||||
}
|
||||
}
|
||||
|
||||
// 9. 最后删除设备
|
||||
return baseMapper.deleteByIds(ids) > 0;
|
||||
}
|
||||
|
||||
|
||||
@@ -12,6 +12,8 @@ import com.intc.fishery.domain.TimingCtrl;
|
||||
import com.intc.fishery.domain.Pond;
|
||||
import com.intc.fishery.domain.LinkedCtrl;
|
||||
import com.intc.fishery.domain.DeviceCorrectRecord;
|
||||
import com.intc.fishery.domain.DeviceBindRecord;
|
||||
import com.intc.fishery.domain.DeviceErrorCode;
|
||||
import com.intc.fishery.domain.AquUser;
|
||||
import com.intc.fishery.domain.bo.*;
|
||||
import com.intc.fishery.domain.vo.DeviceSwitchVo;
|
||||
@@ -19,6 +21,8 @@ import com.intc.fishery.mapper.DeviceMapper;
|
||||
import com.intc.fishery.mapper.PondMapper;
|
||||
import com.intc.fishery.mapper.DeviceSwitchMapper;
|
||||
import com.intc.fishery.mapper.DeviceCorrectRecordMapper;
|
||||
import com.intc.fishery.mapper.DeviceBindRecordMapper;
|
||||
import com.intc.fishery.mapper.DeviceErrorCodeMapper;
|
||||
import com.intc.fishery.mapper.TimingCtrlMapper;
|
||||
import com.intc.iot.domain.bo.AddDeviceControllerBo;
|
||||
import com.intc.iot.domain.bo.AddDeviceDetectorBo;
|
||||
@@ -109,6 +113,12 @@ public class IotController extends BaseController {
|
||||
@Autowired(required = false)
|
||||
private TimingCtrlMapper timingCtrlMapper;
|
||||
|
||||
@Autowired(required = false)
|
||||
private DeviceBindRecordMapper deviceBindRecordMapper;
|
||||
|
||||
@Autowired(required = false)
|
||||
private DeviceErrorCodeMapper deviceErrorCodeMapper;
|
||||
|
||||
@Autowired(required = false)
|
||||
private IotCloudService iotCloudService;
|
||||
|
||||
@@ -1094,6 +1104,9 @@ public class IotController extends BaseController {
|
||||
|
||||
device.setWarnCode(warnCode);
|
||||
|
||||
// 添加绑定记录
|
||||
addNewDeviceBindRecord(device, true);
|
||||
|
||||
// 保存到数据库
|
||||
if (isNew) {
|
||||
deviceMapper.insert(device);
|
||||
@@ -1109,7 +1122,17 @@ public class IotController extends BaseController {
|
||||
deviceSwitchMapper.insert(deviceSwitch);
|
||||
}
|
||||
|
||||
// TODO: 记录绑定历史和故障码(如果需要的话)
|
||||
// 保存故障码记录
|
||||
if (errorCode != 0) {
|
||||
Date createTime = new Date();
|
||||
DeviceErrorCode errorCodeData = new DeviceErrorCode();
|
||||
errorCodeData.setDeviceId(device.getId());
|
||||
errorCodeData.setSwitchIndex(0); // 0表示设备本身的故障码
|
||||
errorCodeData.setErrorCode(errorCode);
|
||||
// 注意:DeviceErrorCode继承自TenantEntity,已包含createTime和updateTime字段
|
||||
deviceErrorCodeMapper.insert(errorCodeData);
|
||||
log.info("保存设备故障码: deviceId={}, errorCode={}", device.getId(), errorCode);
|
||||
}
|
||||
|
||||
return R.ok();
|
||||
} catch (Exception e) {
|
||||
@@ -1390,6 +1413,9 @@ public class IotController extends BaseController {
|
||||
return R.fail("设备缺少物联网卡号(ICCID)");
|
||||
}
|
||||
|
||||
// 添加绑定记录
|
||||
addNewDeviceBindRecord(device, true);
|
||||
|
||||
// 保存到数据库
|
||||
if (isNew) {
|
||||
deviceMapper.insert(device);
|
||||
@@ -1503,6 +1529,14 @@ public class IotController extends BaseController {
|
||||
deviceCorrectRecordMapper.insert(correctRecord);
|
||||
}
|
||||
|
||||
// 添加操作记录
|
||||
MessageOpRecordUtil.addMessageOpRecordUser(
|
||||
device.getUserId(),
|
||||
userId,
|
||||
"设备操作",
|
||||
String.format("%s(%s),设备校准。", device.getDeviceName(), device.getSerialNum())
|
||||
);
|
||||
|
||||
log.info("设备校准成功: userId={}, deviceId={}, deviceName={}, serialNum={}",
|
||||
userId, device.getId(), device.getDeviceName(), device.getSerialNum());
|
||||
|
||||
@@ -2637,4 +2671,35 @@ public class IotController extends BaseController {
|
||||
return R.fail("删除失败: " + e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 添加新设备绑定记录
|
||||
*
|
||||
* @param device 设备对象
|
||||
* @param isBind 是否绑定(true-绑定,false-解绑)
|
||||
*/
|
||||
private void addNewDeviceBindRecord(Device device, boolean isBind) {
|
||||
if (deviceBindRecordMapper == null) {
|
||||
log.warn("DeviceBindRecordMapper未注入,跳过添加绑定记录");
|
||||
return;
|
||||
}
|
||||
|
||||
if (device.getUserId() == null) {
|
||||
log.warn("设备userId为空,跳过添加绑定记录");
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
DeviceBindRecord deviceBind = new DeviceBindRecord();
|
||||
deviceBind.setUserId(device.getUserId());
|
||||
deviceBind.setIsBind(isBind ? 1 : 0);
|
||||
deviceBind.setIotId(device.getIotId());
|
||||
deviceBind.setDeviceType(device.getDeviceType() != null ? Long.valueOf(device.getDeviceType()) : null);
|
||||
deviceBind.setSerialNum(device.getSerialNum());
|
||||
deviceBindRecordMapper.insert(deviceBind);
|
||||
log.info("添加设备绑定记录成功: userId={}, iotId={}, isBind={}", device.getUserId(), device.getIotId(), isBind);
|
||||
} catch (Exception e) {
|
||||
log.error("添加设备绑定记录失败: {}", e.getMessage(), e);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user