Kaynağa Gözat

试车检查内容预览接口与导出接口整合

zhoudazhuang 9 ay önce
ebeveyn
işleme
12059c6fbe

+ 0 - 8
business-common/src/main/java/com/rongwei/bscommon/sys/service/ZhcxCommissionCheckService.java

@@ -44,12 +44,4 @@ public interface ZhcxCommissionCheckService extends IService<ZhcxCommissionCheck
      * @return
      */
     int exportFile(Map<String, Object> map, HttpServletResponse response) throws Exception;
-
-    /**
-     * 试车报告预览
-     * @param map
-     * @param response
-     * @return
-     */
-    R wordPreview(Map<String, Object> map, HttpServletResponse response) throws Exception;
 }

+ 60 - 186
business-common/src/main/java/com/rongwei/bscommon/sys/service/impl/ZhcxCommissionCheckServiceImpl.java

@@ -18,7 +18,6 @@ import com.rongwei.bsentity.dto.CheckDetailDTO;
 import com.rongwei.bsentity.dto.CommissionCheckDTO;
 import com.rongwei.bsentity.vo.FormDataCheckVO;
 import com.rongwei.bsentity.vo.ZhcxCommissionCheckDetailWordVO;
-import com.rongwei.rwadmincommon.system.service.SysDictService;
 import com.rongwei.rwcommon.base.R;
 import com.rongwei.rwcommon.utils.DateUtils;
 import com.rongwei.rwcommon.utils.SecurityUtil;
@@ -79,6 +78,9 @@ public class ZhcxCommissionCheckServiceImpl extends ServiceImpl<ZhcxCommissionCh
     @Autowired
     private CXCommonFeginClient autoCommonFeginClient;
 
+    @Autowired
+    private ExcelUtils excelUtils;
+
     private static CXCommonFeginClient commonFeginClient;
 
     @PostConstruct
@@ -596,11 +598,14 @@ public class ZhcxCommissionCheckServiceImpl extends ServiceImpl<ZhcxCommissionCh
      */
     @Override
     public int exportFile(Map<String, Object> map, HttpServletResponse response) throws Exception {
+        excelUtils.GetWordLicense();
         Object projectObj = map.get("projectId");
         Object machineNoObj = map.get("machineNo");
+        Object exportObj = map.get("exportType");
         String day = DateUtils.format(new Date());
         String projectId = (String) projectObj;
         String machineNo = (String) machineNoObj;
+        String exportType = (String) exportObj;
         ZhcxProjectManageDo projectManageDo = zhcxProjectManageService.getById(projectId);
         SysFileItemDo fileItemDo = sysFileItemService.getById("d1b3271ce2ec45c2915f2dbcd819f45a");
         final Document[] indexDoc = {WordHelpUtils.getDoc(fileItemDo.getFullpath())};
@@ -628,20 +633,20 @@ public class ZhcxCommissionCheckServiceImpl extends ServiceImpl<ZhcxCommissionCh
             builder = new DocumentBuilder(doc);
         } catch (Exception e) {
             log.error("导出空模板出现异常");
+            return 0;
         }
