diff --git a/xhpc-modules/xhpc-general/src/main/java/com/xhpc/general/controller/XhpcStatisticsExcelController.java b/xhpc-modules/xhpc-general/src/main/java/com/xhpc/general/controller/XhpcStatisticsExcelController.java deleted file mode 100644 index cc24791b..00000000 --- a/xhpc-modules/xhpc-general/src/main/java/com/xhpc/general/controller/XhpcStatisticsExcelController.java +++ /dev/null @@ -1,53 +0,0 @@ -package com.xhpc.general.controller; - -import cn.hutool.core.date.DateUtil; -import com.xhpc.general.service.IXhpcStatisticsExcelService; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.web.bind.annotation.GetMapping; -import org.springframework.web.bind.annotation.RequestMapping; -import org.springframework.web.bind.annotation.RestController; - -import javax.servlet.ServletOutputStream; -import javax.servlet.http.HttpServletResponse; -import java.io.UnsupportedEncodingException; -import java.net.URLEncoder; -import java.util.Date; - -/** - * @Author HongYun on 2021/11/15 - */ -@RestController -@RequestMapping(value = "/statistics") -public class XhpcStatisticsExcelController { - - - @Autowired - private IXhpcStatisticsExcelService iXhpcStatisticsExcelService; - - - @GetMapping(value = "/downByDay") - public Object downByDay(HttpServletResponse response) { - - String[] titles = {"时段", " 电量(度)", "充电时长(小时)", "充电次数(次)", "电费(元)", "服务费(元)" - , "订单总金额(元)", "抵扣的总金额(元)", "用户支付的金额(元)", "运营商电费(元)", "运营商服务费(元)" - , "流量方总金额抽成(元)", "流量方服务费抽成(元)", "平台总金额抽成(元)", "平台服务费抽成(元)", "运维总金额抽成(元)", "运维服务费抽成(元)"}; - String[] keys = {"time", "chargingDegree", "chargingTime", "chargingNumber", "powerPrice", "servicePrice", "totalPrice" - , "promotionDiscount", "actPrice", "actPowerPrice", "actServicePrice", "internetCommission", "internetSvcCommission" - , "platformCommission", "platformSvcCommission", "operationCommission", "operationSvcCommission"}; - response.setContentType("application/x-xls"); - try { - ServletOutputStream out = response.getOutputStream(); - try { - String name = DateUtil.format(new Date(), "yyyyMMddHHmmss"); - response.setHeader("Content-Disposition", "attachment;fileName=" + URLEncoder.encode(name + ".xls", "UTF-8")); - } catch (UnsupportedEncodingException e1) { - e1.printStackTrace(); - } - iXhpcStatisticsExcelService.exportByDay(out, titles, keys); - return "success"; - } catch (Exception e) { - e.printStackTrace(); - return "导出信息失败"; - } - } -} diff --git a/xhpc-modules/xhpc-general/src/main/java/com/xhpc/general/service/IXhpcStatisticsExcelService.java b/xhpc-modules/xhpc-general/src/main/java/com/xhpc/general/service/IXhpcStatisticsExcelService.java deleted file mode 100644 index a19a2279..00000000 --- a/xhpc-modules/xhpc-general/src/main/java/com/xhpc/general/service/IXhpcStatisticsExcelService.java +++ /dev/null @@ -1,17 +0,0 @@ -package com.xhpc.general.service; - -import javax.servlet.ServletOutputStream; -import java.util.List; -import java.util.Map; - -/** - * @Author HongYun on 2021/11/15 - */ - -public interface IXhpcStatisticsExcelService { - - - void export(ServletOutputStream out, String[] titles, String[] keys, List> data) throws Exception; - - void exportByDay(ServletOutputStream out, String[] titles, String[] keys) throws Exception; -} diff --git a/xhpc-modules/xhpc-general/src/main/java/com/xhpc/general/service/XhpcStatisticsExcelServiceImpl.java b/xhpc-modules/xhpc-general/src/main/java/com/xhpc/general/service/XhpcStatisticsExcelServiceImpl.java deleted file mode 100644 index ed9cc300..00000000 --- a/xhpc-modules/xhpc-general/src/main/java/com/xhpc/general/service/XhpcStatisticsExcelServiceImpl.java +++ /dev/null @@ -1,80 +0,0 @@ -package com.xhpc.general.service; - -import com.xhpc.general.mapper.XhpcStatisticsExcelMapper; -import org.apache.poi.hssf.usermodel.*; -import org.apache.poi.hssf.util.HSSFColor; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.stereotype.Service; - -import javax.servlet.ServletOutputStream; -import java.util.List; -import java.util.Map; - -/** - * @Author HongYun on 2021/11/15 - */ -@Service -public class XhpcStatisticsExcelServiceImpl implements IXhpcStatisticsExcelService { - - - @Autowired - private XhpcStatisticsExcelMapper xhpcStatisticsExcelMapper; - - @Override - public void exportByDay(ServletOutputStream out, String[] titles, String[] keys) throws Exception { - - List> data = xhpcStatisticsExcelMapper.selectByDay(); - export(out, titles, keys, data); - } - - public void export(ServletOutputStream out, String[] titles, String[] keys, List> data) throws Exception { - - try { - //Making the head. - HSSFWorkbook workbook = new HSSFWorkbook(); - HSSFSheet hssfSheet = workbook.createSheet("sheet1"); - HSSFRow row = hssfSheet.createRow(0); - HSSFCellStyle hssfCellStyle = workbook.createCellStyle(); - hssfCellStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER); - hssfCellStyle.setFillForegroundColor(HSSFColor.SKY_BLUE.index); - hssfCellStyle.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND); - HSSFCell hssfCell; - for (int i = 0; i < titles.length; i++) { - hssfCell = row.createCell(i); - hssfCell.setCellValue(titles[i]); - hssfCell.setCellStyle(hssfCellStyle); - hssfSheet.setColumnWidth(i, hssfCell.getStringCellValue().getBytes().length * 256 + 200); - } - - //Loading data. - HSSFRow dataRow; - HSSFCell dataCell; - int valuesLength = keys.length; - for (int i = 1; i <= data.size(); i++) { - Map map = data.get(i - 1); - if (null != map) { - dataRow = hssfSheet.createRow(i); - for (int j = 0; j < valuesLength; j++) { - if (null != map.get(keys[j])) { - dataCell = dataRow.createCell(j); - String s = String.valueOf(map.get(keys[j])); - dataCell.setCellValue(s); - } - } - } - } - // Exporting to the client. - try { - workbook.write(out); - out.flush(); - out.close(); - } catch (Exception e) { - e.printStackTrace(); - } - } catch (Exception e) { - e.printStackTrace(); - throw new Exception("导出信息失败!"); - } - } - -} diff --git a/xhpc-modules/xhpc-order/pom.xml b/xhpc-modules/xhpc-order/pom.xml index 51c3b412..bdb09969 100644 --- a/xhpc-modules/xhpc-order/pom.xml +++ b/xhpc-modules/xhpc-order/pom.xml @@ -101,6 +101,11 @@ org.springframework.boot spring-boot-starter-websocket + + org.apache.poi + poi-ooxml + 3.9 + diff --git a/xhpc-modules/xhpc-order/src/main/java/com/xhpc/order/controller/XhpcStatisticsController.java b/xhpc-modules/xhpc-order/src/main/java/com/xhpc/order/controller/XhpcStatisticsController.java index e9bfb20e..e8d0c6d5 100644 --- a/xhpc-modules/xhpc-order/src/main/java/com/xhpc/order/controller/XhpcStatisticsController.java +++ b/xhpc-modules/xhpc-order/src/main/java/com/xhpc/order/controller/XhpcStatisticsController.java @@ -1,8 +1,8 @@ package com.xhpc.order.controller; +import cn.hutool.core.date.DateUtil; import com.xhpc.common.core.web.controller.BaseController; import com.xhpc.common.core.web.domain.AjaxResult; -import com.xhpc.common.core.web.domain.BaseEntity; import com.xhpc.common.core.web.page.TableDataInfo; import com.xhpc.order.service.IXhpcStatisticsService; import io.swagger.annotations.Api; @@ -12,6 +12,11 @@ import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RestController; +import javax.servlet.ServletOutputStream; +import javax.servlet.http.HttpServletResponse; +import java.io.UnsupportedEncodingException; +import java.net.URLEncoder; +import java.util.Date; import java.util.List; import java.util.Map; @@ -64,7 +69,6 @@ public class XhpcStatisticsController extends BaseController { List> list = xhpcStatisticsService.getDateIntervalPage(chargingStationIds,internetUserId,operatorId,startTime,endTime,userId,type); return getDataTable(list); } - /** * 电站统计 * @param chargingStationIds 电站集合 @@ -197,11 +201,43 @@ public class XhpcStatisticsController extends BaseController { * @return */ @GetMapping("/getTerminalList") - public AjaxResult getTerminalList(Long chargingStationId, @RequestParam("userId") Long userId, @RequestParam("type")Integer type) - { - if (type == null || type==3) { + public AjaxResult getTerminalList(Long chargingStationId, @RequestParam("userId") Long userId, @RequestParam("type") Integer type) { + if (type == null || type == 3) { return AjaxResult.success(); } return xhpcStatisticsService.getTerminalList(chargingStationId, userId, type); } + + /** + * export excel by date. + * + * @param response + * @return + */ + @GetMapping(value = "/exportExcelByDate") + public Object exportExcelByDate(HttpServletResponse response, String chargingStationIds, Long internetUserId, Long operatorId, String startTime, String endTime, @RequestParam("userId") Long userId, @RequestParam("type") Integer type) { + + String[] titles = {"时段", " 电量(度)", "充电时长(小时)", "充电次数(次)", "电费(元)", "服务费(元)" + , "订单总金额(元)", "抵扣的总金额(元)", "用户支付的金额(元)", "运营商电费(元)", "运营商服务费(元)" + , "流量方总金额抽成(元)", "流量方服务费抽成(元)", "平台总金额抽成(元)", "平台服务费抽成(元)", "运维总金额抽成(元)", "运维服务费抽成(元)"}; + String[] keys = {"createTime", "chargingDegree", "chargingTime", "chargingNumber", "powerPrice", "servicePrice", "totalPrice" + , "promotionDiscount", "actPrice", "actPowerPrice", "actServicePrice", "internetCommission", "internetSvcCommission" + , "platformCommission", "platformSvcCommisssion", "operationCommission", "operationSvcCommission"}; + response.setContentType("application/x-xls"); + try { + ServletOutputStream out = response.getOutputStream(); + try { + String name = DateUtil.format(new Date(), "yyyyMMddHHmmss"); + response.setHeader("Content-Disposition", "attachment;fileName=" + URLEncoder.encode(name + ".xls", "UTF-8")); + } catch (UnsupportedEncodingException e1) { + e1.printStackTrace(); + } + List> data = xhpcStatisticsService.getDateIntervalPage(chargingStationIds, internetUserId, operatorId, startTime, endTime, userId, type); + xhpcStatisticsService.exportByDay(out, titles, keys, data); + return "success"; + } catch (Exception e) { + e.printStackTrace(); + return "导出信息失败"; + } + } } diff --git a/xhpc-modules/xhpc-order/src/main/java/com/xhpc/order/mapper/XhpcStatisticsServiceMapper.java b/xhpc-modules/xhpc-order/src/main/java/com/xhpc/order/mapper/XhpcStatisticsServiceMapper.java index 43a607b0..4b85c26e 100644 --- a/xhpc-modules/xhpc-order/src/main/java/com/xhpc/order/mapper/XhpcStatisticsServiceMapper.java +++ b/xhpc-modules/xhpc-order/src/main/java/com/xhpc/order/mapper/XhpcStatisticsServiceMapper.java @@ -2,6 +2,7 @@ package com.xhpc.order.mapper; import com.xhpc.order.domain.XhpcStatisticsStation; import com.xhpc.order.domain.XhpcStatisticsTimeInterval; +import org.apache.ibatis.annotations.Mapper; import org.apache.ibatis.annotations.Param; import java.util.List; @@ -12,6 +13,7 @@ import java.util.Map; * @date 2021/9/1 10:00 * @Version 1.0 */ +@Mapper public interface XhpcStatisticsServiceMapper { /** diff --git a/xhpc-modules/xhpc-order/src/main/java/com/xhpc/order/service/IXhpcStatisticsService.java b/xhpc-modules/xhpc-order/src/main/java/com/xhpc/order/service/IXhpcStatisticsService.java index db209e5d..0370821d 100644 --- a/xhpc-modules/xhpc-order/src/main/java/com/xhpc/order/service/IXhpcStatisticsService.java +++ b/xhpc-modules/xhpc-order/src/main/java/com/xhpc/order/service/IXhpcStatisticsService.java @@ -4,6 +4,7 @@ import com.xhpc.common.core.web.domain.AjaxResult; import com.xhpc.order.domain.XhpcStatisticsStation; import com.xhpc.order.domain.XhpcStatisticsTimeInterval; +import javax.servlet.ServletOutputStream; import java.util.List; import java.util.Map; @@ -140,8 +141,15 @@ public interface IXhpcStatisticsService { /** * 添加订单统计 + * * @param xhpcStatisticsStation */ void addStatisticsStation(XhpcStatisticsStation xhpcStatisticsStation); + void export(ServletOutputStream out, String[] titles, String[] keys, List> data) throws Exception; + + void exportByDay(ServletOutputStream out, String[] titles, String[] keys, List> data) throws Exception; + + void exportByDate(ServletOutputStream out, String[] titles, String[] keys, List> data) throws Exception; + } diff --git a/xhpc-modules/xhpc-order/src/main/java/com/xhpc/order/service/impl/XhpcStatisticsServiceImpl.java b/xhpc-modules/xhpc-order/src/main/java/com/xhpc/order/service/impl/XhpcStatisticsServiceImpl.java index 5e5dfece..d8f82570 100644 --- a/xhpc-modules/xhpc-order/src/main/java/com/xhpc/order/service/impl/XhpcStatisticsServiceImpl.java +++ b/xhpc-modules/xhpc-order/src/main/java/com/xhpc/order/service/impl/XhpcStatisticsServiceImpl.java @@ -7,9 +7,12 @@ import com.xhpc.order.domain.XhpcStatisticsStation; import com.xhpc.order.domain.XhpcStatisticsTimeInterval; import com.xhpc.order.mapper.XhpcStatisticsServiceMapper; import com.xhpc.order.service.IXhpcStatisticsService; +import org.apache.poi.hssf.usermodel.*; +import org.apache.poi.hssf.util.HSSFColor; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; +import javax.servlet.ServletOutputStream; import java.util.*; /** @@ -303,5 +306,66 @@ public class XhpcStatisticsServiceImpl implements IXhpcStatisticsService { // System.out.println(">>>>>>>>>"+beginOfDay.toString()); // } + @Override + public void exportByDay(ServletOutputStream out, String[] titles, String[] keys, List> data) throws Exception { + + export(out, titles, keys, data); + } + + @Override + public void exportByDate(ServletOutputStream out, String[] titles, String[] keys, List> data) throws Exception { + export(out, titles, keys, data); + } + + public void export(ServletOutputStream out, String[] titles, String[] keys, List> data) throws Exception { + + try { + //Making the head. + HSSFWorkbook workbook = new HSSFWorkbook(); + HSSFSheet hssfSheet = workbook.createSheet("sheet1"); + HSSFRow row = hssfSheet.createRow(0); + HSSFCellStyle hssfCellStyle = workbook.createCellStyle(); + hssfCellStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER); + hssfCellStyle.setFillForegroundColor(HSSFColor.SKY_BLUE.index); + hssfCellStyle.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND); + HSSFCell hssfCell; + for (int i = 0; i < titles.length; i++) { + hssfCell = row.createCell(i); + hssfCell.setCellValue(titles[i]); + hssfCell.setCellStyle(hssfCellStyle); + hssfSheet.setColumnWidth(i, hssfCell.getStringCellValue().getBytes().length * 256 + 200); + } + + //Loading data. + HSSFRow dataRow; + HSSFCell dataCell; + int valuesLength = keys.length; + for (int i = 1; i <= data.size(); i++) { + Map map = data.get(i - 1); + if (null != map) { + dataRow = hssfSheet.createRow(i); + for (int j = 0; j < valuesLength; j++) { + if (null != map.get(keys[j])) { + dataCell = dataRow.createCell(j); + String s = String.valueOf(map.get(keys[j])); + dataCell.setCellValue(s); + } + } + } + } + // Exporting to the client. + try { + workbook.write(out); + out.flush(); + out.close(); + } catch (Exception e) { + e.printStackTrace(); + } + } catch (Exception e) { + e.printStackTrace(); + throw new Exception("导出信息失败!"); + } + } + }