feat: 设备分组批量导入

V0.5.x
gaoyoulong 2024-03-08 11:20:30 +08:00
parent 246c6b4c37
commit b2524e67b4
5 changed files with 178 additions and 4 deletions

View File

@ -12,6 +12,7 @@ package cc.iotkit.manager.controller;
import cc.iotkit.common.api.PageRequest;
import cc.iotkit.common.api.Paging;
import cc.iotkit.common.api.Request;
import cc.iotkit.common.excel.utils.ExcelUtil;
import cc.iotkit.common.thing.ThingModelMessage;
import cc.iotkit.manager.dto.bo.device.*;
import cc.iotkit.manager.dto.bo.deviceconfig.DeviceConfigAddBo;
@ -19,6 +20,7 @@ import cc.iotkit.manager.dto.bo.devicegroup.DeviceAddGroupBo;
import cc.iotkit.manager.dto.bo.devicegroup.DeviceGroupBo;
import cc.iotkit.manager.dto.bo.thingmodel.ThingModelMessageBo;
import cc.iotkit.manager.dto.vo.deviceconfig.DeviceConfigVo;
import cc.iotkit.manager.dto.vo.devicegroup.DeviceGroupImportVo;
import cc.iotkit.manager.dto.vo.devicegroup.DeviceGroupVo;
import cc.iotkit.manager.dto.vo.deviceinfo.DeviceInfoVo;
import cc.iotkit.manager.dto.vo.deviceinfo.ParentDeviceVo;
@ -37,12 +39,12 @@ import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.context.request.async.DeferredResult;
import org.springframework.web.multipart.MultipartFile;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;
@Api(tags = {"设备"})
@ -232,6 +234,26 @@ public class DeviceController {
return deviceServiceImpl.addGroup(group.getData().to(DeviceGroup.class));
}
/**
* -
*/
@ApiOperation(value = "导入设备分组")
@SaCheckPermission("iot:deviceGroup:add")
@PostMapping("/group/importData")
public String importGroup(@RequestPart("file") MultipartFile file, @RequestParam("requestId") String requestId) {
return deviceServiceImpl.importGroup(file);
}
/**
*
*/
@ApiOperation("下载设备分组模板")
@PostMapping("/group/exportData")
public void exportGroupTemplate(HttpServletResponse response) {
ExcelUtil.exportExcel(new ArrayList<>(), "设备分组", DeviceGroupImportVo.class, response);
}
/**
*
*/

View File

@ -0,0 +1,27 @@
package cc.iotkit.manager.dto.vo.devicegroup;
import cc.iotkit.model.device.DeviceGroup;
import com.alibaba.excel.annotation.ExcelProperty;
import io.github.linpeilie.annotations.AutoMapper;
import lombok.Data;
import lombok.NoArgsConstructor;
import java.io.Serializable;
@Data
@NoArgsConstructor
@AutoMapper(target = DeviceGroup.class, reverseConvertGenerate = false)
public class DeviceGroupImportVo implements Serializable {
private static final long serialVersionUID = -1L;
@ExcelProperty(value = "设备组id")
private String id;
@ExcelProperty(value = "设备组名称")
private String name;
@ExcelProperty(value = "分组说明")
private String remark;
}

View File

@ -0,0 +1,98 @@
package cc.iotkit.manager.listener;
import cc.iotkit.common.excel.core.ExcelListener;
import cc.iotkit.common.excel.core.ExcelResult;
import cc.iotkit.common.exception.BizException;
import cc.iotkit.common.satoken.utils.LoginHelper;
import cc.iotkit.common.utils.SpringUtils;
import cc.iotkit.manager.dto.bo.devicegroup.DeviceGroupBo;
import cc.iotkit.manager.dto.vo.devicegroup.DeviceGroupImportVo;
import cc.iotkit.manager.dto.vo.devicegroup.DeviceGroupVo;
import cc.iotkit.manager.service.IDeviceManagerService;
import cc.iotkit.model.device.DeviceGroup;
import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import lombok.extern.slf4j.Slf4j;
import java.util.Collections;
import java.util.List;
@Slf4j
public class DeviceGroupImportListener extends AnalysisEventListener<DeviceGroupImportVo> implements ExcelListener<DeviceGroupImportVo> {
private final Boolean isUpdateSupport;
private int successNum = 0;
private int failureNum = 0;
private final String userId;
private IDeviceManagerService deviceManagerService;
private final StringBuilder successMsg = new StringBuilder();
private final StringBuilder failureMsg = new StringBuilder();
public DeviceGroupImportListener(Boolean isUpdateSupport) {
this.isUpdateSupport = isUpdateSupport;
this.userId = String.valueOf(LoginHelper.getUserId());
this.deviceManagerService = SpringUtils.getBean(IDeviceManagerService.class);
}
@Override
public ExcelResult<DeviceGroupImportVo> getExcelResult() {
return new ExcelResult<>() {
@Override
public String getAnalysis() {
if (failureNum > 0) {
failureMsg.insert(0, "很抱歉,导入失败!共 " + failureNum + " 条数据格式不正确,错误如下:");
throw new BizException(failureMsg.toString());
} else {
successMsg.insert(0, "恭喜您,数据已全部导入成功!共 " + successNum + " 条");
}
return successMsg.toString();
}
@Override
public List<DeviceGroupImportVo> getList() {
return Collections.emptyList();
}
@Override
public List<String> getErrorList() {
return Collections.emptyList();
}
};
}
@Override
public void invoke(DeviceGroupImportVo deviceGroupImportVo, AnalysisContext analysisContext) {
DeviceGroupVo deviceGroupVo = this.deviceManagerService.getDeviceGroup(deviceGroupImportVo.getId());
try {
if (ObjectUtil.isNull(deviceGroupVo)) {
// 新增
DeviceGroup deviceGroup = BeanUtil.toBean(deviceGroupImportVo, DeviceGroup.class);
deviceGroup.setUid(this.userId);
this.deviceManagerService.addGroup(deviceGroup);
successNum++;
} else if (Boolean.TRUE.equals(isUpdateSupport)) {
// 修改
DeviceGroupBo deviceGroupBo = BeanUtil.toBean(deviceGroupImportVo, DeviceGroupBo.class);
deviceGroupBo.setUid(this.userId);
this.deviceManagerService.updateGroup(deviceGroupBo);
successNum++;
}
} catch (Exception e) {
failureNum++;
String msg = "<br/>第" + failureNum + "条分组: " + deviceGroupImportVo.getName() + " 导入失败:";
failureMsg.append(msg).append(e.getMessage());
}
}
@Override
public void doAfterAllAnalysed(AnalysisContext analysisContext) {
}
}

