GenerateCommon.java 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692
  1. package jnpf.base.util.common;
  2. import cn.hutool.core.bean.BeanUtil;
  3. import cn.hutool.core.collection.CollectionUtil;
  4. import com.alibaba.fastjson.JSONArray;
  5. import com.alibaba.fastjson.JSONObject;
  6. import com.alibaba.fastjson.serializer.SerializerFeature;
  7. import com.baomidou.mybatisplus.core.toolkit.Constants;
  8. import com.baomidou.mybatisplus.core.toolkit.StringPool;
  9. import com.baomidou.mybatisplus.generator.config.OutputFile;
  10. import com.baomidou.mybatisplus.generator.config.builder.CustomFile;
  11. import jnpf.base.entity.SysConfigEntity;
  12. import jnpf.base.entity.VisualdevEntity;
  13. import jnpf.base.model.ColumnDataModel;
  14. import jnpf.base.model.DownloadCodeForm;
  15. import jnpf.base.model.Template6.ColumnListField;
  16. import jnpf.base.model.Template7.Template7Model;
  17. import jnpf.base.model.print.PrintOption;
  18. import jnpf.base.service.PrintDevService;
  19. import jnpf.constant.FileTypeConstant;
  20. import jnpf.constant.GenerateConstant;
  21. import jnpf.constant.JnpfConst;
  22. import jnpf.database.model.superQuery.SuperJsonModel;
  23. import jnpf.database.model.superQuery.SuperQueryJsonModel;
  24. import jnpf.model.generater.GenerConfig;
  25. import jnpf.model.generater.GenerField;
  26. import jnpf.model.visualJson.FieLdsModel;
  27. import jnpf.model.visualJson.TemplateJsonModel;
  28. import jnpf.model.visualJson.analysis.FormAllModel;
  29. import jnpf.model.visualJson.analysis.FormColumnModel;
  30. import jnpf.model.visualJson.analysis.FormEnum;
  31. import jnpf.model.visualJson.config.HeaderModel;
  32. import jnpf.util.*;
  33. import jnpf.util.context.SpringContext;
  34. import jnpf.util.visiual.JnpfKeyConsts;
  35. import lombok.Cleanup;
  36. import org.apache.commons.io.IOUtils;
  37. import org.apache.velocity.Template;
  38. import org.apache.velocity.VelocityContext;
  39. import org.apache.velocity.app.Velocity;
  40. import java.io.File;
  41. import java.io.FileOutputStream;
  42. import java.io.IOException;
  43. import java.io.StringWriter;
  44. import java.util.*;
  45. import java.util.stream.Collectors;
  46. /**
  47. * @author JNPF开发平台组
  48. * @version V3.5.0
  49. * @copyright 引迈信息技术有限公司(https://www.jnpfsoft.com)
  50. * @date 2021/5/31
  51. */
  52. public class GenerateCommon {
  53. public static final String IS_CLOUD = "single";
  54. private static PrintDevService printDevService = SpringContext.getBean(PrintDevService.class);
  55. public static String getLocalBasePath() {
  56. return FileUploadUtils.getLocalBasePath();
  57. }
  58. public static String getPath(String type) {
  59. return FilePathUtil.getFilePath(type);
  60. }
  61. public static List<PrintOption> getList(List<String> ids) {
  62. return printDevService.getPrintTemplateOptions(ids);
  63. }
  64. /**
  65. * 获取代码生成基础信息
  66. *
  67. * @return
  68. */
  69. public static Template7Model getTemplate7Model(List<SysConfigEntity> list) {
  70. String author = "";
  71. String copyright = "";
  72. String version = "";
  73. for (SysConfigEntity item : list) {
  74. if ("companyName".equals(item.getFkey())) {
  75. author = item.getValue();
  76. }
  77. if ("copyright".equals(item.getFkey())) {
  78. copyright = item.getValue();
  79. }
  80. if ("sysVersion".equals(item.getFkey())) {
  81. version = item.getValue();
  82. }
  83. }
  84. Template7Model temModel = new Template7Model();
  85. temModel.setServiceDirectory(GenerateCommon.getLocalBasePath() + GenerateCommon.getPath(FileTypeConstant.CODETEMP));
  86. temModel.setCreateDate(DateUtil.daFormat(new Date()));
  87. temModel.setCreateUser(StringUtil.isNotEmpty(author) ? author : GenerateConstant.AUTHOR);
  88. temModel.setCopyright(StringUtil.isNotEmpty(copyright) ? copyright : GenerateConstant.COPYRIGHT);
  89. temModel.setVersion(StringUtil.isNotEmpty(version) ? version : GenerateConstant.VERSION);
  90. temModel.setDescription(GenerateConstant.DESCRIPTION);
  91. return temModel;
  92. }
  93. /**
  94. * 获取自定义文件(模板和生成路径)
  95. *
  96. * @param generateParamModel
  97. * @return
  98. */
  99. public static List<CustomFile> getCustomFileList(GenerateParamModel generateParamModel) {
  100. Template7Model template7Model = generateParamModel.getTemplate7Model();
  101. String path = template7Model.getServiceDirectory() + generateParamModel.getFileName() + File.separator;
  102. DownloadCodeForm downloadCodeForm = generateParamModel.getDownloadCodeForm();
  103. ArrayList<CustomFile> custFileList = new ArrayList<>();
  104. if (generateParamModel.isMainTable()) {
  105. custFileList.add(new CustomFile.Builder().templatePath("java/Controller.java.vm").formatNameFunction(tableInfo -> tableInfo.getControllerName())
  106. .fileName(StringPool.DOT_JAVA).filePath(getCustomFilePath(path, OutputFile.controller.name(), downloadCodeForm)).enableFileOverride().build());
  107. }
  108. custFileList.add(new CustomFile.Builder().templatePath("java/Entity.java.vm").formatNameFunction(tableInfo -> tableInfo.getEntityName())
  109. .fileName(StringPool.DOT_JAVA).filePath(getCustomFilePath(path, OutputFile.entity.name(), downloadCodeForm)).enableFileOverride().build());
  110. custFileList.add(new CustomFile.Builder().templatePath("java/Mapper.java.vm").formatNameFunction(tableInfo -> tableInfo.getMapperName())
  111. .fileName(StringPool.DOT_JAVA).filePath(getCustomFilePath(path, OutputFile.mapper.name(), downloadCodeForm)).enableFileOverride().build());
  112. custFileList.add(new CustomFile.Builder().templatePath("java/Service.java.vm").formatNameFunction(tableInfo -> tableInfo.getServiceName())
  113. .fileName(StringPool.DOT_JAVA).filePath(getCustomFilePath(path, OutputFile.service.name(), downloadCodeForm)).enableFileOverride().build());
  114. custFileList.add(new CustomFile.Builder().templatePath("java/ServiceImpl.java.vm").formatNameFunction(tableInfo ->
  115. tableInfo.getServiceImplName())
  116. .fileName(StringPool.DOT_JAVA).filePath(getCustomFilePath(path, OutputFile.serviceImpl.name(), downloadCodeForm)).enableFileOverride().build());
  117. custFileList.add(new CustomFile.Builder().templatePath("java/Mapper.xml.vm").formatNameFunction(tableInfo -> tableInfo.getXmlName())
  118. .fileName(StringPool.DOT_XML).filePath(getCustomFilePath(path, OutputFile.xml.name(), downloadCodeForm)).enableFileOverride().build());
  119. return custFileList;
  120. }
  121. /**
  122. * 根据模板名称获取生成文件路径
  123. *
  124. * @param path
  125. * @param codeName
  126. * @param downloadCodeForm
  127. * @return
  128. */
  129. public static String getCustomFilePath(String path, String codeName, DownloadCodeForm downloadCodeForm) {
  130. String eachName = "";
  131. String frontName = "";
  132. String modulName = downloadCodeForm.getModule();
  133. String framePath = downloadCodeForm.getModulePackageName();
  134. switch (codeName) {
  135. case "controller":
  136. framePath = getCloudPath("-controller", downloadCodeForm);
  137. break;
  138. case "entity":
  139. framePath = getCloudPath("-entity", downloadCodeForm);
  140. break;
  141. case "mapper":
  142. framePath = getCloudPath("-biz", downloadCodeForm);
  143. break;
  144. case "xml":
  145. if ("cloud".equals(GenerateCommon.IS_CLOUD)) {
  146. String fileFront = "jnpf-" + modulName + File.separator + "jnpf-" + modulName + "-biz" + File.separator;
  147. if ("form".equals(modulName)) {
  148. fileFront = "jnpf-workflow" + File.separator + "jnpf-workflow-form" + File.separator + "jnpf-workflow-form-biz" + File.separator;
  149. }
  150. framePath = fileFront + "src" + File.separator + "main" + File.separator + "resources";
  151. return path + File.separator + "java" + File.separator + framePath + File.separator + "mapper"
  152. + File.separator;
  153. }
  154. return path + File.separator + "resources" + File.separator + "mapper"
  155. + File.separator;
  156. case "service":
  157. framePath = getCloudPath("-biz", downloadCodeForm);
  158. break;
  159. case "serviceImpl":
  160. codeName = "impl";
  161. frontName = "service" + File.separator;
  162. framePath = getCloudPath("-biz", downloadCodeForm);
  163. default:
  164. break;
  165. }
  166. return path + File.separator + "java" + File.separator + framePath + File.separator + frontName + codeName
  167. + File.separator;
  168. }
  169. /**
  170. * 获取微服务框架路径
  171. *
  172. * @param
  173. * @return
  174. * @copyright 引迈信息技术有限公司
  175. * @date 2023/3/22
  176. */
  177. public static String getCloudPath(String houzui, DownloadCodeForm downloadCodeForm) {
  178. //发起表单
  179. if ("form".equals(downloadCodeForm.getModule())) {
  180. return "jnpf-workflow" + File.separator + "jnpf-workflow-form" + File.separator + "jnpf-workflow-" + downloadCodeForm.getModule() + houzui + File.separator
  181. + "src" + File.separator + "main" + File.separator + "java" + File.separator + "jnpf" + File.separator + "form";
  182. }
  183. String framePath = "jnpf-" + downloadCodeForm.getModule() + File.separator + "jnpf-" + downloadCodeForm.getModule() + houzui + File.separator
  184. + "src" + File.separator + "main" + File.separator + "java" + File.separator + downloadCodeForm.getModulePackageName();
  185. return framePath;
  186. }
  187. /**
  188. * 获取导出字段
  189. *
  190. * @param columnList
  191. * @return
  192. */
  193. public static List<ColumnListField> getExpotColumn(List<ColumnListField> columnList) {
  194. List<ColumnListField> listOptions = new ArrayList<>();
  195. columnList.forEach(item -> {
  196. ColumnListField columnListField = new ColumnListField();
  197. BeanUtil.copyProperties(item, columnListField);
  198. if (item.getVModel().toLowerCase().startsWith(JnpfKeyConsts.CHILD_TABLE_PREFIX)) {
  199. columnListField.setTableType(2);
  200. columnListField.setVModel(item.getVModel().split("-")[1]);
  201. } else if (item.getVModel().toLowerCase().contains(JnpfConst.SIDE_MARK)) {
  202. columnListField.setTableType(1);
  203. columnListField.setVModel(item.getVModel().split(JnpfConst.SIDE_MARK)[1]);
  204. } else {
  205. columnListField.setTableType(0);
  206. }
  207. if ("static".equals(item.getConfig().getDataType())) {
  208. columnListField.setOptions(JsonUtil.getObjectToString(item.getOptions()));
  209. if (item.getJnpfKey().equals(JnpfKeyConsts.CHECKBOX)) {
  210. columnListField.setMultiple(true);
  211. }
  212. }
  213. listOptions.add(columnListField);
  214. });
  215. return listOptions;
  216. }
  217. /**
  218. * 合计千分位字段列表
  219. *
  220. * @param mast 主表字段
  221. * @param mastTable 副表字段
  222. * @param type 列表类型 4-行内编辑
  223. * @return
  224. */
  225. public static List<String> getSummaryThousandList(List<FormAllModel> mast, List<FormAllModel> mastTable, Integer type) {
  226. String suffix = "_name";
  227. if (type == 4) {
  228. suffix = "";
  229. }
  230. List<String> thousandsField = new ArrayList<>();
  231. for (FormAllModel f : mast) {
  232. FieLdsModel fm = f.getFormColumnModel().getFieLdsModel();
  233. if (fm.isThousands()) {
  234. thousandsField.add(fm.getVModel() + suffix);
  235. }
  236. }
  237. for (FormAllModel f : mastTable) {
  238. FieLdsModel fm = f.getFormMastTableModel().getMastTable().getFieLdsModel();
  239. if (fm.isThousands()) {
  240. thousandsField.add(f.getFormMastTableModel().getTable() + "." + fm.getVModel() + suffix);
  241. }
  242. }
  243. return thousandsField;
  244. }
  245. /**
  246. * 复杂表头 对象生成。
  247. *
  248. * @param path
  249. * @param generateParamModel
  250. * @param entity
  251. * @param downloadCodeForm
  252. * @param objectAll
  253. */
  254. public static void createComplexHeaderExcelVo(String path, GenerateParamModel generateParamModel, VisualdevEntity entity,
  255. DownloadCodeForm downloadCodeForm, Map<String, Object> objectAll) {
  256. ColumnDataModel columnDataModel = JsonUtil.getJsonToBean(entity.getColumnData(), ColumnDataModel.class);
  257. JsonUtil.getListToJsonArray(columnDataModel.getComplexHeaderList());
  258. List<HeaderModel> complexHeaderList = columnDataModel.getComplexHeaderList();
  259. String templateName = File.separator + "java" + File.separator + "ExcelVO.java.vm";
  260. VelocityContext context = new VelocityContext();
  261. Map<String, Object> object = new HashMap<>();
  262. object.put("genInfo", generateParamModel.getTemplate7Model());
  263. object.put("package", generateParamModel.getDownloadCodeForm().getModulePackageName());
  264. object.put("module", downloadCodeForm.getModule());
  265. object.put("isMain", true);
  266. object.put("isComplexVo", true);
  267. object.put("importFields", objectAll.get("importFields"));
  268. for (HeaderModel item : complexHeaderList) {
  269. if (item.getChildColumns().size() > 0) {
  270. String className = "Complex" + item.getId();
  271. object.put("complexList", JsonUtil.getListToJsonArray(item.getChildColumns()));
  272. object.put("className", className);
  273. context.put("context", object);
  274. String fileNames = GenerateCommon.getFileName(path, templateName, className, downloadCodeForm);
  275. GenerateCommon.velocityWriterFile(context, templateName, fileNames);
  276. }
  277. }
  278. }
  279. /**
  280. * 渲染html模板
  281. *
  282. * @param path 路径
  283. * @param object 模板数据
  284. * @param templatePath 模板路径
  285. */
  286. public static void htmlTemplates(String path, Map<String, Object> object, String templatePath, int type, boolean hasImport,
  287. DownloadCodeForm downloadCodeForm, GenerateInterface codeUtil) throws Exception {
  288. //获取模板列表
  289. List<String> templates = codeUtil.getTemplates(templatePath, type, hasImport);
  290. templates.add(File.separator + "java" + File.separator + "Json.json.vm" );
  291. //界面模板
  292. VelocityContext context = new VelocityContext();
  293. context.put("context", object);
  294. for (String template : templates) {
  295. String className = object.get("className").toString();
  296. String fileNames = GenerateCommon.getFileName(path, template, className, downloadCodeForm);
  297. GenerateCommon.velocityWriterFile(context, template, fileNames);
  298. }
  299. }
  300. /**
  301. * 根据模板 获取文件名
  302. *
  303. * @param path 路径
  304. * @param template 模板名称
  305. * @param className 文件名称
  306. * @return
  307. */
  308. public static String getFileName(String path, String template, String className, DownloadCodeForm downloadCodeForm) {
  309. String framePath = GenerateCommon.getCloudPath("-entity", downloadCodeForm);
  310. String modelfolder = downloadCodeForm.getMainClassName();
  311. String modelPath = XSSEscape.escapePath(path + File.separator + "java" + File.separator + framePath + File.separator + "model"
  312. + File.separator + modelfolder.toLowerCase());
  313. String htmlPath = XSSEscape.escapePath(path + File.separator + "html" + File.separator + "web" + File.separator + modelfolder.toLowerCase());
  314. File htmlfile = new File(htmlPath);
  315. File modelfile = new File(modelPath);
  316. if (!htmlfile.exists()) {
  317. htmlfile.mkdirs();
  318. }
  319. if (!modelfile.exists()) {
  320. modelfile.mkdirs();
  321. }
  322. if (template.contains("extraForm.vue.vm") || template.contains("ExtraForm.vue.vm")) {
  323. return htmlPath + File.separator + "ExtraForm.vue";
  324. }
  325. if (template.contains("Form.vue.vm")) {
  326. return htmlPath + File.separator + "Form.vue";
  327. }
  328. if (template.contains("FormPopup.vue.vm")) {
  329. return htmlPath + File.separator + "FormPopup.vue";
  330. }
  331. if (template.contains("index.vue.vm")) {
  332. return htmlPath + File.separator + "index.vue";
  333. }
  334. if (template.contains("indexEdit.vue.vm")) {
  335. return htmlPath + File.separator + "index.vue";
  336. }
  337. if (template.contains("Detail.vue.vm")) {
  338. return htmlPath + File.separator + "Detail.vue";
  339. }
  340. if (template.contains("api.ts.vm")) {
  341. //vue3生成ts文件夹
  342. String htmlTSPath = XSSEscape.escapePath(path + File.separator + "html" + File.separator + "web" + File.separator
  343. + modelfolder.toLowerCase() + File.separator + "helper");
  344. File htmlJSfile = new File(htmlTSPath);
  345. if (!htmlJSfile.exists() && !"form".equals(downloadCodeForm.getModule())) {
  346. htmlJSfile.mkdirs();
  347. }
  348. return htmlPath + File.separator + "helper" + File.separator + "api.ts";
  349. }
  350. //后端代码
  351. if (template.contains("InfoVO.java.vm")) {
  352. return modelPath + File.separator + className + "InfoVO.java";
  353. }
  354. if (template.contains("Form.java.vm")) {
  355. return modelPath + File.separator + className + "Form.java";
  356. }
  357. if (template.contains("ListVO.java.vm")) {
  358. return modelPath + File.separator + className + "ListVO.java";
  359. }
  360. if (template.contains("GroupVO.java.vm")) {
  361. return modelPath + File.separator + className + "GroupVO.java";
  362. }
  363. if (template.contains("Pagination.java.vm")) {
  364. return modelPath + File.separator + className + "Pagination.java";
  365. }
  366. if (template.contains("ExcelVO.java.vm")) {
  367. return modelPath + File.separator + className + "ExcelVO.java";
  368. }
  369. if (template.contains("ExcelErrorVO.java.vm")) {
  370. return modelPath + File.separator + className + "ExcelErrorVO.java";
  371. }
  372. if (template.contains("Model.java.vm")) {
  373. return modelPath + File.separator + className + "Model.java";
  374. }
  375. if (template.contains("ListVO.java.vm")) {
  376. return modelPath + File.separator + className + "ListVO.java";
  377. }
  378. if (template.contains("Constant.java.vm")) {
  379. return modelPath + File.separator + className + "Constant.java";
  380. }
  381. if (template.contains("Json.json.vm")) {
  382. return modelPath + File.separator + className + "Json.json";
  383. }
  384. return null;
  385. }
  386. /**
  387. * 生成代码
  388. *
  389. * @param context
  390. * @param template
  391. * @param fileNames
  392. */
  393. public static void velocityWriterFile(VelocityContext context, String template, String fileNames) {
  394. try {
  395. // 渲染模板
  396. @Cleanup StringWriter sw = new StringWriter();
  397. Template tpl = Velocity.getTemplate(template, Constants.UTF_8);
  398. tpl.merge(context, sw);
  399. if (fileNames != null) {
  400. File file = new File(XSSEscape.escapePath(fileNames));
  401. if (!file.exists()) {
  402. file.createNewFile();
  403. }
  404. @Cleanup FileOutputStream fos = new FileOutputStream(file);
  405. IOUtils.write(sw.toString(), fos, Constants.UTF_8);
  406. IOUtils.closeQuietly(sw);
  407. IOUtils.closeQuietly(fos);
  408. }
  409. } catch (IOException e) {
  410. e.printStackTrace();
  411. System.out.println("渲染模板失败,表名:" + e);
  412. }
  413. }
  414. /**
  415. * 获取接口参数配置,templatejson
  416. *
  417. * @param allModels
  418. * @return
  419. */
  420. public static Map<String, Object> getInterTemplateJson(List<FormAllModel> allModels, Map<String, String> childTableKey) {
  421. Map<String, Object> map = new HashMap<>();
  422. for (FormAllModel item : allModels) {
  423. if (FormEnum.mast.getMessage().equals(item.getJnpfKey())) {
  424. FieLdsModel fieLdsModel = BeanUtil.copyProperties(item.getFormColumnModel().getFieLdsModel(), FieLdsModel.class);
  425. if (StringUtil.isNotEmpty(fieLdsModel.getVModel())) {
  426. map.put(fieLdsModel.getVModel(), getTemJsonModel(fieLdsModel, childTableKey));
  427. }
  428. }
  429. if (FormEnum.mastTable.getMessage().equals(item.getJnpfKey())) {
  430. FieLdsModel fieLdsModel = BeanUtil.copyProperties(item.getFormMastTableModel().getMastTable().getFieLdsModel(), FieLdsModel.class);
  431. if (StringUtil.isNotEmpty(fieLdsModel.getVModel())) {
  432. map.put(item.getFormMastTableModel().getVModel(), getTemJsonModel(fieLdsModel, childTableKey));
  433. }
  434. }
  435. if (FormEnum.table.getMessage().equals(item.getJnpfKey())) {
  436. List<FormColumnModel> childList = item.getChildList().getChildList();
  437. for (FormColumnModel columnModel : childList) {
  438. FieLdsModel fieLdsModel = BeanUtil.copyProperties(columnModel.getFieLdsModel(), FieLdsModel.class);
  439. if (StringUtil.isNotEmpty(fieLdsModel.getVModel())) {
  440. HashMap<String, String> childTableMap = new HashMap<>();
  441. childTableMap.put(item.getChildList().getTableModel(), item.getChildList().getAliasLowName());
  442. map.put(item.getChildList().getAliasLowName() + fieLdsModel.getVModel(), getTemJsonModel(fieLdsModel, childTableMap));
  443. }
  444. }
  445. }
  446. }
  447. return map;
  448. }
  449. private static List<TemplateJsonModel> getTemJsonModel(FieLdsModel fieLdsModel, Map<String, String> childTableKey) {
  450. List<TemplateJsonModel> templateJson = fieLdsModel.getConfig().getTemplateJson();
  451. List<TemplateJsonModel> json = templateJson.size() > 0 ? templateJson :
  452. JsonUtil.getJsonToList(fieLdsModel.getTemplateJson(), TemplateJsonModel.class);
  453. for (TemplateJsonModel t : json) {
  454. if (t.getRelationField() != null && t.getRelationField().contains("-") && Objects.equals(t.getSourceType(), 1)) {
  455. String[] split = t.getRelationField().split("-");
  456. t.setRelationField(childTableKey.get(split[0]) + "List-" + split[1]);
  457. }
  458. }
  459. return json;
  460. }
  461. /**
  462. * 获取非系统控件字段
  463. *
  464. * @return
  465. */
  466. public static List<String> getNotSystemFields(List<FormAllModel> mast, List<FormAllModel> mastTable, List<FormAllModel> childTable,
  467. GenerateParamModel generateParamModel) {
  468. List<String> list = new ArrayList<>();
  469. String table = generateParamModel.getTable();
  470. boolean mainTable = generateParamModel.isMainTable();
  471. List<FormColumnModel> fields = new ArrayList<>();
  472. for (FormAllModel fam : mast) {
  473. if (mainTable) {
  474. fields.add(fam.getFormColumnModel());
  475. }
  476. }
  477. for (FormAllModel fam : mastTable) {
  478. if (table.equals(fam.getFormMastTableModel().getTable())) {
  479. fields.add(fam.getFormMastTableModel().getMastTable());
  480. }
  481. }
  482. for (FormAllModel fam : childTable) {
  483. if (table.equals(fam.getChildList().getTableName())) {
  484. fields.addAll(fam.getChildList().getChildList());
  485. }
  486. }
  487. for (FormColumnModel fcm : fields) {
  488. if (!JnpfKeyConsts.getSystemKey().contains(fcm.getFieLdsModel().getConfig().getJnpfKey())
  489. && StringUtil.isNotEmpty(fcm.getFieLdsModel().getFieldAlias())) {
  490. list.add(fcm.getFieLdsModel().getFieldAlias().toUpperCase());
  491. }
  492. }
  493. return list;
  494. }
  495. /**
  496. * 移除对象内的json字符串
  497. *
  498. * @param str
  499. * @return
  500. */
  501. public static String objRemoveJson(String str) {
  502. JSONObject object = JSONObject.parseObject(str);
  503. JSONArray columnList = object.getJSONArray("columnList");
  504. removeJson(columnList);
  505. object.put("columnList", columnList);
  506. JSONArray searchList = object.getJSONArray("searchList");
  507. removeJson(searchList);
  508. object.put("searchList", searchList);
  509. JSONObject ruleList = object.getJSONObject("ruleList");
  510. ruleRemoveJson(ruleList);
  511. object.put("ruleList", ruleList);
  512. JSONObject ruleListApp = object.getJSONObject("ruleListApp");
  513. ruleRemoveJson(ruleListApp);
  514. object.put("ruleListApp", ruleListApp);
  515. JSONArray columnOptions = object.getJSONArray("columnOptions");
  516. removeJson(columnOptions);
  517. object.put("columnOptions", columnOptions);
  518. JSONArray defaultColumnList = object.getJSONArray("defaultColumnList");
  519. removeJson(defaultColumnList);
  520. object.put("defaultColumnList", defaultColumnList);
  521. JSONArray sortList = object.getJSONArray("sortList");
  522. removeJson(sortList);
  523. object.put("sortList", sortList);
  524. JSONArray fields = object.getJSONArray("fields");
  525. removeJson(fields);
  526. object.put("fields", fields);
  527. object.remove("funcs");
  528. return JSONObject.toJSONString(object, SerializerFeature.WriteMapNullValue, SerializerFeature.PrettyFormat);
  529. }
  530. private static void ruleRemoveJson(JSONObject ruleList) {
  531. if (ruleList != null) {
  532. JSONArray conditionList = ruleList.getJSONArray("conditionList");
  533. if (conditionList != null) {
  534. for (Object o : conditionList) {
  535. JSONObject obj = (JSONObject) o;
  536. JSONArray groups = obj.getJSONArray("groups");
  537. removeJson(groups);
  538. obj.put("groups", groups);
  539. }
  540. ruleList.put("conditionList", conditionList);
  541. }
  542. }
  543. }
  544. /**
  545. * 递归移除对应属性
  546. *
  547. * @param jsonArray
  548. */
  549. public static void removeJson(JSONArray jsonArray) {
  550. if (jsonArray == null || jsonArray.size() == 0) {
  551. return;
  552. }
  553. for (int i = 0; i < jsonArray.size(); i++) {
  554. JSONObject jsonObject = (JSONObject) jsonArray.get(i);
  555. String jnpfkey = jsonObject.getJSONObject("__config__").getString("jnpfKey");
  556. List<String> childrenListAll = new ArrayList() {{
  557. add(FormEnum.card.getMessage());
  558. add(FormEnum.row.getMessage());
  559. add(FormEnum.tab.getMessage());
  560. add(FormEnum.collapse.getMessage());
  561. add(FormEnum.collapseItem.getMessage());
  562. add(FormEnum.tabItem.getMessage());
  563. add(FormEnum.tableGrid.getMessage());
  564. add(FormEnum.tableGridTr.getMessage());
  565. add(FormEnum.tableGridTd.getMessage());
  566. add(FormEnum.STEPS.getMessage());
  567. add(FormEnum.STEP_ITEM.getMessage());
  568. }};
  569. if (childrenListAll.contains(jnpfkey) || StringUtil.isEmpty(jnpfkey)) {
  570. JSONObject config = jsonObject.getJSONObject("__config__");
  571. config.remove("on");
  572. JSONArray childArray = config.getJSONArray("children");
  573. removeJson(childArray);
  574. config.put("children", childArray);
  575. jsonObject.put("__config__", config);
  576. } else if (FormEnum.table.getMessage().equals(jnpfkey)) {
  577. JSONObject configA = jsonObject.getJSONObject("__config__");
  578. configA.remove("on");
  579. JSONArray children = configA.getJSONArray("children");
  580. for (int k = 0; k < children.size(); k++) {
  581. JSONObject childrenObject = (JSONObject) children.get(k);
  582. childrenObject.remove("on");
  583. JSONObject config = childrenObject.getJSONObject("__config__");
  584. config.remove("on");
  585. if (!"static".equals(config.get("dataType"))) {
  586. childrenObject.remove("options");
  587. config.remove("options");
  588. }
  589. childrenObject.put("__config__", config);
  590. }
  591. configA.put("children", children);
  592. if (!"static".equals(configA.get("dataType"))) {
  593. jsonObject.remove("options");
  594. configA.remove("options");
  595. }
  596. jsonObject.put("__config__", configA);
  597. }
  598. jsonObject.remove("on");
  599. JSONObject config = jsonObject.getJSONObject("__config__");
  600. config.remove("on");
  601. if (!"static".equals(config.get("dataType"))) {
  602. jsonObject.remove("options");
  603. config.remove("options");
  604. }
  605. jsonObject.put("__config__", config);
  606. }
  607. }
  608. /**
  609. * 移除代码生成不支持的对象
  610. *
  611. * @param obj
  612. * @return
  613. */
  614. public static String delNotSupport(Object obj) {
  615. if (obj instanceof JSONArray) {
  616. List<Object> collect = JsonUtil.getJsonToJsonArray(JsonUtil.getObjectToString(obj)).stream()
  617. .filter(t -> !"calculate".equals(JsonUtil.entityToMap(t).get("jnpfKey"))).collect(Collectors.toList());
  618. return JSONObject.toJSONString(collect, SerializerFeature.WriteMapNullValue, SerializerFeature.PrettyFormat);
  619. }
  620. return JSONObject.toJSONString(obj, SerializerFeature.WriteMapNullValue, SerializerFeature.PrettyFormat);
  621. }
  622. public static String swapRuleFields(SuperJsonModel superJsonModel){
  623. SuperJsonModel model= new SuperJsonModel();
  624. JSONObject obj = JSONObject.parseObject(JsonUtil.getObjectToString(model));
  625. if(superJsonModel!=null){
  626. List<SuperQueryJsonModel> conditionList = superJsonModel.getConditionList();
  627. JSONArray arr = new JSONArray();
  628. if(CollectionUtil.isNotEmpty(conditionList)){
  629. for (SuperQueryJsonModel superQueryJsonModel : conditionList) {
  630. JSONObject item = JSONObject.parseObject(JsonUtil.getObjectToString(superQueryJsonModel));
  631. List<FieLdsModel> groups = superQueryJsonModel.getGroups();
  632. List<GenerField> newGroups = new ArrayList<>();
  633. if(CollectionUtil.isNotEmpty(groups)){
  634. for (FieLdsModel group : groups) {
  635. GenerField generField = JsonUtil.getJsonToBean(group, GenerField.class);
  636. generField.setOptions(null);
  637. generField.setTemplateJson(null);
  638. generField.setProps(null);
  639. GenerConfig config = new GenerConfig();
  640. config.setJnpfKey(group.getConfig().getJnpfKey());
  641. config.setTableName(group.getConfig().getTableName());
  642. config.setRelationTable(group.getConfig().getRelationTable());
  643. generField.setConfig(config);
  644. newGroups.add(generField);
  645. }
  646. }
  647. item.put("groups", newGroups);
  648. arr.add(item);
  649. }
  650. obj.put("conditionList", arr);
  651. }
  652. }
  653. return obj.toJSONString();
  654. }
  655. }