MakeHelper.cs 10 KB

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