From d9de769c21b1600d94f52eaceb4382aaa3523575 Mon Sep 17 00:00:00 2001
From: Crunchy <3114200645@qq.com>
Date: 星期五, 19 七月 2024 17:55:09 +0800
Subject: [PATCH] Merge remote-tracking branch 'origin/master'

---
 inspect-server/src/main/java/com/yuanchu/mom/service/impl/InsOrderServiceImpl.java |  133 +++++++++++++++++++++++++++-----------------
 1 files changed, 81 insertions(+), 52 deletions(-)

diff --git a/inspect-server/src/main/java/com/yuanchu/mom/service/impl/InsOrderServiceImpl.java b/inspect-server/src/main/java/com/yuanchu/mom/service/impl/InsOrderServiceImpl.java
index 103b180..c9689b7 100644
--- a/inspect-server/src/main/java/com/yuanchu/mom/service/impl/InsOrderServiceImpl.java
+++ b/inspect-server/src/main/java/com/yuanchu/mom/service/impl/InsOrderServiceImpl.java
@@ -2,6 +2,10 @@
 
 import cn.hutool.core.util.ObjectUtil;
 import cn.hutool.core.util.StrUtil;
+import com.alibaba.excel.EasyExcel;
+import com.alibaba.excel.ExcelWriter;
+import com.alibaba.excel.write.metadata.WriteSheet;
+import com.alibaba.excel.write.style.column.LongestMatchColumnWidthStyleStrategy;
 import com.alibaba.fastjson.JSON;
 import com.baomidou.mybatisplus.core.metadata.IPage;
 import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
@@ -25,6 +29,10 @@
 import org.springframework.stereotype.Service;
 import org.springframework.transaction.annotation.Transactional;
 
+import javax.servlet.ServletException;
+import javax.servlet.http.HttpServletResponse;
+import java.io.IOException;
+import java.net.URLEncoder;
 import java.time.LocalDate;
 import java.time.LocalDateTime;
 import java.time.format.DateTimeFormatter;
@@ -84,13 +92,13 @@
         Integer roleId = user.getRoleId();
         //鑾峰彇褰撳墠浜烘墍灞炲疄楠屽id
         String departLimsId = user.getDepartLimsId();
-        String laboratory=null;
-        if (ObjectUtils.isNotEmpty(departLimsId) && !departLimsId.equals("")){
+        String laboratory = null;
+        if (ObjectUtils.isNotEmpty(departLimsId) && !departLimsId.equals("")) {
             String[] split = departLimsId.split(",");
-             //鏌ヨ瀵瑰簲鏋舵瀯鍚嶇О(閫氫俊瀹為獙瀹�,鐢靛姏瀹為獙瀹�,妫�娴嬪姙)
-            String departLims=baseMapper.seldepLimsId(Integer.parseInt(split[split.length - 1]));
-            if (departLims.contains("瀹為獙瀹�")){
-                 laboratory=departLims;
+            //鏌ヨ瀵瑰簲鏋舵瀯鍚嶇О(閫氫俊瀹為獙瀹�,鐢靛姏瀹為獙瀹�,妫�娴嬪姙)
+            String departLims = baseMapper.seldepLimsId(Integer.parseInt(split[split.length - 1]));
+            if (departLims.contains("瀹為獙瀹�")) {
+                laboratory = departLims;
             }
         }
         //鍒ゆ柇鏄惁鏄叏閮ㄦ潈闄�
@@ -108,7 +116,7 @@
                 }
             }
         }
-        map.put("body", insOrderMapper.selectInsOrderPage(page, QueryWrappers.queryWrappers(sampleOrderDto),laboratory));
+        map.put("body", insOrderMapper.selectInsOrderPage(page, QueryWrappers.queryWrappers(sampleOrderDto), laboratory));
         return map;
     }
 
@@ -336,6 +344,8 @@
                 //鍒ゆ柇鐑惊鐜」鐩殑娣诲姞鍜屾俯鍗囪瘯楠岄」鐩殑娣诲姞
                 else if (product.getInspectionItem().equals("鐑惊鐜�") || product.getInspectionItem().equals("娓╁崌璇曢獙")) {
                     List<InsProduct> insProductess = new ArrayList<>();
+                    product.setId(null);
+                    product.setInsSampleId(sampleId);
                     insProductess.add(product);
                     List<InsProduct> insProductes = new ArrayList<>();
                     List<InsProduct> insProducts = new ArrayList<>();
@@ -363,8 +373,6 @@
                         insProduct.setInspectionItemType("0");
                         insProduct.setInspectionValueType("1");
                         insProducts.add(insProduct);
-                        insProductes.add(insProduct);
-                        insProductess.add(insProduct);
                     }
                     //鐑惊鐜墠鏈夊娆″惊鐜鏁�
                     if (product.getInspectionItem().equals("鐑惊鐜�")) {
@@ -379,20 +387,19 @@
                             insProductess.add(insProduct1);
                         }
                     }
