zss
2024-07-31 0e2e829f10a5e5b239d6583d6be3afbcaed0dcc0
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,20 @@
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.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
/**
@@ -73,10 +86,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,15 +118,13 @@
                }
            }
        }
        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;
    }
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int upInsOrder(Integer orderId, Integer sampleId, String appointed, Integer userId) {
    public int upInsOrder(Integer orderId, Integer sampleId, String appointed, Integer userId, String sonLaboratory) {
        InsOrder insOrder = new InsOrder();
        insOrder.setId(orderId);
        insOrder.setAppointed(LocalDate.parse(appointed));
@@ -125,6 +149,7 @@
            insSampleUser.setState(0);
            insSampleUser.setUserId(userId);
            insSampleUser.setInsSampleId(orderId);
            insSampleUser.setSonLaboratory(sonLaboratory);
            insSampleUserMapper.insert(insSampleUser);
        }
        return 1;
@@ -136,7 +161,7 @@
        insOrder.setState(0);
        String code = customMapper.selectById(insOrder.getCompanyId()).getCode2();
        //insOrder.setEntrustCode(giveCode.giveCode("JCZX-" + code + "-", "ins_order", "", "yyMMdd"));
        String giveCode = this.giveCode.giveCode("JCZX-" + code + "-", "ins_order", "", "yyMMdd");
        String giveCode = this.giveCode.giveCode("JCZX-" + code + "-", "ins_sample", "", "yyMMdd");
        insOrderMapper.insert(insOrder);
        AtomicInteger count = new AtomicInteger();
        list.forEach(a -> {
@@ -260,68 +285,68 @@
            if (product.getState() == 1 && !product.getInspectionItem().equals("光纤接头损耗")) {
                //判断光缆的温度循环项目添加
                if (insOrder.getSampleType().equals("光缆") && product.getInspectionItem().equals("温度循环") && type != 0) {
                    List<InsProduct> insProductes = new ArrayList<>();
                    List<InsProduct> insProducts = new ArrayList<>();
                    String[] strings = product.getAsk().split(";");
                    for (int i = 0; i < strings.length; i++) {
                        int count = Integer.parseInt(strings[strings.length - 1]);
                        if (i != strings.length - 1) {
                            InsProduct insProduct = new InsProduct();
                            switch (type) {
                                case 1:
                                    insProduct.setInsFibersId(id);
                                    break;
                                case 2:
                                    insProduct.setInsFiberId(id);
                                    break;
                            }
                            String[] split = strings[i].split(",");
                            if (split[0].equals("20℃")) {
                                insProduct.setInspectionItem(count + "");//检验项--循环次数
                            } else {
                                insProduct.setInspectionItem("1");//检验项--循环次数
                            }
                            insProduct.setInspectionItemSubclass(split[0]);//检验项--温度
                            insProduct.setInspectionItemClass(split[1]);//检验项--光纤项目
                            insProduct.setAsk(split[2]);//检验要求
                            insProduct.setTell(strings[i]);//检验描述
                            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(product.getUnit());
                            insProduct.setManHourGroup(product.getManHourGroup());
                            insProduct.setInspectionItemType("0");
                            insProduct.setInspectionValueType("1");
                            insProducts.add(insProduct);
                            insProductes.add(insProduct);
                    //判断选择的标准方法是委托要求还是其他标准方法
                    String name = insSampleMapper.selMethodById(sampleId);
                    if (!name.equals("委托要求")) {
                        //判断标准方法的温度循环的要求描述是否有填写
                        if (ObjectUtils.isEmpty(product.getTell()) || product.getTell().equals("")) {
                            throw new ErrorException("光缆的温度循环的要求描述为空,需要在标准库配置要求描述!!!");
                        } else {
                            //最后一个数据是说明会循环多少次
                            if (count > 1) {
                                //循环超过1次
                                for (int j = 2; j <= count; j++) {
                                    for (InsProduct insProduct : insProducts) {
                                        if (!insProduct.getInspectionItemSubclass().equals("20℃") && !insProduct.getInspectionItemSubclass().equals("20℃(常温)")) {
                                            InsProduct insProduct1 = new InsProduct();
                                            BeanUtils.copyProperties(insProduct, insProduct1);
                                            insProduct1.setInspectionItem(j + "");
                                            insProductes.add(insProduct1);
                                        }
                                    }
                            //解析(温度范围:20℃,-40℃,65℃;保温时间:12h; 循环次数:2次; 光纤(1310nm,1550nm)附加衰减不大于0.03dB/km)
                            String tell = product.getTell().replace(")", ")")
                                    .replace("(", "(")
                                    .replace(")", ")")
                                    .replace(",", ",")
                                    .replace(":", ":")
                                    .replace(";", ";")
                                    .replace("不大于", "≤")
                                    .replace("不小于", "≥")
                                    .replace("大于", ">")
                                    .replace("小于", "<")
                                    .replace("等于", "=");
                            String[] message=null;
                            String[] nm=null;
                            String ask=null;
                            String count=null;
                            try {
                                String[] strings = tell.split(";");
                                //温度
                                String temperature = strings[0];
                                String[] split = temperature.split(":");
                                 message = split[1].split(",");
                                //循环次数
                                 count = strings[2].split(":")[1].split("次")[0];
                                //光纤项目和要求值
                                String string = strings[3];
                                 nm = string.split("(")[1].split(")")[0].split(",");
                                 ask = string.split("衰减")[1].split("dB")[0];
                            }catch (Exception e){
                                throw new ErrorException("温度循环的要求描述格式异常,请参照温度范围:20℃,-40℃,65℃;保温时间:12h; 循环次数:2次; 光纤(1310nm,1550nm)附加衰减不大于0.03dB/km");
                            }
                            //拼接
                            String s=null;
                            for (int i = 0; i < nm.length; i++) {
                                 s+= "20℃(常温)," + nm[i] + ",null;";
                                for (int j = 0; j < message.length; j++) {
                                    s+= message[j] + "," + nm[i] + "," + ask + ";" ;
                                }
                            }
                            s+=count;
                            product.setAsk(s);
                            insProductMapper.updateById(product);
                            dealWithTemperatureLoop(type,id,sampleId,product);
                        }
                    }
                    insProductService.saveBatch(insProductes);
                    else {
                        //20℃(常温),1310nm,null;-40℃,1310nm,≤0.2;75℃,1310nm,<0.3;20℃,1310nm,≤0.1;20℃(常温),1550nm,null;-40℃,1550nm,≤0.2;75℃,1550nm,<0.3;20℃,1550nm,≤0.1;3
                        dealWithTemperatureLoop(type,id,sampleId,product);
                    }
                }
                //判断热循环项目的添加和温升试验项目的添加
                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<>();
@@ -348,37 +373,29 @@
                        insProduct.setManHourGroup(product.getManHourGroup());
                        insProduct.setInspectionItemType("0");
                        insProduct.setInspectionValueType("1");
                        if (product.getInspectionItem().equals("热循环")) {
                            insProduct.setInspectionItemClass("直流电阻");//检验子子项--直流电阻
                            insProduct.setInspectionItemClassEn("DC resistance");//检验子子项英文--直流电阻
                            insProduct.setUnit("Ω/km");//单位
                        }
                        insProducts.add(insProduct);
                        insProductes.add(insProduct);
                        insProductess.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);
                                }
                    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:
@@ -402,6 +419,67 @@
                }
            }
        }
    }
    //温度循环的处理
    private void dealWithTemperatureLoop(Integer type, Integer id,Integer sampleId,InsProduct product) {
        List<InsProduct> insProductes = new ArrayList<>();
        List<InsProduct> insProducts = new ArrayList<>();
        String[] strings = product.getAsk().split(";");
        for (int i = 0; i < strings.length; i++) {
            int count = Integer.parseInt(strings[strings.length - 1]);
            if (i != strings.length - 1) {
                InsProduct insProduct = new InsProduct();
                switch (type) {
                    case 1:
                        insProduct.setInsFibersId(id);
                        break;
                    case 2:
                        insProduct.setInsFiberId(id);
                        break;
                }
                String[] split = strings[i].split(",");
                if (split[0].equals("20℃")) {
                    insProduct.setInspectionItem(count + "");//检验项--循环次数
                } else {
                    insProduct.setInspectionItem("1");//检验项--循环次数
                }
                insProduct.setInspectionItemSubclass(split[0]);//检验项--温度
                insProduct.setInspectionItemClass(split[1]);//检验项--光纤项目
                insProduct.setAsk(split[2]);//检验要求
                insProduct.setTell(strings[i]);//检验描述
                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(product.getUnit());
                insProduct.setManHourGroup(product.getManHourGroup());
                insProduct.setInspectionItemType("0");
                insProduct.setInspectionValueType("1");
                insProducts.add(insProduct);
                insProductes.add(insProduct);
            } else {
                //最后一个数据是说明会循环多少次
                if (count > 1) {
                    //循环超过1次
                    for (int j = 2; j <= count; j++) {
                        for (InsProduct insProduct : insProducts) {
                            if (!insProduct.getInspectionItemSubclass().equals("20℃") && !insProduct.getInspectionItemSubclass().equals("20℃(常温)")) {
                                InsProduct insProduct1 = new InsProduct();
                                BeanUtils.copyProperties(insProduct, insProduct1);
                                insProduct1.setInspectionItem(j + "");
                                insProductes.add(insProduct1);
                            }
                        }
                    }
                }
            }
        }
        insProductService.saveBatch(insProductes);
    }
    @Override
@@ -459,7 +537,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;
    }
@@ -472,7 +564,7 @@
        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);
        return map;
@@ -513,6 +605,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可以防止中文乱码 当然和easyexcel没有关系
        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("导出失败");
        }
    }
}