|
|
@@ -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");
|
|
|
+ }
|
|
|
}
|