forked from dyf/fys-Multi-tenant
WEB:导入设备数据
This commit is contained in:
@ -1,23 +1,33 @@
|
||||
package com.fuyuanshen.equipment.controller;
|
||||
|
||||
|
||||
import com.alibaba.excel.EasyExcel;
|
||||
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
||||
import com.fuyuanshen.common.core.constant.ResponseMessageConstants;
|
||||
import com.fuyuanshen.common.core.domain.ResponseVO;
|
||||
import com.fuyuanshen.common.core.domain.model.LoginUser;
|
||||
import com.fuyuanshen.common.core.utils.file.FileUtil;
|
||||
import com.fuyuanshen.common.mybatis.core.page.TableDataInfo;
|
||||
import com.fuyuanshen.common.satoken.utils.LoginHelper;
|
||||
import com.fuyuanshen.customer.mapper.CustomerMapper;
|
||||
import com.fuyuanshen.equipment.domain.Device;
|
||||
import com.fuyuanshen.equipment.domain.dto.DeviceExcelImportDTO;
|
||||
import com.fuyuanshen.equipment.domain.dto.ImportResult;
|
||||
import com.fuyuanshen.equipment.domain.form.DeviceForm;
|
||||
import com.fuyuanshen.equipment.domain.query.DeviceQueryCriteria;
|
||||
import com.fuyuanshen.equipment.domain.vo.CustomerVo;
|
||||
import com.fuyuanshen.equipment.excel.DeviceImportParams;
|
||||
import com.fuyuanshen.equipment.excel.UploadDeviceDataListener;
|
||||
import com.fuyuanshen.equipment.mapper.DeviceMapper;
|
||||
import com.fuyuanshen.equipment.mapper.DeviceTypeMapper;
|
||||
import com.fuyuanshen.equipment.service.DeviceService;
|
||||
import com.fuyuanshen.equipment.service.impl.DeviceExportService;
|
||||
import com.fuyuanshen.system.domain.vo.SysOssVo;
|
||||
import com.fuyuanshen.system.service.ISysOssService;
|
||||
import io.swagger.v3.oas.annotations.Operation;
|
||||
import io.swagger.v3.oas.annotations.Parameter;
|
||||
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
|
||||
import io.swagger.v3.oas.annotations.tags.Tag;
|
||||
import io.undertow.util.BadRequestException;
|
||||
import jakarta.servlet.http.HttpServletResponse;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
@ -42,6 +52,9 @@ public class DeviceController {
|
||||
|
||||
private final ISysOssService ossService;
|
||||
private final DeviceService deviceService;
|
||||
private final DeviceMapper deviceMapper;
|
||||
private final CustomerMapper customerMapper;
|
||||
private final DeviceTypeMapper deviceTypeMapper;
|
||||
|
||||
private final DeviceExportService exportService;
|
||||
|
||||
@ -156,36 +169,34 @@ public class DeviceController {
|
||||
|
||||
@Operation(summary = "导入设备数据")
|
||||
@PostMapping(value = "/import", consumes = "multipart/form-data")
|
||||
public ResponseVO<ImportResult> importData(@Parameter(name = "文件", required = true) @RequestPart("file") MultipartFile file) {
|
||||
public ResponseVO<ImportResult> importData(@Parameter(name = "文件", required = true) @RequestPart("file") MultipartFile file) throws BadRequestException {
|
||||
|
||||
SysOssVo upload = ossService.upload(file);
|
||||
String suffix = FileUtil.getExtensionName(file.getOriginalFilename());
|
||||
if (!("xlsx".equalsIgnoreCase(suffix))) {
|
||||
throw new BadRequestException("只能上传Excel——xlsx格式文件");
|
||||
}
|
||||
|
||||
// String suffix = FileUtil.getExtensionName(file.getOriginalFilename());
|
||||
// if (!("xlsx".equalsIgnoreCase(suffix))) {
|
||||
// throw new BadRequestException("只能上传Excel——xlsx格式文件");
|
||||
// }
|
||||
//
|
||||
// ImportResult result = new ImportResult();
|
||||
// try {
|
||||
// User currentUser = userMapper.findByUsername(SecurityUtils.getCurrentUsername());
|
||||
// DeviceImportParams params = DeviceImportParams.builder().ip(ip).deviceService(deviceService).tenantId(currentUser.getTenantId()).file(file).filePath(filePath).deviceMapper(deviceMapper).deviceAssignmentsService(deviceAssignmentsService).deviceTypeMapper(deviceTypeMapper).userId(currentUser.getId()).userMapper(userMapper).build();
|
||||
// // 创建监听器
|
||||
// UploadDeviceDataListener listener = new UploadDeviceDataListener(params);
|
||||
// // 读取Excel
|
||||
// EasyExcel.read(file.getInputStream(), DeviceExcelImportDTO.class, listener).sheet().doRead();
|
||||
// // 获取导入结果
|
||||
// result = listener.getImportResult();
|
||||
// // 设置响应消息
|
||||
// String message = String.format("成功导入 %d 条数据,失败 %d 条", result.getSuccessCount(), result.getFailureCount());
|
||||
// // 返回带有正确泛型的响应
|
||||
// return ResponseVO.<ImportResult>success(message, result);
|
||||
// } catch (Exception e) {
|
||||
// log.error("导入设备数据出错: {}", e.getMessage(), e);
|
||||
// // 在异常情况下,设置默认结果
|
||||
// String errorMessage = String.format("导入失败: %s。成功 %d 条,失败 %d 条", e.getMessage(), result.getSuccessCount(), result.getFailureCount());
|
||||
// // 使用新方法确保类型正确
|
||||
// return ResponseVO.<ImportResult>fail(errorMessage, result);
|
||||
return null;
|
||||
ImportResult result = new ImportResult();
|
||||
try {
|
||||
LoginUser loginUser = LoginHelper.getLoginUser();
|
||||
DeviceImportParams params = DeviceImportParams.builder().ossService(ossService).deviceService(deviceService).tenantId(loginUser.getTenantId()).file(file).filePath("").deviceMapper(deviceMapper).deviceTypeMapper(deviceTypeMapper).userId(loginUser.getUserId()).customerMapper(customerMapper).build();
|
||||
// 创建监听器
|
||||
UploadDeviceDataListener listener = new UploadDeviceDataListener(params);
|
||||
// 读取Excel
|
||||
EasyExcel.read(file.getInputStream(), DeviceExcelImportDTO.class, listener).sheet().doRead();
|
||||
// 获取导入结果
|
||||
result = listener.getImportResult();
|
||||
// 设置响应消息
|
||||
String message = String.format("成功导入 %d 条数据,失败 %d 条", result.getSuccessCount(), result.getFailureCount());
|
||||
// 返回带有正确泛型的响应
|
||||
return ResponseVO.<ImportResult>success(message, result);
|
||||
} catch (Exception e) {
|
||||
log.error("导入设备数据出错: {}", e.getMessage(), e);
|
||||
// 在异常情况下,设置默认结果
|
||||
String errorMessage = String.format("导入失败: %s。成功 %d 条,失败 %d 条", e.getMessage(), result.getSuccessCount(), result.getFailureCount());
|
||||
// 使用新方法确保类型正确
|
||||
return ResponseVO.<ImportResult>fail(errorMessage, result);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
Reference in New Issue
Block a user