From cddcf22a61489ee9c2dfee169d817ccd66f95430 Mon Sep 17 00:00:00 2001
From: java110 <928255095@qq.com>
Date: 星期五, 03 九月 2021 02:23:43 +0800
Subject: [PATCH] 优化代码

---
 service-front/src/main/java/com/java110/front/smo/assetExport/impl/ExportFeeManualCollectionSMOImpl.java |  368 ++++++++++++++++++++++++++++++++++++++++++++++++++++
 1 files changed, 364 insertions(+), 4 deletions(-)

diff --git a/service-front/src/main/java/com/java110/front/smo/assetExport/impl/ExportFeeManualCollectionSMOImpl.java b/service-front/src/main/java/com/java110/front/smo/assetExport/impl/ExportFeeManualCollectionSMOImpl.java
old mode 100644
new mode 100755
index 4b07417..5735f16
--- a/service-front/src/main/java/com/java110/front/smo/assetExport/impl/ExportFeeManualCollectionSMOImpl.java
+++ b/service-front/src/main/java/com/java110/front/smo/assetExport/impl/ExportFeeManualCollectionSMOImpl.java
@@ -5,13 +5,17 @@
 import com.alibaba.fastjson.parser.Feature;
 import com.java110.core.component.BaseComponentSMO;
 import com.java110.core.context.IPageData;
+import com.java110.core.factory.Java110ThreadPoolFactory;
 import com.java110.entity.component.ComponentValidateResult;
 import com.java110.front.smo.assetExport.IExportFeeManualCollectionSMO;
 import com.java110.utils.constant.ServiceConstant;
 import com.java110.utils.util.Assert;
+import com.java110.utils.util.Base64Convert;
 import com.java110.utils.util.DateUtil;
+import com.java110.utils.util.Money2ChineseUtil;
 import org.apache.poi.ss.usermodel.*;
 import org.apache.poi.ss.util.CellRangeAddress;
+import org.apache.poi.xssf.usermodel.XSSFClientAnchor;
 import org.apache.poi.xssf.usermodel.XSSFWorkbook;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
@@ -26,6 +30,10 @@
 
 import java.io.ByteArrayOutputStream;
 import java.io.IOException;
