From 8644eefbb3e1780cc2c738c4cc9f84851079a200 Mon Sep 17 00:00:00 2001
From: huangxiaoqiang <huangxiaoqiang@hnkhzn.com>
Date: 星期二, 17 三月 2026 12:32:48 +0800
Subject: [PATCH] 1
---
项目代码/WCS/WCSServer/WIDESEAWCS_Core/CodeGenerator/CodeGenertors.cs | 349 +++++++++++++++++++++++++++++++++++++++++++++++++
项目代码/WCS/WCSServer/WIDESEAWCS_Server/Controllers/System/Sys_UserController.cs | 13 +
2 files changed, 362 insertions(+), 0 deletions(-)
diff --git "a/\351\241\271\347\233\256\344\273\243\347\240\201/WCS/WCSServer/WIDESEAWCS_Core/CodeGenerator/CodeGenertors.cs" "b/\351\241\271\347\233\256\344\273\243\347\240\201/WCS/WCSServer/WIDESEAWCS_Core/CodeGenerator/CodeGenertors.cs"
new file mode 100644
index 0000000..43e3275
--- /dev/null
+++ "b/\351\241\271\347\233\256\344\273\243\347\240\201/WCS/WCSServer/WIDESEAWCS_Core/CodeGenerator/CodeGenertors.cs"
@@ -0,0 +1,349 @@
+锘縰sing System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Reflection;
+using System.Text;
+using System.Threading.Tasks;
+using SqlSugar;
+using WIDESEAWCS_Core.DB;
+using WIDESEAWCS_Core.Helper;
+
+namespace WIDESEAWCS_Core.CodeGenerator
+{
+ public class CodeGenertors
+ {
+ public static WebResponseContent CreateIRepository(string tableName, string module)
+ {
+ try
+ {
+ string startName = "WIDESEA";
+
+ string thisNameSpace = typeof(CodeGenertors).Namespace ?? "WIDESEA_";
+ int nameSpaceIndex = thisNameSpace.IndexOf("_");
+
+ if (nameSpaceIndex > -1)
+ {
+ startName = thisNameSpace.Substring(0, nameSpaceIndex);
+ }
+
+ List<Assembly> assemblies = App.Assemblies.ToList();
+ Assembly? assembly = assemblies.FirstOrDefault(x => x.GetName()?.Name?.Contains($"I{module}Repository") ?? false);
+ if (assembly == null)
+ {
+ return WebResponseContent.Instance.Error($"鏈壘鍒扮▼搴忛泦{startName}_I{module}Repository");
+ }
+
+ string? nameSpaceFullName = assembly.GetName()?.Name;
+ if (string.IsNullOrEmpty(nameSpaceFullName))
+ {
+ return WebResponseContent.Instance.Error($"{nameSpaceFullName} not found.");
+ }
+
+ int index = tableName.IndexOf("_");
+
+ string tableShortName = tableName;
+
+ if (index > -1)
+ {
+ tableShortName = tableName.Substring(index + 1);
+ }
+
+ string rootPath = App.WebHostEnvironment.WebRootPath;
+ string templatePath = Path.Combine(rootPath, $"CodeTemplate\\BaseIRepository.txt");
+
+ if (!File.Exists(templatePath))
+ {
+ return WebResponseContent.Instance.Error($"鏈壘鍒版ā鏉挎枃浠�");
+ }
+
+ string template = FileHelper.ReadFile(templatePath);
+
+ string classStr = template.Replace("[TableName]", tableName).Replace("[TableShortName]", tableShortName).Replace("[NameSpace]", nameSpaceFullName).Replace("[StartName]", startName).Replace("[Module]", module);
+
+ int rootPathIndex = App.HostEnvironment.ContentRootPath.LastIndexOf("\\");
+
+ string rootPaht = App.HostEnvironment.ContentRootPath.Substring(0, rootPathIndex - 1);
+
+ int rootPathIndex2 = rootPaht.LastIndexOf("\\");
+
+ string projectPath = Path.Combine(rootPaht.Substring(0, rootPathIndex2), nameSpaceFullName);
+
+ string filePath = Path.Combine(projectPath, $"I{tableShortName}Repository.cs");
+
+ FileHelper.WriteFileAndDelOldFile(filePath, classStr);
+
+ return WebResponseContent.Instance.OK();
+ }
+ catch (Exception ex)
+ {
+ return WebResponseContent.Instance.Error(ex.Message);
+ }
+ }
+
+ public static WebResponseContent CreateRepository(string tableName, string module)
+ {
+ try
+ {
+ string startName = "WIDESEA";
+
+ string thisNameSpace = typeof(CodeGenertors).Namespace ?? "WIDESEA_";
+ int nameSpaceIndex = thisNameSpace.IndexOf("_");
+
+ if (nameSpaceIndex > -1)
+ {
+ startName = thisNameSpace.Substring(0, nameSpaceIndex);
+ }
+
+ List<Assembly> assemblies = App.Assemblies.ToList();
+ Assembly? assembly = assemblies.FirstOrDefault(x => (x.GetName()?.Name?.Contains($"{module}Repository") ?? false) && (!x.GetName()?.Name?.Contains($"I{module}Repository") ?? false));
+ if (assembly == null)
+ {
+ return WebResponseContent.Instance.Error($"鏈壘鍒扮▼搴忛泦{startName}_{module}Repository");
+ }
+
+ string? nameSpaceFullName = assembly.GetName()?.Name;
+ if (string.IsNullOrEmpty(nameSpaceFullName))
+ {
+ return WebResponseContent.Instance.Error($"{nameSpaceFullName} not found.");
+ }
+
+ int index = tableName.IndexOf("_");
+
+ string tableShortName = tableName;
+
+ if (index > -1)
+ {
+ tableShortName = tableName.Substring(index + 1);
+ }
+
+ string rootPath = App.WebHostEnvironment.WebRootPath;
+ string templatePath = Path.Combine(rootPath, $"CodeTemplate\\BaseRepository.txt");
+
+ if (!File.Exists(templatePath))
+ {
+ return WebResponseContent.Instance.Error($"鏈壘鍒版ā鏉挎枃浠�");
+ }
+
+ string template = FileHelper.ReadFile(templatePath);
+
+ string classStr = template.Replace("[TableName]", tableName).Replace("[TableShortName]", tableShortName).Replace("[NameSpace]", nameSpaceFullName).Replace("[StartName]", startName).Replace("[Module]", module);
+
+ int rootPathIndex = App.HostEnvironment.ContentRootPath.LastIndexOf("\\");
+
+ string rootPaht = App.HostEnvironment.ContentRootPath.Substring(0, rootPathIndex - 1);
+
+ int rootPathIndex2 = rootPaht.LastIndexOf("\\");
+
+ string projectPath = Path.Combine(rootPaht.Substring(0, rootPathIndex2), nameSpaceFullName);
+
+ string filePath = Path.Combine(projectPath, $"{tableShortName}Repository.cs");
+
+ FileHelper.WriteFileAndDelOldFile(filePath, classStr);
+
+ return WebResponseContent.Instance.OK();
+ }
+ catch (Exception ex)
+ {
+ return WebResponseContent.Instance.Error(ex.Message);
+ }
+ }
+
+ public static WebResponseContent CreateIService(string tableName, string module)
+ {
+ try
+ {
+ string startName = "WIDESEA";
+
+ string thisNameSpace = typeof(CodeGenertors).Namespace ?? "WIDESEA_";
+ int nameSpaceIndex = thisNameSpace.IndexOf("_");
+
+ if (nameSpaceIndex > -1)
+ {
+ startName = thisNameSpace.Substring(0, nameSpaceIndex);
+ }
+
+ List<Assembly> assemblies = App.Assemblies.ToList();
+ Assembly? assembly = assemblies.FirstOrDefault(x => x.GetName()?.Name?.Contains($"I{module}Service") ?? false);
+ if (assembly == null)
+ {
+ return WebResponseContent.Instance.Error($"鏈壘鍒扮▼搴忛泦{startName}_I{module}Service");
+ }
+
+ string? nameSpaceFullName = assembly.GetName()?.Name;
+ if (string.IsNullOrEmpty(nameSpaceFullName))
+ {
+ return WebResponseContent.Instance.Error($"{nameSpaceFullName} not found.");
+ }
+
+ int index = tableName.IndexOf("_");
+
+ string tableShortName = tableName;
+
+ if (index > -1)
+ {
+ tableShortName = tableName.Substring(index + 1);
+ }
+
+ string rootPath = App.WebHostEnvironment.WebRootPath;
+ string templatePath = Path.Combine(rootPath, $"CodeTemplate\\BaseIService.txt");
+
+ if (!File.Exists(templatePath))
+ {
+ return WebResponseContent.Instance.Error($"鏈壘鍒版ā鏉挎枃浠�");
+ }
+
+ string template = FileHelper.ReadFile(templatePath);
+
+ string classStr = template.Replace("[TableName]", tableName).Replace("[TableShortName]", tableShortName).Replace("[NameSpace]", nameSpaceFullName).Replace("[StartName]", startName).Replace("[Module]", module);
+
+ int rootPathIndex = App.HostEnvironment.ContentRootPath.LastIndexOf("\\");
+
+ string rootPaht = App.HostEnvironment.ContentRootPath.Substring(0, rootPathIndex - 1);
+
+ int rootPathIndex2 = rootPaht.LastIndexOf("\\");
+
+ string projectPath = Path.Combine(rootPaht.Substring(0, rootPathIndex2), nameSpaceFullName);
+
+ string filePath = Path.Combine(projectPath, $"I{tableShortName}Service.cs");
+
+ FileHelper.WriteFileAndDelOldFile(filePath, classStr);
+
+ return WebResponseContent.Instance.OK();
+ }
+ catch (Exception ex)
+ {
+ return WebResponseContent.Instance.Error(ex.Message);
+ }
+ }
+
+ public static WebResponseContent CreateService(string tableName, string module)
+ {
+ try
+ {
+ string startName = "WIDESEA";
+
+ string thisNameSpace = typeof(CodeGenertors).Namespace ?? "WIDESEA_";
+ int nameSpaceIndex = thisNameSpace.IndexOf("_");
+
+ if (nameSpaceIndex > -1)
+ {
+ startName = thisNameSpace.Substring(0, nameSpaceIndex);
+ }
+
+ List<Assembly> assemblies = App.Assemblies.ToList();
+ Assembly? assembly = assemblies.FirstOrDefault(x => (x.GetName()?.Name?.Contains($"{module}Service") ?? false) && (!x.GetName()?.Name?.Contains($"I{module}Service") ?? false));
+ if (assembly == null)
+ {
+ return WebResponseContent.Instance.Error($"鏈壘鍒扮▼搴忛泦{startName}_{module}Service");
+ }
+
+ string? nameSpaceFullName = assembly.GetName()?.Name;
+ if (string.IsNullOrEmpty(nameSpaceFullName))
+ {
+ return WebResponseContent.Instance.Error($"{nameSpaceFullName} not found.");
+ }
+
+ int index = tableName.IndexOf("_");
+
+ string tableShortName = tableName;
+
+ if (index > -1)
+ {
+ tableShortName = tableName.Substring(index + 1);
+ }
+
+ string rootPath = App.WebHostEnvironment.WebRootPath;
+ string templatePath = Path.Combine(rootPath, $"CodeTemplate\\BaseService.txt");
+
+ if (!File.Exists(templatePath))
+ {
+ return WebResponseContent.Instance.Error($"鏈壘鍒版ā鏉挎枃浠�");
+ }
+
+ string template = FileHelper.ReadFile(templatePath);
+
+ string classStr = template.Replace("[TableName]", tableName).Replace("[TableShortName]", tableShortName).Replace("[NameSpace]", nameSpaceFullName).Replace("[StartName]", startName).Replace("[Module]", module);
+
+ int rootPathIndex = App.HostEnvironment.ContentRootPath.LastIndexOf("\\");
+
+ string rootPaht = App.HostEnvironment.ContentRootPath.Substring(0, rootPathIndex - 1);
+
+ int rootPathIndex2 = rootPaht.LastIndexOf("\\");
+
+ string projectPath = Path.Combine(rootPaht.Substring(0, rootPathIndex2), nameSpaceFullName);
+
+ string filePath = Path.Combine(projectPath, $"{tableShortName}Service.cs");
+
+ FileHelper.WriteFileAndDelOldFile(filePath, classStr);
+
+ return WebResponseContent.Instance.OK();
+ }
+ catch (Exception ex)
+ {
+ return WebResponseContent.Instance.Error(ex.Message);
+ }
+ }
+
+ public static WebResponseContent CreateController(string tableName, string module)
+ {
+ try
+ {
+ string startName = "WIDESEA";
+
+ string thisNameSpace = typeof(CodeGenertors).Namespace ?? "WIDESEA_";
+ int nameSpaceIndex = thisNameSpace.IndexOf("_");
+
+ if (nameSpaceIndex > -1)
+ {
+ startName = thisNameSpace.Substring(0, nameSpaceIndex);
+ }
+
+ List<Assembly> assemblies = App.Assemblies.ToList();
+ Assembly? assembly = assemblies.FirstOrDefault(x => (x.GetName()?.Name?.Contains($"{module}Service") ?? false) && (!x.GetName()?.Name?.Contains($"I{module}Service") ?? false));
+ if (assembly == null)
+ {
+ return WebResponseContent.Instance.Error($"鏈壘鍒扮▼搴忛泦{startName}_{module}Service");
+ }
+
+ string? nameSpaceFullName = assembly.GetName()?.Name;
+ if (string.IsNullOrEmpty(nameSpaceFullName))
+ {
+ return WebResponseContent.Instance.Error($"{nameSpaceFullName} not found.");
+ }
+
+ int index = tableName.IndexOf("_");
+
+ string tableShortName = tableName;
+
+ if (index > -1)
+ {
+ tableShortName = tableName.Substring(index + 1);
+ }
+
+ string rootPath = App.WebHostEnvironment.WebRootPath;
+ string templatePath = Path.Combine(rootPath, $"CodeTemplate\\BaseController.txt");
+
+ if (!File.Exists(templatePath))
+ {
+ return WebResponseContent.Instance.Error($"鏈壘鍒版ā鏉挎枃浠�");
+ }
+
+ string template = FileHelper.ReadFile(templatePath);
+
+ string classStr = template.Replace("[TableName]", tableName).Replace("[TableShortName]", tableShortName).Replace("[NameSpace]", nameSpaceFullName).Replace("[StartName]", startName).Replace("[Module]", module);
+
+ string projectPath = Path.Combine(App.HostEnvironment.ContentRootPath , $"Controllers\\{module}");
+
+ string filePath = Path.Combine(projectPath, $"{tableShortName}Controller.cs");
+
+ FileHelper.WriteFileAndDelOldFile(filePath, classStr);
+
+ return WebResponseContent.Instance.OK();
+ }
+ catch (Exception ex)
+ {
+ return WebResponseContent.Instance.Error(ex.Message);
+ }
+ }
+ }
+}
diff --git "a/\351\241\271\347\233\256\344\273\243\347\240\201/WCS/WCSServer/WIDESEAWCS_Server/Controllers/System/Sys_UserController.cs" "b/\351\241\271\347\233\256\344\273\243\347\240\201/WCS/WCSServer/WIDESEAWCS_Server/Controllers/System/Sys_UserController.cs"
index 4d1ac2b..5afd529 100644
--- "a/\351\241\271\347\233\256\344\273\243\347\240\201/WCS/WCSServer/WIDESEAWCS_Server/Controllers/System/Sys_UserController.cs"
+++ "b/\351\241\271\347\233\256\344\273\243\347\240\201/WCS/WCSServer/WIDESEAWCS_Server/Controllers/System/Sys_UserController.cs"
@@ -4,9 +4,11 @@
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Caching.Memory;
using StackExchange.Profiling;
+using System.CodeDom.Compiler;
using WIDESEAWCS_Core;
using WIDESEAWCS_Core.Authorization;
using WIDESEAWCS_Core.BaseController;
+using WIDESEAWCS_Core.CodeGenerator;
using WIDESEAWCS_Core.Const;
using WIDESEAWCS_Core.Helper;
using WIDESEAWCS_Core.HttpContextUser;
@@ -90,5 +92,16 @@
}
}
+
+ [HttpPost, Route("CreateIRepository"), AllowAnonymous]
+ public WebResponseContent CreateIRepository(string tableName, string nameSpace)
+ {
+ CodeGenertors.CreateIRepository(tableName, nameSpace);
+ CodeGenertors.CreateRepository(tableName, nameSpace);
+ CodeGenertors.CreateIService(tableName, nameSpace);
+ CodeGenertors.CreateService(tableName, nameSpace);
+ CodeGenertors.CreateController(tableName, nameSpace);
+ return WebResponseContent.Instance.OK();
+ }
}
}
--
Gitblit v1.9.3