lvy 2 сар өмнө
parent
commit
9415790336

+ 268 - 6
blade-service/blade-repair/src/main/java/org/springblade/repair/controller/CheckAndRepairController.java

@@ -6,14 +6,19 @@ import io.swagger.annotations.Api;
 import io.swagger.annotations.ApiOperation;
 import lombok.AllArgsConstructor;
 import org.apache.commons.lang.SystemUtils;
+import org.jsoup.Jsoup;
+import org.jsoup.nodes.Document;
+import org.jsoup.nodes.Element;
+import org.jsoup.select.Elements;
 import org.springblade.common.constant.CommonConstant;
 import org.springblade.common.utils.CommonUtil;
 import org.springblade.common.utils.SnowFlakeUtil;
 import org.springblade.core.oss.model.BladeFile;
+import org.springblade.core.tool.utils.FileUtil;
+import org.springblade.core.tool.utils.IoUtil;
 import org.springblade.core.tool.utils.ObjectUtil;
-import org.springblade.manager.entity.ExcelTab;
-import org.springblade.manager.entity.WbsTreeContract;
-import org.springblade.manager.entity.WbsTreePrivate;
+import org.springblade.core.tool.utils.StringUtil;
+import org.springblade.manager.entity.*;
 import org.springblade.repair.util.ExcelInfoUtils;
 import org.springblade.repair.util.FileUtils;
 import org.springblade.resource.feign.NewIOSSClient;
@@ -21,19 +26,19 @@ import org.springblade.system.cache.ParamCache;
 import org.springframework.jdbc.core.BeanPropertyRowMapper;
 import org.springframework.jdbc.core.JdbcTemplate;
 import org.springframework.scheduling.annotation.Scheduled;
+import org.springframework.transaction.support.TransactionTemplate;
 import org.springframework.web.bind.annotation.GetMapping;
 import org.springframework.web.bind.annotation.RequestMapping;
 import org.springframework.web.bind.annotation.RestController;
 import org.springblade.manager.feign.ExcelTabClient;
 
+import javax.validation.constraints.NotNull;
 import java.io.*;
 import java.nio.file.Files;
 import java.nio.file.Path;
 import java.nio.file.Paths;
 import java.nio.file.StandardCopyOption;
-import java.util.List;
-import java.util.Map;
-import java.util.Set;
+import java.util.*;
 import java.util.stream.Collectors;
 
 
@@ -418,4 +423,261 @@ public class CheckAndRepairController {
         }
         System.out.println("检查完毕,更新了"+i+"条记录");
     }