+import java.math.BigDecimal;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
 
 /**
  * @ClassName AssetImportSmoImpl
@@ -77,6 +85,341 @@
         }
         // 淇濆瓨鏁版嵁
         return new ResponseEntity<Object>(context, headers, HttpStatus.OK);
+    }
+
+    @Override
+    public ResponseEntity<Object> downloadCollectionLetterOrder(IPageData pd) {
+        ComponentValidateResult result = this.validateStoreStaffCommunityRelationship(pd, restTemplate);
+
+        Assert.hasKeyAndValue(JSONObject.parseObject(pd.getReqData()), "communityId", "璇锋眰涓湭鍖呭惈灏忓尯");
+
+        Workbook workbook = null;  //宸ヤ綔绨�
+        //宸ヤ綔琛�
+        workbook = new XSSFWorkbook();
+        //鑾峰彇妤间俊鎭�
+        getRoomOweFees(pd, result, workbook);
+
+
+        ByteArrayOutputStream os = new ByteArrayOutputStream();
+        MultiValueMap headers = new HttpHeaders();
+        headers.add("content-type", "application/octet-stream;charset=UTF-8");
+        headers.add("Content-Disposition", "attachment;filename=downloadCollectionLetterOrder_" + DateUtil.getyyyyMMddhhmmssDateString() + ".xlsx");
+        headers.add("Pargam", "no-cache");
+        headers.add("Cache-Control", "no-cache");
+        //headers.add("Content-Disposition", "attachment; filename=" + outParam.getString("fileName"));
+        headers.add("Accept-Ranges", "bytes");
+        byte[] context = null;
+        try {
+            workbook.write(os);
+            context = os.toByteArray();
+
+        } catch (IOException e) {
+            e.printStackTrace();
+            // 淇濆瓨鏁版嵁
+            return new ResponseEntity<Object>("瀵煎嚭澶辫触", HttpStatus.INTERNAL_SERVER_ERROR);
+        } finally {
+            try {
+                os.close();
+            } catch (IOException e) {
+                e.printStackTrace();
+            }
+            try {
+                workbook.close();
+            } catch (IOException e) {
+                e.printStackTrace();
+            }
+        }
+        // 淇濆瓨鏁版嵁
+        return new ResponseEntity<Object>(context, headers, HttpStatus.OK);
+    }
+
+    /**
+     * 鏌ヨ鎴垮眿娆犺垂
+     *
+     * @param pd
+     * @param result
+     * @param workbook
+     */
+    private void getRoomOweFees(IPageData pd, ComponentValidateResult result, Workbook workbook) {
+        Sheet sheet = workbook.createSheet("鍌即鍗�");
+        Drawing patriarch = sheet.createDrawingPatriarch();
+        String apiUrl = "";
+        ResponseEntity<String> responseEntity = null;
+        JSONObject reqJson = JSONObject.parseObject(pd.getReqData());
+        apiUrl = ServiceConstant.SERVICE_API_URL + "/api/feeApi/listAllRoomOweFees" + mapToUrlParam(reqJson);
+        responseEntity = this.callCenterService(restTemplate, pd, "", apiUrl, HttpMethod.GET);
+
+        if (responseEntity.getStatusCode() != HttpStatus.OK) { //璺宠繃 淇濆瓨鍗曞厓淇℃伅
+            return;
+        }
+
+        JSONObject savedRoomInfoResults = JSONObject.parseObject(responseEntity.getBody(), Feature.OrderedField);
+
+
+        if (!savedRoomInfoResults.containsKey("data")) {
+            return;
+        }
+
+
+        JSONArray rooms = savedRoomInfoResults.getJSONArray("data");
+
+        if (rooms == null || rooms.size() < 1) {
+            return;
+        }
+
+        //鏌ヨ鍌即鍗曚簩缁寸爜
+        JSONObject feePrint = null;
+        apiUrl = ServiceConstant.SERVICE_API_URL + "/api/feePrintSpec/queryFeePrintSpec?page=1&row=1&specCd=1010&communityId=" + result.getCommunityId();
+        responseEntity = this.callCenterService(restTemplate, pd, "", apiUrl, HttpMethod.GET);
+        if (responseEntity.getStatusCode() == HttpStatus.OK) {
+            JSONObject feePrintResults = JSONObject.parseObject(responseEntity.getBody(), Feature.OrderedField);
+            if (feePrintResults.containsKey("data")) {
+                JSONArray feePrints = feePrintResults.getJSONArray("data");
+                if (feePrints != null && feePrints.size() > 0) {
+                    feePrint = feePrints.getJSONObject(0);
+                }
+            }
+
+        }
+
+
+        int line = 0;
+        double totalPageHeight = 0;
+        for (int roomIndex = 0; roomIndex < rooms.size(); roomIndex++) {
+            Map<String, Object> info = generatorRoomOweFee(sheet, workbook, rooms.getJSONObject(roomIndex), line, totalPageHeight, patriarch, feePrint);
+            line = Integer.parseInt(info.get("line").toString()) + 1;
+            totalPageHeight = Double.parseDouble(info.get("totalPageHeight").toString());
+        }
+    }
+
+    private Map<String, Object> generatorRoomOweFee(Sheet sheet, Workbook workbook, JSONObject room, int line, double totalPageHeight, Drawing patriarch, JSONObject feePrint) {
+        JSONArray fees = room.getJSONArray("fees");
+        String[] feePrintRemarks = null;
+        if (feePrint != null) {
+            feePrintRemarks = feePrint.getString("content").toLowerCase().replace("</br>", "").split("\n");
+        } else {
+            feePrintRemarks = new String[]{""};
+        }
+        int defaultRowHeight = 280;
+        //璁$畻褰撳墠鍗曞瓙鐨勯珮搴�
+        int titleHeight = defaultRowHeight * 3;
+        int subTitleHeight = defaultRowHeight * 5;
+        int totalHeight = titleHeight + subTitleHeight + defaultRowHeight * 3 + fees.size() * defaultRowHeight;
+        //澶囨敞锛屽姞涓婃墦鍗伴厤缃唴瀹�
+        if (feePrintRemarks != null && feePrintRemarks.length > 0) {
+            totalHeight += (feePrintRemarks.length * defaultRowHeight);
+        }
+        double A4_lengthways_pageSize = defaultRowHeight * 57;//15960
+
+        //褰撳墠椤� 宸茬粡鍗犵敤鐨勯珮搴�
+        double curPageHeight = totalPageHeight % A4_lengthways_pageSize;
+        //褰撳墠椤电┖闂查珮搴�
+        double freePageHeight = A4_lengthways_pageSize - curPageHeight;
+        if (freePageHeight < totalHeight && curPageHeight != 0) {
+            line += Math.ceil(freePageHeight / defaultRowHeight);
+            totalPageHeight += freePageHeight;
+        }
+
+        totalPageHeight += totalHeight;
+
+
+        sheet.setColumnWidth(0, 8 * 256 * 2);
+        sheet.setColumnWidth(1, 8 * 256 * 1);
+        sheet.setColumnWidth(2, 8 * 256 * 1);
+        sheet.setColumnWidth(3, 8 * 256 * 3);
+        sheet.setColumnWidth(4, 8 * 256 * 1);
+        sheet.setColumnWidth(5, 8 * 256 * 1);
+        sheet.setColumnWidth(6, 8 * 256 * 1);
+
+        //閫氱敤鏍峰紡
+        CellStyle cellStyle = workbook.createCellStyle();
+//璁剧疆鏍峰紡瀵硅薄锛岃繖閲屼粎璁剧疆浜嗚竟妗嗗睘鎬�
+        cellStyle.setBorderBottom(BorderStyle.THIN); //涓嬭竟妗�
+        cellStyle.setBorderLeft(BorderStyle.THIN);//宸﹁竟妗�
+        cellStyle.setBorderTop(BorderStyle.THIN);//涓婅竟妗�
+        cellStyle.setBorderRight(BorderStyle.THIN);//鍙宠竟妗�
+        cellStyle.setAlignment(HorizontalAlignment.CENTER);
+
+        // 鏍囬
+        Row row = sheet.createRow(0 + line);
+        Cell cell0 = row.createCell(0);
+        //cell0.setCellValue("缂磋垂閫氱煡鍗晅otalHeight:" + totalHeight + "-totalPageHeight:" + totalPageHeight + "-curPageHeight:" + curPageHeight + "-freePageHeight:" + freePageHeight + "-line:" + line);
+        if (feePrint != null) {
+            cell0.setCellValue(feePrint.getString("printName") + "缂磋垂閫氱煡鍗�");
+        } else {
+            cell0.setCellValue("缂磋垂閫氱煡鍗�");
+        }
+        //鏍囬璁剧疆瀛椾綋
+        Font font = workbook.createFont();
+        font.setFontName("榛戜綋");
+        font.setFontHeightInPoints((short) 26);
+        CellStyle titleCellStyle = workbook.createCellStyle();
+        titleCellStyle.setFont(font);
+        titleCellStyle.setAlignment(HorizontalAlignment.CENTER);
+        cell0.setCellStyle(titleCellStyle);
+        row.setHeight((short) (titleHeight));
+
+        //鍚堝苟鏍囬
+        CellRangeAddress region = new CellRangeAddress(0 + line, 0 + line, 0, 6);
+        sheet.addMergedRegion(region);
+
+
+        //瀛愭爣棰�
+        if (feePrint != null) {
+            XSSFClientAnchor anchor = new XSSFClientAnchor(0, 0, 0, 0, (short) 0, 1 + line, (short) 1, 1 + line + 1);
+            anchor.setAnchorType(ClientAnchor.AnchorType.MOVE_AND_RESIZE);//璁剧疆鍥剧墖闅忓崟鍏冪Щ鍔ㄨ皟鏁村ぇ灏�
+            try {
+                String qrImg = feePrint.getString("qrImg").replace("data:image/webp;base64,", "")
+                        .replace("data:image/png;base64,", "")
+                        .replace("data:image/jpeg;base64,", "");
+                patriarch.createPicture(anchor, workbook.addPicture(Base64Convert.base64ToByte(qrImg), XSSFWorkbook.PICTURE_TYPE_JPEG));
+            } catch (IOException e) {
+                e.printStackTrace();
+            }
+        }
+        CellStyle subTitleCellStyle = workbook.createCellStyle();
+        subTitleCellStyle.setAlignment(HorizontalAlignment.CENTER);
+        subTitleCellStyle.setVerticalAlignment(VerticalAlignment.BOTTOM);
+        row = sheet.createRow(1 + line);
+        Cell cell1 = row.createCell(1);
+        if (fees != null && fees.size() > 0) {
+            cell1.setCellValue("涓氫富锛�" + fees.getJSONObject(0).getString("ownerName"));
+        } else {
+            cell1.setCellValue("涓氫富锛氭棤");
+        }
+        cell1.setCellStyle(subTitleCellStyle);
+
+        Cell cell2 = row.createCell(2);
+        cell2.setCellValue("鎴垮彿锛�" + room.getString("floorNum")
+                + "-" + room.getString("unitNum")
+                + "-" + room.getString("roomNum"));
+        cell2.setCellStyle(subTitleCellStyle);
+
+        row.createCell(3).setCellValue("");
+        row.createCell(4).setCellValue("");
+        Cell cell5 = row.createCell(5);
+        cell5.setCellValue(DateUtil.getNow(DateUtil.DATE_FORMATE_STRING_B));
+        cell5.setCellStyle(subTitleCellStyle);
+        CellStyle rowCellStyle = workbook.createCellStyle();
+        rowCellStyle.setVerticalAlignment(VerticalAlignment.BOTTOM);
+        row.setRowStyle(rowCellStyle);
+
+        //璁剧疆琛ㄥご涔嬩笂
+        region = new CellRangeAddress(1 + line, 1 + line, 2, 3);
+        sheet.addMergedRegion(region);
+        region = new CellRangeAddress(1 + line, 1 + line, 5, 6);
+        sheet.addMergedRegion(region);
+        //瀛愭爣棰橀珮搴�
+        row.setHeight((short) (subTitleHeight));
+
+
+        row = sheet.createRow(2 + line);
+        cell0 = row.createCell(0);
+        cell0.setCellValue("鏀惰垂鍚嶇О");
+        cell0.setCellStyle(cellStyle);
+        cell1 = row.createCell(1);
+        cell1.setCellValue("鏀惰垂鏍囧噯");
+        cell1.setCellStyle(cellStyle);
+        cell2 = row.createCell(2);
+        cell2.setCellValue("鏁伴噺/闈㈢Н");
+        cell2.setCellStyle(cellStyle);
+        Cell cell3 = row.createCell(3);
+        cell3.setCellValue("娆犺垂鏃堕棿");
+        cell3.setCellStyle(cellStyle);
+        Cell cell4 = row.createCell(4);
+        cell4.setCellValue("搴旂即閲戦锛堝厓锛�");
+        cell4.setCellStyle(cellStyle);
+        cell5 = row.createCell(5);
+        cell5.setCellValue("杩濈害閲戯紙鍏冿級");
+        cell5.setCellStyle(cellStyle);
+        Cell cell6 = row.createCell(6);
+        cell6.setCellValue("澶囨敞");
+        cell6.setCellStyle(cellStyle);
+        row.setHeight((short) (defaultRowHeight));
+
+
+        BigDecimal totalPrice = new BigDecimal(0);
+        String startTime = "";
+        String endTime = "";
+        for (int feeIndex = 0; feeIndex < fees.size(); feeIndex++) {
+            JSONObject feeObj = fees.getJSONObject(feeIndex);
+            row = sheet.createRow(line + feeIndex + 3);
+            startTime = feeObj.getString("endTime").length() > 10 ? feeObj.getString("endTime").substring(0, 10) : feeObj.getString("endTime");
+            endTime = feeObj.getString("deadlineTime").length() > 10 ? feeObj.getString("deadlineTime").substring(0, 10) : feeObj.getString("deadlineTime");
+
+            cell0 = row.createCell(0);
+            cell0.setCellValue(feeObj.getString("feeName"));
+            cell0.setCellStyle(cellStyle);
+            cell1 = row.createCell(1);
+            cell1.setCellValue(feeObj.getString("squarePrice"));
+            cell1.setCellStyle(cellStyle);
+            cell2 = row.createCell(2);
+            cell2.setCellValue(room.getString("builtUpArea"));
+            cell2.setCellStyle(cellStyle);
+            cell3 = row.createCell(3);
+            if (feeObj.containsKey("curDegrees")) {
+                cell3.setCellValue(startTime + "鑷�" + endTime + " " + feeObj.getString("preDegrees") + "鑷�" + feeObj.getString("curDegrees"));
+            } else {
+                cell3.setCellValue(startTime + "鑷�" + endTime);
+            }
+            cell3.setCellStyle(cellStyle);
+            cell4 = row.createCell(4);
+            cell4.setCellValue(feeObj.getString("feePrice"));
+            cell4.setCellStyle(cellStyle);
+            cell5 = row.createCell(5);
+            cell5.setCellValue("0");
+            cell5.setCellStyle(cellStyle);
+            cell6 = row.createCell(6);
+            cell6.setCellValue("");
+            cell6.setCellStyle(cellStyle);
+            row.setHeight((short) (defaultRowHeight));
+            totalPrice = totalPrice.add(new BigDecimal(feeObj.getString("feePrice")));
+        }
+
+        row = sheet.createRow(line + fees.size() + 3);
+
+        cell0 = row.createCell(0);
+        cell0.setCellValue("鍚堣锛堝ぇ鍐欙級");
+        cell0.setCellStyle(cellStyle);
+        cell1 = row.createCell(1);
+        cell1.setCellValue(Money2ChineseUtil.toChineseChar(totalPrice.doubleValue()));
+        cell1.setCellStyle(cellStyle);
+        cell2 = row.createCell(2);
+        cell2.setCellValue("");
+        cell2.setCellStyle(cellStyle);
+        cell3 = row.createCell(3);
+        cell3.setCellValue("");
+        cell3.setCellStyle(cellStyle);
+        cell4 = row.createCell(4);
+        cell4.setCellValue(totalPrice.doubleValue());
+        cell4.setCellStyle(cellStyle);
+        cell5 = row.createCell(5);
+        cell5.setCellValue("");
+        cell5.setCellStyle(cellStyle);
+        cell6 = row.createCell(6);
+        cell6.setCellValue("");
+        cell6.setCellStyle(cellStyle);
+        row.setHeight((short) (defaultRowHeight));
+
+
+        //鍚堣 鍚堝苟
+        region = new CellRangeAddress(line + fees.size() + 3, line + fees.size() + 3, 1, 3);
+        sheet.addMergedRegion(region);
+
+        if (feePrintRemarks != null && feePrintRemarks.length > 0) {
+            for (int remarkIndex = 0; remarkIndex < feePrintRemarks.length; remarkIndex++) {
+                row = sheet.createRow(line + fees.size() + 4 + remarkIndex);
+                row.createCell(0).setCellValue(feePrintRemarks[remarkIndex]);
+                row.setHeight((short) (defaultRowHeight));
+            }
+        }
+        row = sheet.createRow(line + fees.size() + 4 + feePrintRemarks.length);
+        row.createCell(0).setCellValue("");
+        row.setHeight((short) (defaultRowHeight));
+        Map info = new HashMap();
+        info.put("line", line + fees.size() + 4 + feePrintRemarks.length);
+        info.put("totalPageHeight", totalPageHeight);
+        return info;
     }
 
 
