Explorar o código

整改清单报表导出

zhuang hai 1 ano
pai
achega
fe6d618125

+ 3 - 0
business-common/src/main/java/com/rongwei/bscommon/sys/service/ZhcxProjectManageService.java

@@ -7,6 +7,7 @@ import com.rongwei.bsentity.dto.project.SavePorjectSummaryDto;
 import com.rongwei.rwcommon.base.R;
 import com.rongwei.rwcommonentity.commonservers.domain.SysFileItemDo;
 
+import javax.servlet.http.HttpServletResponse;
 import java.util.Map;
 
 /**
@@ -42,4 +43,6 @@ public interface ZhcxProjectManageService extends IService<ZhcxProjectManageDo>
     void saveSummaryData(SavePorjectSummaryDto dto);
 
     R getRectifyReportData(Map<String, Object> map);
+
+    int exportRectifyReportData(Map<String, Object> map, HttpServletResponse response);
 }

+ 111 - 4
business-common/src/main/java/com/rongwei/bscommon/sys/service/impl/ZhcxProjectManageServiceImpl.java

@@ -36,6 +36,8 @@ import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.context.annotation.Lazy;
 import org.springframework.stereotype.Service;
 
+import javax.servlet.http.HttpServletResponse;
+import java.io.ByteArrayOutputStream;
 import java.util.*;
 import java.util.stream.Collectors;
 
@@ -69,6 +71,8 @@ public class ZhcxProjectManageServiceImpl extends ServiceImpl<ZhcxProjectManageD
 
     @Autowired
     private ZhcxProjectRectifyMachineService projectRectifyMachineService;
+    @Autowired
+    private ExcelUtils excelUtils;
 
     /**
      * 生成汇总文件
@@ -188,9 +192,16 @@ public class ZhcxProjectManageServiceImpl extends ServiceImpl<ZhcxProjectManageD
                         RectifyMachineDto rectifyMachine = new RectifyMachineDto();
                         //设置机号
                         rectifyMachine.setNo(v2.get(0).getMachineno());
+                        List<ZhcxProjectRectifyMachineDo> distinctEntities = v2.stream()
+                                .collect(Collectors.toMap(
+                                        e -> Arrays.asList(e.getRectifyid(), e.getDeptid()), // key extractor
+                                        e -> e,
+                                        (e1, e2) -> e1))
+                                .values().stream()
+                                .collect(Collectors.toList());
                         //设置整改总数
-                        rectifyMachine.setTotalCount(v2.size());
-                        Map<String, Long> collect = v2.stream().collect(Collectors.groupingBy(
+                        rectifyMachine.setTotalCount(distinctEntities.size());
+                        Map<String, Long> collect = distinctEntities.stream().collect(Collectors.groupingBy(
                                 ZhcxProjectRectifyMachineDo::getStatus, Collectors.counting()));
                         //关闭数量
                         int ok = collect.getOrDefault("OK", 0L).intValue();
@@ -202,10 +213,10 @@ public class ZhcxProjectManageServiceImpl extends ServiceImpl<ZhcxProjectManageD
                         int x = collect.getOrDefault("X", 0L).intValue();
                         rectifyMachine.setUnfinishedCount(x);
                         //完成率1
-                        int rateOne = divideAndRoundUp((ok + i),v2.size());
+                        int rateOne = divideAndRoundUp((ok + i),distinctEntities.size());
                          rectifyMachine.setFinishRateOne(rateOne+"%");
                         //完成率2
-                        int rateTwo = divideAndRoundUp(ok,v2.size());
+                        int rateTwo = divideAndRoundUp(ok,distinctEntities.size());
                         rectifyMachine.setFinishRateTwo(rateTwo+"%");
                         rectifyMachineDtoList.add(rectifyMachine);
                     });
@@ -214,6 +225,14 @@ public class ZhcxProjectManageServiceImpl extends ServiceImpl<ZhcxProjectManageD
                     rectifyDtoList.add(rectifyDto);
                 });
                 rectifyDtoList.sort(Comparator.comparing(RectifyDto::getFirstdeptid,Comparator.nullsLast(String::compareTo)));
+                rectifyDtoList.forEach(ev -> {
+                    machineNoList.forEach(no -> {
+                        if (!ev.getData().stream().anyMatch(data -> data.getNo().equals(no))) {
+                            ev.getData().add(new RectifyMachineDto(no));
+                        }
+                    });
+                    ev.getData().sort(Comparator.comparingInt(a -> machineNoList.indexOf(a.getNo())));
+                });
                 rectifyReportDto.setData(rectifyDtoList);
                 return R.ok(rectifyReportDto);
             }
@@ -221,6 +240,94 @@ public class ZhcxProjectManageServiceImpl extends ServiceImpl<ZhcxProjectManageD
         return R.ok();
     }
 
+    @Override
+    public int exportRectifyReportData(Map<String, Object> map, HttpServletResponse response) {
+        R r = this.getRectifyReportData(map);
+        Object depttypeobj = map.getOrDefault("depttype", "施工部门");
+        if(ObjectUtil.isEmpty(depttypeobj)){
+            return 0;
+        }
+        String deptType = (String) depttypeobj;
+        RectifyReportDto data = (RectifyReportDto) r.getData();
+        if (data != null) {
+            try {
+                if(!excelUtils.GetLicense()) {
+                    return 0;
+                }
+                Workbook workbook = new Workbook();
+                WorksheetCollection worksheets = workbook.getWorksheets();
+                Worksheet worksheet = worksheets.get(0);
+                Cells cells = worksheet.getCells();
+                List<String> noList = data.getNoList();
+                // 设置列头
+                cells.merge(0, 0, 2, 1);
+                cells.merge(0, 1, 2, 1);
+                cells.get(0, 0).putValue("一级部门");
+                cells.get(0, 1).putValue(deptType);
+                int colIndex = 2;
+                for (String no : noList) {
+                    cells.merge(0, colIndex, 1, 6);
+                    cells.get(0, colIndex).putValue(no);
+                    cells.get(1, colIndex).putValue("整改总数");
+                    cells.get(1, colIndex + 1).putValue("完成率1");
+                    cells.get(1, colIndex + 2).putValue("完成率2");
+                    cells.get(1, colIndex + 3).putValue("关闭");
+                    cells.get(1, colIndex + 4).putValue("未完成");
+                    cells.get(1, colIndex + 5).putValue("待确认");
+                    colIndex += 6;
+                }
+                // 填充数据
+                int rowIndex = 2;
+                for (RectifyDto row : data.getData()) {
+                    cells.get(rowIndex, 0).putValue(row.getFirstdeptname());
+                    cells.get(rowIndex, 1).putValue(row.getDeptname());
+
+                    //List<Map<String, Object>> innerDataList = (List<Map<String, Object>>) row.getData();
+                    List<RectifyMachineDto> innerDataList = row.getData();
+                    colIndex = 2;
+                    for (String no : noList) {
+                        RectifyMachineDto rectifyMachineDto = innerDataList.stream()
+                                .filter(innerRow -> no.equals(innerRow.getNo()))
+                                .findFirst()
+                                .orElse(null);
+                        if (rectifyMachineDto != null) {
+                            cells.get(rowIndex, colIndex).putValue(rectifyMachineDto.getTotalCount());
+                            cells.get(rowIndex, colIndex + 1).putValue(rectifyMachineDto.getFinishRateOne());
+                            cells.get(rowIndex, colIndex + 2).putValue(rectifyMachineDto.getFinishRateTwo());
+                            cells.get(rowIndex, colIndex + 3).putValue(rectifyMachineDto.getClosedCount());
+                            cells.get(rowIndex, colIndex + 4).putValue(rectifyMachineDto.getUnfinishedCount());
+                            cells.get(rowIndex, colIndex + 5).putValue(rectifyMachineDto.getTbcCount());
+                        }
+                        colIndex += 6;
+                    }
+                    rowIndex++;
+                }
+                // 设置列头样式
+                Style style = workbook.createStyle();
+//                style.getFont().setBold(true);
+//                style.setPattern(BackgroundType.SOLID);
+//                style.setForegroundColor(Color.getLightGray());
+                style.setHorizontalAlignment(TextAlignmentType.CENTER);
+                style.setVerticalAlignment(TextAlignmentType.CENTER);
+                for (int i = 0; i < colIndex; i++) {
+                    //cells.get(1, i).setStyle(style);
+                    for (int x = 0; x < rowIndex; x++) {
+                        cells.get(x, i).setStyle(style);
+                    }
+                }
+                response.addHeader("Pargam", "no-cache");
+                response.addHeader("Cache-Control", "no-cache");
+                response.setContentType("application/octet-stream;charset=ISO8859-1");
+                response.setHeader("Content-Disposition", "attachment;filename=整改清单");
+                workbook.save(response.getOutputStream(), SaveFormat.XLSX);
+                workbook.dispose();
+            } catch (Exception e) {
+                log.error("整改清单导出报错", e);
+            }
+        }
+        return 1;
+    }
+
     private int divideAndRoundUp(int numerator, int denominator) {
         if (denominator == 0) {
             log.info("整改清单统计报错{},被除数是0");

+ 4 - 0
business-entity/src/main/java/com/rongwei/bsentity/dto/RectifyMachineDto.java

@@ -39,4 +39,8 @@ public class RectifyMachineDto {
      * 机号
      */
     private String no;
+
+    public RectifyMachineDto(String no) {
+        this.no =no;
+    }
 }

+ 9 - 0
business-server/src/main/java/com/rongwei/bsserver/controller/ZhcxProjectManageController.java

@@ -16,6 +16,7 @@ import org.springframework.web.bind.annotation.RequestBody;
 import org.springframework.web.bind.annotation.RequestMapping;
 import org.springframework.web.bind.annotation.RestController;
 
+import javax.servlet.http.HttpServletResponse;
 import java.util.Map;
 
 /**
@@ -81,5 +82,13 @@ public class ZhcxProjectManageController {
         R r = service.getRectifyReportData(map);
         return r;
     }
+    @PostMapping("/exportRectifyReportData")
+    @ApiOperation("导出整改报表数据")
+    public void exportRectifyReportData(@RequestBody Map<String,Object> map, HttpServletResponse response) {
+        int i = service.exportRectifyReportData(map,response);
+        if(i == 0){
+            throw new CustomException("导出失败");
+        }
+    }
 }