From 0c96fa59709daf27deff4ab3bfac0b6ac049d62b Mon Sep 17 00:00:00 2001
From: mrz <17966059@qq.com>
Date: Wed, 15 Apr 2020 17:04:43 +0800
Subject: [PATCH] 后端导出excel

---
 config/application.yml                                                    |   7 ++++---
 pom.xml                                                                   |   6 ++++++
 src/main/java/com/tianbo/imfClient/controller/AgentController.java        | 248 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++---
 src/main/java/com/tianbo/imfClient/controller/NmmsAnalysisController.java |   5 ++++-
 src/main/resources/mapping/AGENTSTATISTICS.xml                            |   2 +-
 src/main/resources/mapping/ORIGINMANIFESTMASTERMapper.xml                 |   3 +++
 6 files changed, 263 insertions(+), 8 deletions(-)

diff --git a/config/application.yml b/config/application.yml
index 5279311..6d01046 100644
--- a/config/application.yml
+++ b/config/application.yml
@@ -16,7 +16,7 @@ spring:
     static-locations: classpath:/META-INF/resources/,classpath:/static,classpath:/resources/,file:${web.upload-path}
 
   application:
-    name: analysis-nmms
+    name: analysis-agent
 
   jackson:
     serialization:
@@ -44,8 +44,8 @@ spring:
         #oracle
         driver-class-name: oracle.jdbc.OracleDriver
         url: jdbc:oracle:thin:@10.50.3.69:1521:CGODB
-        username: CGONMS
-        password: 1q2w3e4r
+        username: CGOASM
+        password: vmvnv1v2
         #spring datasource mysql,注意编码配置,缺少数据库编码配置容易引起中文入库乱码
 #        url: jdbc:mysql://127.0.0.1:3307/statistics?useUnicode=true&characterEncoding=utf8
 #        username: root
@@ -88,6 +88,7 @@ eureka:
     service-url:
 #      defaultZone: http://10.50.3.82:19527/eureka/
       defaultZone: http://192.168.1.53:12345/eureka/
+#      defaultZone: http://127.0.0.1:12345/eureka/
     registry-fetch-interval-seconds:  30
     lease-renewal-interval-in-seconds: 15
     lease-expiration-duration-in-seconds: 45
diff --git a/pom.xml b/pom.xml
index b4d963b..5e5e644 100644
--- a/pom.xml
+++ b/pom.xml
@@ -88,6 +88,12 @@
             <artifactId>util</artifactId>
             <version>1.0-SNAPSHOT</version>
         </dependency>
+        <!--        excel导出-->
+        <dependency>
+            <groupId>org.apache.poi</groupId>
+            <artifactId>poi-ooxml</artifactId>
+            <version>4.1.0</version>
+        </dependency>
         <!--util依赖-->
         <!--&lt;!&ndash;IMF需要的包&ndash;&gt;-->
         <!--&lt;!&ndash; https://mvnrepository.com/artifact/org.apache.xmlbeans/xmlbeans &ndash;&gt;-->
diff --git a/src/main/java/com/tianbo/imfClient/controller/AgentController.java b/src/main/java/com/tianbo/imfClient/controller/AgentController.java
index 45586af..4b75e4c 100644
--- a/src/main/java/com/tianbo/imfClient/controller/AgentController.java
+++ b/src/main/java/com/tianbo/imfClient/controller/AgentController.java
@@ -4,9 +4,19 @@ import com.tianbo.imfClient.dao.AGENTSTATISTICSMapper;
 import com.tianbo.imfClient.dao.ARRIVEDSECONDARYMapper;
 import com.tianbo.imfClient.model.ORIGINMANIFESTMASTER;
 import com.tianbo.imfClient.model.ResultJson;
+import com.tianbo.util.POI.ExportExcelUtil;
+import lombok.extern.slf4j.Slf4j;
+import oracle.sql.DATE;
+import org.apache.poi.hssf.usermodel.*;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.web.bind.annotation.*;
 
+import javax.servlet.http.HttpServletResponse;
+import java.io.IOException;
+import java.io.OutputStream;
+import java.io.UnsupportedEncodingException;
+import java.net.URLEncoder;
+import java.nio.charset.StandardCharsets;
 import java.util.HashMap;
 import java.util.List;
 import java.util.Map;