View File

@ -18,6 +18,7 @@ import cc.iotkit.model.device.DeviceGroup;
import cc.iotkit.model.device.DeviceInfo;
import cc.iotkit.model.device.message.DeviceProperty;
import org.springframework.web.context.request.async.DeferredResult;
import org.springframework.web.multipart.MultipartFile;
import java.util.List;
@ -75,4 +76,8 @@ public interface IDeviceManagerService {
DeviceConfigVo getConfig(String deviceId);
boolean saveDevice(DeviceInfoBo data);
String importGroup(MultipartFile file);
DeviceGroupVo getDeviceGroup(String id);
}

View File

@ -4,6 +4,8 @@ import cc.iotkit.common.api.PageRequest;
import cc.iotkit.common.api.Paging;
import cc.iotkit.common.constant.Constants;
import cc.iotkit.common.enums.ErrCode;
import cc.iotkit.common.excel.core.ExcelResult;
import cc.iotkit.common.excel.utils.ExcelUtil;
import cc.iotkit.common.exception.BizException;
import cc.iotkit.common.satoken.utils.AuthUtil;
import cc.iotkit.common.thing.ThingModelMessage;
@ -19,9 +21,11 @@ import cc.iotkit.manager.dto.bo.device.DeviceTagAddBo;
import cc.iotkit.manager.dto.bo.devicegroup.DeviceAddGroupBo;
import cc.iotkit.manager.dto.bo.devicegroup.DeviceGroupBo;
import cc.iotkit.manager.dto.vo.deviceconfig.DeviceConfigVo;
import cc.iotkit.manager.dto.vo.devicegroup.DeviceGroupImportVo;
import cc.iotkit.manager.dto.vo.devicegroup.DeviceGroupVo;
import cc.iotkit.manager.dto.vo.deviceinfo.DeviceInfoVo;
import cc.iotkit.manager.dto.vo.deviceinfo.ParentDeviceVo;
import cc.iotkit.manager.listener.DeviceGroupImportListener;
import cc.iotkit.manager.service.DataOwnerService;
import cc.iotkit.manager.service.DeferredDataConsumer;
import cc.iotkit.manager.service.DeviceCtrlService;
@ -34,13 +38,16 @@ import cc.iotkit.model.product.Product;
import cc.iotkit.mq.MqProducer;
import cc.iotkit.temporal.IDevicePropertyData;
import cc.iotkit.temporal.IThingModelMessageData;
import lombok.SneakyThrows;
import org.apache.commons.lang3.RandomStringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;
import org.springframework.web.context.request.async.DeferredResult;
import org.springframework.web.multipart.MultipartFile;
import java.io.IOException;
import java.util.List;
import java.util.stream.Collectors;
@ -423,4 +430,19 @@ public class DeviceManagerServiceImpl implements IDeviceManagerService {
return deviceInfoData.save(di) != null;
}
@SneakyThrows
@Override
public String importGroup(MultipartFile file) {
ExcelResult<DeviceGroupImportVo> result = ExcelUtil.importExcel(file.getInputStream(), DeviceGroupImportVo.class, new DeviceGroupImportListener(true));
return result.getAnalysis();
}
@Override
public DeviceGroupVo getDeviceGroup(String id) {
DeviceGroup deviceGroup = deviceGroupData.findById(id);
return MapstructUtils.convert(deviceGroup, DeviceGroupVo.class);
}
}