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);
|
}
|
}
|
|
//public WebResponseContent CreateVuePage(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);
|
// }
|
//}
|
}
|
}
|