From 085e4b6b0a97c9a20a5b92a6d6f5756ba98144f3 Mon Sep 17 00:00:00 2001
From: zss <zss@example.com>
Date: 星期五, 19 七月 2024 17:41:39 +0800
Subject: [PATCH] 费用统计导出+工时展示

---
 inspect-server/src/main/java/com/yuanchu/mom/service/impl/InsOrderServiceImpl.java |  148 +++++++++++++++++++++++++++++++++++++++++++++++--
 1 files changed, 141 insertions(+), 7 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 952e942..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,11 +29,18 @@
 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;
 import java.util.*;
+import java.util.concurrent.ConcurrentHashMap;
 import java.util.concurrent.atomic.AtomicInteger;
+import java.util.function.Function;
+import java.util.function.Predicate;
 import java.util.stream.Collectors;
 
 /**
@@ -73,10 +84,23 @@
     //鑾峰彇妫�楠屼笅鍗曟暟鎹�
     @Override
     public Map<String, Object> selectInsOrderParameter(IPage<InsOrder> page, SampleOrderDto sampleOrderDto) {
+        Map<String, Object> map = new HashMap<>();
+        map.put("head", PrintChina.printChina(SampleOrderDto.class));
         //鍒ゆ柇鍏ㄩ儴,涓汉,缁勭粐鐨勬潈闄�
         Map<String, Integer> map1 = getLook.selectPowerByMethodAndUserId("selectInsOrderParameter");
-        User user = userMapper.selectById(map1.get("userId"));
+        User user = userMapper.selectById(map1.get("userId"));//褰撳墠鐧诲綍鐨勪汉
         Integer roleId = user.getRoleId();
+        //鑾峰彇褰撳墠浜烘墍灞炲疄楠屽id
+        String departLimsId = user.getDepartLimsId();
+        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;
+            }
+        }
         //鍒ゆ柇鏄惁鏄叏閮ㄦ潈闄�
         Power power = powerMapper.selectOne(Wrappers.<Power>lambdaQuery().eq(Power::getRoleId, roleId).eq(Power::getMenuMethod, "selectAllInsOrder"));
         if (ObjectUtils.isEmpty(power)) {
@@ -92,9 +116,7 @@
                 }
             }
         }
-        Map<String, Object> map = new HashMap<>();
-        map.put("head", PrintChina.printChina(SampleOrderDto.class));
-        map.put("body", insOrderMapper.selectInsOrderPage(page, QueryWrappers.queryWrappers(sampleOrderDto)));
+        map.put("body", insOrderMapper.selectInsOrderPage(page, QueryWrappers.queryWrappers(sampleOrderDto), laboratory));
         return map;
     }
 
@@ -318,6 +340,65 @@
                         }
                     }
                     insProductService.saveBatch(insProductes);
+                }
+                //鍒ゆ柇鐑惊鐜」鐩殑娣诲姞鍜屾俯鍗囪瘯楠岄」鐩殑娣诲姞
+                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<>();
+                    String[] strings = product.getAsk().split(";");
+                    //寰幆娆℃暟
+                    int count = Integer.parseInt(strings[strings.length - 1]);
+                    for (int i = 0; i < strings.length - 1; i++) {
+                        String[] split = strings[i].split(",");
+                        InsProduct insProduct = new InsProduct();
+                        insProduct.setInspectionItem("1");//妫�楠岀埗椤�--寰幆娆℃暟
+                        insProduct.setInspectionItemSubclass(split[0]);//妫�楠屽瓙椤�--鐜娓╁害/瀵肩嚎娓╁害/鑰愬紶娓╁害/鎺ョ画娓╁害
+                        insProduct.setInspectionItemSubclassEn(split[1]);//妫�楠屽瓙椤硅嫳鏂�--鐜娓╁害/瀵肩嚎娓╁害/鑰愬紶娓╁害/鎺ョ画娓╁害
+                        insProduct.setAsk(null);//妫�楠岃姹�
+                        insProduct.setTell(null);//妫�楠屾弿杩�
+                        insProduct.setInsSampleId(sampleId);
+                        insProduct.setState(1);
+                        insProduct.setFactory(product.getFactory());
+                        insProduct.setLaboratory(product.getLaboratory());
+                        insProduct.setSampleType(product.getSampleType());
+                        insProduct.setSample(product.getSample());
+                        insProduct.setModel(product.getModel());
+                        insProduct.setSonLaboratory(product.getSonLaboratory());
+                        insProduct.setUnit("鈩�");//鍗曚綅
+                        insProduct.setManHourGroup(product.getManHourGroup());
+                        insProduct.setInspectionItemType("0");
+                        insProduct.setInspectionValueType("1");
+                        insProducts.add(insProduct);
+                    }
+                    //鐑惊鐜墠鏈夊娆″惊鐜鏁�
+                    if (product.getInspectionItem().equals("鐑惊鐜�")) {
+                        //鐑惊鐜繕瑕佸姞涓婄洿娴佺數闃�
+                        for (InsProduct insProduct : insProducts) {
+                            InsProduct insProduct1 = new InsProduct();
+                            BeanUtils.copyProperties(insProduct, insProduct1);
+                            insProduct1.setInspectionItemClass("鐩存祦鐢甸樆");//妫�楠屽瓙瀛愰」--鐩存祦鐢甸樆
+                            insProduct1.setInspectionItemClassEn("DC resistance");//妫�楠屽瓙瀛愰」鑻辨枃--鐩存祦鐢甸樆
+                            insProduct1.setUnit("惟/km");//鍗曚綅
+                            insProductes.add(insProduct1);
+                            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 {
                     if (!is) {
                         switch (type) {
@@ -399,7 +480,21 @@
         map.put("head", PrintChina.printChina(CostStatisticsDto.class));
         Map<String, Integer> map1 = getLook.selectPowerByMethodAndUserId("costStatistics");
         if (map1.get("look") == 1) costStatisticsDto.setCreateUser(map1.get("userId"));
-        map.put("body", insOrderMapper.selectCostStatistics(page, QueryWrappers.queryWrappers(costStatisticsDto).ge("create_time", split[0]).le("create_time", split[1] + " 23:59:59")));
+        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 {
+                uniqueTags.add(dto.getInspectionItem().split("@")[0]);
+            }
+            dto.setInspectionItem(uniqueTags.toString());
+            return dto;
+        }).collect(Collectors.toList());
+        dtoIPage.setRecords(collect);
+        map.put("body", dtoIPage);
         return map;
     }
 
@@ -412,9 +507,9 @@
         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(dto -> dto.getPrice() != null) // 杩囨护鎺変环鏍间负 null 鐨勫璞�
-                .mapToDouble(value -> value.getPrice().doubleValue())
+                .mapToDouble(value -> value.getPrice().doubleValue() * value.getNum())
                 .sum();
-        map.put("total",totalPrice);
+        map.put("total", totalPrice);
         return map;
     }
 
@@ -453,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