-                        if (count > 1) {
-                            //寰幆瓒呰繃1娆�
-                            for (int j = 2; j <= count; j++) {
-                                for (InsProduct insProduct : insProductes) {
-                                    InsProduct insProduct1 = new InsProduct();
-                                    BeanUtils.copyProperties(insProduct, insProduct1);
-                                    insProduct1.setInspectionItem(j + "");//寰幆娆℃暟
-                                    insProductess.add(insProduct1);
-                                }
+                    if (count > 1) {
+                        //寰幆瓒呰繃1娆�
+                        for (int j = 2; j <= count; j++) {
+                            for (InsProduct insProduct : insProductes) {
+                                InsProduct insProduct1 = new InsProduct();
+                                BeanUtils.copyProperties(insProduct, insProduct1);
+                                insProduct1.setInspectionItem(j + "");//寰幆娆℃暟
+                                insProductess.add(insProduct1);
                             }
                         }
+                    }
                     insProductService.saveBatch(insProductess);
-                }
-                else {
+                } else {
                     if (!is) {
                         switch (type) {
                             case 1:
@@ -473,34 +480,22 @@
         map.put("head", PrintChina.printChina(CostStatisticsDto.class));
         Map<String, Integer> map1 = getLook.selectPowerByMethodAndUserId("costStatistics");
         if (map1.get("look") == 1) costStatisticsDto.setCreateUser(map1.get("userId"));
-        IPage<CostStatisticsDto> costStatisticsDtoIPage = insOrderMapper.selectCostStatistics(page, QueryWrappers.queryWrappers(costStatisticsDto).ge("create_time", split[0]).le("create_time", split[1] + " 23:59:59"));
-        List<CostStatisticsDto> costStatisticsDtoList = costStatisticsDtoIPage.getRecords();
-        for (CostStatisticsDto statisticsDto : costStatisticsDtoList) {
-            statisticsDto.setNum(1);
-        }
-        List<CostStatisticsDto> costStatisticsDtoList1 = deduplicateAndSumCounts(costStatisticsDtoList);
-        costStatisticsDtoIPage.setRecords(costStatisticsDtoList1);
-        map.put("body", costStatisticsDtoIPage);
-        return map;
-    }
-    private static List<CostStatisticsDto> deduplicateAndSumCounts(List<CostStatisticsDto> list) {
-        // 浣跨敤涓�涓� Map 鏉ュ瓨鏀惧凡缁忓嚭鐜拌繃鐨� inspectionItem + entrustCode + sample 鐨勭粍鍚�
-        Map<String, CostStatisticsDto> map = new HashMap<>();
-        // 閬嶅巻鍒楄〃杩涜鍘婚噸鍜岃鏁�
-        for (CostStatisticsDto dto : list) {
-            String key = dto.getInspectionItem() + dto.getEntrustCode() + dto.getSample();
-            if (map.containsKey(key)) {
-                // 濡傛灉宸茬粡瀛樺湪锛屽垯绱姞鏁伴噺
-                CostStatisticsDto existingDto = map.get(key);
-                existingDto.setNum(existingDto.getNum() + dto.getNum());
-                existingDto.setPrice(existingDto.getPrice().add(dto.getPrice()));
+        IPage<CostStatisticsDto> dtoIPage = insOrderMapper.selectCostStatistics(page, QueryWrappers.queryWrappers(costStatisticsDto).ge("create_time", split[0]).le("create_time", split[1] + " 23:59:59"));
+        List<CostStatisticsDto> collect = dtoIPage.getRecords().stream().map(dto -> {
+            Set<String> uniqueTags = new HashSet<>();
+            if (dto.getInspectionItem().contains(",")) {
+                for (String s : dto.getInspectionItem().split(",")) {
+                    uniqueTags.add(s.split("@")[0]);
+                }
             } else {
-                // 濡傛灉涓嶅瓨鍦紝鍒欐斁鍏� Map 涓�
-                map.put(key, dto);
+                uniqueTags.add(dto.getInspectionItem().split("@")[0]);
             }
-        }
-        // 灏� Map 涓殑鍊艰浆涓� List 杩斿洖
-        return new ArrayList<>(map.values());
+            dto.setInspectionItem(uniqueTags.toString());
+            return dto;
+        }).collect(Collectors.toList());
+        dtoIPage.setRecords(collect);
+        map.put("body", dtoIPage);
+        return map;
     }
 
     @Override
@@ -511,16 +506,11 @@
         costStatisticsDto.setDates(null);
         List<CostStatisticsDto> costStatisticsDtos = insOrderMapper.selectCostStatistics2(QueryWrappers.queryWrappers(costStatisticsDto).ge("create_time", split[0]).le("create_time", split[1] + " 23:59:59"));
         double totalPrice = costStatisticsDtos.stream()
-                .filter(distinctByKey(dto -> Arrays.asList(dto.getEntrustCode(), dto.getSample(), dto.getCost())))
                 .filter(dto -> dto.getPrice() != null) // 杩囨护鎺変环鏍间负 null 鐨勫璞�
-                .mapToDouble(value -> value.getPrice().doubleValue())
+                .mapToDouble(value -> value.getPrice().doubleValue() * value.getNum())
                 .sum();
         map.put("total", totalPrice);
         return map;
-    }
-    public static <T> Predicate<T> distinctByKey(Function<? super T, ?> keyExtractor) {
-        Map<Object, Boolean> seen = new ConcurrentHashMap<>();
-        return t -> seen.putIfAbsent(keyExtractor.apply(t), Boolean.TRUE) == null;
     }
 
     @Override
@@ -558,6 +548,45 @@
         }).collect(Collectors.toList());
         return sampleProductDto3s;
     }
