From 1c74351afdec0db7e3afdb563d2fafd49f95631c Mon Sep 17 00:00:00 2001 From: whyclxw <810412026@qq.com> Date: 星期四, 05 六月 2025 14:20:29 +0800 Subject: [PATCH] 单节数量统计导出 --- src/main/java/com/whyc/service/BatttestdataInfService.java | 13 ++ src/main/java/com/whyc/service/ExportService.java | 174 ++++++++++++++++++++++++++++++++++ src/main/resources/config/application-dev.yml | 1 src/main/java/com/whyc/controller/ExportFileController.java | 26 +++++ src/main/java/com/whyc/controller/StatisticController.java | 6 + pom.xml | 6 + src/main/resources/mapper/BattInfMapper.xml | 3 src/main/java/com/whyc/util/ServletUtils.java | 14 ++ src/main/java/com/whyc/dto/Statistic/SticMonRes.java | 2 9 files changed, 240 insertions(+), 5 deletions(-) diff --git a/pom.xml b/pom.xml index 16e29d8..2a1ccf4 100644 --- a/pom.xml +++ b/pom.xml @@ -142,6 +142,12 @@ <artifactId>spring-context-support</artifactId> <version>5.3.9</version> </dependency> + <!--poi--> + <dependency> + <groupId>org.apache.poi</groupId> + <artifactId>poi-ooxml</artifactId> + <version>3.10.1</version> + </dependency> <!--<dependency> <groupId>org.springframework.plugin</groupId> diff --git a/src/main/java/com/whyc/controller/ExportFileController.java b/src/main/java/com/whyc/controller/ExportFileController.java new file mode 100644 index 0000000..2038cb7 --- /dev/null +++ b/src/main/java/com/whyc/controller/ExportFileController.java @@ -0,0 +1,26 @@ +package com.whyc.controller; + +import com.whyc.dto.Statistic.MonStic; +import com.whyc.service.ExportService; +import io.swagger.annotations.Api; +import io.swagger.annotations.ApiOperation; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.web.bind.annotation.*; + +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; +import java.text.ParseException; + +@RestController +@Api(tags = "瀵煎嚭鏂囦欢") +@RequestMapping("export") +public class ExportFileController { + @Autowired + private ExportService service; + + @PostMapping("exportBattTinfStatistic") + @ApiOperation(value = "鍗曡妭鏁伴噺缁熻瀵煎嚭锛�1.2.14锛�") + public void exportBattTinfStatistic(HttpServletRequest req, HttpServletResponse resp ) throws ParseException { + service.exportBattTinfStatistic(req,resp); + } +} \ No newline at end of file diff --git a/src/main/java/com/whyc/controller/StatisticController.java b/src/main/java/com/whyc/controller/StatisticController.java index 048cbf2..7aa11c5 100644 --- a/src/main/java/com/whyc/controller/StatisticController.java +++ b/src/main/java/com/whyc/controller/StatisticController.java @@ -13,6 +13,8 @@ import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; +import java.util.Map; + @RestController @Api(tags = "缁熻绠$悊") @RequestMapping("statistic") @@ -22,6 +24,7 @@ @Autowired private BatttestdataInfService battTinfService; + @Autowired private BattInfService battService; @@ -60,7 +63,8 @@ public Response getMonStatistic(@RequestBody MonStic stic){ User uinf= ActionUtil.getUser(); stic.setUid(uinf.getId()); - return battTinfService.getMonStatistic(stic); + Map<String,Object> map= battTinfService.getMonStatistic(stic); + return new Response().setII(1,true,map,"鍗曡妭鏁伴噺缁熻"); } diff --git a/src/main/java/com/whyc/dto/Statistic/SticMonRes.java b/src/main/java/com/whyc/dto/Statistic/SticMonRes.java index fc606b7..006d76d 100644 --- a/src/main/java/com/whyc/dto/Statistic/SticMonRes.java +++ b/src/main/java/com/whyc/dto/Statistic/SticMonRes.java @@ -5,6 +5,8 @@ @Data public class SticMonRes { private Integer battgroupId; + private String stationName; + private String devName; private String battgroupName; private Integer monNum; private Float monVol; diff --git a/src/main/java/com/whyc/service/BatttestdataInfService.java b/src/main/java/com/whyc/service/BatttestdataInfService.java index ac8a26e..2f24916 100644 --- a/src/main/java/com/whyc/service/BatttestdataInfService.java +++ b/src/main/java/com/whyc/service/BatttestdataInfService.java @@ -122,7 +122,7 @@ 3鐒跺悗鎵惧埌鍒ゆ柇浼樼锛屽姡鍖栵紝鎹熷潖鐨勫弬鏁帮紝寰楀埌缁撴灉銆� 4娴厖鐢靛帇鍥撅紝闇�瑕佹樉绀哄崟浣�+瀹炴椂鏁版嵁鐨勫崟浣撶數鍘嬶紝鍗曚綋鍐呴樆 */ - public Response getMonStatistic(MonStic stic) { + public Map getMonStatistic(MonStic stic) { Map<String,Object> map=new HashMap<>(); List<SticMonRes> goodlist=new ArrayList(); List<SticMonRes> badlist=new ArrayList(); @@ -151,7 +151,7 @@ List<BattInf> binfList=battInfService.getMonStatistic(stic); Float realCap=0f; if(binfList==null){ - return new Response().set(1,false,"褰撳墠鐢ㄦ埛鏈鐞嗘弧瓒虫潯浠剁殑鐢垫睜缁�"); + return map; } for (BattInf binf:binfList) { BatttestdataInf tinf =getLastStandardTestData(binf.getBattgroupId()); @@ -174,6 +174,8 @@ SticMonRes res=new SticMonRes(); res.setBattgroupId(binf.getBattgroupId()); res.setBattgroupName(binf.getBattgroupName()); + res.setStationName(binf.getStationName()); + res.setDevName(binf.getDevName()); res.setMonNum(data.getMonNum()); //鑾峰彇鍗曚綋鐨勫疄鏃舵暟鎹� BattRtdata rtdata=rtdataService.getRtdataByNum(binf.getBattgroupId(),data.getMonNum()); @@ -201,7 +203,8 @@ map.put("goodlistNum",goodlist.size()); map.put("badlistNum",badlist.size()); map.put("damagelistNum",damagelist.size()); - return new Response().setII(1,true,map,"鍗曡妭鏁伴噺缁熻"); + return map; + } //灏嗕笉婊¤冻鏉′欢鐨勭數姹犵粍鐨勬墍鏈夊崟浣撴斁鍏amage涓� private void setDamage(List damagelist, BattInf binf) { @@ -212,6 +215,8 @@ SticMonRes res=new SticMonRes(); res.setBattgroupId(binf.getBattgroupId()); res.setBattgroupName(binf.getBattgroupName()); + res.setStationName(binf.getStationName()); + res.setDevName(binf.getDevName()); res.setMonNum(i+1); res.setMonVol(0f); res.setMonRes(0f); @@ -222,6 +227,8 @@ SticMonRes res=new SticMonRes(); res.setBattgroupId(binf.getBattgroupId()); res.setBattgroupName(binf.getBattgroupName()); + res.setStationName(binf.getStationName()); + res.setDevName(binf.getDevName()); res.setMonNum(rtdata.getMonNum()); res.setMonVol(rtdata.getMonVol()); res.setMonRes(rtdata.getMonRes()); diff --git a/src/main/java/com/whyc/service/ExportService.java b/src/main/java/com/whyc/service/ExportService.java new file mode 100644 index 0000000..1c790f9 --- /dev/null +++ b/src/main/java/com/whyc/service/ExportService.java @@ -0,0 +1,174 @@ +package com.whyc.service; + +import com.whyc.dto.Statistic.MonStic; +import com.whyc.dto.Statistic.SticMonRes; +import com.whyc.pojo.db_user.User; +import com.whyc.util.ActionUtil; +import com.whyc.util.ServletUtils; +import org.apache.poi.hssf.usermodel.*; +import org.apache.poi.ss.formula.functions.T; +import org.apache.poi.ss.usermodel.ClientAnchor; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Service; +import sun.misc.BASE64Decoder; + +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; +import java.io.IOException; +import java.io.OutputStream; +import java.io.UnsupportedEncodingException; +import java.net.URLEncoder; +import java.text.ParseException; +import java.text.SimpleDateFormat; +import java.util.ArrayList; +import java.util.Date; +import java.util.List; +import java.util.Map; + +@Service +public class ExportService { + + @Autowired + private BatttestdataInfService battTinfService; + + + //鍗曡妭鏁伴噺缁熻瀵煎嚭锛�1.2.14锛� + public void exportBattTinfStatistic(HttpServletRequest req, HttpServletResponse resp) throws ParseException { + String topPic = req.getParameter("topPic"); //澶撮儴鏌辩姸鍥� + String goodVolPic = req.getParameter("goodVolPic"); //浼樼鐢靛帇鍥� + String goodResPic = req.getParameter("goodResPic"); //浼樼鍐呴樆 + String badVolPic = req.getParameter("badVolPic"); //鍔e寲鐢靛帇鍥� + String badResPic = req.getParameter("badResPic"); //鍔e寲鍐呴樆 + String damageVolPic = req.getParameter("damageVolPic"); //鎹熷潖鐢靛帇鍥� + String damageResPic = req.getParameter("damageResPic"); //鎹熷潖鍐呴樆 + MonStic stic=ActionUtil.getGson("yyyy-MM-dd").fromJson(req.getParameter("stic"), MonStic.class); + User uinf= ActionUtil.getUser(); + stic.setUid(uinf.getId()); + Map<String,Object> map= battTinfService.getMonStatistic(stic); + List<byte[]> bytes = new ArrayList<byte[]>(); + //澶勭悊鍥剧墖 + try { + if (ServletUtils.isNotNull(topPic)) { + String[] url = topPic.split(","); + bytes.add(new BASE64Decoder().decodeBuffer(url[1])); + } + if (ServletUtils.isNotNull(goodVolPic)) { + String[] url = goodVolPic.split(","); + bytes.add(new BASE64Decoder().decodeBuffer(url[1])); + } + if (ServletUtils.isNotNull(goodResPic)) { + String[] url = goodResPic.split(","); + bytes.add(new BASE64Decoder().decodeBuffer(url[1])); + } + if (ServletUtils.isNotNull(badVolPic)) { + String[] url = badVolPic.split(","); + bytes.add(new BASE64Decoder().decodeBuffer(url[1])); + } + if (ServletUtils.isNotNull(badResPic)) { + String[] url = badResPic.split(","); + bytes.add(new BASE64Decoder().decodeBuffer(url[1])); + } + if (ServletUtils.isNotNull(damageVolPic)) { + String[] url = damageVolPic.split(","); + bytes.add(new BASE64Decoder().decodeBuffer(url[1])); + } + if (ServletUtils.isNotNull(damageResPic)) { + String[] url = damageResPic.split(","); + bytes.add(new BASE64Decoder().decodeBuffer(url[1])); + } + } catch (Exception e) { + e.printStackTrace(); + } + + //褰撳墠鏃ユ湡 + String nowFormat = new SimpleDateFormat("yyyyMMddHHmmss").format(new Date()); + String excelName = "鍗曡妭鏁伴噺缁熻-"+nowFormat; + + HSSFWorkbook wb = new HSSFWorkbook(); + //鍗曚綋鎬ц兘缁熻琛� + createMonSicPic(wb, bytes); + List<SticMonRes> goodList=(List<SticMonRes>) map.get("goodlist"); + //鍒涘缓浼樼鍗曚綋鏁版嵁琛� + createMonSicTable(wb, goodList,"浼樼鍗曚綋鏁版嵁琛�"); + List<SticMonRes> badList=(List<SticMonRes>) map.get("badlist"); + //鍒涘缓鍔e寲鍗曚綋鏁版嵁琛� + createMonSicTable(wb, badList,"鍔e寲鍗曚綋鏁版嵁琛�"); + List<SticMonRes> damageList=(List<SticMonRes>) map.get("damagelist"); + //鍒涘缓鎹熷潖鍗曚綋鏁版嵁琛� + createMonSicTable(wb, damageList,"鎹熷潖鍗曚綋鏁版嵁琛�"); + + try { + // 杞爜闃叉涔辩爜 + resp.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode ( excelName+".xlsx", "utf-8")); + OutputStream out = resp.getOutputStream(); + wb.write(out); + out.close(); + } catch (UnsupportedEncodingException e) { + e.printStackTrace(); + } catch (IOException e) { + e.printStackTrace(); + } + } + + //鍒涘缓浼樼/鍔e寲/鎹熷潖鍗曚綋鏁版嵁琛� + private void createMonSicTable(HSSFWorkbook wb, List<SticMonRes> list,String sheetName) { + HSSFSheet sheet = wb.createSheet(sheetName); + sheet.setDefaultColumnWidth(15); //璁剧疆榛樿鍒楀锛屽疄闄呬笂鍥炲鍑�2涓瓧绗︼紝涓嶇煡閬撲负浠�涔� + sheet.setDefaultRowHeight((short) (1 * 256));//璁剧疆榛樿琛岄珮锛岃〃绀�2涓瓧绗︾殑楂樺害 + int rownum = 1; + + String[] arrTh = new String[]{ + "鏈烘埧鍚嶇О","璁惧鍚嶇О","鐢垫睜缁勫悕绉�","鍗曚綋缂栧彿","娴厖鐢靛帇(V)","鍗曚綋鍐呴樆(m惟)" + }; + sheet.createRow(rownum); + for(int i = 0 ;i<arrTh.length;i++){ + sheet.getRow(rownum).createCell(i+1).setCellValue(arrTh[i]); + } + rownum++; + if(list!=null&&list.size()>=0){ + for(SticMonRes res:list){ + sheet.createRow(rownum); + sheet.getRow(rownum).createCell(1).setCellValue(res.getStationName()); + sheet.getRow(rownum).createCell(2).setCellValue(res.getDevName()); + sheet.getRow(rownum).createCell(3).setCellValue(res.getBattgroupName()); + sheet.getRow(rownum).createCell(4).setCellValue(res.getMonNum()); + sheet.getRow(rownum).createCell(5).setCellValue(res.getMonVol()); + sheet.getRow(rownum).createCell(6).setCellValue(res.getMonRes()); + rownum++; + } + } + } + + //鍗曚綋鎬ц兘缁熻琛� + private void createMonSicPic(HSSFWorkbook wb, List<byte[]> bytes) { + + HSSFSheet sheet = wb.createSheet("鍗曚綋鎬ц兘缁熻琛�"); + sheet.setDefaultColumnWidth(15); //璁剧疆榛樿鍒楀锛屽疄闄呬笂鍥炲鍑�2涓瓧绗︼紝涓嶇煡閬撲负浠�涔� + sheet.setDefaultRowHeight((short) (2 * 256)); //璁剧疆榛樿琛岄珮锛岃〃绀�2涓瓧绗︾殑楂樺害 + //瀛椾綋鏍煎紡-鍔犵矖 + HSSFCellStyle cellStyle = wb.createCellStyle(); + HSSFFont font = wb.createFont(); + font.setBold(true); + cellStyle.setFont(font); + + //鍥剧墖鍏冪礌 + HSSFPatriarch patriarch = sheet.createDrawingPatriarch(); + int rowNum = 1; + int rowNumAdd=27; + //鎻掑叆鍥剧墖 + int picNum = 0; + String[] picName = new String[]{ + "鍗曚綋鎬ц兘缁熻鍥�", "浼樼鍗曚綋鐢靛帇鍥�", "浼樼鍗曚綋鍐呴樆鍥�", "鍔e寲鍗曚綋鐢靛帇鍥�", "鍔e寲鍗曚綋鍐呴樆鍥�", "鎹熷潖鍗曚綋鐢靛帇鍥�", "鎹熷潖鍗曚綋鍐呴樆鍥�" + }; + for (int i = 0; i < picName.length; i++) { + sheet.createRow(rowNum - 1); + sheet.getRow(rowNum - 1).createCell(0).setCellValue(picName[i]); + HSSFClientAnchor anchor = new HSSFClientAnchor(0, 0, 512, 255, (short) 0, rowNum, (short) 10, rowNum + rowNumAdd); + anchor.setAnchorType(ClientAnchor.AnchorType.DONT_MOVE_AND_RESIZE); + patriarch.createPicture(anchor, wb.addPicture(bytes.get(picNum), HSSFWorkbook.PICTURE_TYPE_PNG)).resize(1); + picNum++; + rowNum += rowNumAdd; + } + rowNum++; + } +} \ No newline at end of file diff --git a/src/main/java/com/whyc/util/ServletUtils.java b/src/main/java/com/whyc/util/ServletUtils.java new file mode 100644 index 0000000..e27c2d7 --- /dev/null +++ b/src/main/java/com/whyc/util/ServletUtils.java @@ -0,0 +1,14 @@ +package com.whyc.util; + +public class ServletUtils { + public static boolean isNotNull(Object obj){ + boolean flag=false; + if(obj!=null){ + if(obj.toString().trim().length()>0){ + flag=true; + } + } + return flag; + } + +} diff --git a/src/main/resources/config/application-dev.yml b/src/main/resources/config/application-dev.yml index 8a15011..0095d60 100644 --- a/src/main/resources/config/application-dev.yml +++ b/src/main/resources/config/application-dev.yml @@ -17,6 +17,7 @@ max-age: -1 tomcat: max-http-form-post-size: 102400000 + max-http-header-size: 655360000 http: port: -1 #鏁版嵁搴� diff --git a/src/main/resources/mapper/BattInfMapper.xml b/src/main/resources/mapper/BattInfMapper.xml index 0ec27f3..5dcffe5 100644 --- a/src/main/resources/mapper/BattInfMapper.xml +++ b/src/main/resources/mapper/BattInfMapper.xml @@ -183,8 +183,9 @@ </where> </select> <select id="getMonStatistic" resultType="com.whyc.pojo.db_station.BattInf"> - select distinct tb_batt_inf.* from db_station.tb_batt_inf + select distinct tb_batt_inf.*,tb_station_inf.station_name from db_station.tb_batt_inf,db_station.tb_station_inf <where> + tb_batt_inf.station_id=tb_station_inf.station_id <if test="stic.product!=null"> and tb_batt_inf.product=#{stic.product} </if> -- Gitblit v1.9.1