+
+
+    private final TransactionTemplate transactionTemplate;
+    @RequestMapping("/checkAndRepairExcelHtmlDefaultValue")
+    @ApiOperation("定时检测修复ExcelHtml")
+    @Scheduled(cron = "00 00 01 * * ?")
+    public void checkAndRepairExcelHtmlDefaultValue() {
+//        String projectSql = "SELECT * from m_project_info WHERE is_deleted = 0 and id != 1904814720589430785";
+        String projectSql = "SELECT * from m_project_info WHERE is_deleted = 0 and id = 1630011899725201410";
+        List<ProjectInfo> projectInfos = jdbcTemplate.query(projectSql, new BeanPropertyRowMapper<>(ProjectInfo.class));
+        projectInfos.forEach(projectInfo -> {
+            String sql = "SELECT html_url from m_wbs_tree_private WHERE html_url is not null and html_url != '' and is_deleted = 0 and project_id = ? group by html_url LIMIT ? offset ?;";
+            long pageSize = 2000L;
+            long offset = 0L;
+            boolean flag = true;
+            while (flag) {
+                List<String> htmlUrls = jdbcTemplate.queryForList(sql, String.class,projectInfo.getId(), pageSize, offset);
+                if (!htmlUrls.isEmpty()) {
+                    for (String htmlUrl : htmlUrls) {
+                        try  {
+                            List<TextdictInfo> textDictInfoDefaultByHtml = new ArrayList<>();
+                            List<TextdictInfo> textDictInfoTipsByHtml = new ArrayList<>();
+                            // 获取html中的 默认值信息和提示信息
+                            getTextDictInfos(htmlUrl, textDictInfoTipsByHtml, textDictInfoDefaultByHtml);
+                            List<WbsTreePrivate> wbsTreePrivates = jdbcTemplate.query("SELECT * from m_wbs_tree_private WHERE is_deleted = 0 and project_id = ? and html_url = ?", new BeanPropertyRowMapper<>(WbsTreePrivate.class),
+                                    projectInfo.getId(), htmlUrl);
+                            // 获取表元素信息
+                            Map<String, Map<String, String>> tableElementInfo = getTableElementInfo(wbsTreePrivates);
+                            for (WbsTreePrivate wbsTreePrivate : wbsTreePrivates) {
+                                List<TextdictInfo> textDictInfoList = jdbcTemplate.query("SELECT * from m_textdict_info WHERE type in (4,5) and is_deleted = 0 and tab_id = " + wbsTreePrivate.getPKeyId() + " and project_id = " + wbsTreePrivate.getProjectId(),
+                                        new BeanPropertyRowMapper<>(TextdictInfo.class));
+                                Map<String, String> map = tableElementInfo.get(wbsTreePrivate.getInitTableName());
+                                Map<String, TextdictInfo> insertTextDictInfoMaps = new HashMap<>();
+                                List<TextdictInfo> updateHtmlTextDictInfos = new ArrayList<>();
+                                if (textDictInfoList.isEmpty()) {
+                                    createTextDictInfos(wbsTreePrivate, textDictInfoTipsByHtml, map, insertTextDictInfoMaps, 5, "提示信息");
+                                    createTextDictInfos(wbsTreePrivate, textDictInfoDefaultByHtml, map, insertTextDictInfoMaps, 4, "默认值");
+                                } else {
+                                    // 去重并保留id最大的
+                                    Map<String, TextdictInfo> tempMap = new HashMap<>();
+                                    textDictInfoList.forEach(info -> {
+                                        if (info.getColKey() == null || info.getColKey().isEmpty()) {
+                                            return;
+                                        }
+                                        TextdictInfo temp = tempMap.get(info.getColKey() + "_" + info.getType());
+                                        if (temp == null || info.getId() > temp.getId()) {
+                                            tempMap.put(info.getColKey() + "_" + info.getType(), info);
+                                        }
+                                    });
+                                    // 与html中的进行对比, 保留数据库中的值
+                                    textDictInfoDefaultByHtml.forEach(info -> {
+                                        TextdictInfo temp = tempMap.get(info.getColKey() + "_" + info.getType());
+                                        if (temp != null) {
+                                            if (!temp.getSigRoleName().equals(info.getSigRoleName())) {
+                                                // 更新html中的值
+                                                temp.setColName(info.getColName());
+                                                info.setSigRoleName(temp.getSigRoleName());
+                                                updateHtmlTextDictInfos.add(temp);
+                                            }
+                                            tempMap.remove(info.getColKey() + "_" + info.getType());
+                                        }
+                                        TextdictInfo textdictInfo = createTextDictInfo(wbsTreePrivate, map, 4, "默认值", info);
+                                        insertTextDictInfoMaps.put(textdictInfo.getColKey() + "_" + textdictInfo.getType(), textdictInfo);
+                                    });
+                                    textDictInfoTipsByHtml.forEach(info -> {
+                                        TextdictInfo temp = tempMap.get(info.getColKey() + "_" + info.getType());
+                                        if (temp != null) {
+                                            if (!temp.getSigRoleName().equals(info.getSigRoleName())) {
+                                                // 更新html中的值
+                                                temp.setColName(info.getColName());
+                                                info.setSigRoleName(temp.getSigRoleName());
+                                                updateHtmlTextDictInfos.add(temp);
+                                            }
+                                            tempMap.remove(info.getColKey() + "_" + info.getType());
+                                        }
+                                        TextdictInfo textdictInfo = createTextDictInfo(wbsTreePrivate, map, 5, "提示信息", info);
+                                        insertTextDictInfoMaps.put(textdictInfo.getColKey() + "_" + textdictInfo.getType(), textdictInfo);
+                                    });
+                                    if (!tempMap.isEmpty()) {
+                                        tempMap.forEach((key, value) -> {
+                                            if (value.getType() == 4) {
+                                                TextdictInfo textdictInfo = createTextDictInfo(wbsTreePrivate, map, 4, "默认值", value);
+                                                insertTextDictInfoMaps.put(textdictInfo.getColKey() + "_" + textdictInfo.getType(), textdictInfo);
+                                            } else if (value.getType() == 5) {
+                                                TextdictInfo textdictInfo = createTextDictInfo(wbsTreePrivate, map, 5, "提示信息", value);
+                                                insertTextDictInfoMaps.put(textdictInfo.getColKey() + "_" + textdictInfo.getType(), textdictInfo);
+                                            }
+                                            updateHtmlTextDictInfos.add(value);
+                                        });
+                                    }
+                                }
+                                String replace;
+                                if (!updateHtmlTextDictInfos.isEmpty()) {
+                                    InputStream fileInputStream = FileUtils.getInputStreamByUrl(wbsTreePrivate.getHtmlUrl());
+                                    String htmlString = IoUtil.readToString(fileInputStream);
+                                    Document doc = Jsoup.parse(htmlString);
+                                    updateHtmlTextDictInfos.forEach(textdictInfo -> {
+                                        Element element = doc.getElementById(textdictInfo.getColKey());
+                                        if (element != null) {
+                                            element.attr("placeholder", textdictInfo.getColName());
+                                            if (textdictInfo.getType() == 4) {
+                                                element.attr("defText", textdictInfo.getSigRoleName());
+                                            } else if (textdictInfo.getType() == 5) {
+                                                Element parent = element.parent();
+                                                // 判断 parent 是否是el-tooltip
+                                                if (parent.tagName().equals("el-tooltip")) {
+                                                    parent.attr("content", textdictInfo.getSigRoleName());
+                                                } else {
+                                                    String lastHtml = " <el-tooltip content='" + textdictInfo.getSigRoleName() + "' placement='top' effect='customized'>" + parent.html() + "</el-tooltip>";
+                                                    parent.empty().append(lastHtml);
+                                                }
+                                            }
+                                        }
+                                    });
+                                    String url = wbsTreePrivate.getHtmlUrl();
+                                    File writeFile = new File(url);
+                                    FileUtil.writeToFile(writeFile, doc.html(), Boolean.parseBoolean("UTF-8"));
+                                    String str1 = url.replace("Desktop//privateUrl", "Desktop/privateUrl");
+                                    replace = str1.replace("\\", "\\\\");
+                                } else {
+                                    replace = null;
+                                }
+                                transactionTemplate.execute( status -> {
+                                    if (!insertTextDictInfoMaps.isEmpty()) {
+                                        // 使用jdbcTemplate批量插入
+                                        jdbcTemplate.batchUpdate("insert into m_textdict_info_copy3 (id,name,type,tab_id,col_key,col_name,sig_role_id,sig_role_name,pyzbx,pyzby,excel_id,project_id,is_deleted,time_col_key,time_name,time_state) values (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)",
+                                                insertTextDictInfoMaps.values(),
+                                                insertTextDictInfoMaps.values().size(), (ps, textdictInfo) -> {
+                                                    ps.setLong(1, textdictInfo.getId());
+                                                    ps.setString(2, textdictInfo.getName());
+                                                    ps.setInt(3, textdictInfo.getType());
+                                                    ps.setString(4, textdictInfo.getTabId());
+                                                    ps.setString(5, textdictInfo.getColKey());
+                                                    ps.setString(6, textdictInfo.getColName());
+                                                    ps.setString(7, textdictInfo.getSigRoleId());
+                                                    ps.setString(8, textdictInfo.getSigRoleName());
+                                                    ps.setDouble(9, 0);
+                                                    ps.setDouble(10, 0);
+                                                    ps.setString(11, textdictInfo.getExcelId());
+                                                    ps.setString(12, textdictInfo.getProjectId());
+                                                    ps.setInt(13, 0);
+                                                    ps.setString(14, textdictInfo.getTimeColKey());
+                                                    ps.setString(15, textdictInfo.getTimeName());
+                                                    ps.setInt(16, 0);
+                                                }
+                                        );
+                                    }
+                                    if (replace != null) {
+                                        String updateSqlP = "update m_wbs_tree_private_copy6 set html_url = '" + replace + "' where p_key_id = " + wbsTreePrivate.getPKeyId() + " and project_id = " + wbsTreePrivate.getProjectId();
+                                        jdbcTemplate.execute(updateSqlP);
+                                    }
+                                    if (!textDictInfoList.isEmpty()) {
+                                        List<Long> ids = textDictInfoList.stream().map(TextdictInfo::getId).collect(Collectors.toList());
+                                        jdbcTemplate.execute("update  m_textdict_info_copy3 set is_deleted = 2 where  id in (" + StringUtil.join(ids, ",") + ")");
+                                    }
+                                    return true;
+                                });
+                            }
+                        } catch (Exception e) {
+                            e.printStackTrace();
+                        }
+                    }
+                } else {
+                    flag = false;
+                }
+                offset  += pageSize;
+            }
+        });
+    }
+    private static void createTextDictInfos(WbsTreePrivate wbsTreePrivate, List<TextdictInfo> textDictInfos, Map<String, String> map, Map<String, TextdictInfo> insertTextDictInfoMaps, int type, String name) {
+        if (!textDictInfos.isEmpty()) {
+            for (TextdictInfo textdictInfo : textDictInfos) {
+                TextdictInfo saveTextdictInfo = createTextDictInfo(wbsTreePrivate, map, type, name, textdictInfo);
+                insertTextDictInfoMaps.put(saveTextdictInfo.getColKey() + "_" + saveTextdictInfo.getType(), saveTextdictInfo);
+            }
+        }
+    }
+    @NotNull
+    private static TextdictInfo createTextDictInfo(WbsTreePrivate wbsTreePrivate, Map<String, String> map, int type, String name, TextdictInfo textdictInfo) {
+        TextdictInfo saveTextdictInfo = new TextdictInfo();
+        saveTextdictInfo.setColKey(textdictInfo.getColKey());
+        saveTextdictInfo.setColName(textdictInfo.getColName());
+        saveTextdictInfo.setSigRoleName(textdictInfo.getSigRoleName());
+        saveTextdictInfo.setId(SnowFlakeUtil.getId());
+        saveTextdictInfo.setTabId(wbsTreePrivate.getPKeyId() + "");
+        saveTextdictInfo.setType(type);
+        saveTextdictInfo.setName(name);
+        saveTextdictInfo.setExcelId(wbsTreePrivate.getExcelId() + "");
+        saveTextdictInfo.setProjectId(wbsTreePrivate.getProjectId());
+        if (map != null) {
+            String s = map.get(saveTextdictInfo.getColKey().split("__")[0]);
+            if (s != null && !s.trim().isEmpty()) {
+                saveTextdictInfo.setColName(s);
+            }
+        }
+        return saveTextdictInfo;
+    }
+    private Map<String, Map<String, String>> getTableElementInfo(List<WbsTreePrivate> wbsTreePrivates) {
+        String inSql = wbsTreePrivates.stream().map(e -> "'" + e.getInitTableName() + "'").distinct().collect(Collectors.joining(","));
+        List<TableInfo> tableInfos = jdbcTemplate.query("SELECT id, tab_en_name from m_table_info  where tab_en_name in (" + inSql + ")", new BeanPropertyRowMapper<>(TableInfo.class));
+        if (tableInfos.isEmpty()) {
+            return new HashMap<>();
+        }
+        Map<String, String> map = tableInfos.stream().collect(Collectors.toMap(tableInfo -> tableInfo.getId() + "", TableInfo::getTabEnName));
+        List<WbsFormElement> wbsFormElementList = jdbcTemplate.query("SELECT f_id,e_key,e_name from m_wbs_form_element WHERE f_id in (" + StringUtil.join(map.keySet(), ",") + ")", new BeanPropertyRowMapper<>(WbsFormElement.class));
+        // 将 initTableNames 与 wbsFormElementList 关联
+        return wbsFormElementList.stream().peek(item -> item.setFId(map.get(item.getFId())))
+                .collect(Collectors.groupingBy(WbsFormElement::getFId, Collectors.toMap(WbsFormElement::getEKey, WbsFormElement::getEName, (v1, v2) -> v1)));
+    }
+    private static void getTextDictInfos(String htmlUrl, List<TextdictInfo> textDictInfoTips, List<TextdictInfo> textDictInfoDefault) throws Exception {
+        InputStream fileInputStream = FileUtils.getInputStreamByUrl(htmlUrl);
+        String htmlString = IoUtil.readToString(fileInputStream);
+        Document doc = Jsoup.parse(htmlString);
+        Element table = doc.select("table").first();
+        Elements trs = table.select("tr");
+        trs.forEach(tr -> {
+            Elements td = tr.select("td");
+            for (Element element : td) {
+                Elements tooltips = element.select("el-tooltip");
+                getTextDictInfo(tooltips, "content", textDictInfoTips);
+                Elements inputs = element.select("el-input");
+                getTextDictInfo(inputs, "deftext", textDictInfoDefault);
+            }
+        });
+    }
+    private static void getTextDictInfo(Elements elements, String attrName, List<TextdictInfo> textDictInfos) {
+        if (!elements.isEmpty()) {
+            elements.forEach(element -> {
+                String content = element.attr(attrName);
+                if (content != null && !content.trim().isEmpty()) {
+                    String keyname = element.children().attr("keyname");
+                    String placeholder = element.children().attr("placeholder");
+                    if (keyname == null || !keyname.trim().isEmpty()) {
+                        String html = element.html();
+                        keyname = getHtmlAttrValue(html, "keyname");
+                        placeholder = getHtmlAttrValue(html, "placeholder");
+                    }
+                    if (keyname != null) {
+                        TextdictInfo info = new TextdictInfo();
+                        info.setColKey(keyname);
+                        info.setColName(placeholder);
+                        info.setSigRoleName(content);
+                        textDictInfos.add(info);
+                    }
+                }
+            });
+        }
+    }
+    private static String getHtmlAttrValue(String html, String attr) {
+        int index = html.indexOf(attr);
+        if (index > 0) {
+            int start = attr.length() + index + 1;
+            char c = html.charAt(start);
+            return html.substring(start + 1, html.indexOf(c, start + 1));
+        }
+        return null;
+    }
 }