首页 > 其他分享 >springboot导入EXCEL数据10+浏览器等待超时问题

springboot导入EXCEL数据10+浏览器等待超时问题

时间:2024-11-14 17:07:54浏览次数:1  
标签:10 springboot EXCEL logParmInsert 导入 result new orderTable String

解决办法异步导入法:

  • 前台js解决思路:调用导入按钮后,把前台相应的操作按钮置为不可用,每个10秒查询一次导入结果返回值,直到查询到导入成功或失败的返回值后,将后续的操作按钮置为可用状态
    关键代码如下:
com.langyashi.dialog.asyncImportExcel({
                ctx: "${ctx}",
                excelMapper: xmlNameStr,
                queryParams: {
                    headerId: headerId,
                    poSegment1: poSegment1,
                    orderTable: orderTable,
                    orderPKName: headerId
                },
                errorWriteback: true,//错误回写导入文件
                callbackFun: function (resultData) {
                    if (resultData.code == '0') {
                        var message = "导入成功,送货单号" + segment1 + "已生成,请等待装箱信息验证成功后才可提交单据。验证错误文件请查看页面上“异步验证错误文件”附件。";
                        BootstrapDialog.alert(message, function () {
                        });
                        setTimeout(function () {
                            let intervalId = setInterval(function () {
                                var result = com.langyashi.dialog.getAsyncImportResult({
                                    ctx: "${ctx}",
                                    orderTable: orderTable,
                                    orderPKName: headerId
                                });
                                if (result.code != "") {
                                    //停止轮询
                                    clearInterval(intervalId);
                                    intervalId = null;
                                    $("#daoruannv").attr("disabled",false);
                                    $("#daoru").attr("disabled",false);
                                    //导入成功或者失败时,//刷新错误附件
                                    if (result.code == "END_ERROR" || result.code == "END_SUCCESS") {
                                      var fileName =  com.langyashi.dialog.getAsycImportErroFile('${ctx}',orderTable,headerId);
                                      $("#asyncImportFileName").text(fileName);
                                    }
                                    //导入成功后,刷新数据行
                                    if (result.code == "END_SUCCESS") {
                                        refreshDeliverLines();
                                        queryTotal();
                                    }
                                    //导入异常,提示信息
                                    if (result.code == "END_EXCEPTION") {
                                        BootstrapDialog.alert("导入失败:" + result.message, function () {
                                        });
                                    }
                                }
                            }, 10000);
                        }, 5000)
                    } else {
                        BootstrapDialog.alert(resultData.message, function () {
                        });
                    }
                }
            });

后台实现类,先删除之前导入的数据,然后创建子线程导入

Map<String, Object> resultret = new HashMap<>();
        resultret.put("code", "0");
        try {
            String excelMapperFileName = excelImportParams.getExcelMapper();
            ExcelMapper excelMapper = ExcelConfigUtils.generateExcelMapper(excelMapperFileName);
            MultipartFile file = excelImportParams.getExcelfile();
            Workbook workbook = WorkbookFactory.create(file.getInputStream());

            //回调服务增加登陆用户id
            int currentUserId = "123456";

            //解析后的Excel数据表对象
            Map excelData = ExcelReadUtils.parseExcel(excelMapper, workbook);

            //异步校验用变量
            Workbook finalWorkbook = workbook;
            Map finalExcelData = excelData;
            ExcelMapper finalExcelMapper = excelMapper;

            String orderType = "IMPORT_EXCEL";
            String orderTable = (String) excelImportParams.getQueryParamsMap().get("orderTable");
            Object orderPKNameO = excelImportParams.getQueryParamsMap().get("orderPKName");
            String orderPKName = String.valueOf(orderPKNameO);
            //删除导入历史
            IesAsyncImportLog logParm = new IesAsyncImportLog();
            logParm.setOrderTable(orderTable);
            logParm.setOrderPkValue(orderPKName);
            asyncImportLogService.deleteByOrder(logParm);
            new Thread(new Runnable() {
                @Override
                public void run() {
                    String logResult = "";
                    String logErrorMessage = "";
                    try {
                        //导入成功后调用服务处理数据
                        ExceldpCallbackService service = (ExceldpCallbackService) applicationContext.getBean(Class.forName
                                (finalExcelMapper.getCallbackBean()));
                        ExceldpResult result = service.afterImportExcel(excelImportParams.getQueryParamsMap(), finalExcelData);

                        //删除之前的错误文件
                        AttachmentParam attachmentParam = new AttachmentParam();
                        attachmentParam.setOrderPKName(orderPKName);
                        attachmentParam.setOrderTable(orderTable);
                        attachmentParam.setOrderType(orderType);
                        AttachmentForm attachmentForm = attachmentService.getAttachmentForm(attachmentParam);
                        for (Attachment att : attachmentForm.getAttachmentList()) {
                            attachmentService.delete(att.getOrderPKValue());
                        }

                        //验证有问题时回写错误并上传错误文件
                        if (!result.isSucceed()) {
                            errorWriteback(finalWorkbook, finalExcelData, finalExcelMapper);
                            DateFormat dformat = new SimpleDateFormat("yyyyMMdd-HHmmss");
                            String dateTime = dformat.format(new Date());
                            String fileName = String.format("[error][%s]%s", dateTime, file.getOriginalFilename());
                            Attachment attachment = new Attachment();
                            attachment.setOrderType(orderType);
                            attachment.setOrderTable(orderTable);
                            attachment.setOrderPKName(orderPKName);
                            attachment.setFileName(fileName);
                            attachment.setFileType(file.getOriginalFilename().substring(file.getOriginalFilename().lastIndexOf(".") + 1));
                            attachmentService.saveWorkbookForAtt(attachment, finalWorkbook);
                            logResult = "END_ERROR";
                        } else {
                            logResult = "END_SUCCESS";
                        }
                    } catch (Throwable e) {
                        logResult = "END_EXCEPTION";
                        logErrorMessage = e.getMessage();
                        logger.error("文件导入异常.", e);
                    } finally {
                        IesAsyncImportLog logParmInsert = new IesAsyncImportLog();
                        logParmInsert.setId(UUIDHexGenerator.getInstance().generate());
                        logParmInsert.setImportResult(logResult);
                        logParmInsert.setOrderTable(orderTable);
                        logParmInsert.setOrderPkValue(orderPKName);
                        logParmInsert.setCreatedBy(currentUserId);
                        logParmInsert.setCreationDate(new Date());
                        logParmInsert.setErrorMessage(logErrorMessage);
                        asyncImportLogService.insert(logParmInsert);
                    }
                }
            }).start();
        } catch (Exception e) {
            resultret.put("code", "1");
            resultret.put("message", e.getMessage());
        }
        return resultret;

标签:10,springboot,EXCEL,logParmInsert,导入,result,new,orderTable,String
From: https://www.cnblogs.com/xiaowanghaha/p/18546429

相关文章