zss
2025-03-28 aa056390e38523e73c3e91ee15669f0d1838a1eb
inspect-server/src/main/java/com/yuanchu/mom/service/impl/InsOrderPlanServiceImpl.java
@@ -24,6 +24,7 @@
import com.yuanchu.mom.service.*;
import com.yuanchu.mom.utils.*;
import com.yuanchu.mom.vo.*;
import org.apache.poi.hwpf.usermodel.*;
import org.apache.poi.ss.usermodel.CellValue;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFRow;
@@ -35,6 +36,7 @@
import org.springframework.web.multipart.MultipartFile;
import javax.annotation.Resource;
import javax.imageio.ImageIO;
import javax.servlet.http.HttpServletRequest;
import java.io.*;
import java.nio.file.Files;
@@ -48,6 +50,8 @@
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
import org.apache.poi.hwpf.HWPFDocument;
/**
 * 检验任务-业务实现层
@@ -357,6 +361,14 @@
            } else if (ObjectUtils.isNotEmpty(sonLaboratory) && sonLaboratory.equals("电路试验") && split[split.length - 1].equals("csv") && insOrder.getSampleType().equals("无源器件")) {
                dianLuUtils.readDianLuFile3(sampleId, insOrderFile);
            }
            // 电路实验返回文件名
            if(ObjectUtils.isNotEmpty(sonLaboratory) && sonLaboratory.equals("电路试验")){
                return pathName;
            }
            // 震动doc文件解析
            if(filename.toLowerCase().contains(".doc") && sonLaboratory.equals("振动")){
                return docAnalysis(urlString,orderId,sampleId,file.getOriginalFilename());
            }
        } catch (Exception e) {
            throw new ErrorException(e.getMessage());
        }
@@ -413,6 +425,7 @@
            if (orderState.getInsState() == 6) {
                orderState.setInsState(0);
                orderState.setNum(orderState.getNum() + 1);//递增
                orderState.setCreateTime(LocalDateTime.now());
                insOrderStateMapper.updateById(orderState);
            }
        } else {
@@ -424,10 +437,17 @@
            insOrderState.setNum(1);//初始是第一次
            insOrderStateMapper.insert(insOrderState);
        }
        //同一样品在其他站点入库之后需要将其他站点的任务不显示
        insOrderStateMapper.update(null,Wrappers.<InsOrderState>lambdaUpdate()
        .eq(InsOrderState::getInsOrderId,insSample.getInsOrderId())
        .eq(InsOrderState::getInsSampleId,insSample.getId())
        .eq(InsOrderState::getInsState,6)//复核继续试验
        .ne(InsOrderState::getLaboratory,sonLaboratory)
        .set(InsOrderState::getIsOut,1));//不显示
        return 1;
    }
    //扫码出库
    //扫码出库(只有复核结束才会调用)
    @Override
    public int outInsOrderState(String sonLaboratory, String sampleCode, String entrustCode) {
        //判断该样品的检验任务是否已经结束(继续试验或者是结束试验)
@@ -442,22 +462,16 @@
        if (!collect.contains(sonLaboratory)) {
            throw new ErrorException("该样品没有" + sonLaboratory + "站点的相关项目,请查看下单详情!");
        }
        //判断该任务是否是结束试验
        //判断该订单是否站点任务有(继续试验或者是结束试验)
        List<InsOrderState> orderStates = insOrderStateMapper.selectList(Wrappers.<InsOrderState>lambdaQuery()
                .eq(InsOrderState::getInsOrderId, insSample.getInsOrderId())
                .eq(InsOrderState::getInsState, 5));//结束试验
        if (CollectionUtils.isEmpty(orderStates)) {
            List<InsOrderState> orderStates1 = insOrderStateMapper.selectList(Wrappers.<InsOrderState>lambdaQuery()
                    .eq(InsOrderState::getInsOrderId, insSample.getInsOrderId())
                    .eq(InsOrderState::getInsSampleId, insSample.getId())
                    .eq(InsOrderState::getLaboratory, sonLaboratory)
                    .eq(InsOrderState::getInsState, 6));//继续试验
            if (CollectionUtils.isEmpty(orderStates1)) {
                throw new ErrorException("该样品的检验任务还没有结束试验或继续试验,暂时无法出库!");
            }
        InsOrderState orderStates = insOrderStateMapper.selectOne(Wrappers.<InsOrderState>lambdaQuery()
                .eq(InsOrderState::getLaboratory,sonLaboratory)
                .eq(InsOrderState::getInsSampleId, insSample.getId())
                .eq(InsOrderState::getInsOrderId, insSample.getInsOrderId()));
        if (orderStates.getInsState()!=5) {
                throw new ErrorException("当前样品的站点任务并不是复核的结束试验,无需手动出库!");
        }
        boolean a = true;
        warehouseService.outWarehouse(sampleCode, a, entrustCode);
        warehouseService.outWarehouse(sampleCode, true, entrustCode);
        return 0;
    }
@@ -651,6 +665,16 @@
                    }
                } catch (Exception e) {
                    result.setAfterCheck("");//''
                }
                //实验结果
                try {
                    JSONObject resValue = JSON.parseObject(JSON.toJSONString(jo.get("testResult")));
                    if (resValue.get("v") != null) {
                        Object o = JSON.parseObject(JSON.toJSONString(resValue.get("v"))).get("v");
                        result.setTestResult(o.equals("") ? null : (o.toString()));
                    }
                } catch (Exception e) {
                    result.setTestResult("");//''
                }
                //设备编号
                if (jo.get("equipValue") != null) {
@@ -894,6 +918,122 @@
        return 1;
    }
    /**
     * 震动doc文件解析
     * @param urlString
     * @param orderId
     * @param sampleId
     * @param originFileName
     * @return
     */
    private Map<String, String> docAnalysis(String urlString,Integer orderId,Integer sampleId,String originFileName) {
        Map<String, String> analysisMap = new HashMap<>();
        try {
            File file = new File(urlString);
            HWPFDocument document = new HWPFDocument(new FileInputStream(file));
            Range range = document.getRange();
            TableIterator tableIterator = new TableIterator(range);
            List<Table> tableList = new ArrayList<>();
            while (tableIterator.hasNext()) {
                tableList.add(tableIterator.next());
            }
            for (int tableIndex = 0; tableIndex < tableList.size(); tableIndex++) {
                // 取第一个表单限制参数的  振动台型号、运动部件等效质量、试件质量数值
                if(tableIndex == 0){
                    Table table = tableList.get(0);
                    for (int i = 0; i < table.numRows(); i++) {
                        if(table.getRow(i).numCells() < 2){
                            continue;
                        }
                        String columnText = ObjectUtils.isEmpty(table.getRow(i).getCell(0))? "" : table.getRow(i).getCell(0).text();
                        String columnValue = ObjectUtils.isEmpty(table.getRow(i).getCell(1))? "" : table.getRow(i).getCell(1).text();
                        if(columnValue.contains("\u0007")){
                            columnValue = columnValue.replace("\u0007","");
                        }
                        if(columnText.contains("\u0007")){
                            columnText = columnText.replace("\u0007","");
                        }
                        if(columnText.equals("振动台型号")){
                            analysisMap.put("11",columnValue);
                        }
                        if(columnText.equals("运动部件等效质量")){
                            analysisMap.put("12",columnValue);
                        }
                        if(columnText.equals("试件质量")){
                            analysisMap.put("1",columnValue);
                        }
                    }
                }
                //取第一个表单输入通道参数第一条数据
                if(tableIndex == 1){
                    Table table = tableList.get(1);
                    if(ObjectUtils.isNotEmpty(table) && table.numRows() > 1 && table.getRow(1).numCells() >= 3){
                        String columnValue = ObjectUtils.isEmpty(table.getRow(1).getCell(2))? "" : table.getRow(1).getCell(2).text();
                        if(columnValue.contains("\u0007")){
                            columnValue = columnValue.replace("\u0007","");
                        }
                        analysisMap.put("9",columnValue);
                    }
                }
                // 取最后一个表单第三第四行信息
                if(tableIndex == tableList.size() - 1){
                    Table table = tableList.get(tableIndex);
                    for (int i = 0; i < table.numRows(); i++) {
                        if(table.getRow(i).numCells() < 2){
                            continue;
                        }
                        String columnText = ObjectUtils.isEmpty(table.getRow(i).getCell(0))? "" : table.getRow(i).getCell(0).text();
                        String columnValue = ObjectUtils.isEmpty(table.getRow(i).getCell(1))? "" : table.getRow(i).getCell(1).text();
                        if(columnValue.contains("\u0007")){
                            columnValue = columnValue.replace("\u0007","");
                        }
                        if(columnText.contains("\u0007")){
                            columnText = columnText.replace("\u0007","");
                        }
                        if(columnText.contains("总运行时间")){
                            analysisMap.put("13",columnValue);
                        }
                        if(columnText.contains("试验扫频数")){
                            analysisMap.put("10",columnValue);
                        }
                    }
                }
            }
            // 图片绑定
            List<Picture> pictureList = document.getPicturesTable().getAllPictures();
            String[] split = originFileName.split("\\.");
            String docName = "";
            if(null != split && split.length > 0){
                docName = split[0];
            }
            for (Picture picture : pictureList) {
                byte[] pictureData = picture.getContent();
                File imgDir = new File(imgUrl);
                if (!imgDir.exists()) {
                    imgDir.mkdirs();
                }
                String filename = docName + "_" + UUID.randomUUID()+".png";
                File pictureFile = new File(imgUrl + File.separatorChar + filename);
                // 创建输出流以将图片数据写入到指定路径
                try (OutputStream outputStream = new FileOutputStream(pictureFile)) {
                    // 写入数据
                    outputStream.write(pictureData);
                }
                InsOrderFile insOrderFile = new InsOrderFile();
                insOrderFile.setInsOrderId(orderId);
                insOrderFile.setInsSampleId(sampleId);
                insOrderFile.setSonLaboratory("振动");
                insOrderFile.setType(1);
                insOrderFile.setFileName(filename);
                insOrderFile.setFileUrl(filename);
                insOrderFileMapper.insert(insOrderFile);
            }
        }catch (Exception e){
            log.error(e.getMessage());
        }
        return analysisMap;
    }
    //温湿度试验+功率试验的保存检验内容
    @Override
    public int saveInsContext3(InsProductResult insProductResult) {
@@ -979,7 +1119,7 @@
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int verifyPlan(Integer sampleId, String laboratory, Integer type, String tell) throws IOException {
    public int verifyPlan(Integer sampleId, String laboratory, Integer type, String tell)  {
        LocalDateTime now = LocalDateTime.now();
        Integer userId = getLook.selectPowerByMethodAndUserId(null).get("userId");
        List<InsUnPass> insUnPasses = new ArrayList<>();
@@ -1050,6 +1190,15 @@
                            .set(InsOrderState::getInsState, 6)
                            .set(InsOrderState::getVerifyTell, tell)
                            .set(InsOrderState::getVerifyUser, userId));
                    //继续试验自动出库
                    WarehouseHistory history = new WarehouseHistory();
                    history.setState(2);
                    history.setInsSampleId(sampleId);
                    history.setCellId(insSample.getCellId());
                    history.setWarehouseCode(warehouseHistoryMapper.selectOne(Wrappers.<WarehouseHistory>lambdaQuery()
                            .eq(WarehouseHistory::getInsSampleId, sampleId).select(WarehouseHistory::getWarehouseCode).orderByDesc(WarehouseHistory::getId).last("limit 1")).getWarehouseCode());
                    insSampleMapper.update(null, Wrappers.<InsSample>lambdaUpdate().eq(InsSample::getId, sampleId).set(InsSample::getCellId, null));
                    warehouseHistoryMapper.insert(history);
                    break;
                case 2:
                    //结束试验:此委托单停止试验(无法再次检验)。
@@ -1060,17 +1209,9 @@
                            .set(InsOrderState::getInsState, 5)
                            .set(InsOrderState::getVerifyTell, tell)
                            .set(InsOrderState::getVerifyUser, userId));
                    //如果订单类型是原理样机,则该订单直接闭环
                    List<InsSample> insSamples = insSampleMapper.selectList(Wrappers.<InsSample>lambdaQuery().eq(InsSample::getInsOrderId, insSample.getInsOrderId()));
                    for (InsSample sample : insSamples) {
                        try{
                            outInsOrderState(laboratory, sample.getSampleCode(), insOrder.getEntrustCode());
                        }catch (Exception e){
                        }
                    }
                    insOrder.setState(4);
                    insOrderMapper.updateById(insOrder);
                    /*生成对应的检验报告*/
                    List<InsReportDto1> insReportDto1s=insOrderUserMapper.selectInsReportDto1(insOrder.getId());
                    createReport(insOrder.getId(),insReportDto1s);
                    break;
            }
        }
