lipenghui 1 giorno fa
parent
commit
dc960ba8be

+ 92 - 1
yudao-module-pms/yudao-module-pms-biz/src/main/java/cn/iocoder/yudao/module/pms/controller/admin/stat/IotReportDeviceController.java

@@ -1,6 +1,8 @@
 package cn.iocoder.yudao.module.pms.controller.admin.stat;
 
 import cn.hutool.core.collection.CollUtil;
+import cn.iocoder.yudao.framework.common.exception.ErrorCode;
+import cn.iocoder.yudao.framework.common.exception.ServiceException;
 import cn.iocoder.yudao.framework.common.pojo.CommonResult;
 import cn.iocoder.yudao.framework.common.pojo.PageResult;
 import cn.iocoder.yudao.framework.security.core.util.SecurityFrameworkUtils;
@@ -21,17 +23,24 @@ import cn.iocoder.yudao.module.system.dal.dataobject.dept.DeptDO;
 import cn.iocoder.yudao.module.system.dal.dataobject.dict.DictDataDO;
 import cn.iocoder.yudao.module.system.service.dept.DeptService;
 import cn.iocoder.yudao.module.system.service.dict.DictDataService;
+import com.alibaba.excel.EasyExcel;
 import com.google.common.collect.ImmutableList;
 import io.swagger.v3.oas.annotations.Operation;
 import io.swagger.v3.oas.annotations.tags.Tag;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.security.access.prepost.PreAuthorize;
 import org.springframework.validation.annotation.Validated;
-import org.springframework.web.bind.annotation.*;
+import org.springframework.web.bind.annotation.GetMapping;
+import org.springframework.web.bind.annotation.PathVariable;
+import org.springframework.web.bind.annotation.RequestMapping;
+import org.springframework.web.bind.annotation.RestController;
 
 import javax.annotation.Resource;
 import javax.annotation.security.PermitAll;
+import javax.servlet.http.HttpServletResponse;
 import javax.validation.Valid;
+import java.io.IOException;
+import java.net.URLEncoder;
 import java.util.*;
 import java.util.concurrent.atomic.AtomicInteger;
 import java.util.stream.Collectors;
@@ -293,4 +302,86 @@ public class IotReportDeviceController {
         String companyCode = deptUtil.getCompanyCode(deptId);
         return CommonResult.success(companyCode);
     }
+
+//    @PostMapping("/export-excel")
+//    @Operation(summary = "导出设备台账 Excel")
+//    @PreAuthorize("@ss.hasPermission('rq:iot-device:export')")
+//    @ApiAccessLog(operateType = EXPORT)
+//    public void exportIotDeviceExcel(@RequestBody IotDevicePageReqVO pageReqVO,
+//                                     HttpServletResponse response) throws IOException {
+//
+//    }
+
+    @GetMapping("/export-excel")
+    public void downloadExcelByStringHeader(@Valid IotDevicePageReqVO pageReqVO,
+            HttpServletResponse response) throws IOException {
+        String includeFields = pageReqVO.getExportFields();
+        Set<Long> ids;
+        if (Objects.isNull(pageReqVO.getDeptId())){
+            Long loginUserDeptId = SecurityFrameworkUtils.getLoginUserDeptId();
+            String companyCode = deptUtil.getCompanyCode(loginUserDeptId);
+            ids = deptUtil.getDeptIds(companyCode);
+        } else {
+            ids = deptService.getChildDeptIdListFromCache(pageReqVO.getDeptId());
+            ids.add(pageReqVO.getDeptId());
+        }
+        PageResult<IotDeviceDO> pageResult = iotDeviceMapper.selectPageReport(pageReqVO, ids);
+        if (CollUtil.isEmpty(pageResult.getList())) {
+            throw new ServiceException(new ErrorCode(2,"导出异常"));
+        }
+        Map<Long, DeptDO> deptMap = deptService.getDeptMap(
+                convertList(pageResult.getList(), IotDeviceDO::getDeptId));
+        List<IotDeviceRespVO> iotDeviceRespVOS = IotDeviceConvert.INSTANCE.convertList(pageResult.getList(), deptMap);
+        iotDeviceRespVOS.forEach(e ->{
+            if (Objects.nonNull(e.getAssetClass())) {
+                IotProductClassifyDO iotProductClassify = iotProductClassifyService.getIotProductClassify(e.getAssetClass());
+                e.setAssetClassName(Objects.nonNull(iotProductClassify)?iotProductClassify.getName():"");
+            }
+            SupplierDO supplierDO = supplierService.get(e.getManufacturerId());
+            if (Objects.nonNull(supplierDO)) {
+                e.setManufacturer(supplierDO.getName());
+            }
+            DeptRespDTO dept = deptApi.getDept(e.getDeptId());
+            if (Objects.nonNull(dept)) {
+                if ("1".equals(dept.getType())) {
+                    e.setCompany(dept.getName());
+                } else if ("2".equals(dept.getType())) {
+                    e.setProject(dept.getName());
+                    DeptRespDTO dept1 = deptApi.getDept(dept.getParentId());
+                    if (Objects.nonNull(dept1)) {
+                        e.setCompany(dept1.getName());
+                    }
+                } else if ("3".equals(dept.getType())) {
+                    e.setDeptName(dept.getName());
+                    DeptRespDTO dept1 = deptApi.getDept(dept.getParentId());
+                    if (Objects.nonNull(dept1)) {
+                        e.setProject(dept1.getName());
+                        DeptRespDTO dept2 = deptApi.getDept(dept1.getParentId());
+                        if (Objects.nonNull(dept2)) {
+                            e.setCompany(dept2.getName());
+                        }
+                    }
+                }
+            }
+        });
+
+        // 2. 下载配置(复用工具方法)
+        setExcelDownloadResponse(response, "设备报表");
+
+
+        String[] includeFieldArr = includeFields.split(",");
+        EasyExcel.write(response.getOutputStream(), IotDeviceRespVO.class)
+                .sheet("设备报表数据")
+                .includeColumnFiledNames(Arrays.asList(includeFieldArr))
+                // 核心方法:正向筛选
+                .doWrite(iotDeviceRespVOS);
+    }
+
+    // Excel下载响应配置(解决中文乱码、指定文件类型)
+    private void setExcelDownloadResponse(HttpServletResponse response, String fileName) throws IOException {
+        response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
+        response.setCharacterEncoding("utf-8");
+        String encodeFileName = URLEncoder.encode(fileName, "UTF-8").replaceAll("\\+", "%20");
+        response.setHeader("Content-disposition", "attachment;filename*=utf-8''" + encodeFileName + ".xlsx");
+    }
 }

