|
@@ -0,0 +1,225 @@
|
|
|
+package com.rongwei.bscommon.sys.utils;
|
|
|
+
|
|
|
+import cn.hutool.core.util.ObjectUtil;
|
|
|
+import cn.hutool.core.util.StrUtil;
|
|
|
+import com.alibaba.fastjson.JSONObject;
|
|
|
+import com.aspose.words.*;
|
|
|
+import com.rongwei.bsentity.domain.ZhcxCommissionCheckDetailDo;
|
|
|
+import com.rongwei.bsentity.domain.ZhcxCommissionCheckDetailTreeDo;
|
|
|
+import com.rongwei.bsentity.domain.ZhcxProjectManageDo;
|
|
|
+import com.rongwei.bsentity.dto.commission.ImportProjectCheckBO;
|
|
|
+import com.rongwei.bsentity.dto.commission.ParseDocBO;
|
|
|
+import com.rongwei.rwcommon.utils.SecurityUtil;
|
|
|
+
|
|
|
+import java.util.*;
|
|
|
+import java.util.List;
|
|
|
+
|
|
|
+public class ImportCommissionHelp {
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 解析word
|
|
|
+ *
|
|
|
+ * @param doc
|
|
|
+ * @param project
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public static ImportProjectCheckBO parseDoc(Document doc, ZhcxProjectManageDo project, ParseDocBO importDto) {
|
|
|
+ List<ZhcxCommissionCheckDetailDo> detailList = new ArrayList<>();
|
|
|
+ //树的一维结构
|
|
|
+ List<ZhcxCommissionCheckDetailTreeDo> treeList = new ArrayList<>();
|
|
|
+ Map<String, ZhcxCommissionCheckDetailTreeDo> treeMap = new HashMap<>();
|
|
|
+
|
|
|
+ for (Section section : doc.getSections()) {
|
|
|
+ // 遍历节的主体
|
|
|
+ Body body = section.getBody();
|
|
|
+
|
|
|
+ final NodeCollection childNodes = body.getChildNodes();
|
|
|
+ for(int m = 0, n = childNodes.getCount(); m < n; m++) {
|
|
|
+ Node node = childNodes.get(m);
|
|
|
+
|
|
|
+ if(node.getNodeType() == 5) { //表格
|
|
|
+ Table table = (Table) node;
|
|
|
+ //table转换成json结构
|
|
|
+ final List<JSONObject> treeDetailList = ImportCommissionHelp.table2Json(table);
|
|
|
+ final ZhcxCommissionCheckDetailTreeDo treeDo = treeList.get(treeList.size() - 1);
|
|
|
+ for(JSONObject rowJson : treeDetailList) {
|
|
|
+ ZhcxCommissionCheckDetailDo detail = new ZhcxCommissionCheckDetailDo();
|
|
|
+ detail.setId(SecurityUtil.getUUID());
|
|
|
+ detail.setDeleted("0");
|
|
|
+ detail.setPid(importDto.getId());
|
|
|
+ detail.setChapterno(rowJson.getString("NO."));
|
|
|
+ detail.setClassification(rowJson.getString("模式1"));
|
|
|
+ detail.setClassificationtwo(rowJson.getString("模式2"));
|
|
|
+ detail.setChtestcontent(rowJson.getString("测试内容中文"));
|
|
|
+ detail.setEntestcontent(rowJson.getString("测试内容英文"));
|
|
|
+ detail.setTestmethod(rowJson.getString("标准测试方法"));
|
|
|
+ String testAddress = rowJson.getString("测试地点");
|
|
|
+ if(ObjectUtil.isEmpty(testAddress) || ObjectUtil.isEmpty(importDto.getDictMap().get(testAddress))) {
|
|
|
+ testAddress = "10";
|
|
|
+ } else {
|
|
|
+ testAddress = importDto.getDictMap().get(testAddress).getValue();
|
|
|
+ }
|
|
|
+ detail.setTestaddress(testAddress);
|
|
|
+ if(ObjectUtil.isNotEmpty(treeDo.getFullpid())) {
|
|
|
+ detail.setTreefullid(treeDo.getFullpid().concat(",").concat(treeDo.getId()));
|
|
|
+ } else {
|
|
|
+ detail.setTreefullid(",".concat(treeDo.getId()));
|
|
|
+ }
|
|
|
+
|
|
|
+ if(treeDo.getLevel() == 1) {
|
|
|
+ detail.setOnelevelclassification(treeDo.getId());
|
|
|
+ } else if(treeDo.getLevel() == 2) {
|
|
|
+ String[] fullpids = treeDo.getFullpid().split(",");
|
|
|
+ detail.setOnelevelclassification(fullpids[1]);
|
|
|
+ detail.setSecondlevelclassification(treeDo.getId());
|
|
|
+ } else if(treeDo.getLevel() == 3) {
|
|
|
+ String[] fullpids = treeDo.getFullpid().split(",");
|
|
|
+ detail.setOnelevelclassification(fullpids[1]);
|
|
|
+ detail.setSecondlevelclassification(fullpids[2]);
|
|
|
+ detail.setThreelevelclassification(treeDo.getId());
|
|
|
+ } else if(treeDo.getLevel() == 4) {
|
|
|
+ String[] fullpids = treeDo.getFullpid().split(",");
|
|
|
+ detail.setOnelevelclassification(fullpids[1]);
|
|
|
+ detail.setSecondlevelclassification(fullpids[2]);
|
|
|
+ detail.setThreelevelclassification(fullpids[3]);
|
|
|
+ detail.setFouronelevelclassification(treeDo.getId());
|
|
|
+ }
|
|
|
+
|
|
|
+ detailList.add(detail);
|
|
|
+ }
|
|
|
+
|
|
|
+ } else if(node.getNodeType() == 8) { //段落
|
|
|
+ Paragraph paragraph = (Paragraph) node;
|
|
|
+ if(!paragraph.getParagraphFormat().isHeading()) { //标题
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+
|
|
|
+ //树结构中添加元素
|
|
|
+ final ZhcxCommissionCheckDetailTreeDo detailTree = ImportCommissionHelp.getDetailTree(paragraph, project, treeList);
|
|
|
+ treeList.add(detailTree);
|
|
|
+ treeMap.put(detailTree.getId(), detailTree);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ return ImportProjectCheckBO.builder()
|
|
|
+ .detailList(detailList)
|
|
|
+ .treeList(treeList)
|
|
|
+ .build();
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 设置树结构
|
|
|
+ *
|
|
|
+ * @param paragraph
|
|
|
+ * @param project
|
|
|
+ * @param treeList
|
|
|
+ */
|
|
|
+ public static ZhcxCommissionCheckDetailTreeDo getDetailTree(Paragraph paragraph, ZhcxProjectManageDo project, List<ZhcxCommissionCheckDetailTreeDo> treeList) {
|
|
|
+ //当前标题等级
|
|
|
+ int currentLevelNumber = paragraph.getListFormat().getListLevelNumber();
|
|
|
+ String cellVal = getNodeText(paragraph);
|
|
|
+
|
|
|
+ ZhcxCommissionCheckDetailTreeDo treeDo = new ZhcxCommissionCheckDetailTreeDo();
|
|
|
+ treeDo.setId(SecurityUtil.getUUID());
|
|
|
+ treeDo.setDeleted("0");
|
|
|
+ treeDo.setName(cellVal);
|
|
|
+ treeDo.setBaseinfoid(project.getProjectCode());
|
|
|
+ treeDo.setLevel(currentLevelNumber + 1);
|
|
|
+
|
|
|
+ if(currentLevelNumber == 0) { //第一层节点
|
|
|
+ treeDo.setPid("-1");
|
|
|
+ } else {
|
|
|
+ final ZhcxCommissionCheckDetailTreeDo parentTree = ImportCommissionHelp.getParentTree(treeList, currentLevelNumber + 1);
|
|
|
+ final String fullpid = parentTree.getFullpid();
|
|
|
+ StringBuilder fullPidSb = new StringBuilder();
|
|
|
+ if(ObjectUtil.isNotEmpty(fullpid)) {
|
|
|
+ fullPidSb.append(parentTree.getFullpid());
|
|
|
+ }
|
|
|
+ fullPidSb.append(",");
|
|
|
+ fullPidSb.append(parentTree.getId());
|
|
|
+
|
|
|
+ treeDo.setFullpid(fullPidSb.toString());
|
|
|
+ treeDo.setPid(parentTree.getId());
|
|
|
+ }
|
|
|
+
|
|
|
+ return treeDo;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * aspose转json列表
|
|
|
+ *
|
|
|
+ * @param table
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public static List<JSONObject> table2Json(Table table) {
|
|
|
+
|
|
|
+ if(ObjectUtil.isEmpty(table)) {
|
|
|
+ return Collections.emptyList();
|
|
|
+ }
|
|
|
+
|
|
|
+ List<JSONObject> treeDetailList = new ArrayList<>();
|
|
|
+ List<String> titleList = new ArrayList<>();
|
|
|
+ int rowIdx = -1; //行数
|
|
|
+ for (Row row : table) {
|
|
|
+ rowIdx++;
|
|
|
+ // 遍历行的每个单元格
|
|
|
+ int colIdx = -1; //列数
|
|
|
+ JSONObject rowValueObj = new JSONObject();
|
|
|
+ for (Cell cell : row) {
|
|
|
+ colIdx++;
|
|
|
+ String cellVal = getNodeText(cell);
|
|
|
+ if(rowIdx == 0) {//标题行
|
|
|
+ titleList.add(cellVal);
|
|
|
+ } else {
|
|
|
+ rowValueObj.put(titleList.get(colIdx), cellVal);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if(rowIdx == 0) {
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+ treeDetailList.add(rowValueObj);
|
|
|
+ }
|
|
|
+
|
|
|
+ return treeDetailList;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 由于导入是一级一级加入,父级目录有可能是上一个元素或者上上个元素,需要循环往前找
|
|
|
+ *
|
|
|
+ * @param treeList
|
|
|
+ * @param level
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public static ZhcxCommissionCheckDetailTreeDo getParentTree(List<ZhcxCommissionCheckDetailTreeDo> treeList, int level) {
|
|
|
+ int treeSize = treeList.size();
|
|
|
+ ZhcxCommissionCheckDetailTreeDo treeDo;
|
|
|
+ while (true) {
|
|
|
+ treeDo = treeList.get(treeSize - 1);
|
|
|
+ if(ObjectUtil.isNull(treeDo)) {//找不到前面一个元素,结束循环
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ if(treeDo.getLevel() < level) {
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ treeSize--;
|
|
|
+ }
|
|
|
+ return treeDo;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获取文本
|
|
|
+ *
|
|
|
+ * @param node
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public static String getNodeText(Node node) {
|
|
|
+ final String text = node.getText();
|
|
|
+ if(ObjectUtil.isEmpty(text)) {
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+
|
|
|
+ return StrUtil.trim(text.replace("\u0007", ""));
|
|
|
+ }
|
|
|
+}
|