1
huangxiaoqiang
2026-03-17 8644eefbb3e1780cc2c738c4cc9f84851079a200
1
已添加1个文件
已修改1个文件
362 ■■■■■ 文件已修改
项目代码/WCS/WCSServer/WIDESEAWCS_Core/CodeGenerator/CodeGenertors.cs 349 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
项目代码/WCS/WCSServer/WIDESEAWCS_Server/Controllers/System/Sys_UserController.cs 13 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
ÏîÄ¿´úÂë/WCS/WCSServer/WIDESEAWCS_Core/CodeGenerator/CodeGenertors.cs
¶Ô±ÈÐÂÎļþ
@@ -0,0 +1,349 @@
using 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);
            }
        }
    }
}
ÏîÄ¿´úÂë/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();
        }
    }
}