@@ -1079,7 +1220,7 @@
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int verifyPlan2(Integer sampleId, String laboratory, Integer type, String tell) throws IOException {
    public int verifyPlan2(Integer sampleId, String laboratory, Integer type, String tell) {
        Integer userId = getLook.selectPowerByMethodAndUserId(null).get("userId");
        InsSample insSample = insSampleMapper.selectById(sampleId);
        InsOrder insOrder = insOrderMapper.selectById(insSample.getInsOrderId());
@@ -1103,7 +1244,6 @@
                            .set(InsOrderState::getVerifyUser, userId)
                            .set(InsOrderState::getNum, record.getNum() + 1)//次数加1
                            .set(InsOrderState::getCreateTime, LocalDateTime.now()));
                    insOrder.setState(1);
                    break;
                case 1:
                    //继续试验:该样品该站点任务结束(可以再次扫码检验);
@@ -1126,7 +1266,16 @@
                                .set(InsOrderState::getVerifyTell, tell)
                                .set(InsOrderState::getVerifyUser, userId));
                    }
                    insOrder.setState(1);
                    //继续试验需要出库
                    WarehouseHistory history = new WarehouseHistory();
                    history.setState(2);
                    history.setInsSampleId(sampleId);
                    history.setCellId(insSample.getCellId());
                    history.setWarehouseCode(warehouseHistoryMapper.selectOne(Wrappers.<WarehouseHistory>lambdaQuery()
                            .eq(WarehouseHistory::getInsSampleId, sampleId).select(WarehouseHistory::getWarehouseCode).orderByDesc(WarehouseHistory::getId).last("limit 1")).getWarehouseCode());
                    insSampleMapper.update(null, Wrappers.<InsSample>lambdaUpdate()
                            .eq(InsSample::getId, sampleId).set(InsSample::getCellId, null));
                    warehouseHistoryMapper.insert(history);
                    break;
                case 2:
                    //结束试验:此委托单停止试验(无法再次检验)。