-        try {
-            doc = new Document(classPathResource.getInputStream());
-            builder = new DocumentBuilder(doc);
-        } catch (Exception e) {
-            log.error("导出空模板出现异常");
-        }
-
-        if (list.isEmpty()) {
-            exportWord(doc, response);
-            return 1;
+        // 移动到页眉
+        builder.moveToHeaderFooter(HeaderFooterType.HEADER_PRIMARY); // 主页眉
+        // 如果有多个页眉,可以选择HeaderFooterType.HEADER_FIRST, HeaderFooterType.HEADER_EVEN等
+        for (com.aspose.words.Field field : doc.getRange().getFields()) {
+            if ("«projectName»".equals(field.getResult())) { // 替换FieldName为你的字段名称
+                // 在这里操作field对象
+                builder.moveToField(field, true);
+                builder.write(projectManageDo.getProjectName());
+            }
         }
         List<JSONObject> resultType = zhcxCommissionCheckDetailDao.getResultDictList();
-        if(list.size() == 0){
+        if(list.isEmpty()){
             return 1;
         }else {
             //标题
@@ -669,7 +674,7 @@ public class ZhcxCommissionCheckServiceImpl extends ServiceImpl<ZhcxCommissionCh
                 int i = chapternopuls.lastIndexOf(".");
                 String key = chapternopuls.substring(0, i);
                 if(!resMap.containsKey(key)){
-                    return -1;
+                    return 2;
                 }
                 ZhcxCommissionCheckDetailWordVO checkDetailWordVO = resMap.get(key);
                 List<ZhcxCommissionCheckDetailDo> detailList = checkDetailWordVO.getDetailList();
@@ -691,6 +696,7 @@ public class ZhcxCommissionCheckServiceImpl extends ServiceImpl<ZhcxCommissionCh
                         return sort1(a1,b1);
                     });
             for (ZhcxCommissionCheckDetailWordVO res : resList) {
+                builder.moveToDocumentStart();
                 int titleType = res.getChapterno().split("\\.").length;
                 if (titleType==1){
                     buildText(builder, DEFAULT_FONT_NAME, titleType, 12, true,
@@ -703,18 +709,34 @@ public class ZhcxCommissionCheckServiceImpl extends ServiceImpl<ZhcxCommissionCh
                     builder.getFont().setSize(15);
                     // 插入表头
                     builder.insertCell();
+                    builder.getRowFormat().setHeight(20);
+                    builder.getRowFormat().setHeightRule(HeightRule.EXACTLY);//设置HeightRule
+                    builder.getCellFormat().setOrientation(TextOrientation.HORIZONTAL);
+                    builder.getCellFormat().setVerticalAlignment(CellVerticalAlignment.CENTER);
                     builder.getCellFormat().setWidth(60);
                     buildTextContentTitle(builder, DEFAULT_FONT_NAME, StyleIdentifier.HEADING_6, 12, true,
                             "NO.", null);
                     builder.insertCell();
+                    builder.getRowFormat().setHeight(20);
+                    builder.getRowFormat().setHeightRule(HeightRule.EXACTLY);//设置HeightRule
+                    builder.getCellFormat().setOrientation(TextOrientation.HORIZONTAL);
+                    builder.getCellFormat().setVerticalAlignment(CellVerticalAlignment.CENTER);
                     builder.getCellFormat().setWidth(200);
                     buildTextContentTitle(builder, DEFAULT_FONT_NAME, StyleIdentifier.HEADING_6, 12, true,
                             "检查项目", null);
                     builder.insertCell();
+                    builder.getRowFormat().setHeight(20);
+                    builder.getRowFormat().setHeightRule(HeightRule.EXACTLY);//设置HeightRule
+                    builder.getCellFormat().setOrientation(TextOrientation.HORIZONTAL);
+                    builder.getCellFormat().setVerticalAlignment(CellVerticalAlignment.CENTER);
                     builder.getCellFormat().setWidth(60);
                     buildTextContentTitle(builder, DEFAULT_FONT_NAME, StyleIdentifier.HEADING_6, 12, true,
                             "结果", null);
                     builder.insertCell();
+                    builder.getRowFormat().setHeight(20);
+                    builder.getRowFormat().setHeightRule(HeightRule.EXACTLY);//设置HeightRule
+                    builder.getCellFormat().setOrientation(TextOrientation.HORIZONTAL);
+                    builder.getCellFormat().setVerticalAlignment(CellVerticalAlignment.CENTER);
                     builder.getCellFormat().setWidth(150);
                     buildTextContentTitle(builder, DEFAULT_FONT_NAME, StyleIdentifier.HEADING_6, 12, true,
                             "备注", null);
@@ -722,22 +744,34 @@ public class ZhcxCommissionCheckServiceImpl extends ServiceImpl<ZhcxCommissionCh
                     for (ZhcxCommissionCheckDetailDo detailWordVO:res.getDetailList()){
                         // 插入表格的第一行
                         builder.insertCell();
+                        builder.getRowFormat().setHeight(40);
+                        builder.getRowFormat().setHeightRule(HeightRule.EXACTLY);//设置HeightRule
+                        builder.getCellFormat().setOrientation(TextOrientation.HORIZONTAL);
+                        builder.getCellFormat().setVerticalAlignment(CellVerticalAlignment.CENTER);
                         builder.getCellFormat().setWidth(60);
                         buildTextContent(builder, DEFAULT_FONT_NAME, StyleIdentifier.HEADING_7, 11, false,
-                                detailWordVO.getChapterno(), null);
+                                detailWordVO.getChapterno().replaceAll("\\s", ""), null);
                         builder.insertCell();
+                        builder.getRowFormat().setHeight(40);
+                        builder.getRowFormat().setHeightRule(HeightRule.EXACTLY);//设置HeightRule
+                        builder.getCellFormat().setOrientation(TextOrientation.HORIZONTAL);
+                        builder.getCellFormat().setVerticalAlignment(CellVerticalAlignment.CENTER);
                         builder.getCellFormat().setWidth(200);
                         String chtestcontent = "";
                         String entestcontent = "";
                         if (StringUtils.isNotBlank(detailWordVO.getChtestcontent())){
-                            chtestcontent = detailWordVO.getChtestcontent();
+                            chtestcontent = detailWordVO.getChtestcontent().replaceAll("\\s", "");
                         }
                         if (StringUtils.isNotBlank(detailWordVO.getEntestcontent())){
-                            entestcontent = detailWordVO.getEntestcontent();
+                            entestcontent = detailWordVO.getEntestcontent().replaceAll("\\s", "");
                         }
                         buildTextContent(builder, DEFAULT_FONT_NAME, StyleIdentifier.HEADING_7, 11, false,
                                 chtestcontent+entestcontent, null);
                         builder.insertCell();
+                        builder.getRowFormat().setHeight(40);
+                        builder.getRowFormat().setHeightRule(HeightRule.EXACTLY);//设置HeightRule
+                        builder.getCellFormat().setOrientation(TextOrientation.HORIZONTAL);
+                        builder.getCellFormat().setVerticalAlignment(CellVerticalAlignment.CENTER);
                         builder.getCellFormat().setWidth(60);
                         String testresult = "";
                         if (StringUtils.isNotBlank(detailWordVO.getTestresult())){
@@ -746,12 +780,15 @@ public class ZhcxCommissionCheckServiceImpl extends ServiceImpl<ZhcxCommissionCh
                         buildTextContent(builder, DEFAULT_FONT_NAME, StyleIdentifier.HEADING_7, 11, false,
                                 testresult, null);
                         builder.insertCell();
+                        builder.getRowFormat().setHeight(40);
+                        builder.getRowFormat().setHeightRule(HeightRule.EXACTLY);//设置HeightRule
+                        builder.getCellFormat().setOrientation(TextOrientation.HORIZONTAL);
+                        builder.getCellFormat().setVerticalAlignment(CellVerticalAlignment.CENTER);
                         builder.getCellFormat().setWidth(150);
                         String testnotes = "";
                         if (StringUtils.isNotBlank(detailWordVO.getTestnotes())){
-                            testnotes = detailWordVO.getTestnotes();
+                            testnotes = detailWordVO.getTestnotes().replaceAll("\\s", "");
                         }
-                        builder.write(testnotes);
                         buildTextContent(builder, DEFAULT_FONT_NAME, StyleIdentifier.HEADING_7, 11, false,
                                 testnotes, null);
                         builder.endRow();
@@ -761,176 +798,13 @@ public class ZhcxCommissionCheckServiceImpl extends ServiceImpl<ZhcxCommissionCh
                 }
             }
             indexDoc[0].appendDocument(doc, ImportFormatMode.KEEP_DIFFERENT_STYLES);
-            exportWord(indexDoc[0], response);
-        }
-        return 1;
-    }
-
-    @Override
-    public R wordPreview(Map<String, Object> map, HttpServletResponse response) throws Exception {
-        Object projectObj = map.get("projectId");
-        Object machineNoObj = map.get("machineNo");
-        String day = DateUtils.format(new Date());
-        String projectId = (String) projectObj;
-        String machineNo = (String) machineNoObj;
-        ZhcxProjectManageDo projectManageDo = zhcxProjectManageService.getById(projectId);
-        SysFileItemDo fileItemDo = sysFileItemService.getById("d1b3271ce2ec45c2915f2dbcd819f45a");
-        final Document[] indexDoc = {WordHelpUtils.getDoc(fileItemDo.getFullpath())};
-        FormData formData = new FormData();
-        List<Map<String,Object>> mainDataList = new ArrayList<>();
-        Map<String,Object> mainMap = new HashMap<>(16);
-        String projectEnName = "";
-        if (StringUtils.isNotBlank(projectManageDo.getProjectEnName())){
-            projectEnName = projectManageDo.getProjectEnName();
-        }
-        mainMap.put("projectNames",projectManageDo.getProjectName()+projectEnName);
-        mainMap.put("projectCode",projectManageDo.getProjectCode());
-        mainMap.put("projectName",projectManageDo.getProjectName());
-        mainMap.put("currentDate",day);
-        mainMap.put("machineNo",machineNo);
-        mainDataList.add(mainMap);
-        formData.setMainDataList(mainDataList);
-        indexDoc[0] = ExcelExportUtil.fillWordDataByMap(fileItemDo.getFullpath(), formData);
-        List<ZhcxCommissionCheckDetailDo> list = zhcxCommissionCheckDetailDao.listData(map);
-        Document doc = null;
-        DocumentBuilder builder = null;
-        ClassPathResource classPathResource = new ClassPathResource("template/检查内容明细模版.doc");
-        try {
-            doc = new Document(classPathResource.getInputStream());
-            builder = new DocumentBuilder(doc);
-        } catch (Exception e) {
-            log.error("预览报告异常",e);
-        }
-        try {
-            doc = new Document(classPathResource.getInputStream());
-            builder = new DocumentBuilder(doc);
-        } catch (Exception e) {
-            log.error("预览报告异常",e);
-        }
-        List<JSONObject> resultType = zhcxCommissionCheckDetailDao.getResultDictList();
-        if(list.isEmpty()){
-            R.error("检查内容暂无数据");
-        }else {
-            //标题
-            Set<ZhcxCommissionCheckDetailDo> noSet = list.stream().filter(item -> ObjectUtil.isNotEmpty(item.getIstitle()) && "y".equals(item.getIstitle())).collect(Collectors.toSet());
-            Map<String, ZhcxCommissionCheckDetailWordVO> resMap = new HashMap<>();
-            for (ZhcxCommissionCheckDetailDo item : noSet) {
-                ZhcxCommissionCheckDetailWordVO checkDetailWordVO = new ZhcxCommissionCheckDetailWordVO();
-                checkDetailWordVO.setChapterno(item.getChapternopuls());
-                String chtitle = "";
-                String entitle = "";
-                if (StringUtils.isNotBlank(item.getChtitle())) {
-                    chtitle = item.getChtitle();
-                }
-                if (StringUtils.isNotBlank(item.getEntitle())) {
-                    entitle = item.getEntitle();
-                }
-                checkDetailWordVO.setTitleContent(chtitle + entitle);
-                checkDetailWordVO.setDetailList(new LinkedList<>());
-                resMap.put(item.getChapternopuls(), checkDetailWordVO);
+            if("export".equals(exportType)){
+                exportWord(indexDoc[0], response);
+            }else {
+                previewWord(indexDoc[0], response);
             }
-            //非标题
-            List<ZhcxCommissionCheckDetailDo> detailLists = list.stream().filter(item -> !(ObjectUtil.isNotEmpty(item.getIstitle()) && "y".equals(item.getIstitle()))).collect(Collectors.toList());
-            for (ZhcxCommissionCheckDetailDo item : detailLists) {
-                String chapternopuls = item.getChapternopuls();
-                int i = chapternopuls.lastIndexOf(".");
-                String key = chapternopuls.substring(0, i);
-                if (!resMap.containsKey(key)) {
-                    return R.error("检查内容项章节号有误");
-                }
-                ZhcxCommissionCheckDetailWordVO checkDetailWordVO = resMap.get(key);
-                List<ZhcxCommissionCheckDetailDo> detailList = checkDetailWordVO.getDetailList();
-                detailList.add(item);
-                detailList.sort((a, b) -> {
-                            LinkedList<String> a1 = new LinkedList<>(Arrays.asList(a.getChapternopuls().split("\\.")));
-                            LinkedList<String> b1 = new LinkedList<>(Arrays.asList(b.getChapternopuls().split("\\.")));
-                            return sort1(a1, b1);
-                        }
-                );
-            }
-            List<ZhcxCommissionCheckDetailWordVO> resList = new LinkedList<>();
-            for (String s : resMap.keySet()) {
-                resList.add(resMap.get(s));
-            }
-            resList.sort((a, b) -> {
-                LinkedList<String> a1 = new LinkedList<>(Arrays.asList(a.getChapterno().split("\\.")));
-                LinkedList<String> b1 = new LinkedList<>(Arrays.asList(b.getChapterno().split("\\.")));
-                return sort1(a1, b1);
-            });
-            for (ZhcxCommissionCheckDetailWordVO res : resList) {
-                int titleType = res.getChapterno().split("\\.").length;
-                if (titleType == 1) {
-                    buildText(builder, DEFAULT_FONT_NAME, titleType, 12, true,
-                            res.getChapterno() + "." + res.getTitleContent(), null);
-                } else {
-                    buildText(builder, DEFAULT_FONT_NAME, titleType, 12, true,
-                            res.getChapterno() + res.getTitleContent(), null);
-                }
-                if (ObjectUtil.isNotEmpty(res.getDetailList())) {
-                    builder.getFont().setSize(15);
-                    // 插入表头
-                    builder.insertCell();
-                    builder.getCellFormat().setWidth(60);
-                    buildTextContentTitle(builder, DEFAULT_FONT_NAME, StyleIdentifier.HEADING_6, 12, true,
-                            "NO.", null);
-                    builder.insertCell();
-                    builder.getCellFormat().setWidth(200);
-                    buildTextContentTitle(builder, DEFAULT_FONT_NAME, StyleIdentifier.HEADING_6, 12, true,
-                            "检查项目", null);
-                    builder.insertCell();
-                    builder.getCellFormat().setWidth(60);
-                    buildTextContentTitle(builder, DEFAULT_FONT_NAME, StyleIdentifier.HEADING_6, 12, true,
-                            "结果", null);
-                    builder.insertCell();
-                    builder.getCellFormat().setWidth(150);
-                    buildTextContentTitle(builder, DEFAULT_FONT_NAME, StyleIdentifier.HEADING_6, 12, true,
-                            "备注", null);
-                    builder.endRow();
-                    for (ZhcxCommissionCheckDetailDo detailWordVO : res.getDetailList()) {
-                        // 插入表格的第一行
-                        builder.insertCell();
-                        builder.getCellFormat().setWidth(60);
-                        buildTextContent(builder, DEFAULT_FONT_NAME, StyleIdentifier.HEADING_7, 11, false,
-                                detailWordVO.getChapterno(), null);
-                        builder.insertCell();
-                        builder.getCellFormat().setWidth(200);
-                        String chtestcontent = "";
-                        String entestcontent = "";
-                        if (StringUtils.isNotBlank(detailWordVO.getChtestcontent())) {
-                            chtestcontent = detailWordVO.getChtestcontent();
-                        }
-                        if (StringUtils.isNotBlank(detailWordVO.getEntestcontent())) {
-                            entestcontent = detailWordVO.getEntestcontent();
-                        }
-                        buildTextContent(builder, DEFAULT_FONT_NAME, StyleIdentifier.HEADING_7, 11, false,
-                                chtestcontent + entestcontent, null);
-                        builder.insertCell();
-                        builder.getCellFormat().setWidth(60);
-                        String testresult = "";
-                        if (StringUtils.isNotBlank(detailWordVO.getTestresult())) {
-                            testresult = resultType.stream().filter(dictDo -> detailWordVO.getTestresult().contains(dictDo.getString("VALUE"))).findFirst().get().getString("NAME").toString();
-                        }
-                        buildTextContent(builder, DEFAULT_FONT_NAME, StyleIdentifier.HEADING_7, 11, false,
-                                testresult, null);
-                        builder.insertCell();
-                        builder.getCellFormat().setWidth(150);
-                        String testnotes = "";
-                        if (StringUtils.isNotBlank(detailWordVO.getTestnotes())) {
-                            testnotes = detailWordVO.getTestnotes();
-                        }
-                        builder.write(testnotes);
-                        buildTextContent(builder, DEFAULT_FONT_NAME, StyleIdentifier.HEADING_7, 11, false,
-                                testnotes, null);
-                        builder.endRow();
-                    }
-                    // 结束表格
-                    builder.endTable();
-                }
-            }
-            indexDoc[0].appendDocument(doc, ImportFormatMode.KEEP_DIFFERENT_STYLES);
-            previewWord(indexDoc[0], response);
         }
-        return R.ok();
+        return 3;
     }
 
     private int sort1(List<String> parts1,List<String> parts2){

+ 5 - 13
business-server/src/main/java/com/rongwei/bsserver/controller/ZhcxCommissionCheckController.java

@@ -63,26 +63,18 @@ public class ZhcxCommissionCheckController {
     }
 
     @PostMapping("wordExport")
-    @ApiOperation("word导出")
+    @ApiOperation("word导出/预览")
     public void wordExport(@RequestBody Map<String,Object> map, HttpServletResponse response) throws Exception {
         int i = commissionCheckService.exportFile(map,response);
         if(i == 0){
-            throw new CustomException("导出失败");
+            throw new CustomException("程序异常");
         }else if(i == 1){
             throw new CustomException("检查内容暂无数据");
         }else {
-            throw new CustomException("数据异常");
+            if (i==2){
+                throw new CustomException("数据异常");
+            }
         }
     }
-
-    @PostMapping("wordPreview")
-    @ApiOperation("word预览")
-    public R wordPreview(@RequestBody Map<String,Object> map, HttpServletResponse response) throws Exception {
-        R  r = commissionCheckService.wordPreview(map,response);
-        if("500".equals(r.getCode())){
-            throw new CustomException("预览失败");
-        }
-        return r;
-    }
 }
 

BIN
business-server/src/main/resources/template/检查内容明细模版.doc