MakeHelper.cs 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  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 = jsonObj["attach"];
  45. string versionNo = attach["versionNo"].ToString();
  46. string fileType = attach["fileType"].ToString();
  47. //读取模板文件内容
  48. // string templateString = Function.ReadInstance("Template/" + modePath + ".json");
  49. var templateService = App.GetRequiredService<IMakeTemplateService>();
  50. var template = templateService.GetFirst(m => m.templateName == modePath);
  51. if(template == null) return;
  52. string templateString = template.templateContent;
  53. templateString = HttpUtility.UrlDecode(templateString);
  54. //解析模板内容
  55. string resultString = Util.Maker.StartMake(jsonObj["data"], templateString);
  56. resultString = Util.Maker.replaceKeyToValue(jsonObj["data"], resultString);
  57. int serviceId = int.Parse(jsonObj["data"]["projectService"]["id"].ToString());
  58. int projectId = int.Parse(jsonObj["data"]["projectService"]["projectId"].ToString());
  59. int javaVersion = codeVersion(serviceId, versionNo);
  60. //生成文件
  61. string path = template.makePath.Substring(0, template.makePath.LastIndexOf("/") + 1);
  62. string fileName = template.makePath.Substring(template.makePath.LastIndexOf("/") + 1);
  63. path = Util.Maker.replaceKeyToValue(jsonObj["data"], path);
  64. fileName = Util.Maker.replaceKeyToValue(jsonObj["data"], fileName);
  65. Function.WritePage(path, fileName, resultString);
  66. string startPath = Function.getPath(path + fileName);
  67. OssHelper.Instance.Upload(startPath);
  68. addFile(projectId, serviceId, 0, versionNo, fileName, path + fileName, path + fileName, fileType, javaVersion, "java");
  69. //生成数据入库
  70. int dataId = int.Parse(Function.CheckInt(attach["dataId"].ToString()));
  71. string makeFileType = attach["fileType"].ToString();
  72. var makeDataService = App.GetRequiredService<IMakeDataService>();
  73. var item = makeDataService.GetFirst(m => m.dataId == dataId && m.makeFileType == makeFileType);
  74. if(item == null)
  75. {
  76. makeDataService.Add(new MakeData()
  77. {
  78. dataId = dataId,
  79. makeFileName = fileName,
  80. makeFilePath = path,
  81. makeFileType = makeFileType,
  82. createTime = DateTime.Now,
  83. createBy = attach["createBy"].ToString()
  84. });
  85. }
  86. else
  87. {
  88. item.makeFileName = fileName;
  89. item.makeFilePath = path;
  90. item.updateTime = DateTime.Now;
  91. item.updateBy = attach["createBy"].ToString();
  92. makeDataService.Update(item);
  93. }
  94. }
  95. catch (Exception ex)
  96. {
  97. Utils.WriteLog(DateTime.Now.ToString() + "\n" + ex, "生成异常");
  98. }
  99. //MQ回调解析的结果
  100. // RabbitMQClient.Instance.Push("MakeSqlCallBackQueue", requestId + "#cut#" + attach + "#cut#" + resultString);
  101. }
  102. //生成代码版本
  103. public int codeVersion(int DatabaseId, string versionNo)
  104. {
  105. int codeVersion = RedisServer.Cache.Get<int>("codeVersion:" + DatabaseId + ":" + versionNo);
  106. if(codeVersion > 0)
  107. {
  108. return codeVersion;
  109. }
  110. var versionForProjectService = App.GetRequiredService<IVersionForProjectService>();
  111. VersionForProject pro = versionForProjectService.GetFirst(m => m.databaseId == DatabaseId) ?? versionForProjectService.InsertReturnEntity(new VersionForProject()
  112. {
  113. databaseId = DatabaseId,
  114. });
  115. pro.javaVersion += 1;
  116. versionForProjectService.Update(pro);
  117. RedisServer.Cache.Set("codeVersion:" + DatabaseId + ":" + versionNo, pro.javaVersion, TimeSpan.FromMinutes(5));
  118. return pro.javaVersion;
  119. }
  120. //生成代码版本-添加文件
  121. public void addFile(int projectId, int serverId, int groupId, string versionNo, string fileName, string downloadPath, string localPath, string fileType = "", int codeVersion = 0, string language = "")
  122. {
  123. var versionsService = App.GetRequiredService<IVersionsService>();
  124. var makeFilesService = App.GetRequiredService<IMakeFilesService>();
  125. var filesForAllService = App.GetRequiredService<IFilesForAllService>();
  126. Versions version = versionsService.GetFirst(m => m.projectId == projectId && m.serverId == serverId && m.groupId == groupId && m.versionNo == versionNo) ?? versionsService.InsertReturnEntity(new Versions()
  127. {
  128. createTime = DateTime.Now,
  129. projectId = projectId,
  130. serverId = serverId,
  131. versionNo = versionNo,
  132. groupId = groupId,
  133. });
  134. string[] PathList = localPath.TrimStart('/').Split('/');
  135. int ParentFileId = 0;
  136. int AllParentFileId = 0;
  137. string DirPath = "";
  138. for (int i = 0; i < PathList.Length - 1; i++)
  139. {
  140. string Path = PathList[i];
  141. DirPath += PathList[i] + "/";
  142. MakeFiles filePath = makeFilesService.GetFirst(m => m.projectId == projectId && (m.versionId == version.versionId || m.versionId == 0) && m.localPath == DirPath) ?? makeFilesService.InsertReturnEntity(new MakeFiles()
  143. {
  144. createTime = DateTime.Now,
  145. projectId = projectId,
  146. versionId = version.versionId,
  147. fileName = Path,
  148. parentFileId = ParentFileId,
  149. fileType = "path",
  150. localPath = DirPath,
  151. language = language,
  152. });
  153. filePath.fileVersionNo = codeVersion;
  154. ParentFileId = filePath.fileId;
  155. FilesForAll fileAllPath = filesForAllService.GetFirst(m => m.projectId == projectId && m.localPath == DirPath);
  156. if(fileAllPath == null)
  157. {
  158. fileAllPath = filesForAllService.InsertReturnEntity(new FilesForAll()
  159. {
  160. createTime = DateTime.Now,
  161. projectId = projectId,
  162. fileName = Path,
  163. parentFileId = AllParentFileId,
  164. fileType = "path",
  165. localPath = DirPath,
  166. });
  167. }
  168. else
  169. {
  170. fileAllPath.versionNo = version.versionId;
  171. filesForAllService.Update(fileAllPath);
  172. }
  173. AllParentFileId = fileAllPath.fileId;
  174. }
  175. 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()
  176. {
  177. createTime = DateTime.Now,
  178. projectId = projectId,
  179. versionId = version.versionId,
  180. fileName = fileName,
  181. fileType = fileType,
  182. localPath = localPath,
  183. parentFileId = ParentFileId,
  184. language = language,
  185. });
  186. file.downloadPath = downloadPath;
  187. file.fileVersionNo = codeVersion;
  188. FilesForAll fileAll = filesForAllService.GetFirst(m => m.projectId == projectId && m.localPath == localPath && m.fileName == fileName) ?? filesForAllService.InsertReturnEntity(new FilesForAll()
  189. {
  190. createTime = DateTime.Now,
  191. projectId = projectId,
  192. fileName = fileName,
  193. fileType = fileType,
  194. localPath = localPath,
  195. parentFileId = ParentFileId,
  196. });
  197. fileAll.downloadPath = downloadPath;
  198. fileAll.versionNo = codeVersion;
  199. filesForAllService.Update(fileAll);
  200. var files = makeFilesService.GetList(m => m.projectId == projectId && m.language == language && m.versionId == 0);
  201. foreach(MakeFiles sub in files)
  202. {
  203. sub.versionId = version.versionId;
  204. makeFilesService.Update(sub);
  205. }
  206. }
  207. }
  208. }