zhuo
2024-07-23 5ed5f51d479989d5b2149b09c82b6b3ea495b589
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;
@@ -21,14 +25,22 @@
import com.yuanchu.mom.vo.SampleDefectsFatherVo;
import lombok.AllArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
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;
/**
@@ -72,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)) {
@@ -91,15 +116,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));
@@ -124,6 +147,7 @@
            insSampleUser.setState(0);
            insSampleUser.setUserId(userId);
            insSampleUser.setInsSampleId(orderId);
            insSampleUser.setSonLaboratory(sonLaboratory);
            insSampleUserMapper.insert(insSampleUser);
        }
        return 1;
@@ -147,8 +171,35 @@
            }
            insSampleMapper.insert(a);
            if (ObjectUtil.isNotEmpty(a.getInsProduct())) {
                //普通检验项目(除了光纤接头损耗)的添加
                addInsProductMethod(a.getId(), a.getInsProduct(), null, 0);
                addInsProductMethod(a.getId(), a.getInsProduct(), null, 0, false);
                if (ObjectUtil.isNotEmpty(a.getBushing())) {
                    for (BushingDto bushingDto : a.getBushing()) {
                        bushingDto.setInsSampleId(a.getId());
                        insBushingMapper.insert(bushingDto);
                        if (bushingDto.getFiber().size() != 0) {
                            for (FiberDto fiberDto : bushingDto.getFiber()) {
                                fiberDto.setInsBushingId(bushingDto.getId());
                                insFiberMapper.insert(fiberDto);
                                addInsProductMethod(a.getId(), a.getInsProduct(), fiberDto.getId(), 2, true);
                                addInsProductMethod(a.getId(), fiberDto.getProductList(), fiberDto.getId(), 2, false);
                            }
                        } else {
                            for (FibersDto fibersDto : bushingDto.getFibers()) {
                                fibersDto.setInsBushingId(bushingDto.getId());
                                fibersDto.setCode(this.giveCode.giveCode("", "ins_fibers", "", "yyMMdd"));
                                insFibersMapper.insert(fibersDto);
                                for (FiberDto fiberDto : fibersDto.getFiber()) {
                                    fiberDto.setInsFibersId(fibersDto.getId());
                                    insFiberMapper.insert(fiberDto);
                                    addInsProductMethod(a.getId(), a.getInsProduct(), fiberDto.getId(), 2, true);
                                    addInsProductMethod(a.getId(), fiberDto.getProductList(), fiberDto.getId(), 2, false);
                                }
                                //addInsProductMethod(a.getId(), a.getInsProduct(), fibersDto.getId(), 1);
                                addInsProductMethod(a.getId(), fibersDto.getProductList(), fibersDto.getId(), 1, false);
                            }
                        }
                    }
                }
            }
            if (ObjectUtil.isNotEmpty(a.getInsulating())) {
                List<InsProduct> ip2 = new ArrayList<>();
@@ -158,13 +209,13 @@
                for (InsProduct product : ip2) {
                    product.setStandardMethodListId(a.getInsulating().getStandardMethodListId());
                }
                addInsProductMethod(a.getId(), ip2, null, 0);
                addInsProductMethod(a.getId(), ip2, null, 0, false);
            }
            if (ObjectUtil.isNotEmpty(a.getSheath())) {
                for (InsProduct product : a.getSheath().getInsProduct()) {
                    product.setStandardMethodListId(a.getSheath().getStandardMethodListId());
                }
                addInsProductMethod(a.getId(), a.getSheath().getInsProduct(), null, 0);
                addInsProductMethod(a.getId(), a.getSheath().getInsProduct(), null, 0, false);
            }
            if (ObjectUtil.isNotEmpty(a.getChildSampleList())) {
                for (SampleProductDto b : a.getChildSampleList()) {
@@ -177,12 +228,12 @@
                        }
                        insSampleMapper.insert(b);
                        if (ObjectUtil.isNotEmpty(b.getInsProduct())) {
                            addInsProductMethod(b.getId(), b.getInsProduct(), null, 0);
                            addInsProductMethod(b.getId(), b.getInsProduct(), null, 0, false);
                        }
                    }
                }
            }
            if (ObjectUtil.isNotEmpty(a.getBushing())) {
            if (ObjectUtil.isEmpty(a.getInsProduct()) && ObjectUtil.isNotEmpty(a.getBushing())) {
                for (BushingDto bushingDto : a.getBushing()) {
                    bushingDto.setInsSampleId(a.getId());
                    insBushingMapper.insert(bushingDto);
@@ -190,7 +241,7 @@
                        for (FiberDto fiberDto : bushingDto.getFiber()) {
                            fiberDto.setInsBushingId(bushingDto.getId());
                            insFiberMapper.insert(fiberDto);
                            addInsProductMethod(a.getId(), fiberDto.getProductList(), fiberDto.getId(), 2);
                            addInsProductMethod(a.getId(), fiberDto.getProductList(), fiberDto.getId(), 2, false);
                        }
                    } else {
                        for (FibersDto fibersDto : bushingDto.getFibers()) {
@@ -200,9 +251,9 @@
                            for (FiberDto fiberDto : fibersDto.getFiber()) {
                                fiberDto.setInsFibersId(fibersDto.getId());
                                insFiberMapper.insert(fiberDto);
                                addInsProductMethod(a.getId(), fiberDto.getProductList(), fiberDto.getId(), 2);
                                addInsProductMethod(a.getId(), fiberDto.getProductList(), fiberDto.getId(), 2, false);
                            }
                            addInsProductMethod(a.getId(), fibersDto.getProductList(), fibersDto.getId(), 1);
                            addInsProductMethod(a.getId(), fibersDto.getProductList(), fibersDto.getId(), 1, false);
                        }
                    }
                }
@@ -226,7 +277,7 @@
        return insOrder.getId();
    }
    private void addInsProductMethod(Integer sampleId, List<InsProduct> productList, Integer id, Integer type) {
    private void addInsProductMethod(Integer sampleId, List<InsProduct> productList, Integer id, Integer type, Boolean is) {
        InsOrder insOrder = insOrderMapper.selectById(insSampleMapper.selectById(sampleId).getInsOrderId());
        for (InsProduct product : productList) {
            if (product.getState() == 1 && !product.getInspectionItem().equals("光纤接头损耗")) {
@@ -235,7 +286,9 @@
                    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) {
@@ -247,23 +300,40 @@
                                    break;
                            }
                            String[] split = strings[i].split(",");
                            insProduct.setInspectionItem("1次");//检验项--循环次数
                            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 {
                            //最后一个数据是说明会循环多少次
                            int count = Integer.parseInt(strings[i]);
                            if (count > 1) {
                                //循环超过1次
                                for (int j = 2; j <= count; j++) {
                                    for (InsProduct insProduct : insProducts) {
                                        if (!insProduct.getInspectionItemSubclass().equals("20℃")) {
                                            insProduct.setInspectionItem(j + "次");
                                            insProductes.add(insProduct);
                                        if (!insProduct.getInspectionItemSubclass().equals("20℃") && !insProduct.getInspectionItemSubclass().equals("20℃(常温)")) {
                                            InsProduct insProduct1 = new InsProduct();
                                            BeanUtils.copyProperties(insProduct, insProduct1);
                                            insProduct1.setInspectionItem(j + "");
                                            insProductes.add(insProduct1);
                                        }
                                    }
                                }
@@ -271,25 +341,86 @@
                        }
                    }
                    insProductService.saveBatch(insProductes);
                } else {
                    switch (type) {
                        case 1:
                            product.setInsFibersId(id);
                            break;
                        case 2:
                            product.setInsFiberId(id);
                            break;
                    }
                }
                //判断热循环项目的添加和温升试验项目的添加
                else if (product.getInspectionItem().equals("热循环") || product.getInspectionItem().equals("温升试验")) {
                    List<InsProduct> insProductess = new ArrayList<>();
                    product.setId(null);
                    product.setCreateTime(null);
                    product.setUpdateTime(null);
                    product.setUpdateTime(null);
                    product.setUpdateUser(null);
                    product.setInsSampleId(sampleId);
                    if (product.getInspectionItemSubclass() == null) {
                        product.setInspectionItemSubclass("");
                    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);
                    }
                    insProductMapper.insert(product);
                    //热循环才有多次循环次数
                    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) {
                            case 1:
                                product.setInsFibersId(id);
                                break;
                            case 2:
                                product.setInsFiberId(id);
                                break;
                        }
                        product.setId(null);
                        product.setCreateTime(null);
                        product.setUpdateTime(null);
                        product.setUpdateTime(null);
                        product.setUpdateUser(null);
                        product.setInsSampleId(sampleId);
                        if (product.getInspectionItemSubclass() == null) {
                            product.setInspectionItemSubclass("");
                        }
                        insProductMapper.insert(product);
                    }
                }
            }
        }
@@ -350,7 +481,36 @@
        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;
    }
    @Override
    public Map<String, Object> costStatistics2(CostStatisticsDto costStatisticsDto) {
        Map<String, Object> map = new HashMap<>();
        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"));
        double totalPrice = costStatisticsDtos.stream()
                .filter(dto -> dto.getPrice() != null) // 过滤掉价格为 null 的对象
                .mapToDouble(value -> value.getPrice().doubleValue() * value.getNum())
                .sum();
        map.put("total", totalPrice);
        return map;
    }
@@ -389,6 +549,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("导出失败");
        }
    }
}