From 50f92d69cb712beba1a6155e27bcc92c2c3a38c3 Mon Sep 17 00:00:00 2001 From: yixian Date: Tue, 24 Dec 2019 14:21:10 +0800 Subject: [PATCH 1/3] export csv --- .../manage/tradelog/core/TradeLogService.java | 2 + .../core/impls/TradeLogServiceImpl.java | 189 +++++++++++++----- .../tradelog/web/TradeFlowController.java | 36 ++-- 3 files changed, 161 insertions(+), 66 deletions(-) diff --git a/src/main/java/au/com/royalpay/payment/manage/tradelog/core/TradeLogService.java b/src/main/java/au/com/royalpay/payment/manage/tradelog/core/TradeLogService.java index 1b4c91ff6..2f45b373e 100644 --- a/src/main/java/au/com/royalpay/payment/manage/tradelog/core/TradeLogService.java +++ b/src/main/java/au/com/royalpay/payment/manage/tradelog/core/TradeLogService.java @@ -58,6 +58,8 @@ public interface TradeLogService { void exportSettlementLog(TradeLogQuery query, JSONObject partner, HttpServletResponse response); + void exportSettlementLogCSV(TradeLogQuery query, JSONObject partner, HttpServletResponse response); + void exportPDFSettlement(TradeLogQuery query, JSONObject partner, HttpServletResponse response); void exportExcelAllPartner(TradeLogQuery query, JSONObject partner, HttpServletResponse httpResponse) throws Exception; diff --git a/src/main/java/au/com/royalpay/payment/manage/tradelog/core/impls/TradeLogServiceImpl.java b/src/main/java/au/com/royalpay/payment/manage/tradelog/core/impls/TradeLogServiceImpl.java index 760daebd6..0654b9941 100644 --- a/src/main/java/au/com/royalpay/payment/manage/tradelog/core/impls/TradeLogServiceImpl.java +++ b/src/main/java/au/com/royalpay/payment/manage/tradelog/core/impls/TradeLogServiceImpl.java @@ -22,6 +22,7 @@ import au.com.royalpay.payment.manage.tradelog.core.TradeLogService; import au.com.royalpay.payment.tools.defines.TradeType; import au.com.royalpay.payment.tools.env.PlatformEnvironment; import au.com.royalpay.payment.tools.exceptions.BadRequestException; +import au.com.royalpay.payment.tools.exceptions.ServerErrorException; import au.com.royalpay.payment.tools.permission.enums.ManagerRole; import au.com.royalpay.payment.tools.permission.enums.PartnerRole; import au.com.royalpay.payment.tools.utils.CurrencyAmountUtils; @@ -40,6 +41,8 @@ import net.sf.jasperreports.engine.data.JRBeanCollectionDataSource; import net.sf.jasperreports.engine.export.JRXlsExporter; import net.sf.jasperreports.engine.export.ooxml.JRXlsxExporter; import net.sf.jasperreports.export.*; +import org.apache.commons.csv.CSVFormat; +import org.apache.commons.csv.CSVPrinter; import org.apache.commons.io.IOUtils; import org.apache.commons.lang3.StringEscapeUtils; import org.apache.commons.lang3.StringUtils; @@ -58,9 +61,7 @@ import org.springframework.ui.Model; import javax.annotation.Resource; import javax.servlet.http.HttpServletResponse; -import java.io.ByteArrayOutputStream; -import java.io.IOException; -import java.io.OutputStream; +import java.io.*; import java.math.BigDecimal; import java.math.RoundingMode; import java.text.DateFormat; @@ -150,8 +151,10 @@ public class TradeLogServiceImpl implements TradeLogService { if (manager != null && manager.getInteger("org_id") != null) { params.put("org_id", manager.getIntValue("org_id")); } - if(query.getGatewayChild()!=null){ - params.put("trade_type",new ArrayList(){{add(query.getGatewayChild());}}); + if (query.getGatewayChild() != null) { + params.put("trade_type", new ArrayList() {{ + add(query.getGatewayChild()); + }}); } PageList logs = orderMapper.listOrdersByClients(params, new PageBounds(query.getPage(), query.getLimit(), Order.formString("create_time.desc"))); @@ -185,8 +188,10 @@ public class TradeLogServiceImpl implements TradeLogService { if (manager != null && manager.getInteger("org_id") != null) { params.put("org_id", manager.getIntValue("org_id")); } - if(query.getGatewayChild()!=null){ - params.put("trade_type",new ArrayList(){{add(query.getGatewayChild());}}); + if (query.getGatewayChild() != null) { + params.put("trade_type", new ArrayList() {{ + add(query.getGatewayChild()); + }}); } PageList logs = orderMapper.listIncrementalOrdersByClients(params, new PageBounds(query.getPage(), query.getLimit(), Order.formString("create_time.desc"))); @@ -222,8 +227,10 @@ public class TradeLogServiceImpl implements TradeLogService { } } orgManager.checkOrgIds(manager, params); - if(query.getGatewayChild()!=null){ - params.put("trade_type",new ArrayList(){{add(query.getGatewayChild());}}); + if (query.getGatewayChild() != null) { + params.put("trade_type", new ArrayList() {{ + add(query.getGatewayChild()); + }}); } PageList logs = orderMapper.listOrders(params, new PageBounds(query.getPage(), query.getLimit(), Order.formString("create_time.desc"))); JSONObject result = PageListUtils.buildPageListResult(logs); @@ -246,12 +253,14 @@ public class TradeLogServiceImpl implements TradeLogService { } } orgManager.checkOrgIds(manager, params); - if(query.getGatewayChild()!=null){ - params.put("trade_type",new ArrayList(){{add(query.getGatewayChild());}}); + if (query.getGatewayChild() != null) { + params.put("trade_type", new ArrayList() {{ + add(query.getGatewayChild()); + }}); } PageList logs = orderMapper.listIncrementalOrders(params, new PageBounds(query.getPage(), query.getLimit(), Order.formString("create_time.desc"))); for (JSONObject log : logs) { - log.put("rate_value",clientIncrementalMapper.findByChannelAndClientId(log.getIntValue("client_id"),log.getString("source"))); + log.put("rate_value", clientIncrementalMapper.findByChannelAndClientId(log.getIntValue("client_id"), log.getString("source"))); } JSONObject result = PageListUtils.buildPageListResult(logs); JSONObject analysis = orderMapper.analysisOrders(params); @@ -791,7 +800,7 @@ public class TradeLogServiceImpl implements TradeLogService { row.createCell(10, Cell.CELL_TYPE_STRING).setCellValue(data.getString("clearing_amount")); row.createCell(11, Cell.CELL_TYPE_STRING).setCellValue(data.getString("settle_amount")); row.createCell(12, Cell.CELL_TYPE_STRING).setCellValue(data.getString("total_surcharge")); - row.createCell(13, Cell.CELL_TYPE_STRING).setCellValue(StringUtils.defaultString(data.getString("incremental_surcharge"),"0.0000")); + row.createCell(13, Cell.CELL_TYPE_STRING).setCellValue(StringUtils.defaultString(data.getString("incremental_surcharge"), "0.0000")); row.createCell(14, Cell.CELL_TYPE_STRING).setCellValue(data.getString("tax_amount")); row.createCell(15, Cell.CELL_TYPE_STRING).setCellValue(data.getString("clearing_time")); row.createCell(16, Cell.CELL_TYPE_STRING).setCellValue(data.getString("trans_type").equals("clearing") ? "-" : data.getBigDecimal("exchange_rate").toString()); @@ -907,9 +916,9 @@ public class TradeLogServiceImpl implements TradeLogService { cell0.setCellValue(text0); cell1.setCellValue(text1); cell2.setCellValue(text2); - if(dataItem.getString("transaction_type").equals("Debit") ){ + if (dataItem.getString("transaction_type").equals("Debit")) { cell2.setCellStyle(style3); - }else{ + } else { cell2.setCellStyle(style2); } cell3.setCellValue(text3); @@ -971,12 +980,12 @@ public class TradeLogServiceImpl implements TradeLogService { } @Override - public void listSettlementLogExcel(TradeLogQuery query, JSONObject partner,HttpServletResponse response) { + public void listSettlementLogExcel(TradeLogQuery query, JSONObject partner, HttpServletResponse response) { JSONObject client = clientManager.getClientInfoByMoniker(partner.getString("client_moniker")); int clientId = partner.getIntValue("client_id"); String timezone = partner.getJSONObject("client").getString("timezone"); - if(query.getClient_ids()!=null){ - if(query.getClient_ids().length>1){ + if (query.getClient_ids() != null) { + if (query.getClient_ids().length > 1) { query.setClient_ids(null); } } @@ -985,21 +994,21 @@ public class TradeLogServiceImpl implements TradeLogService { PageList logs = transactionMapper.listSettlementLog(params, new PageBounds(query.getPage(), query.getLimit(), Order.formString("clearing_time.desc"))); try (HSSFWorkbook workbook = new HSSFWorkbook()) { - if(query.getClient_ids() ==null){ - addSheet(0,workbook,client,logs); + if (query.getClient_ids() == null) { + addSheet(0, workbook, client, logs); List childs = clientMapper.listChildClients(client.getIntValue("client_id")); - for (int i=0;i childLogs = transactionMapper.listSettlementLog(params, new PageBounds(query.getPage(), 10000, Order.formString("clearing_time.desc"))); - if(childLogs.size()>0){ - addSheet(i+1,workbook, childs.get(i),childLogs); + if (childLogs.size() > 0) { + addSheet(i + 1, workbook, childs.get(i), childLogs); } } - }else{ + } else { JSONObject chlidClient = clientMapper.findClient(Integer.parseInt(query.getClient_ids()[0])); - addSheet(0,workbook,chlidClient,logs); + addSheet(0, workbook, chlidClient, logs); } - String fileName = "Settlement Log - "+ (query.getDatefrom() + "~" + query.getDateto() ); + String fileName = "Settlement Log - " + (query.getDatefrom() + "~" + query.getDateto()); response.setHeader("Content-Disposition", "attachment;filename=" + fileName + ".xls"); OutputStream outputStream = response.getOutputStream(); try { @@ -1013,9 +1022,9 @@ public class TradeLogServiceImpl implements TradeLogService { } } - private void addSheet(int sheetNum,HSSFWorkbook workbook,JSONObject client,List logs){ + private void addSheet(int sheetNum, HSSFWorkbook workbook, JSONObject client, List logs) { HSSFSheet sheet = workbook.createSheet(); - workbook.setSheetName(sheetNum,client.getString("client_moniker")+"_excel"); + workbook.setSheetName(sheetNum, client.getString("client_moniker") + "_excel"); sheet.setDefaultColumnWidth((short) 40); HSSFCellStyle style = workbook.createCellStyle(); style.setFillForegroundColor(HSSFColor.LIGHT_ORANGE.index); @@ -1086,7 +1095,7 @@ public class TradeLogServiceImpl implements TradeLogService { JSONObject dataItem = logs.get(i); HSSFRichTextString text0 = new HSSFRichTextString(client.getString("short_name")); HSSFRichTextString text1 = new HSSFRichTextString(client.getString("client_moniker")); - HSSFRichTextString text2 = new HSSFRichTextString(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format( dataItem.getDate("report_date"))); + HSSFRichTextString text2 = new HSSFRichTextString(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(dataItem.getDate("report_date"))); HSSFRichTextString text3 = new HSSFRichTextString(dataItem.getBigDecimal("total").setScale(2, BigDecimal.ROUND_HALF_DOWN).toString()); HSSFRichTextString text4 = new HSSFRichTextString(dataItem.getBigDecimal("income").setScale(2, BigDecimal.ROUND_HALF_DOWN).toString()); HSSFRichTextString text5 = new HSSFRichTextString(dataItem.getBigDecimal("fee").setScale(2, BigDecimal.ROUND_HALF_DOWN).toString()); @@ -1468,33 +1477,21 @@ public class TradeLogServiceImpl implements TradeLogService { row.createCell(5, Cell.CELL_TYPE_STRING).setCellValue(settle.getBigDecimal("exchange_rate").setScale(5, RoundingMode.DOWN).toPlainString()); cell = row.createCell(6, Cell.CELL_TYPE_STRING); cell.setCellValue(settle.getString("transaction_type")); - cell.setCellStyle(!"Credit".equals(settle.getString("transaction_type"))?analysisStyle2:null); + cell.setCellStyle(!"Credit".equals(settle.getString("transaction_type")) ? analysisStyle2 : null); row.createCell(7, Cell.CELL_TYPE_STRING).setCellValue(settle.getString("transaction_currency")); cell = row.createCell(8, Cell.CELL_TYPE_STRING); - cell.setCellValue(settle.getBigDecimal("display_amount") == null ? "" - : "Credit".equals(settle.getString("transaction_type"))? - settle.getBigDecimal("display_amount").setScale(2, BigDecimal.ROUND_DOWN).toPlainString(): - "-"+settle.getBigDecimal("display_amount").setScale(2, BigDecimal.ROUND_DOWN).toPlainString()); - cell.setCellStyle(!"Credit".equals(settle.getString("transaction_type"))?analysisStyle2:null); + cell.setCellValue(convertMarkedAmountString(settle, "display_amount")); + cell.setCellStyle(!"Credit".equals(settle.getString("transaction_type")) ? analysisStyle2 : null); cell = row.createCell(9, Cell.CELL_TYPE_STRING); - cell.setCellValue(settle.getBigDecimal("transaction_amount") == null ? "" - : "Credit".equals(settle.getString("transaction_type"))? - settle.getBigDecimal("transaction_amount").setScale(2, BigDecimal.ROUND_DOWN).toPlainString(): - "-"+settle.getBigDecimal("transaction_amount").setScale(2, BigDecimal.ROUND_DOWN).toPlainString()); - cell.setCellStyle(!"Credit".equals(settle.getString("transaction_type"))?analysisStyle2:null); + cell.setCellValue(convertMarkedAmountString(settle, "transaction_amount")); + cell.setCellStyle(!"Credit".equals(settle.getString("transaction_type")) ? analysisStyle2 : null); cell = row.createCell(10, Cell.CELL_TYPE_STRING); - cell.setCellValue(settle.getBigDecimal("clearing_amount") == null ? "" - : "Credit".equals(settle.getString("transaction_type"))? - settle.getBigDecimal("clearing_amount").setScale(2, BigDecimal.ROUND_DOWN).toPlainString(): - "-"+settle.getBigDecimal("clearing_amount").setScale(2, BigDecimal.ROUND_DOWN).toPlainString()); - cell.setCellStyle(!"Credit".equals(settle.getString("transaction_type"))?analysisStyle2:null); + cell.setCellValue(convertMarkedAmountString(settle, "clearing_amount")); + cell.setCellStyle(!"Credit".equals(settle.getString("transaction_type")) ? analysisStyle2 : null); row.createCell(11, Cell.CELL_TYPE_STRING).setCellValue(settle.getBigDecimal("rate") == null ? "" : settle.getBigDecimal("rate").toPlainString() + "%"); cell = row.createCell(12, Cell.CELL_TYPE_STRING); - cell.setCellValue(settle.getBigDecimal("settle_amount") == null ? "" - : "Credit".equals(settle.getString("transaction_type"))? - settle.getBigDecimal("settle_amount").setScale(2, BigDecimal.ROUND_DOWN).toPlainString(): - "-"+settle.getBigDecimal("settle_amount").setScale(2, BigDecimal.ROUND_DOWN).toPlainString()); - cell.setCellStyle(!"Credit".equals(settle.getString("transaction_type"))?analysisStyle2:null); + cell.setCellValue(convertMarkedAmountString(settle, "settle_amount")); + cell.setCellStyle(!"Credit".equals(settle.getString("transaction_type")) ? analysisStyle2 : null); row.createCell(13, Cell.CELL_TYPE_STRING).setCellValue(settle.getBigDecimal("total_surcharge") == null ? "" : settle.getBigDecimal("total_surcharge").setScale(2, RoundingMode.DOWN).toPlainString()); row.createCell(14, Cell.CELL_TYPE_STRING).setCellValue(settle.getBigDecimal("tax_amount") == null ? "" @@ -1524,9 +1521,97 @@ public class TradeLogServiceImpl implements TradeLogService { wb.write(ous); ous.flush(); } catch (IOException e) { - e.printStackTrace(); + throw new ServerErrorException(e); + } + + } + + @Override + public void exportSettlementLogCSV(TradeLogQuery query, JSONObject partner, HttpServletResponse response) { + String begin = query.getDatefrom() == null ? "" : query.getDatefrom(); + String end = query.getDateto() == null ? "" : query.getDateto(); + String timezone = partner.getJSONObject("client").getString("timezone"); + JSONObject params = query.toParams(timezone); + if (params.get("client_ids") == null) { + params.put("client_id", partner.getJSONObject("client").getString("client_id")); + } + List clientOrderList = transactionMapper.getClientOrderByTransactionTime(params); + List clientOrders = new ArrayList<>(clientOrderList.size()); + clientOrderList.parallelStream().forEach(p -> clientOrders.add(p.getInteger("clearing_order"))); + List settlementLogDetailList = transactionMapper.getSettlementLogDetailList(clientOrders); + String[] title = {"order Id", "Client Order Id", "Transaction Time", "Channel", "Gateway", "Exchange Rate", "Transaction Type", "Currency", + "Input Amount", "Total Amount", "Clearing Amount", "Surcharge Rate", "Settle Amount", "Surcharge", "GST", "Remark", "Dev No", "Company Name", "Short Name", "Settlement Date"}; + CSVFormat format = CSVFormat.DEFAULT.withFirstRecordAsHeader().withHeader(title); + response.setContentType("application/octet-stream;"); + response.addHeader("Content-Disposition", + "attachment; filename=" + "Merchant_Settlement_Info_" + begin + "_" + end + ".csv"); + try (PrintWriter writer = response.getWriter(); + CSVPrinter printer = new CSVPrinter(writer, format)) { + JSONObject clearDetailTotal = transactionMapper.getClearDetailTotal(params); + + + for (JSONObject settle : settlementLogDetailList) { + if (settle.getBigDecimal("clearing_amount").compareTo(BigDecimal.ZERO) == 0) { + continue; + } + String gateway = settle.getInteger("gateway") != null ? TradeType.fromGatewayNumber(settle.getIntValue("gateway")).getTradeType() : "-"; + String displayAmount = convertMarkedAmountString(settle, "display_amount"); + String transAmt = convertMarkedAmountString(settle, "transaction_amount"); + String clearAmt = convertMarkedAmountString(settle, "clearing_amount"); + String settleAmt = convertMarkedAmountString(settle, "settle_amount"); + JSONObject device = clientDeviceMapper.find(settle.getString("dev_id")); + String clientDevId = device == null ? "" : device.getString("client_dev_id"); + //print row + printer.printRecord( + settle.getString("order_id"), + settle.getString("client_order_id"), + settle.getString("transaction_time"), + settle.getString("channel"), + gateway, + settle.getBigDecimal("exchange_rate").setScale(5, RoundingMode.DOWN).toPlainString(), + settle.getString("transaction_type"), + settle.getString("transaction_currency"), + displayAmount, + transAmt, + clearAmt, + settle.getBigDecimal("rate") == null ? "" : settle.getBigDecimal("rate").toPlainString() + "%", + settleAmt, + settle.getBigDecimal("total_surcharge") == null ? "" + : settle.getBigDecimal("total_surcharge").setScale(2, RoundingMode.DOWN).toPlainString(), + settle.getBigDecimal("tax_amount") == null ? "" + : settle.getBigDecimal("tax_amount").setScale(2, RoundingMode.DOWN).toPlainString(), + settle.getString("remark"), + clientDevId, settle.getString("company_name"), + settle.getString("short_name"), + settle.getString("settle_date")); + } + String[] analysisRow = {"Total Credit", + clearDetailTotal.getString("total_payment"), + "Total Debit", + clearDetailTotal.getString("total_refund"), + "Gross Amount", + clearDetailTotal.getString("gross_amount"), + "Total Charge", + clearDetailTotal.getBigDecimal("total_charge").add(clearDetailTotal.getBigDecimal("tax_amount")).toPlainString(), + "Net Amount", + clearDetailTotal.getString("clearing_amount")}; + + + printer.printRecord((Object[]) analysisRow); + writer.flush(); + } catch (IOException e) { + throw new ServerErrorException(e); } + } + public String convertMarkedAmountString(JSONObject settle, String key) { + if (settle.getBigDecimal(key) == null) { + return ""; + } else { + return "Credit".equals(settle.getString("transaction_type")) ? + settle.getBigDecimal(key).setScale(2, RoundingMode.DOWN).toPlainString() : + "-" + settle.getBigDecimal(key).setScale(2, RoundingMode.DOWN).toPlainString(); + } } @Override @@ -1574,7 +1659,7 @@ public class TradeLogServiceImpl implements TradeLogService { outs.flush(); outs.close(); } catch (Exception e) { - e.printStackTrace(); + throw new ServerErrorException(e); } } diff --git a/src/main/java/au/com/royalpay/payment/manage/tradelog/web/TradeFlowController.java b/src/main/java/au/com/royalpay/payment/manage/tradelog/web/TradeFlowController.java index 7faab9aee..42c2660fa 100644 --- a/src/main/java/au/com/royalpay/payment/manage/tradelog/web/TradeFlowController.java +++ b/src/main/java/au/com/royalpay/payment/manage/tradelog/web/TradeFlowController.java @@ -36,51 +36,59 @@ public class TradeFlowController { @PartnerMapping(value = "/report/pdf", method = RequestMethod.GET) @ReadOnlyConnection public void exportPDF(TradeLogQuery query, @ModelAttribute(CommonConsts.PARTNER_STATUS) JSONObject partner, HttpServletResponse httpResponse) throws Exception { - tradeLogService.exportTransFlow(query,partner,httpResponse); + tradeLogService.exportTransFlow(query, partner, httpResponse); } @PartnerMapping(value = "/report/excel", method = RequestMethod.GET) @ReadOnlyConnection public void exportExcel(TradeLogQuery query, @ModelAttribute(CommonConsts.PARTNER_STATUS) JSONObject partner, HttpServletResponse httpResponse) throws Exception { - tradeLogService.exportExcelNew(query,partner,httpResponse); + tradeLogService.exportExcelNew(query, partner, httpResponse); } + @PartnerMapping(value = "/report/excel_detail", method = RequestMethod.GET) @ReadOnlyConnection public void exportExcelDetail(TradeLogQuery query, @ModelAttribute(CommonConsts.PARTNER_STATUS) JSONObject partner, HttpServletResponse httpResponse) throws Exception { - tradeLogService.exportExcel(query,partner,httpResponse); + tradeLogService.exportExcel(query, partner, httpResponse); } - @PartnerMapping(value = "/settlement/log",method = RequestMethod.GET) + @PartnerMapping(value = "/settlement/log", method = RequestMethod.GET) @ReadOnlyConnection @ResponseBody - public JSONObject listSettlementLog(TradeLogQuery query,@ModelAttribute(CommonConsts.PARTNER_STATUS) JSONObject partner){ - return tradeLogService.listSettlementLog(query,partner); + public JSONObject listSettlementLog(TradeLogQuery query, @ModelAttribute(CommonConsts.PARTNER_STATUS) JSONObject partner) { + return tradeLogService.listSettlementLog(query, partner); } - @PartnerMapping(value = "/settlement/log/excelAll",method = RequestMethod.GET) + @PartnerMapping(value = "/settlement/log/excelAll", method = RequestMethod.GET) @ReadOnlyConnection @ResponseBody - public void listSettlementLogExcel(TradeLogQuery query,@ModelAttribute(CommonConsts.PARTNER_STATUS) JSONObject partner,HttpServletResponse response){ - tradeLogService.listSettlementLogExcel(query,partner,response); + public void listSettlementLogExcel(TradeLogQuery query, @ModelAttribute(CommonConsts.PARTNER_STATUS) JSONObject partner, HttpServletResponse response) { + tradeLogService.listSettlementLogExcel(query, partner, response); } - @PartnerMapping(value = "/settlement/unclear",method = RequestMethod.GET) + @PartnerMapping(value = "/settlement/unclear", method = RequestMethod.GET) @ReadOnlyConnection @ResponseBody - public String getClientUnClearedAmount(@ModelAttribute(CommonConsts.PARTNER_STATUS) JSONObject partner){ + public String getClientUnClearedAmount(@ModelAttribute(CommonConsts.PARTNER_STATUS) JSONObject partner) { // JSONObject res = new JSONObject(); // res.put("unCleared",tradeLogService.getClientUnClearedAmount(partner)); return tradeLogService.getClientUnClearedAmount(partner).toString(); } - @PartnerMapping(value = "/settlement/log/excel",method = RequestMethod.GET) + @PartnerMapping(value = "/settlement/log/excel", method = RequestMethod.GET) @ReadOnlyConnection @ResponseBody public void exportSettlementLog(TradeLogQuery query, @ModelAttribute(CommonConsts.PARTNER_STATUS) JSONObject partner, HttpServletResponse response) { tradeLogService.exportSettlementLog(query, partner, response); } - @PartnerMapping(value = "/settlement/log/pdf",method = RequestMethod.GET) + @PartnerMapping(value = "/settlement/log/csv", method = RequestMethod.GET) + @ReadOnlyConnection + @ResponseBody + public void exportSettlementLogCSV(TradeLogQuery query, @ModelAttribute(CommonConsts.PARTNER_STATUS) JSONObject partner, HttpServletResponse response) { + tradeLogService.exportSettlementLogCSV(query, partner, response); + } + + @PartnerMapping(value = "/settlement/log/pdf", method = RequestMethod.GET) @ReadOnlyConnection @ResponseBody public void exportLogPDF(TradeLogQuery query, @ModelAttribute(CommonConsts.PARTNER_STATUS) JSONObject partner, HttpServletResponse response) { @@ -90,6 +98,6 @@ public class TradeFlowController { @PartnerMapping(value = "/report/excel_detail_all", method = RequestMethod.GET) @ReadOnlyConnection public void exportExcelDetailAllPartner(TradeLogQuery query, @ModelAttribute(CommonConsts.PARTNER_STATUS) JSONObject partner, HttpServletResponse httpResponse) throws Exception { - tradeLogService.exportExcelAllPartner(query,partner,httpResponse); + tradeLogService.exportExcelAllPartner(query, partner, httpResponse); } } From 3b6db8dff54c419f6fbee8119a0448e17621fb42 Mon Sep 17 00:00:00 2001 From: yixian Date: Tue, 24 Dec 2019 14:25:05 +0800 Subject: [PATCH 2/3] ui update --- .../payment/tradelog/partner-settlement-log.js | 18 ++++++++++-------- .../templates/partner_settlement_logs.html | 1 + 2 files changed, 11 insertions(+), 8 deletions(-) diff --git a/src/main/ui/static/payment/tradelog/partner-settlement-log.js b/src/main/ui/static/payment/tradelog/partner-settlement-log.js index dc6399f91..d515dd67b 100644 --- a/src/main/ui/static/payment/tradelog/partner-settlement-log.js +++ b/src/main/ui/static/payment/tradelog/partner-settlement-log.js @@ -97,9 +97,9 @@ define(['angular', 'uiBootstrap', 'uiRouter'], function (angular) { $scope.analysis = resp.data.analysis; }); }; - $scope.excelSettlementAllLogs = function(){ + $scope.excelSettlementAllLogs = function () { var params = angular.copy($scope.params); - var url = '/client/trans_flow/settlement/log/excelAll'; + var url = '/client/trans_flow/settlement/log/excelAll'; var connectSymbol = '?'; if (params.datefrom) { params.datefrom = $filter('date')(params.datefrom, 'yyyyMMdd'); @@ -110,7 +110,7 @@ define(['angular', 'uiBootstrap', 'uiRouter'], function (angular) { params.dateto = $filter('date')(params.dateto, 'yyyyMMdd'); url += connectSymbol + 'dateto=' + params.dateto; } - if (params.client_ids){ + if (params.client_ids) { params.client_ids.forEach(function (i) { url += connectSymbol + 'client_ids=' + i; connectSymbol = '&'; @@ -120,13 +120,15 @@ define(['angular', 'uiBootstrap', 'uiRouter'], function (angular) { }; $scope.chooseLast7Days(); - $scope.exportSettlementLogs = function(pattern) { - var params = angular.copy($scope.params); - var url = '/client/trans_flow/settlement/log/excel'; + $scope.exportSettlementLogs = function (pattern) { + let params = angular.copy($scope.params); + let url = '/client/trans_flow/settlement/log/excel'; if (pattern === 'pdf') { url = '/client/trans_flow/settlement/log/pdf'; + } else if (pattern === 'csv') { + url = '/client/trans_flow/settlement/log/csv'; } - var connectSymbol = '?'; + let connectSymbol = '?'; if (params.datefrom) { params.datefrom = $filter('date')(params.datefrom, 'yyyyMMdd'); url += connectSymbol + 'datefrom=' + params.datefrom; @@ -136,7 +138,7 @@ define(['angular', 'uiBootstrap', 'uiRouter'], function (angular) { params.dateto = $filter('date')(params.dateto, 'yyyyMMdd'); url += connectSymbol + 'dateto=' + params.dateto; } - if (params.client_ids){ + if (params.client_ids) { params.client_ids.forEach(function (i) { url += connectSymbol + 'client_ids=' + i; connectSymbol = '&'; diff --git a/src/main/ui/static/payment/tradelog/templates/partner_settlement_logs.html b/src/main/ui/static/payment/tradelog/templates/partner_settlement_logs.html index e1077c1fd..349501fb0 100644 --- a/src/main/ui/static/payment/tradelog/templates/partner_settlement_logs.html +++ b/src/main/ui/static/payment/tradelog/templates/partner_settlement_logs.html @@ -107,6 +107,7 @@ From 4c975e848542c2680f0c550c698a4fecd47ef46c Mon Sep 17 00:00:00 2001 From: yixian Date: Tue, 24 Dec 2019 15:30:13 +0800 Subject: [PATCH 3/3] csv charset fix --- .../core/impls/TradeLogServiceImpl.java | 3 ++ .../payment/manage/valid/CSVTest.java | 29 +++++++++++++++++++ 2 files changed, 32 insertions(+) create mode 100644 src/test/java/au/com/royalpay/payment/manage/valid/CSVTest.java diff --git a/src/main/java/au/com/royalpay/payment/manage/tradelog/core/impls/TradeLogServiceImpl.java b/src/main/java/au/com/royalpay/payment/manage/tradelog/core/impls/TradeLogServiceImpl.java index 0654b9941..15bdf7b21 100644 --- a/src/main/java/au/com/royalpay/payment/manage/tradelog/core/impls/TradeLogServiceImpl.java +++ b/src/main/java/au/com/royalpay/payment/manage/tradelog/core/impls/TradeLogServiceImpl.java @@ -43,6 +43,7 @@ import net.sf.jasperreports.engine.export.ooxml.JRXlsxExporter; import net.sf.jasperreports.export.*; import org.apache.commons.csv.CSVFormat; import org.apache.commons.csv.CSVPrinter; +import org.apache.commons.csv.QuoteMode; import org.apache.commons.io.IOUtils; import org.apache.commons.lang3.StringEscapeUtils; import org.apache.commons.lang3.StringUtils; @@ -1542,11 +1543,13 @@ public class TradeLogServiceImpl implements TradeLogService { String[] title = {"order Id", "Client Order Id", "Transaction Time", "Channel", "Gateway", "Exchange Rate", "Transaction Type", "Currency", "Input Amount", "Total Amount", "Clearing Amount", "Surcharge Rate", "Settle Amount", "Surcharge", "GST", "Remark", "Dev No", "Company Name", "Short Name", "Settlement Date"}; CSVFormat format = CSVFormat.DEFAULT.withFirstRecordAsHeader().withHeader(title); + response.setCharacterEncoding("UTF-8"); response.setContentType("application/octet-stream;"); response.addHeader("Content-Disposition", "attachment; filename=" + "Merchant_Settlement_Info_" + begin + "_" + end + ".csv"); try (PrintWriter writer = response.getWriter(); CSVPrinter printer = new CSVPrinter(writer, format)) { + printer.printRecord((Object[]) title); JSONObject clearDetailTotal = transactionMapper.getClearDetailTotal(params); diff --git a/src/test/java/au/com/royalpay/payment/manage/valid/CSVTest.java b/src/test/java/au/com/royalpay/payment/manage/valid/CSVTest.java new file mode 100644 index 000000000..4c1ad5366 --- /dev/null +++ b/src/test/java/au/com/royalpay/payment/manage/valid/CSVTest.java @@ -0,0 +1,29 @@ +package au.com.royalpay.payment.manage.valid; + +import org.apache.commons.csv.CSVFormat; +import org.apache.commons.csv.CSVPrinter; +import org.apache.commons.csv.QuoteMode; +import org.junit.Test; + +import java.io.File; +import java.io.FileWriter; +import java.io.IOException; +import java.io.StringWriter; +import java.nio.charset.StandardCharsets; + +/** + * Create by davep at 2019-12-24 15:15 + */ +public class CSVTest { + + @Test + public void test() throws IOException { + File file = File.createTempFile("csvtest",".csv"); + FileWriter writer = new FileWriter(file); + CSVFormat format = CSVFormat.DEFAULT.withFirstRecordAsHeader().withHeader("title", "标题1", "abc");//.withQuoteMode(QuoteMode.ALL_NON_NULL); + CSVPrinter printer = new CSVPrinter(writer, format); + printer.printRecord("test", "订单1234", "hello"); + writer.flush(); + System.out.println(file.getAbsolutePath()); + } +}