Commit 5527ab3a0ec5800b752b49f8ecc014d5302db312

Authored by 盛长浩
1 parent d817e6be

设备台账完善-导入接口暂存

... ... @@ -5,10 +5,16 @@ import io.swagger.annotations.ApiOperation;
5 5 import lombok.RequiredArgsConstructor;
6 6 import lombok.extern.slf4j.Slf4j;
7 7 import org.apache.commons.lang3.StringUtils;
  8 +import org.apache.poi.ss.usermodel.Cell;
  9 +import org.apache.poi.ss.usermodel.Row;
  10 +import org.apache.poi.ss.usermodel.Sheet;
  11 +import org.apache.poi.ss.usermodel.Workbook;
  12 +import org.apache.poi.xssf.usermodel.XSSFWorkbook;
8 13 import org.springframework.http.ResponseEntity;
9 14 import org.springframework.security.access.prepost.PreAuthorize;
10 15 import org.springframework.validation.annotation.Validated;
11 16 import org.springframework.web.bind.annotation.*;
  17 +import org.springframework.web.multipart.MultipartFile;
12 18 import org.thingsboard.server.common.data.exception.ThingsboardException;
13 19 import org.thingsboard.server.common.data.yunteng.common.DeleteGroup;
14 20 import org.thingsboard.server.common.data.yunteng.dto.BrainDeviceDTO;
... ... @@ -18,7 +24,10 @@ import org.thingsboard.server.controller.BaseController;
18 24 import org.thingsboard.server.dao.yunteng.service.BrainDeviceService;
19 25 import org.thingsboard.server.queue.util.TbCoreComponent;
20 26
  27 +import java.io.InputStream;
  28 +import java.util.ArrayList;
21 29 import java.util.HashMap;
  30 +import java.util.List;
22 31 import java.util.Map;
23 32
24 33 import static org.thingsboard.server.common.data.yunteng.constant.QueryConstant.PAGE;
... ... @@ -91,4 +100,67 @@ public class BrainDeviceController extends BaseController {
91 100 return ResponseResult.success(true);
92 101 }
93 102
  103 + @PostMapping("/import")
  104 + public Map<String, Object> importDevices(@RequestParam("file") MultipartFile file) {
  105 + Map<String, Object> result = new HashMap<>();
  106 + List<String> errors = new ArrayList<>();
  107 + int successCount = 0;
  108 + //此接口未完成
  109 + try (InputStream inputStream = file.getInputStream()) {
  110 + Workbook workbook = new XSSFWorkbook(inputStream);
  111 + Sheet sheet = workbook.getSheetAt(0);
  112 +
  113 + for (int i = 1; i <= sheet.getLastRowNum(); i++) {
  114 + Row row = sheet.getRow(i);
  115 + if (row == null) {
  116 + continue;
  117 + }
  118 + try {
  119 + // 4. 解析单元格
  120 + String deviceId = getCellValue(row.getCell(0));
  121 + String deviceName = getCellValue(row.getCell(1));
  122 + String deviceType = getCellValue(row.getCell(2));
  123 +
  124 + // 5. 数据校验逻辑
  125 + if(deviceId.isEmpty() || deviceName.isEmpty()){
  126 + throw new Exception("必填字段为空");
  127 + }
  128 +
  129 + // 6. 调用Service层保存设备(伪代码)
  130 + // deviceService.save(new Device(deviceId, deviceName, deviceType));
  131 + successCount++;
  132 +
  133 + } catch (Exception e) {
  134 + errors.add("第" + (i+1) + "行数据异常:" + e.getMessage());
  135 + }
  136 + }
  137 + result.put("success", true);
  138 + result.put("total", successCount + errors.size());
  139 + result.put("successCount", successCount);
  140 + result.put("errorList", errors);
  141 +
  142 + } catch (Exception e) {
  143 + result.put("success", false);
  144 + result.put("message", "文件处理失败:" + e.getMessage());
  145 + }
  146 + return result;
  147 + }
  148 +
  149 + // 通用单元格值获取方法
  150 + private String getCellValue(Cell cell) {
  151 + if (cell == null) return "";
  152 +
  153 + switch (cell.getCellType()) {
  154 + case STRING:
  155 + return cell.getStringCellValue().trim();
  156 + case NUMERIC:
  157 + return String.valueOf((int)cell.getNumericCellValue());
  158 + case BOOLEAN:
  159 + return String.valueOf(cell.getBooleanCellValue());
  160 + default:
  161 + return "";
  162 + }
  163 + }
  164 +
  165 +
94 166 }
... ...