#修改模型的update接口

This commit is contained in:
2026-05-08 14:57:37 +08:00
parent 66e696d56e
commit f6f4365a1a
2 changed files with 84 additions and 253 deletions

View File

@@ -1,13 +1,20 @@
package com.maibu.web.controller.system;
import cn.hutool.core.convert.Convert;
import com.maibu.core.business.DeviceModel;
import com.maibu.core.business.Product;
import com.maibu.core.controller.BaseController;
import com.maibu.core.domain.AjaxResult;
import com.maibu.core.page.TableDataInfo;
import com.maibu.service.DeviceProductService;
import com.maibu.service.SysDeviceModelService;
import com.maibu.utils.ServletUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import java.util.List;
@@ -17,268 +24,92 @@ import java.util.List;
*/
@Api(tags = "厂家模块")
@RestController
@RequestMapping("/system/deviceproduct")
@RequestMapping("/system/product")
public class SysProductController extends BaseController {
@Autowired
private SysDeviceModelService deviceModelService;
/*@Autowired
private DeviceRunStatisticsMapper deviceRunStatisticsMapper;*/
@PostMapping("/add")
@ApiOperation("新增设备模型")
public AjaxResult add(@RequestBody DeviceModel deviceModel) {
// 参数校验
if (deviceModel.getName() == null || deviceModel.getName().isEmpty()) {
return AjaxResult.error("模型名称不能为空");
}
// 至少需要一个属性或动作
if (CollectionUtils.isEmpty(deviceModel.getSupportProperties())
&& CollectionUtils.isEmpty(deviceModel.getSupportActions())) {
return AjaxResult.error("至少需要定义一个属性或动作");
}
int rows = deviceModelService.insertDeviceModel(deviceModel);
return toAjax(rows);
}
//@PreAuthorize("@ss.hasPermi('iot:deviceModel:list')")
/* @GetMapping("/list")
@ApiOperation("查询设备模型列表")
public AjaxResult list(DeviceModel deviceModel) {
List<DeviceModel> list = deviceModelService.selectDeviceModelList(deviceModel);
return AjaxResult.success(list);
}*/
// @PreAuthorize("@ss.hasPermi('iot:deviceModel:edit')")
@PostMapping("/update") // 或者 @PutMapping
@ApiOperation("修改设备模型")
public AjaxResult edit(@RequestBody DeviceModel deviceModel) {
// 1. 校验 ID
if (deviceModel.getId() == null) {
return AjaxResult.error("模型ID不能为空");
}
// 2. 检查是否存在
DeviceModel existModel = deviceModelService.selectDeviceModelById(deviceModel.getId());
if (existModel == null) {
return AjaxResult.error("模型不存在");
}
// 3. 执行更新
int rows = deviceModelService.updateDeviceModel(deviceModel);
return toAjax(rows);
}
//
// /**
// * 查询模型的列表
// */
// @PreAuthorize("@ss.hasPermi('system:devicemodel:list')")
// @GetMapping("/list")
// @ApiOperation("设备分页列表")
// public TableDataInfo list(Device device) {
// startPage();
// return getDataTable(deviceService.selectDeviceList(device));
// }
//
//
// @GetMapping("/userDevice")
// public AjaxResult userDevice(Device device) {
// return AjaxResult.success(deviceService.selectDeviceList(device));
// }
//
//
// /**
// * 导出设备列表
// */
// @PreAuthorize("@ss.hasPermi('system:devicemodel:export')")
// @Log(title = "设备", businessType = BusinessType.EXPORT)
// @PostMapping("/export")
// @ApiOperation("导出设备")
// public void export(HttpServletResponse response, Device device) {
// List<Device> list = deviceService.selectDeviceList(device);
// ExcelUtil<Device> util = new ExcelUtil<Device>(Device.class);
// util.exportExcel(response, list, "设备数据");
// }
//
//
// /**
// * 根据设备编号详细信息
// */
// @PreAuthorize("@ss.hasPermi('iot:device:query')")
// @GetMapping(value = "/getDeviceBySerialNumber/{serialNumber}")
// @ApiOperation("根据设备编号获取设备详情")
// public AjaxResult getInfoBySerialNumber(@PathVariable("serialNumber") String serialNumber) {
// return AjaxResult.success(deviceService.selectDeviceBySerialNumber(serialNumber));
// }
private DeviceProductService productService;
/**
* 重置设备状态
* 分页查询产品列表
*/
/* @PreAuthorize("@ss.hasPermi('iot:device:edit')")
@Log(title = "重置设备状态", businessType = BusinessType.UPDATE)
@PutMapping("/reset/{serialNumber}")
@ApiOperation("重置设备状态")
public AjaxResult resetDeviceStatus(@PathVariable String serialNumber) {
Device device = new Device();
device.setSerialNumber(serialNumber);
return toAjax(deviceService.resetDeviceStatus(device.getSerialNumber()));
}
*/
// @PreAuthorize("@ss.hasPermi('iot:product:list')")
@GetMapping("/list")
@ApiOperation("查询产品列表")
public TableDataInfo list(Product product) {
Integer pageNum = Convert.toInt(ServletUtils.getParameter("pageNum"), 1);
Integer pageSize = Convert.toInt(ServletUtils.getParameter("pageSize"), 10);
/* @GetMapping("/getDeviceRunStatistics")
public AjaxResult getDeviceRunStatistics(@RequestParam("deviceId") String deviceId) {
if (StringUtils.isEmpty(deviceId)) {
return error("请选择设备");
}
LambdaQueryWrapperX<DeviceRunStatistics> queryWrapperX = new LambdaQueryWrapperX<>();
queryWrapperX.eq(DeviceRunStatistics::getDeviceId, deviceId);
queryWrapperX.orderByDesc(DeviceRunStatistics::getEndTime);
queryWrapperX.last("LIMIT 10");
return AjaxResult.success(deviceRunStatisticsMapper.selectList(queryWrapperX));
}
*/
/* @GetMapping("/getDeviceIntradayData")
public AjaxResult getDeviceIntradayData(@RequestParam("deviceId") String deviceId) {
List<DeviceRunStatistics> resultList = getIntradayData();
DeviceRunStatistics statistics = null;
if (!CollectionUtils.isEmpty(resultList)) {
statistics = resultList.stream().filter(x -> deviceId.equals(x.getDeviceId())).findFirst().orElse(null);
}
return AjaxResult.success(statistics);
Page<Product> page = new Page<>(pageNum, pageSize);
Page<Product> resultPage = productService.selectProductPage(page, product);
return getDataTable(resultPage.getRecords(), resultPage.getTotal());
}
public List<DeviceRunStatistics> getIntradayData() {
LambdaQueryWrapperX<DeviceRunStatistics> wrapper = new LambdaQueryWrapperX<>();
LocalDateTime start = LocalDate.now().atStartOfDay(); // 今天 00:00:00
LocalDateTime end = LocalDate.now().plusDays(1).atStartOfDay(); // 明天 00:00:00
wrapper.ge(DeviceRunStatistics::getStartTime, start)
.lt(DeviceRunStatistics::getStartTime, end);
List<DeviceRunStatistics> list = deviceRunStatisticsMapper.selectList(wrapper);
List<DeviceRunStatistics> resultList = new ArrayList<>();
if (!CollectionUtils.isEmpty(list)) {
resultList = list.stream()
.collect(Collectors.groupingBy(DeviceRunStatistics::getDeviceId))
.values()
.stream()
.map(group -> {
DeviceRunStatistics sum = new DeviceRunStatistics();
sum.setDeviceId(group.get(0).getDeviceId());
sum.setWorkArea(
group.stream().mapToDouble(DeviceRunStatistics::getWorkArea).sum()
);
sum.setTime(
group.stream().mapToLong(DeviceRunStatistics::getTime).sum()
);
return sum;
})
.collect(Collectors.toList());
/**
* 新增产品
*/
//@PreAuthorize("@ss.hasPermi('iot:product:add')")
@PostMapping("/add")
@ApiOperation("新增产品")
public AjaxResult add(@RequestBody Product product) {
// 参数校验
if (product.getName() == null || product.getName().isEmpty()) {
return AjaxResult.error("产品名称不能为空");
}
List<String> l = resultList.stream().map(DeviceRunStatistics::getDeviceId).collect(Collectors.toList());
Device d = new Device();
d.setParams(new HashMap<>());
List<Device> query = deviceService.selectDeviceList(d);
if (!CollectionUtils.isEmpty(query) && !CollectionUtils.isEmpty(l)) {
for (Device device : query) {
if (!l.contains(device.getDeviceName())) {
DeviceRunStatistics statistics = new DeviceRunStatistics();
statistics.setDeviceId(device.getDeviceName());
statistics.setTime(0L);
statistics.setWorkArea(0.0);
resultList.add(statistics);
}
}
}
return resultList;
}*/
/* @GetMapping("/generateOperationReport")
public void generateOperationReport(HttpServletResponse response) throws UnsupportedEncodingException {
List<DeviceRunStatistics> resultList = getIntradayData();
// ✅ 4. 响应头(下载)
response.setContentType(
"application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
String fileName = URLEncoder.encode("设备统计.xlsx", "UTF-8");
response.setHeader("Content-Disposition",
"attachment;filename=" + fileName);
try (Workbook workbook = new XSSFWorkbook();
OutputStream os = response.getOutputStream()) {
Sheet sheet = workbook.createSheet("当日工作量统计");
// ✅ 1. 表头
Row header = sheet.createRow(0);
header.createCell(0).setCellValue("设备号");
header.createCell(1).setCellValue("工作面积(m2)");
header.createCell(2).setCellValue("工作时长(分钟)");
// ✅ 2. 数据
for (int i = 0; i < resultList.size(); i++) {
DeviceRunStatistics d = resultList.get(i);
Row row = sheet.createRow(i + 1);
row.createCell(0).setCellValue(d.getDeviceId());
row.createCell(1).setCellValue(d.getWorkArea());
row.createCell(2).setCellValue(d.getTime());
}
// ✅ 3. 自动列宽(可选)
for (int i = 0; i < 3; i++) {
sheet.autoSizeColumn(i);
}
// ✅ 5. 写出
workbook.write(os);
os.flush();
} catch (Exception e) {
e.printStackTrace();
if (product.getCode() == null || product.getCode().isEmpty()) {
return AjaxResult.error("产品编码不能为空");
}
}*/
// 检查编码是否重复
Product existProduct = productService.selectByCode(product.getCode());
if (existProduct != null) {
return AjaxResult.error("产品编码已存在");
}
boolean success = productService.save(product);
return toAjax(success ? 1 : 0);
}
/**
* 修改产品
*/
//@PreAuthorize("@ss.hasPermi('iot:product:edit')")
@PostMapping("/update")
@ApiOperation("修改产品")
public AjaxResult edit(@RequestBody Product product) {
if (product.getId() == null) {
return AjaxResult.error("产品ID不能为空");
}
// 检查产品是否存在
Product existProduct = productService.getById(product.getId());
if (existProduct == null) {
return AjaxResult.error("产品不存在");
}
// 如果修改了编码,检查是否重复
if (product.getCode() != null && !product.getCode().equals(existProduct.getCode())) {
Product duplicate = productService.selectByCode(product.getCode());
if (duplicate != null) {
return AjaxResult.error("产品编码已存在");
}
}
boolean success = productService.updateById(product);
return toAjax(success ? 1 : 0);
}
/**
* 删除产品
*/
//@PreAuthorize("@ss.hasPermi('iot:product:remove')")
@DeleteMapping("delete/{id}")
@ApiOperation("删除产品")
public AjaxResult remove(@PathVariable Long id) {
boolean success = productService.removeById(id);
return toAjax(success ? 1 : 0);
}
}