using System.Collections; using System.Text.RegularExpressions; using System.Web; using Common; using Infrastructure; using LitJson; using Microsoft.CodeAnalysis.CSharp.Scripting; using Model; using Services; using Util; namespace Tasks { public class MakeHelper { public readonly static MakeHelper Instance = new MakeHelper(); private MakeHelper() { } public void Start(string QueueName) { RabbitMQClient.Instance.StartReceive(QueueName, content => DoWorks(content)); } public void DoWorks(string content) { if (!string.IsNullOrEmpty(content)) { DoQueue(content); } } public void DoQueue(string content) { try { // Utils.WriteLog(DateTime.Now.ToString() + "\n" + content, "生成接收"); JsonData jsonObj = JsonMapper.ToObject(content); //流水号 string requestId = jsonObj["requestId"].ToString(); //模板文件路径 string modePath = jsonObj["modePath"].ToString(); // if(modePath.Contains("controller") && content.Contains("DoubleAward")) // { // string packageName = "123123123"; // } //附加数据,原样返回 JsonData attach = JsonMapper.ToObject(jsonObj["attach"].ToString()); string versionNo = attach["versionNo"].ToString(); string fileType = attach["fileType"].ToString(); string userId = attach["userId"].ToString(); //读取模板文件内容 // string templateString = Function.ReadInstance("Template/" + modePath + ".json"); var templateService = App.GetRequiredService(); var template = templateService.GetFirst(m => m.templateName == modePath); if(template == null) return; string templateString = template.templateContent; templateString = HttpUtility.UrlDecode(templateString); //解析模板内容 string resultString = Util.Maker.StartMake(jsonObj["data"], templateString); resultString = Util.Maker.replaceKeyToValue(jsonObj["data"], resultString); int serviceId = int.Parse(jsonObj["data"]["projectService"]["id"].ToString()); int projectId = int.Parse(jsonObj["data"]["projectService"]["projectId"].ToString()); int javaVersion = codeVersion(serviceId, versionNo); //生成文件 string path = template.makePath.Substring(0, template.makePath.LastIndexOf("/") + 1); string fileName = template.makePath.Substring(template.makePath.LastIndexOf("/") + 1); path = Util.Maker.replaceKeyToValue(jsonObj["data"], path); fileName = Util.Maker.replaceKeyToValue(jsonObj["data"], fileName); Function.WritePage(path, fileName, resultString); string startPath = Function.getPath(path + fileName); OssHelper.Instance.Upload(startPath); addFile(projectId, serviceId, int.Parse(Function.CheckInt(userId)), versionNo, fileName, path + fileName, path + fileName, fileType, javaVersion, "java"); //生成数据入库 int dataId = int.Parse(Function.CheckInt(attach["dataId"].ToString())); string makeFileType = attach["fileType"].ToString(); var makeDataService = App.GetRequiredService(); var item = makeDataService.GetFirst(m => m.dataId == dataId && m.makeFileType == makeFileType); if(item == null) { makeDataService.Add(new MakeData() { dataId = dataId, makeFileName = fileName, makeFilePath = path, makeFileType = makeFileType, createTime = DateTime.Now, createBy = attach["createBy"].ToString() }); } else { item.makeFileName = fileName; item.makeFilePath = path; item.updateTime = DateTime.Now; item.updateBy = attach["createBy"].ToString(); makeDataService.Update(item); } } catch (Exception ex) { Utils.WriteLog(DateTime.Now.ToString() + "\n" + ex, "生成异常"); } //MQ回调解析的结果 // RabbitMQClient.Instance.Push("MakeSqlCallBackQueue", requestId + "#cut#" + attach + "#cut#" + resultString); } //生成代码版本 public int codeVersion(int DatabaseId, string versionNo) { int codeVersion = RedisServer.Cache.Get("codeVersion:" + DatabaseId + ":" + versionNo); if(codeVersion > 0) { return codeVersion; } var versionForProjectService = App.GetRequiredService(); VersionForProject pro = versionForProjectService.GetFirst(m => m.databaseId == DatabaseId) ?? versionForProjectService.InsertReturnEntity(new VersionForProject() { databaseId = DatabaseId, }); pro.javaVersion += 1; versionForProjectService.Update(pro); RedisServer.Cache.Set("codeVersion:" + DatabaseId + ":" + versionNo, pro.javaVersion, TimeSpan.FromMinutes(5)); return pro.javaVersion; } //生成代码版本-添加文件 public void addFile(int projectId, int serverId, int groupId, string versionNo, string fileName, string downloadPath, string localPath, string fileType = "", int codeVersion = 0, string language = "") { var versionsService = App.GetRequiredService(); var makeFilesService = App.GetRequiredService(); var filesForAllService = App.GetRequiredService(); Versions version = versionsService.GetFirst(m => m.projectId == projectId && m.serverId == serverId && m.groupId == groupId && m.versionNo == versionNo) ?? versionsService.InsertReturnEntity(new Versions() { createTime = DateTime.Now, projectId = projectId, serverId = serverId, versionNo = versionNo, groupId = groupId, }); string[] PathList = localPath.TrimStart('/').Split('/'); int ParentFileId = 0; int AllParentFileId = 0; string DirPath = ""; for (int i = 0; i < PathList.Length - 1; i++) { string Path = PathList[i]; DirPath += PathList[i] + "/"; MakeFiles filePath = makeFilesService.GetFirst(m => m.projectId == projectId && (m.versionId == version.versionId || m.versionId == 0) && m.localPath == DirPath) ?? makeFilesService.InsertReturnEntity(new MakeFiles() { createTime = DateTime.Now, projectId = projectId, versionId = version.versionId, fileName = Path, parentFileId = ParentFileId, fileType = "path", localPath = DirPath, language = language, }); filePath.fileVersionNo = codeVersion; ParentFileId = filePath.fileId; FilesForAll fileAllPath = filesForAllService.GetFirst(m => m.projectId == projectId && m.localPath == DirPath); if(fileAllPath == null) { fileAllPath = filesForAllService.InsertReturnEntity(new FilesForAll() { createTime = DateTime.Now, projectId = projectId, fileName = Path, parentFileId = AllParentFileId, fileType = "path", localPath = DirPath, }); } else { fileAllPath.versionNo = version.versionId; filesForAllService.Update(fileAllPath); } AllParentFileId = fileAllPath.fileId; } 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() { createTime = DateTime.Now, projectId = projectId, versionId = version.versionId, fileName = fileName, fileType = fileType, localPath = localPath, parentFileId = ParentFileId, language = language, }); file.downloadPath = downloadPath; file.fileVersionNo = codeVersion; FilesForAll fileAll = filesForAllService.GetFirst(m => m.projectId == projectId && m.localPath == localPath && m.fileName == fileName) ?? filesForAllService.InsertReturnEntity(new FilesForAll() { createTime = DateTime.Now, projectId = projectId, fileName = fileName, fileType = fileType, localPath = localPath, parentFileId = ParentFileId, }); fileAll.downloadPath = downloadPath; fileAll.versionNo = codeVersion; filesForAllService.Update(fileAll); var files = makeFilesService.GetList(m => m.projectId == projectId && m.language == language && m.versionId == 0); foreach(MakeFiles sub in files) { sub.versionId = version.versionId; makeFilesService.Update(sub); } } } }