+
+    @Override
+    public void export(CostStatisticsDto costStatisticsDto,HttpServletResponse response) throws IOException {
+        //鏌ヨ瀵煎嚭鐨勮垂鐢ㄧ粺璁℃暟鎹�
+        String dates = costStatisticsDto.getDates();
+        String[] split = dates.replaceAll("\\[", "").replaceAll("]", "").replaceAll("\"", "").split(",");
+        costStatisticsDto.setDates(null);
+        List<CostStatisticsDto> costStatisticsDtos = insOrderMapper.selectCostStatistics2(QueryWrappers.queryWrappers(costStatisticsDto).ge("create_time", split[0]).le("create_time", split[1] + " 23:59:59"));
+        costStatisticsDtos = costStatisticsDtos.stream().map(dto -> {
+            Set<String> uniqueTags = new HashSet<>();
+            if (dto.getInspectionItem().contains(",")) {
+                for (String s : dto.getInspectionItem().split(",")) {
+                    uniqueTags.add(s.split("@")[0]);
+                }
+            } else {
+                uniqueTags.add(dto.getInspectionItem().split("@")[0]);
+            }
+            dto.setInspectionItem(uniqueTags.toString());
+            return dto;
+        }).collect(Collectors.toList());
+
+        response.setContentType("application/vnd.ms-excel");
+        response.setCharacterEncoding("UTF-8");
+        // 杩欓噷URLEncoder.encode鍙互闃叉涓枃涔辩爜 褰撶劧鍜宔asyexcel娌℃湁鍏崇郴
+        String fileName = URLEncoder.encode("鏍峰搧璐圭敤缁熻瀵煎嚭", "UTF-8");
+        response.setHeader("Content-disposition", "attachment;filename=" + fileName + ".xlsx");
+        try {
+            // 鏂板缓ExcelWriter
+            ExcelWriter excelWriter = EasyExcel.write(response.getOutputStream()).registerWriteHandler(new LongestMatchColumnWidthStyleStrategy()).build();
+
+            WriteSheet mainSheet = EasyExcel.writerSheet( "鏍峰搧璐圭敤缁熻瀵煎嚭").head(CostStatisticsDto.class).build();
+            excelWriter.write(costStatisticsDtos, mainSheet);
+
+            // 鍏抽棴娴�
+            excelWriter.finish();
+        } catch (IOException e) {
+            throw new RuntimeException("瀵煎嚭澶辫触");
+        }
+    }
 }
 
 

--
Gitblit v1.9.3