| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240 |
- 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 string DoQueue(string content, bool push = true)
- {
- 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("serviceImpl") && content.Contains("HtTest"))
- {
- 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<IMakeTemplateService>();
- 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 = 0;
- if(content.Contains("\"projectService\""))
- {
- serviceId = int.Parse(jsonObj["data"]["projectService"]["id"].ToString());
- }
- int projectId = 0;
- if(content.Contains("\"projectService\""))
- {
- 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);
- if(serviceId > 0 && projectId > 0)
- {
- 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<IMakeDataService>();
- 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);
- }
- if(push)
- {
- RabbitMQClient.Instance.Push("MakeCallBackQueue", content + "#cut#" + resultString);
- return "";
- }
- else
- {
- return content + "#cut#" + resultString;
- }
- }
- catch (Exception ex)
- {
- Utils.WriteLog(DateTime.Now.ToString() + "\n" + ex, "生成异常");
- return "";
- }
- }
- //生成代码版本
- public int codeVersion(int DatabaseId, string versionNo)
- {
- int codeVersion = RedisServer.Cache.Get<int>("codeVersion:" + DatabaseId + ":" + versionNo);
- if(codeVersion > 0)
- {
- return codeVersion;
- }
- var versionForProjectService = App.GetRequiredService<IVersionForProjectService>();
- 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<IVersionsService>();
- var makeFilesService = App.GetRequiredService<IMakeFilesService>();
- var filesForAllService = App.GetRequiredService<IFilesForAllService>();
- 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;
- makeFilesService.Update(filePath);
- 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;
- makeFilesService.Update(file);
- 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);
- }
- }
- }
- }
|