VisualdevGenServiceImpl.java 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. package jnpf.generater.service.impl;
  2. import com.google.common.base.CaseFormat;
  3. import jnpf.base.UserInfo;
  4. import jnpf.base.entity.VisualdevEntity;
  5. import jnpf.base.mapper.VisualdevMapper;
  6. import jnpf.base.model.DownloadCodeForm;
  7. import jnpf.base.model.VisualWebTypeEnum;
  8. import jnpf.base.service.DbLinkService;
  9. import jnpf.base.service.SuperServiceImpl;
  10. import jnpf.base.service.SysconfigService;
  11. import jnpf.base.service.VisualAliasService;
  12. import jnpf.base.util.VisualUtils;
  13. import jnpf.base.util.common.AliasModel;
  14. import jnpf.base.util.common.GenerateCommon;
  15. import jnpf.base.util.common.GenerateParamModel;
  16. import jnpf.base.util.custom.VelocityEnum;
  17. import jnpf.config.ConfigValueUtil;
  18. import jnpf.constant.FileTypeConstant;
  19. import jnpf.constant.GenerateConstant;
  20. import jnpf.database.model.entity.DbLinkEntity;
  21. import jnpf.database.util.DataSourceUtil;
  22. import jnpf.entity.FileParameter;
  23. import jnpf.generater.factory.CodeGenerateFactoryV3;
  24. import jnpf.generater.model.FormDesign.TemplateMethodEnum;
  25. import jnpf.generater.service.VisualdevGenService;
  26. import jnpf.model.FileListVO;
  27. import jnpf.model.OnlineDevData;
  28. import jnpf.model.visualJson.TableModel;
  29. import jnpf.util.*;
  30. import org.apache.velocity.app.Velocity;
  31. import org.springframework.beans.factory.annotation.Autowired;
  32. import org.springframework.stereotype.Service;
  33. import java.io.File;
  34. import java.util.*;
  35. /**
  36. * 可视化开发功能表
  37. *
  38. * @author JNPF开发平台组
  39. * @version V3.1.0
  40. * @copyright 引迈信息技术有限公司(https://www.jnpfsoft.com)
  41. * @date 2021-04-02
  42. */
  43. @Service
  44. public class VisualdevGenServiceImpl extends SuperServiceImpl<VisualdevMapper, VisualdevEntity> implements VisualdevGenService {
  45. @Autowired
  46. private DataSourceUtil dataSourceUtil;
  47. @Autowired
  48. private DbLinkService dataSourceApi;
  49. @Autowired
  50. private ConfigValueUtil configValueUtil;
  51. @Autowired
  52. private CodeGenerateFactoryV3 GenerateFactoryV3;
  53. @Autowired
  54. private SysconfigService sysconfigService;
  55. @Autowired
  56. private VisualAliasService aliasService;
  57. // 模板修改时间缓存
  58. private Map<String, String> templateCache = new HashMap<>();
  59. /**
  60. * vue3代码生成
  61. *
  62. * @param entity 可视化开发功能
  63. * @param downloadCodeForm 下载相关信息
  64. * @return
  65. * @throws Exception
  66. */
  67. @Override
  68. public String codeGengerateV3(VisualdevEntity entity, DownloadCodeForm downloadCodeForm) throws Exception {
  69. UserInfo userInfo = UserProvider.getUser();
  70. DbLinkEntity linkEntity = null;
  71. if (entity != null) {
  72. String localBasePath = GenerateCommon.getLocalBasePath() + configValueUtil.getTemplateCodePathVue3();
  73. //初始化模板
  74. Velocity.reset();
  75. VelocityEnum.init.initVelocity(localBasePath);
  76. //视图代码生成独立逻辑
  77. if(VisualWebTypeEnum.DATA_VIEW.getType().equals(entity.getWebType())){
  78. String fileName = entity.getFullName() + "_" + DateUtil.nowDateTime();
  79. String mainClass = "St" + entity.getEnCode();
  80. downloadCodeForm.setMainClassName(mainClass);
  81. //执行代码生成器
  82. GenerateParamModel generateParamModel = GenerateParamModel.builder()
  83. .path(localBasePath)
  84. .fileName(fileName)
  85. .downloadCodeForm(downloadCodeForm)
  86. .entity(entity)
  87. .userInfo(userInfo)
  88. .configValueUtil(configValueUtil)
  89. .className(mainClass)
  90. .templatesPath(TemplateMethodEnum.T6.getMethod())
  91. .template7Model(GenerateCommon.getTemplate7Model(sysconfigService.getList(GenerateConstant.SYSCONFIG)))
  92. .build();
  93. GenerateFactoryV3.runGenerator(TemplateMethodEnum.T6.getMethod(), generateParamModel);
  94. return fileName;
  95. }
  96. // 是否存在关联数据库
  97. if (StringUtil.isNotEmpty(entity.getDbLinkId())) {
  98. linkEntity = dataSourceApi.getInfo(entity.getDbLinkId());
  99. }
  100. // 是否存在关联表
  101. if (StringUtil.isNotEmpty(entity.getVisualTables())) {
  102. String fileName = entity.getFullName() + "_" + DateUtil.nowDateTime();
  103. List<TableModel> list = JsonUtil.getJsonToList(entity.getVisualTables(), TableModel.class);
  104. Map<String, AliasModel> tableAliasMap = aliasService.getAllFiledsAlias(entity.getId());
  105. //获取主表
  106. String mainTable = list.stream().filter(t -> "1".equals(t.getTypeId())).findFirst().orElse(null).getTable();
  107. Map<String, String> fieldsMap = tableAliasMap.get(mainTable).getFieldsMap();
  108. //获取主键
  109. String pKeyName = VisualUtils.getpKey(linkEntity, mainTable);
  110. //自定义包名
  111. String modulePackageName = StringUtil.isNotEmpty(downloadCodeForm.getModulePackageName()) ? downloadCodeForm.getModulePackageName() :
  112. GenerateConstant.PACKAGE_NAME;
  113. downloadCodeForm.setModulePackageName(modulePackageName);
  114. downloadCodeForm.setMainClassName(tableAliasMap.get(mainTable).getAliasName());
  115. //获取其他子表的主键
  116. Map<String, Object> childpKeyMap = new HashMap<>(16);
  117. for (TableModel tableModel : list) {
  118. String childKey = VisualUtils.getpKey(linkEntity, tableModel.getTable());
  119. if (childKey.length() > 2) {
  120. if ("f_".equals(childKey.substring(0, 2).toLowerCase())) {
  121. childKey = childKey.substring(2);
  122. }
  123. }
  124. childpKeyMap.put(tableModel.getTable(), childKey);
  125. }
  126. String templatesPath = null;
  127. //功能表单
  128. if (OnlineDevData.FORM_TYPE_DEV.equals(entity.getType())) {
  129. switch (entity.getWebType()) {
  130. case 1:
  131. templatesPath = downloadCodeForm.getEnableFlow() == 1 ? TemplateMethodEnum.T5.getMethod() : TemplateMethodEnum.T4.getMethod() ;
  132. break;
  133. case 2:
  134. templatesPath = downloadCodeForm.getEnableFlow() == 1 ? TemplateMethodEnum.T3.getMethod() : TemplateMethodEnum.T2.getMethod() ;
  135. break;
  136. default:
  137. break;
  138. }
  139. }
  140. //非本地模板需要下载-获取模板如下
  141. if (!FileUploadUtils.getDefaultPlatform().startsWith(GenerateConstant.LOCAL)) {
  142. List<FileListVO> fileList = new ArrayList<>();
  143. fileList.addAll(FileUploadUtils.getFileList(new FileParameter().setRemotePath(FileTypeConstant.TEMPLATECODEPATHV3).setRecursive(true)));
  144. for (FileListVO fileListVO : fileList) {
  145. String fName = fileListVO.getFileName();
  146. String fPath = fileListVO.getFilePath();
  147. String cacheKey = fPath + fName;
  148. File localFile = new File(localBasePath + fPath + fName);
  149. String lastTime = templateCache.get(cacheKey);
  150. if(localFile.exists() && Objects.equals(lastTime, fileListVO.getFileTime())){
  151. continue;
  152. }
  153. templateCache.put(cacheKey, fileListVO.getFileTime());
  154. FileUploadUtils.downloadFileToLocal(new FileParameter(fPath, fName));
  155. }
  156. }
  157. //执行代码生成器
  158. GenerateParamModel generateParamModel = GenerateParamModel.builder()
  159. .dataSourceUtil(dataSourceUtil)
  160. .path(localBasePath)
  161. .fileName(fileName)
  162. .templatesPath(templatesPath)
  163. .downloadCodeForm(downloadCodeForm)
  164. .entity(entity)
  165. .userInfo(userInfo)
  166. .configValueUtil(configValueUtil)
  167. .linkEntity(linkEntity)
  168. .pKeyNameOriginal(pKeyName)
  169. .pKeyName(fieldsMap.get(pKeyName))
  170. .template7Model(GenerateCommon.getTemplate7Model(sysconfigService.getList(GenerateConstant.SYSCONFIG)))
  171. .tableAliseMap(tableAliasMap)
  172. .build();
  173. GenerateFactoryV3.runGenerator(templatesPath, generateParamModel);
  174. return fileName;
  175. }
  176. }
  177. return null;
  178. }
  179. }