|
|
@@ -1,15 +1,25 @@
|
|
|
package com.platform.manage;
|
|
|
|
|
|
-import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
|
|
|
+import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
|
|
+import com.baomidou.mybatisplus.core.metadata.IPage;
|
|
|
+import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
|
|
|
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
|
|
|
+import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
|
|
import com.platform.dto.DeviceExcelTemplateDTO;
|
|
|
import com.platform.entity.IotDevice;
|
|
|
+import com.platform.entity.IotDeviceGroup;
|
|
|
+import com.platform.entity.IotMod;
|
|
|
+import com.platform.enums.ErrorCodeEnum;
|
|
|
import com.platform.exception.BusinessException;
|
|
|
-import com.platform.request.device.DeviceDetailReq;
|
|
|
+import com.platform.exception.IotException;
|
|
|
+import com.platform.exception.PageResult;
|
|
|
+import com.platform.request.device.IotDeviceDetailRequest;
|
|
|
import com.platform.request.device.IotDeviceRequest;
|
|
|
-import com.platform.response.device.DeviceDetailResp;
|
|
|
-import com.platform.response.device.DevicePageResp;
|
|
|
+import com.platform.request.device.IotDeviceSaveRequest;
|
|
|
+import com.platform.response.device.IotDeviceResp;
|
|
|
+import com.platform.service.IotDeviceGroupService;
|
|
|
import com.platform.service.IotDeviceService;
|
|
|
+import com.platform.service.IotModService;
|
|
|
import com.platform.utils.EasyExcelUtil;
|
|
|
import jakarta.servlet.http.HttpServletResponse;
|
|
|
import lombok.RequiredArgsConstructor;
|
|
|
@@ -17,8 +27,8 @@ import org.apache.commons.lang3.StringUtils;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
import org.springframework.web.multipart.MultipartFile;
|
|
|
|
|
|
-import java.io.IOException;
|
|
|
import java.io.InputStream;
|
|
|
+import java.util.ArrayList;
|
|
|
import java.util.List;
|
|
|
import java.util.Objects;
|
|
|
|
|
|
@@ -29,42 +39,131 @@ import java.util.Objects;
|
|
|
@Service
|
|
|
@RequiredArgsConstructor
|
|
|
public class IotDeviceManage {
|
|
|
+
|
|
|
+
|
|
|
private final IotDeviceService iotDeviceService;
|
|
|
- public Boolean addDevice(IotDeviceRequest request) {
|
|
|
+
|
|
|
+ private final IotDeviceGroupService iotDeviceGroupService;
|
|
|
+
|
|
|
+ private final IotModService iotModService;
|
|
|
+
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 设备分页查询
|
|
|
+ *
|
|
|
+ * @param request
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public PageResult<IotDeviceResp> pageDeviceList(IotDeviceRequest request) {
|
|
|
+ IPage<IotDevice> page = new Page<>(request.getPageNum(), request.getPageSize());
|
|
|
+ LambdaQueryWrapper<IotDevice> queryWrapper = Wrappers.<IotDevice>lambdaQuery()
|
|
|
+ .in(CollectionUtils.isNotEmpty(request.getDeviceGroupCodes()), IotDevice::getDeviceGroupCode, request.getDeviceGroupCodes())
|
|
|
+ .eq(StringUtils.isNotBlank(request.getDeviceCode()), IotDevice::getDeviceGroupCode, request.getDeviceGroupCodes())
|
|
|
+ .eq(IotDevice::getDelFlag, 0);
|
|
|
+
|
|
|
+ //物模型名称模糊查询
|
|
|
+ if (StringUtils.isNotBlank(request.getModName())) {
|
|
|
+ LambdaQueryWrapper<IotMod> modQueryWrapper = Wrappers.<IotMod>lambdaQuery()
|
|
|
+ .like(IotMod::getName, request.getModName())
|
|
|
+ .eq(IotMod::getDelFlag, 0L);
|
|
|
+ List<IotMod> modList = iotModService.list(modQueryWrapper);
|
|
|
+ List<String> modCodeList = modList.stream().map(IotMod::getModCode).toList();
|
|
|
+ queryWrapper.in(IotDevice::getModCode, modCodeList);
|
|
|
+ }
|
|
|
+ List<IotDevice> deviceList = iotDeviceService.list(page, queryWrapper);
|
|
|
+
|
|
|
+ //若为null,返回空分页结果
|
|
|
+ if (CollectionUtils.isEmpty(deviceList)) {
|
|
|
+ return PageResult.empty(request.getPageNum(), request.getPageSize());
|
|
|
+ }
|
|
|
+
|
|
|
+ List<IotDeviceResp> deviceRespList = new ArrayList<>();
|
|
|
+ for (IotDevice device : deviceList) {
|
|
|
+ IotDeviceResp deviceResp = IotDeviceResp.toIotPageResp(device);
|
|
|
+ //设备分组名称
|
|
|
+ IotDeviceGroup deviceGroup = iotDeviceGroupService.getOne(Wrappers.<IotDeviceGroup>lambdaQuery()
|
|
|
+ .eq(IotDeviceGroup::getDeviceGroupCode, device.getDeviceGroupCode()));
|
|
|
+ deviceResp.setDeviceGroupName(deviceGroup.getTitle());
|
|
|
+ //物模型名称
|
|
|
+ IotMod iotMod = iotModService.getOne(Wrappers.<IotMod>lambdaQuery()
|
|
|
+ .eq(IotMod::getModCode, device.getModCode()));
|
|
|
+ deviceResp.setModName(iotMod.getName());
|
|
|
+ deviceRespList.add(deviceResp);
|
|
|
+ }
|
|
|
+ return PageResult.of(deviceRespList.size(), request.getPageNum(), request.getPageSize(), deviceRespList);
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 添加设备
|
|
|
+ *
|
|
|
+ * @param request
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public Boolean addDevice(IotDeviceSaveRequest request) {
|
|
|
IotDevice oneByName = iotDeviceService.getOneByName(request.getDeviceName());
|
|
|
- if (Objects.nonNull(oneByName)){
|
|
|
+ if (Objects.nonNull(oneByName)) {
|
|
|
throw new BusinessException("设备名称已存在");
|
|
|
}
|
|
|
IotDevice iotDevice = IotDevice.toIotDevice(request);
|
|
|
return iotDeviceService.save(iotDevice);
|
|
|
}
|
|
|
|
|
|
- public Boolean updateDevice(IotDeviceRequest request) {
|
|
|
- IotDevice iotDevice = IotDevice.toIotDevice(request);
|
|
|
- if (StringUtils.isBlank(iotDevice.getDeviceCode())){
|
|
|
- throw new BusinessException("设备编号不能为空");
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 更新设备
|
|
|
+ * @param request
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public Boolean updateDevice(IotDeviceSaveRequest request) {
|
|
|
+ if (request.getId() == null) {
|
|
|
+ throw new IotException(ErrorCodeEnum.DATA_DELETE_FAIL, "设备主键id不能为null");
|
|
|
}
|
|
|
- LambdaUpdateWrapper<IotDevice> updateWrapper = Wrappers.<IotDevice>lambdaUpdate()
|
|
|
- .eq(IotDevice::getDeviceCode, iotDevice.getDeviceCode());
|
|
|
- return iotDeviceService.update(iotDevice,updateWrapper);
|
|
|
+ IotDevice iotDevice = IotDevice.toIotDevice(request);
|
|
|
+ iotDevice.setId(request.getId());
|
|
|
+ return iotDeviceService.updateById(iotDevice);
|
|
|
}
|
|
|
|
|
|
- public Boolean deleteDevice(String guid) {
|
|
|
- return iotDeviceService.deleteDevice(guid);
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 删除
|
|
|
+ * @param id
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public Boolean deleteDevice(String id) {
|
|
|
+ if (id == null) {
|
|
|
+ throw new IotException(ErrorCodeEnum.DATA_DELETE_FAIL, "设备主键id不能为null");
|
|
|
+ }
|
|
|
+ return iotDeviceService.deleteDevice(id);
|
|
|
}
|
|
|
|
|
|
- public DeviceDetailResp getDeviceDetail(DeviceDetailReq req) {
|
|
|
- IotDevice oneByGuid = iotDeviceService.getOneByGuid(req.getGuid());
|
|
|
- DeviceDetailResp instance = DeviceDetailResp.getInstance();
|
|
|
- DeviceDetailResp.toDeviceDetailResp(oneByGuid);
|
|
|
- return null;
|
|
|
+ /**
|
|
|
+ * 设备详情
|
|
|
+ * @param request
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public IotDeviceResp getDeviceDetail(IotDeviceDetailRequest request) {
|
|
|
+ IotDevice iotDevice = iotDeviceService.getById(request.getId());
|
|
|
+ IotDeviceResp deviceResp = IotDeviceResp.toIotPageResp(iotDevice);
|
|
|
+ //设备分组名称
|
|
|
+ IotDeviceGroup deviceGroup = iotDeviceGroupService.getOne(Wrappers.<IotDeviceGroup>lambdaQuery()
|
|
|
+ .eq(IotDeviceGroup::getDeviceGroupCode, iotDevice.getDeviceGroupCode()));
|
|
|
+ deviceResp.setDeviceGroupName(deviceGroup.getTitle());
|
|
|
+ //物模型名称
|
|
|
+ IotMod iotMod = iotModService.getOne(Wrappers.<IotMod>lambdaQuery()
|
|
|
+ .eq(IotMod::getModCode, iotDevice.getModCode()));
|
|
|
+ deviceResp.setModName(iotMod.getName());
|
|
|
|
|
|
- }
|
|
|
+ return deviceResp;
|
|
|
|
|
|
- public DevicePageResp pageDeviceDetail(DeviceDetailReq req) {
|
|
|
- return null;
|
|
|
}
|
|
|
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 导出
|
|
|
+ * @param response
|
|
|
+ * @return
|
|
|
+ */
|
|
|
public Void exportTemplate(HttpServletResponse response) {
|
|
|
try {
|
|
|
EasyExcelUtil.exportTemplate("设备导入模板", DeviceExcelTemplateDTO.class, response);
|
|
|
@@ -75,13 +174,18 @@ public class IotDeviceManage {
|
|
|
}
|
|
|
|
|
|
|
|
|
+ /**
|
|
|
+ * 导入
|
|
|
+ * @param file
|
|
|
+ * @return
|
|
|
+ */
|
|
|
public Void importExcel(MultipartFile file) {
|
|
|
if (file.isEmpty()) {
|
|
|
throw new BusinessException("文件不能为空");
|
|
|
}
|
|
|
try {
|
|
|
InputStream inputStream = file.getInputStream();
|
|
|
- List<DeviceDetailReq> deviceDetailReqs = EasyExcelUtil.importExcel(inputStream, DeviceDetailReq.class);
|
|
|
+ List<IotDeviceResp> deviceDetailReqs = EasyExcelUtil.importExcel(inputStream, IotDeviceResp.class);
|
|
|
//todo cxf 导入设备
|
|
|
} catch (Exception e) {
|
|
|
throw new BusinessException("设备导入异常");
|