+ 2 - 0
yudao-module-pms/yudao-module-pms-biz/src/main/java/cn/iocoder/yudao/module/pms/controller/admin/vo/IotDevicePageReqVO.java

@@ -149,4 +149,6 @@ public class IotDevicePageReqVO extends PageParam {
 
     @Schema(description = "设备类别 集合")
     private List<Long> assetClasses;
+
+    private String exportFields;
 }

+ 7 - 2
yudao-module-pms/yudao-module-pms-biz/src/main/java/cn/iocoder/yudao/module/pms/controller/admin/vo/IotDeviceRespVO.java

@@ -22,7 +22,7 @@ public class IotDeviceRespVO {
     private Long id;
 
     @Schema(description = "资产编码", requiredMode = Schema.RequiredMode.REQUIRED)
-    @ExcelProperty("资产编码")
+    @ExcelProperty("设备编码")
     private String deviceCode;
 
     @Schema(description = "设备名称", requiredMode = Schema.RequiredMode.REQUIRED, example = "芋艿")
@@ -130,9 +130,9 @@ public class IotDeviceRespVO {
     private String templateJson;
 
     @Schema(description = "资产类别")
-    @ExcelProperty("资产类别")
     private Long assetClass;
     @Schema(description = "资产类别名称")
+    @ExcelProperty("资产类别")
     private String assetClassName;
     @Schema(description = "创建时间")
     @ExcelProperty("创建时间")
@@ -141,6 +141,7 @@ public class IotDeviceRespVO {
     @ExcelProperty("部门名称")
     private String deptName;
     @Schema(description = "品牌型号")
+    @ExcelProperty("品牌")
     private String brandName;
     @Schema(description = "制造商名称")
     private String zzName;
@@ -150,6 +151,7 @@ public class IotDeviceRespVO {
     private String modelName;
 
     @Schema(description = "责任人")
+    @ExcelProperty("责任人")
     private String chargeName;
 
     /**
@@ -213,11 +215,14 @@ public class IotDeviceRespVO {
     private List<IotDeviceRunLogRespVO> mileageAccumulatedAttrs;
 
     @Schema(description = "启用日期")
+    @ExcelProperty("投运日期")
     private String enableDate;
 
     @Schema(description = "设备对应的 BOM分组 最短保养距离集合")
     private List<IotMaintenanceBomRespVO> groupBomDistances;
 
+    @Schema(description = "生产厂家")
+    @ExcelProperty("生产厂家")
     private String manufacturer;
 
     @Schema(description = "根据设备保养项规则 应该生成保养工单 标识")