@@ -169,12 +512,10 @@
         row = sheet.createRow(rooms.size() + 2);
         cell0 = row.createCell(0);
         cell0.setCellValue("娉細姝ゃ�婃瑺璐圭粺璁¤〃銆嬩氦鐢卞帵闂ㄧ淮搴︽櫤涓寸鎶�鏈夐檺鍏徃杩涜鍌敹");
-        cs = workbook.createCellStyle();
-        cs.setWrapText(true);  //鍏抽敭
-        cell0.setCellStyle(cs);
-        row.setHeight((short) (200 * 10));
 
         CellRangeAddress region = new CellRangeAddress(0, 0, 0, dataKeyIndex);
+        sheet.addMergedRegion(region);
+        region = new CellRangeAddress(rooms.size() + 2, rooms.size() + 2, 0, dataKeyIndex);
         sheet.addMergedRegion(region);
     }
 
@@ -185,4 +526,23 @@
     public void setRestTemplate(RestTemplate restTemplate) {
         this.restTemplate = restTemplate;
     }
+
+    public static void main(String[] args) {
+        Java110ThreadPoolFactory<String> pool = Java110ThreadPoolFactory.getInstance().createThreadPool(20);
+        for (int i = 21; i > 0; i--) {
+            Integer data = i;
+            pool.submit(() -> {
+//杩欎釜鍦版柟鍙互鍐欎竴涓嚱鏁帮紝鍙傛暟鍙互浠庡灞備紶鍏�
+                System.out.println("缃戠粶鎿嶄綔寮�濮�" + data);
+                Thread.sleep(data * 1000);
+                System.out.println("缃戠粶鎿嶄綔缁撴潫" + data);
+                return data + "";
+            });
+        }
+        List<String> users = pool.get();
+        for (String u : users) {
+            System.out.println(u);
+        }
+        pool.stop();
+    }
 }

--
Gitblit v1.8.0