MakeHelper.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  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. RabbitMQClient.Instance.Push("MakeCallBackQueue", content + "#cut#" + resultString);
  107. }
  108. catch (Exception ex)
  109. {
  110. Utils.WriteLog(DateTime.Now.ToString() + "\n" + ex, "生成异常");
  111. }
  112. //MQ回调解析的结果
  113. // RabbitMQClient.Instance.Push("MakeSqlCallBackQueue", requestId + "#cut#" + attach + "#cut#" + resultString);
  114. }
  115. //生成代码版本
  116. public int codeVersion(int DatabaseId, string versionNo)
  117. {
  118. int codeVersion = RedisServer.Cache.Get<int>("codeVersion:" + DatabaseId + ":" + versionNo);
  119. if(codeVersion > 0)
  120. {
  121. return codeVersion;
  122. }
  123. var versionForProjectService = App.GetRequiredService<IVersionForProjectService>();
  124. VersionForProject pro = versionForProjectService.GetFirst(m => m.databaseId == DatabaseId) ?? versionForProjectService.InsertReturnEntity(new VersionForProject()
  125. {
  126. databaseId = DatabaseId,
  127. });
  128. pro.javaVersion += 1;
  129. versionForProjectService.Update(pro);
  130. RedisServer.Cache.Set("codeVersion:" + DatabaseId + ":" + versionNo, pro.javaVersion, TimeSpan.FromMinutes(5));
  131. return pro.javaVersion;
  132. }
  133. //生成代码版本-添加文件
  134. public void addFile(int projectId, int serverId, int groupId, string versionNo, string fileName, string downloadPath, string localPath, string fileType = "", int codeVersion = 0, string language = "")
  135. {
  136. var versionsService = App.GetRequiredService<IVersionsService>();
  137. var makeFilesService = App.GetRequiredService<IMakeFilesService>();
  138. var filesForAllService = App.GetRequiredService<IFilesForAllService>();
  139. Versions version = versionsService.GetFirst(m => m.projectId == projectId && m.serverId == serverId && m.groupId == groupId && m.versionNo == versionNo) ?? versionsService.InsertReturnEntity(new Versions()
  140. {
  141. createTime = DateTime.Now,
  142. projectId = projectId,
  143. serverId = serverId,
  144. versionNo = versionNo,
  145. groupId = groupId,
  146. });
  147. string[] PathList = localPath.TrimStart('/').Split('/');
  148. int ParentFileId = 0;
  149. int AllParentFileId = 0;
  150. string DirPath = "";
  151. for (int i = 0; i < PathList.Length - 1; i++)
  152. {
  153. string Path = PathList[i];
  154. DirPath += PathList[i] + "/";
  155. MakeFiles filePath = makeFilesService.GetFirst(m => m.projectId == projectId && (m.versionId == version.versionId || m.versionId == 0) && m.localPath == DirPath) ?? makeFilesService.InsertReturnEntity(new MakeFiles()
  156. {
  157. createTime = DateTime.Now,
  158. projectId = projectId,
  159. versionId = version.versionId,
  160. fileName = Path,
  161. parentFileId = ParentFileId,
  162. fileType = "path",
  163. localPath = DirPath,
  164. language = language,
  165. });
  166. filePath.fileVersionNo = codeVersion;
  167. makeFilesService.Update(filePath);
  168. ParentFileId = filePath.fileId;
  169. FilesForAll fileAllPath = filesForAllService.GetFirst(m => m.projectId == projectId && m.localPath == DirPath);
  170. if(fileAllPath == null)
  171. {
  172. fileAllPath = filesForAllService.InsertReturnEntity(new FilesForAll()
  173. {
  174. createTime = DateTime.Now,
  175. projectId = projectId,
  176. fileName = Path,
  177. parentFileId = AllParentFileId,
  178. fileType = "path",
  179. localPath = DirPath,
  180. });
  181. }
  182. else
  183. {
  184. fileAllPath.versionNo = version.versionId;
  185. filesForAllService.Update(fileAllPath);
  186. }
  187. AllParentFileId = fileAllPath.fileId;
  188. }
  189. 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()
  190. {
  191. createTime = DateTime.Now,
  192. projectId = projectId,
  193. versionId = version.versionId,
  194. fileName = fileName,
  195. fileType = fileType,
  196. localPath = localPath,
  197. parentFileId = ParentFileId,
  198. language = language,
  199. });
  200. file.downloadPath = downloadPath;
  201. file.fileVersionNo = codeVersion;
  202. makeFilesService.Update(file);
  203. FilesForAll fileAll = filesForAllService.GetFirst(m => m.projectId == projectId && m.localPath == localPath && m.fileName == fileName) ?? filesForAllService.InsertReturnEntity(new FilesForAll()
  204. {
  205. createTime = DateTime.Now,
  206. projectId = projectId,
  207. fileName = fileName,
  208. fileType = fileType,
  209. localPath = localPath,
  210. parentFileId = ParentFileId,
  211. });
  212. fileAll.downloadPath = downloadPath;
  213. fileAll.versionNo = codeVersion;
  214. filesForAllService.Update(fileAll);
  215. var files = makeFilesService.GetList(m => m.projectId == projectId && m.language == language && m.versionId == 0);
  216. foreach(MakeFiles sub in files)
  217. {
  218. sub.versionId = version.versionId;
  219. makeFilesService.Update(sub);
  220. }
  221. }
  222. }
  223. }