@@ -14,6 +24,7 @@ import java.util.Map;
 /**
  * 出港运抵服务接口
  */
+@Slf4j
 @RestController
 @RequestMapping("/agent")
 public class AgentController {
@@ -24,14 +35,245 @@ public class AgentController {
      * 查询统计出港业务数据接口
      */
     @GetMapping("/analysis")
-    public ResultJson analysis(@RequestParam(value = "startdate",required = true) String startdate,
-                                        @RequestParam(value = "enddate",required = true) String enddate){
+    public void analysis(@RequestParam(value = "startdate",required = true) String startdate,
+                                        @RequestParam(value = "enddate",required = true) String enddate,
+                               HttpServletResponse httpServletResponse){
 
         Map map = new HashMap();
         map.put("startdate",startdate);
         map.put("enddate",enddate);
         List<ORIGINMANIFESTMASTER> list = agentstatisticsMapper.statistics(map);
+        exportExcel(list,httpServletResponse);
+    }
+
+    public void exportExcel(List<ORIGINMANIFESTMASTER> list,HttpServletResponse response){
+
+        // 创建工作簿
+        HSSFWorkbook workbook = new HSSFWorkbook();
+
+        // 创建表
+        HSSFSheet sheet = workbook.createSheet("export");
+        // 创建行
+        HSSFRow row = sheet.createRow(0);
+        // 创建单元格样式
+        HSSFCellStyle cellStyle = workbook.createCellStyle();
+        // 表头
+        String[] head = {
+                "运单号",
+                "航空托运人名称",
+                "航空销售代理人名称",
+                "航协编号",
+                "航空公司运输证明",
+                "运输条件鉴定书",
+                "承运人",
+                "航班号",
+                "航班日期",
+                "航班起始站",
+                "航班目的站",
+                "运单件数",
+                "运单重量",
+                "操作时间",
+                "关区",
+                "预配品名",
+                "收货人公司",
+                "收货人地址",
+                "收货人城市",
+                "收货人电话",
+                "收货人国家",
+                "收货人名称",
+                "发货人名称",
+                "发货人地址",
+                "发货人城市",
+                "发货人国家",
+                "发货人电话",
+                "发货人名称",
+                "海关回执状态",
+                "海关回执信息",
+                "发货人编码",
+                "收货人编码",
+                "货物品名",
+                "二级类名称",
+                "一级类名称",
+                "代理人三字码",
+                "代理人全称",
+                "代理人类别",
+                "代理联系人",
+                "代理联系人电话"
+        };
+        HSSFCell cell;
+        // 设置表头
+        for (int i = 0; i < head.length; i++) {
+            cell = row.createCell(i);
+            cell.setCellValue(head[i]);
+            cell.setCellStyle(cellStyle);
+            // 设置单元格宽度
+            sheet.setColumnWidth(i, 4000);
+        }
+        // 设置表格内容
+        for (int i = 0; i < list.size(); i++){
+            row = sheet.createRow( i + 1);
+            ORIGINMANIFESTMASTER originmanifestmaster = list.get(i);
+            // 这里是内容设置,替换则自己的数据即可
+            String[] excelTitle = new String[40];
+            excelTitle[0] = originmanifestmaster.getAutoid();
+            excelTitle[1] = originmanifestmaster.getWaybillnomaster();
+            excelTitle[2] = originmanifestmaster.getSegment();
+            excelTitle[3] = originmanifestmaster.getOriginatingstation();
+            excelTitle[4] = originmanifestmaster.getDestinationstation();
+            excelTitle[5] = originmanifestmaster.getTotalweight();
+            excelTitle[6] = originmanifestmaster.getTotalpiece();
+            excelTitle[7] = originmanifestmaster.getManifesttotalpiece();
+            excelTitle[8] = originmanifestmaster.getManifesttotalweight();
+            excelTitle[9] = originmanifestmaster.getFlightno();
+            excelTitle[10] = originmanifestmaster.getProductname();
+            excelTitle[11] = originmanifestmaster.getCustomsstatus();
+            excelTitle[12] = originmanifestmaster.getCarrier1();
+            excelTitle[13] = originmanifestmaster.getArrivalstation1();
+            excelTitle[14] = originmanifestmaster.getCarrier2();
+            excelTitle[15] = originmanifestmaster.getArrivalstation2();
+            excelTitle[16] = originmanifestmaster.getCarrier3();
+            excelTitle[17] = originmanifestmaster.getArrivalstation3();
+            excelTitle[18] = originmanifestmaster.getPaymode();
+            excelTitle[19] = originmanifestmaster.getCustomscode();
+            excelTitle[20] = originmanifestmaster.getSpecialgoodscode();
+            excelTitle[21] = originmanifestmaster.getShippername();
+            excelTitle[22] = originmanifestmaster.getShipperaddress();
+            excelTitle[23] = originmanifestmaster.getConsigneename();
+            excelTitle[24] = originmanifestmaster.getConsigneeaddress();
+            excelTitle[25] = originmanifestmaster.getReceiptinformation();
+            excelTitle[26] = originmanifestmaster.getSpecificConsigneePhone();
+            excelTitle[27] = originmanifestmaster.getConsigneePhone();
+            excelTitle[28] = originmanifestmaster.getStatus();
+            excelTitle[29] = originmanifestmaster.getIsbatch();
+            excelTitle[30] = originmanifestmaster.getOriginatingstationBill();
+            excelTitle[31] = originmanifestmaster.getDestinationstationBill();
+            excelTitle[32] = originmanifestmaster.getReportorder();
+            excelTitle[33] = originmanifestmaster.getIslast();
+            excelTitle[34] = originmanifestmaster.getShipperCode();
+            excelTitle[35] = originmanifestmaster.getShipperCountrycode();
+            excelTitle[36] = originmanifestmaster.getShipperPhone();
+            excelTitle[37] = originmanifestmaster.getShipperFax();
+            excelTitle[38] = originmanifestmaster.getConsigneeCode();
+            excelTitle[39] = originmanifestmaster.getConsigneeCountrycode();
+            for (int j = 0; j < excelTitle.length; j++){
+                row.createCell(j).setCellValue(excelTitle[j]);
+            }
+        }
+
+        // 设置文件名
+        String title ="export.xls";
+        try {
+            String fileName = new String(title.getBytes(StandardCharsets.UTF_8), "ISO-8859-1");
+             fileName = URLEncoder.encode(fileName,"utf-8");
+            response.setContentType("application/vnd.ms-excel;charset=utf-8");
+            response.setCharacterEncoding("utf-8");
+            //默认Excel名称
+            response.setHeader("Content-Disposition", "attachment;filename="+fileName+";"+"filename*=utf-8''"+fileName);
+        } catch (UnsupportedEncodingException e) {
+            e.printStackTrace();
+        }
+
+        try{
+            OutputStream os = response.getOutputStream();
+            workbook.write(os);
+            os.flush();
+            os.close();
+            log.info("导出完毕");
+        }catch (IOException e) {
+            e.printStackTrace();
+        }
+    }
 
-        return  new ResultJson("200","success",list);
+    public void export(List<ORIGINMANIFESTMASTER> list,HttpServletResponse response){
+        String[] header = {
+                "运单号",
+                "航空托运人名称",
+                "航空销售代理人名称",
+                "航协编号",
+                "航空公司运输证明",
+                "运输条件鉴定书",
+                "承运人",
+                "航班号",
+                "航班日期",
+                "航班起始站",
+                "航班目的站",
+                "运单件数",
+                "运单重量",
+                "操作时间",
+                "关区",
+                "预配品名",
+                "收货人公司",
+                "收货人地址",
+                "收货人城市",
+                "收货人电话",
+                "收货人国家",
+                "收货人名称",
+                "发货人名称",
+                "发货人地址",
+                "发货人城市",
+                "发货人国家",
+                "发货人电话",
+                "发货人名称",
+                "海关回执状态",
+                "海关回执信息",
+                "发货人编码",
+                "收货人编码",
+                "货物品名",
+                "二级类名称",
+                "一级类名称",
+                "代理人三字码",
+                "代理人全称",
+                "代理人类别",
+                "代理联系人",
+                "代理联系人电话"
+        };
+        String[] columns = {
+                "autoid",
+                "waybillnomaster",
+                "segment",
+                "originatingstation",
+                "destinationstation",
+                "totalweight",
+                "totalpiece",
+                "manifesttotalpiece",
+                "manifesttotalweight",
+                "flightno",
+                "productname",
+                "customsstatus",
+                "carrier1",
+                "arrivalstation1",
+                "carrier2",
+                "arrivalstation2",
+                "carrier3",
+                "arrivalstation3",
+                "paymode",
+                "customscode",
+                "specialgoodscode",
+                "shippername",
+                "shipperaddress",
+                "consigneename",
+                "consigneeaddress",
+                "receiptinformation",
+                "specificConsigneePhone",
+                "consigneePhone",
+                "status",
+                "isbatch",
+                "originatingstationBill",
+                "destinationstationBill",
+                "reportorder",
+                "islast",
+                "shipperCode",
+                "shipperCountrycode",
+                "shipperPhone",
+                "shipperFax",
+                "consigneeCode",
+                "consigneeCountrycode"
+        };
+        ExportExcelUtil exportExcelUtil = new ExportExcelUtil();
+        try{
+            exportExcelUtil.export("export.xls","export",header,columns,list,response);
+        }catch (Exception e){
+            e.printStackTrace();
+        }
     }
 }
diff --git a/src/main/java/com/tianbo/imfClient/controller/NmmsAnalysisController.java b/src/main/java/com/tianbo/imfClient/controller/NmmsAnalysisController.java
index c9bf2d0..a239718 100644
--- a/src/main/java/com/tianbo/imfClient/controller/NmmsAnalysisController.java
+++ b/src/main/java/com/tianbo/imfClient/controller/NmmsAnalysisController.java
@@ -27,11 +27,14 @@ public class NmmsAnalysisController {
      */
     @GetMapping("/analysis")
     public ResultJson analysis(@RequestParam(value = "startdate",required = true) String startdate,
-                                        @RequestParam(value = "enddate",required = true) String enddate){
+                               @RequestParam(value = "enddate",required = true) String enddate,
+                               @RequestParam(value = "flightno",required = false) String flightno
+    ){
 
         Map map = new HashMap();
         map.put("startdate",startdate);
         map.put("enddate",enddate);
+        map.put("flightno",flightno);
         List<ORIGINMANIFESTMASTER> list = mapper.analysis(map);
 
         return  new ResultJson("200","success",list);
diff --git a/src/main/resources/mapping/AGENTSTATISTICS.xml b/src/main/resources/mapping/AGENTSTATISTICS.xml
index 0eab49b..b0a4737 100644
--- a/src/main/resources/mapping/AGENTSTATISTICS.xml
+++ b/src/main/resources/mapping/AGENTSTATISTICS.xml
@@ -50,7 +50,7 @@ SELECT
 	SD.SD_WAYBILL  "运单号",
 	SD.SD_CARGO_SHIPPER_NAME  "航空托运人名称" ,
 	SD.SD_CARGO_AGENT_NAME  "航空销售代理人名称",
-	SD.SD_IATA_NUMBER  "航协编号 ",
+	SD.SD_IATA_NUMBER  "航协编号",
 	SD.SD_TRANSPORT_CERTIFICATE  "航空公司运输证明",
 	SD.SD_TRANSPORTATION_PROVE  "运输条件鉴定书",
 	MF.CARRIER  "承运人",
diff --git a/src/main/resources/mapping/ORIGINMANIFESTMASTERMapper.xml b/src/main/resources/mapping/ORIGINMANIFESTMASTERMapper.xml
index 91ad720..591e1f9 100644
--- a/src/main/resources/mapping/ORIGINMANIFESTMASTERMapper.xml
+++ b/src/main/resources/mapping/ORIGINMANIFESTMASTERMapper.xml
@@ -144,6 +144,9 @@ LEFT JOIN
 	WHERE
 	trunc( T.FLIGHT_DATE ) BETWEEN to_date( #{startdate,jdbcType=VARCHAR}, 'yyyy-mm-dd' )
 	AND to_date(#{enddate,jdbcType=VARCHAR},'yyyy-mm-dd')
+    <if test="flightno != null" >
+      AND T.FLIGHTNO = #{flightno,jdbcType=VARCHAR}
+    </if>
 	ORDER BY T.FLIGHT_DATE,T.FLIGHTNO,WAYBILLNOMASTER,WAYBILLNOSECONDARY DESC
 
   </select>
--
libgit2 0.24.0