@@ -1149,21 +1298,28 @@
                                .set(InsOrderState::getVerifyTell, tell)
                                .set(InsOrderState::getVerifyUser, userId));
                    }
                    //出库
                    List<InsSample> insSamples = insSampleMapper.selectList(Wrappers.<InsSample>lambdaQuery().eq(InsSample::getInsOrderId, insSample.getInsOrderId()));
                    for (InsSample sample : insSamples) {
                        try{
                            outInsOrderState(laboratory, sample.getSampleCode(), insOrder.getEntrustCode());
                        }catch (Exception e){
                        }
                    }
                    insOrder.setState(4);
                    /*生成对应的检验报告*/
                    List<InsReportDto1> insReportDto1s=insOrderUserMapper.selectInsReportDto1(insOrder.getId());
                    createReport(insOrder.getId(),insReportDto1s);
                    break;
            }
            insOrderMapper.updateById(insOrder);
        }
        return 1;
    }
    /*生成对应的检验报告*/
    private void createReport(Integer orderId,List<InsReportDto1> insReportDto1s){
        for (InsReportDto1 insReportDto1 : insReportDto1s) {
            if (ObjectUtils.isNotEmpty(insReportDto1.getInsReportDto2s())){
                wordUtils.generateReport(orderId, insReportDto1);
//                try{
//                    wordUtils.generateReport(orderId, insReportDto1);
//                }catch (Exception e){
//                    throw new ErrorException("报告格式生成有误,请联系管理员!");
//                }
            }
        }
    }
    private void getTemplateThing
@@ -1224,8 +1380,8 @@
            String str = "";
            int count = 0;
            for (InsProduct product : insProducts) {
                //辐射试验,温湿度试验,功率试验的检验项目,不需要去手动填写检验值,只需要上传表格文件就可以了,后台提交的时候校验跳过这个检验项目
                if (product.getInspectionItem().equals("辐射试验") || product.getInspectionItem().equals("温湿度试验") || product.getInspectionItem().equals("功率试验")) {
                //温湿度试验,功率试验的检验项目,不需要去手动填写检验值,只需要上传表格文件就可以了,后台提交的时候校验跳过这个检验项目
                if ( product.getInspectionItem().equals("温湿度试验") || product.getInspectionItem().equals("功率试验")) {
                    product.setInsResult(3);//不判定
                    insProductMapper.updateById(product);
                    continue;