MakeHelper.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. using System.Collections;
  2. using System.Text.RegularExpressions;
  3. using System.Web;
  4. using Common;
  5. using Infrastructure;
  6. using LitJson;
  7. using Microsoft.CodeAnalysis.CSharp.Scripting;
  8. using Model;
  9. using Services;
  10. using Util;
  11. namespace Tasks
  12. {
  13. public class MakeHelper
  14. {
  15. public readonly static MakeHelper Instance = new MakeHelper();
  16. private MakeHelper()
  17. { }
  18. public void Start(string QueueName)
  19. {
  20. RabbitMQClient.Instance.StartReceive(QueueName, content => DoWorks(content));
  21. }
  22. public void DoWorks(string content)
  23. {
  24. if (!string.IsNullOrEmpty(content))
  25. {
  26. DoQueue(content);
  27. }
  28. }
  29. public void DoQueue(string content)
  30. {
  31. try
  32. {
  33. // Utils.WriteLog(DateTime.Now.ToString() + "\n" + content, "生成接收");
  34. JsonData jsonObj = JsonMapper.ToObject(content);
  35. //流水号
  36. string requestId = jsonObj["requestId"].ToString();
  37. //模板文件路径
  38. string modePath = jsonObj["modePath"].ToString();
  39. // if(modePath.Contains("controller") && content.Contains("DoubleAward"))
  40. // {
  41. // string packageName = "123123123";
  42. // }
  43. //附加数据,原样返回
  44. JsonData attach = JsonMapper.ToObject(jsonObj["attach"].ToString());
  45. string versionNo = attach["versionNo"].ToString();
  46. string fileType = attach["fileType"].ToString();
  47. string userId = attach["userId"].ToString();
  48. //读取模板文件内容
  49. // string templateString = Function.ReadInstance("Template/" + modePath + ".json");
  50. var templateService = App.GetRequiredService<IMakeTemplateService>();
  51. var template = templateService.GetFirst(m => m.templateName == modePath);
  52. if(template == null) return;
  53. string templateString = template.templateContent;
  54. templateString = HttpUtility.UrlDecode(templateString);
  55. //解析模板内容
  56. string resultString = Util.Maker.StartMake(jsonObj["data"], templateString);
  57. resultString = Util.Maker.replaceKeyToValue(jsonObj["data"], resultString);
  58. int serviceId = 0;
  59. if(content.Contains("\"projectService\""))
  60. {
  61. serviceId = int.Parse(jsonObj["data"]["projectService"]["id"].ToString());
  62. }
  63. int projectId = 0;
  64. if(content.Contains("\"projectService\""))
  65. {
  66. projectId = int.Parse(jsonObj["data"]["projectService"]["projectId"].ToString());
  67. }
  68. int javaVersion = codeVersion(serviceId, versionNo);
  69. //生成文件
  70. string path = template.makePath.Substring(0, template.makePath.LastIndexOf("/") + 1);
  71. string fileName = template.makePath.Substring(template.makePath.LastIndexOf("/") + 1);
  72. path = Util.Maker.replaceKeyToValue(jsonObj["data"], path);
  73. fileName = Util.Maker.replaceKeyToValue(jsonObj["data"], fileName);
  74. Function.WritePage(path, fileName, resultString);
  75. string startPath = Function.getPath(path + fileName);
  76. OssHelper.Instance.Upload(startPath);
  77. if(serviceId > 0 && projectId > 0)
  78. {
  79. addFile(projectId, serviceId, int.Parse(Function.CheckInt(userId)), versionNo, fileName, path + fileName, path + fileName, fileType, javaVersion, "java");
  80. }
  81. //生成数据入库
  82. int dataId = int.Parse(Function.CheckInt(attach["dataId"].ToString()));
  83. string makeFileType = attach["fileType"].ToString();
  84. var makeDataService = App.GetRequiredService<IMakeDataService>();
  85. var item = makeDataService.GetFirst(m => m.dataId == dataId && m.makeFileType == makeFileType);
  86. if(item == null)
  87. {
  88. makeDataService.Add(new MakeData()
  89. {
  90. dataId = dataId,
  91. makeFileName = fileName,
  92. makeFilePath = path,
  93. makeFileType = makeFileType,
  94. createTime = DateTime.Now,
  95. createBy = attach["createBy"].ToString()
  96. });
  97. }
  98. else
  99. {
  100. item.makeFileName = fileName;
  101. item.makeFilePath = path;
  102. item.updateTime = DateTime.Now;
  103. item.updateBy = attach["createBy"].ToString();
  104. makeDataService.Update(item);
  105. }
  106. }
  107. catch (Exception ex)
  108. {
  109. Utils.WriteLog(DateTime.Now.ToString() + "\n" + ex, "生成异常");
  110. }
  111. //MQ回调解析的结果
  112. // RabbitMQClient.Instance.Push("MakeSqlCallBackQueue", requestId + "#cut#" + attach + "#cut#" + resultString);
  113. }
  114. //生成代码版本
  115. public int codeVersion(int DatabaseId, string versionNo)
  116. {
  117. int codeVersion = RedisServer.Cache.Get<int>("codeVersion:" + DatabaseId + ":" + versionNo);
  118. if(codeVersion > 0)
  119. {
  120. return codeVersion;
  121. }
  122. var versionForProjectService = App.GetRequiredService<IVersionForProjectService>();
  123. VersionForProject pro = versionForProjectService.GetFirst(m => m.databaseId == DatabaseId) ?? versionForProjectService.InsertReturnEntity(new VersionForProject()
  124. {
  125. databaseId = DatabaseId,
  126. });
  127. pro.javaVersion += 1;
  128. versionForProjectService.Update(pro);
  129. RedisServer.Cache.Set("codeVersion:" + DatabaseId + ":" + versionNo, pro.javaVersion, TimeSpan.FromMinutes(5));
  130. return pro.javaVersion;
  131. }
  132. //生成代码版本-添加文件
  133. public void addFile(int projectId, int serverId, int groupId, string versionNo, string fileName, string downloadPath, string localPath, string fileType = "", int codeVersion = 0, string language = "")
  134. {
  135. var versionsService = App.GetRequiredService<IVersionsService>();
  136. var makeFilesService = App.GetRequiredService<IMakeFilesService>();
  137. var filesForAllService = App.GetRequiredService<IFilesForAllService>();
  138. Versions version = versionsService.GetFirst(m => m.projectId == projectId && m.serverId == serverId && m.groupId == groupId && m.versionNo == versionNo) ?? versionsService.InsertReturnEntity(new Versions()
  139. {
  140. createTime = DateTime.Now,
  141. projectId = projectId,
  142. serverId = serverId,
  143. versionNo = versionNo,
  144. groupId = groupId,
  145. });
  146. string[] PathList = localPath.TrimStart('/').Split('/');
  147. int ParentFileId = 0;
  148. int AllParentFileId = 0;
  149. string DirPath = "";
  150. for (int i = 0; i < PathList.Length - 1; i++)
  151. {
  152. string Path = PathList[i];
  153. DirPath += PathList[i] + "/";
  154. MakeFiles filePath = makeFilesService.GetFirst(m => m.projectId == projectId && (m.versionId == version.versionId || m.versionId == 0) && m.localPath == DirPath) ?? makeFilesService.InsertReturnEntity(new MakeFiles()
  155. {
  156. createTime = DateTime.Now,
  157. projectId = projectId,
  158. versionId = version.versionId,
  159. fileName = Path,
  160. parentFileId = ParentFileId,
  161. fileType = "path",
  162. localPath = DirPath,
  163. language = language,
  164. });
  165. filePath.fileVersionNo = codeVersion;
  166. makeFilesService.Update(filePath);
  167. ParentFileId = filePath.fileId;
  168. FilesForAll fileAllPath = filesForAllService.GetFirst(m => m.projectId == projectId && m.localPath == DirPath);
  169. if(fileAllPath == null)
  170. {
  171. fileAllPath = filesForAllService.InsertReturnEntity(new FilesForAll()
  172. {
  173. createTime = DateTime.Now,
  174. projectId = projectId,
  175. fileName = Path,
  176. parentFileId = AllParentFileId,
  177. fileType = "path",
  178. localPath = DirPath,
  179. });
  180. }
  181. else
  182. {
  183. fileAllPath.versionNo = version.versionId;
  184. filesForAllService.Update(fileAllPath);
  185. }
  186. AllParentFileId = fileAllPath.fileId;
  187. }
  188. MakeFiles file = makeFilesService.GetFirst(m => m.projectId == projectId && (m.versionId == version.versionId || m.versionId == 0) && m.localPath == localPath && m.fileName == fileName) ?? makeFilesService.InsertReturnEntity(new MakeFiles()
  189. {
  190. createTime = DateTime.Now,
  191. projectId = projectId,
  192. versionId = version.versionId,
  193. fileName = fileName,
  194. fileType = fileType,
  195. localPath = localPath,
  196. parentFileId = ParentFileId,
  197. language = language,
  198. });
  199. file.downloadPath = downloadPath;
  200. file.fileVersionNo = codeVersion;
  201. makeFilesService.Update(file);
  202. FilesForAll fileAll = filesForAllService.GetFirst(m => m.projectId == projectId && m.localPath == localPath && m.fileName == fileName) ?? filesForAllService.InsertReturnEntity(new FilesForAll()
  203. {
  204. createTime = DateTime.Now,
  205. projectId = projectId,
  206. fileName = fileName,
  207. fileType = fileType,
  208. localPath = localPath,
  209. parentFileId = ParentFileId,
  210. });
  211. fileAll.downloadPath = downloadPath;
  212. fileAll.versionNo = codeVersion;
  213. filesForAllService.Update(fileAll);
  214. var files = makeFilesService.GetList(m => m.projectId == projectId && m.language == language && m.versionId == 0);
  215. foreach(MakeFiles sub in files)
  216. {
  217. sub.versionId = version.versionId;
  218. makeFilesService.Update(sub);
  219. }
  220. }
  221. }
  222. }