MakeHelper.cs 11 KB

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