增加修改放一接口

This commit is contained in:
wanxiaoli 2026-01-16 17:26:44 +08:00
parent 7a50fdb1cf
commit 8c0b313b0b
3 changed files with 32 additions and 2 deletions

View File

@ -34,7 +34,13 @@ public class DeviceController {
@PostMapping @PostMapping
public boolean create(@RequestBody Device device) { public boolean create(@RequestBody Device device) {
device.setModifier(currentUsername()); device.setModifier(currentUsername());
return deviceService.save(device); return deviceService.createDevice(device);
}
@PostMapping("/saveOrUpdate")
public boolean saveOrUpdate(@RequestBody Device device) {
device.setModifier(currentUsername());
return deviceService.saveOrUpdateByBusiness(device);
} }
/** /**

View File

@ -9,5 +9,7 @@ public interface DeviceService extends IService<Device> {
*/ */
boolean importDevices(MultipartFile file, String deviceType); boolean importDevices(MultipartFile file, String deviceType);
public boolean createDevice(Device device) ; boolean createDevice(Device device) ;
boolean saveOrUpdateByBusiness(Device device);
} }

View File

@ -67,6 +67,28 @@ public class DeviceServiceImpl
// 2. 如果前端传了 deviceId直接使用 // 2. 如果前端传了 deviceId直接使用
return this.save(device); return this.save(device);
} }
@Override
public boolean saveOrUpdateByBusiness(Device device) {
String deviceId = device.getDeviceId();
// 情况 1未传 deviceId 直接新增
if (StrUtil.isBlank(deviceId)) {
return this.save(device);
}
// 情况 2传了 deviceId判断是否存在
Device dbDevice = this.getById(deviceId);
if (dbDevice == null) {
// 数据库不存在 新增使用传入的 deviceId
return this.save(device);
} else {
// 数据库存在 更新
return this.updateById(device);
}
}
private boolean importExcel(Workbook workbook, String deviceType) { private boolean importExcel(Workbook workbook, String deviceType) {
try (Workbook wb = workbook) { try (Workbook wb = workbook) {