重新生成Entity文件
This commit is contained in:
@@ -1,8 +1,11 @@
|
||||
using System.Collections;
|
||||
using System.ArrayExtensions;
|
||||
using System.Collections;
|
||||
using System.ComponentModel.DataAnnotations.Schema;
|
||||
using System.Data;
|
||||
using System.Text;
|
||||
using System.Text.RegularExpressions;
|
||||
using JNPF.Common.Configuration;
|
||||
using JNPF.Common.Contracts;
|
||||
using JNPF.Common.Core.Manager;
|
||||
using JNPF.Common.Core.Manager.Files;
|
||||
using JNPF.Common.Dtos.DataBase;
|
||||
@@ -22,6 +25,8 @@ using Mapster;
|
||||
using Microsoft.AspNetCore.Authorization;
|
||||
using Microsoft.AspNetCore.Http;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using NPOI.SS.Formula.Functions;
|
||||
using Org.BouncyCastle.Asn1.Cms;
|
||||
using SqlSugar;
|
||||
|
||||
namespace JNPF.Systems;
|
||||
@@ -351,8 +356,9 @@ public class DataBaseService : IDynamicApiController, ITransient
|
||||
}
|
||||
return await sugarClient!.Deleteable<object>().AS(input.TableName).ExecuteCommandAsync();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 生成代码
|
||||
/// 生成实体类代码
|
||||
/// </summary>
|
||||
/// <param name="linkId">数据库配置表主键</param>
|
||||
/// <param name="input">post输入参数</param>
|
||||
@@ -362,6 +368,10 @@ public class DataBaseService : IDynamicApiController, ITransient
|
||||
[HttpPost("{linkId}/gen-code")]
|
||||
public async Task<dynamic> GenerateCode(string linkId, DatabaseTableDataCleanInput input)
|
||||
{
|
||||
if (input.TableName == "bas_customer")
|
||||
{
|
||||
return await GenerateAllEntity(linkId);
|
||||
}
|
||||
if (input.TableName.IsNullOrWhiteSpace())
|
||||
{
|
||||
throw new ArgumentNullException(nameof(input.TableName));
|
||||
@@ -370,26 +380,15 @@ public class DataBaseService : IDynamicApiController, ITransient
|
||||
{
|
||||
throw new ArgumentException($"表【{input.TableName}】,表名之间必须包含_");
|
||||
}
|
||||
var dir = Path.Combine(FileVariable.GenerateCodePath, "DbModel");
|
||||
if (!Directory.Exists(dir))
|
||||
{
|
||||
Directory.CreateDirectory(dir);
|
||||
}
|
||||
ISqlSugarClient sugarClient = null!;
|
||||
if (linkId == "0") //默认时,使用当前默认数据库配置
|
||||
{
|
||||
sugarClient = _repository.AsSugarClient();
|
||||
}
|
||||
else
|
||||
ISqlSugarClient sugarClient = _repository.AsSugarClient();
|
||||
if (linkId != "0")
|
||||
{
|
||||
var link = await _dbLinkService.GetInfo(linkId);
|
||||
sugarClient = _dataBaseManager.ChangeDataBase(link);
|
||||
}
|
||||
|
||||
var entityName = string.Join("", input.TableName.Split('_').Select(a => a.ToPascalCase()));
|
||||
foreach (var item in sugarClient.DbMaintenance.GetTableInfoList().Where(t => t.Name == input.TableName))
|
||||
{
|
||||
//string entityName = CustomFormatName(item.Name);/*实体名首字母大写*/
|
||||
var entityName = string.Join("", input.TableName.Split('_').Select(a => a.ToPascalCase()));
|
||||
sugarClient.MappingTables.Add(entityName, item.Name);
|
||||
//foreach (var col in sugarClient.DbMaintenance.GetColumnInfosByTableName(item.Name))
|
||||
//{
|
||||
@@ -397,36 +396,11 @@ public class DataBaseService : IDynamicApiController, ITransient
|
||||
// sugarClient.MappingColumns.Add(col.DbColumnName /*类的属性首字母大写*/, col.DbColumnName, entityName);
|
||||
//}
|
||||
}
|
||||
Dictionary<string, string> nsMapper = GetNsMapper();
|
||||
var prefix = input.TableName.Split('_')[0];
|
||||
Dictionary<string, string> nsMapper = new()
|
||||
{
|
||||
{"bas", "Tnb.BasicData.Entities" },
|
||||
{"prd", "Tnb.ProductionMgr.Entities" },
|
||||
{"wms", "Tnb.WarehouseMgr.Entities" },
|
||||
{"eqp", "Tnb.EquipMgr.Entities" },
|
||||
{"tool", "Tnb.EquipMgr.Entities" },
|
||||
{"qc", "Tnb.QcMgr.Entities" },
|
||||
};
|
||||
sugarClient.DbFirst.Where(input.TableName)
|
||||
.SettingConstructorTemplate(a =>
|
||||
{
|
||||
return a;
|
||||
})
|
||||
//.IsCreateDefaultValue(true)
|
||||
.IsCreateAttribute()
|
||||
.StringNullable()
|
||||
.CreateClassFile(dir, nsMapper.ContainsKey(prefix) ? nsMapper[prefix] : "Tnb.Entities");
|
||||
|
||||
var previewContent = "";
|
||||
var codeFile = Path.Combine(dir, $"{entityName}.cs");
|
||||
if (File.Exists(codeFile))
|
||||
{
|
||||
using (var sr = File.OpenText(codeFile))
|
||||
{
|
||||
previewContent = await sr.ReadToEndAsync();
|
||||
}
|
||||
}
|
||||
return previewContent;
|
||||
var nsName = nsMapper.ContainsKey(prefix) ? nsMapper[prefix] : "Tnb.Entities";
|
||||
var dict = GenerateEntityConfig(sugarClient.DbFirst.Where(input.TableName)).ToClassStringList(nsName);
|
||||
return dict.Count > 0 ? dict.First().Value.Replace("\r\n", "\n") : string.Empty;
|
||||
}
|
||||
#endregion
|
||||
|
||||
@@ -604,5 +578,96 @@ public class DataBaseService : IDynamicApiController, ITransient
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 生成所有表的实体类文件
|
||||
/// </summary>
|
||||
/// <param name="linkId"></param>
|
||||
/// <returns></returns>
|
||||
private async Task<dynamic> GenerateAllEntity(string linkId)
|
||||
{
|
||||
ISqlSugarClient sugarClient = _repository.AsSugarClient();
|
||||
if (linkId != "0")
|
||||
{
|
||||
var link = await _dbLinkService.GetInfo(linkId);
|
||||
sugarClient = _dataBaseManager.ChangeDataBase(link);
|
||||
}
|
||||
foreach (var item in sugarClient.DbMaintenance.GetTableInfoList())
|
||||
{
|
||||
string entityName = string.Join("", item.Name.Split('_').Select(a => a.ToPascalCase()));
|
||||
sugarClient.MappingTables.Add(entityName, item.Name);
|
||||
}
|
||||
Dictionary<string, string> nsMapper = GetNsMapper();
|
||||
|
||||
List<string> dirs = new List<string>();
|
||||
foreach (string key in nsMapper.Keys)
|
||||
{
|
||||
var nsName = nsMapper[key];
|
||||
var dir = Path.Combine(FileVariable.GenerateCodePath, nsName);
|
||||
dirs.Add(dir);
|
||||
if (!Directory.Exists(dir))
|
||||
Directory.CreateDirectory(dir);
|
||||
else
|
||||
Directory.GetFiles(dir).ToList().ForEach(File.Delete);
|
||||
GenerateEntityConfig(sugarClient.DbFirst.Where(t => t.StartsWith(key))).CreateClassFile(dir, nsName); ;
|
||||
}
|
||||
string content = string.Empty;
|
||||
foreach (var dir in dirs)
|
||||
{
|
||||
var files = Directory.GetFiles(dir, "*.cs").ToList();
|
||||
files.ForEach(f => File.WriteAllText(f, File.ReadAllText(f).Replace("\r\n", "\n")));
|
||||
if (files.Count > 0 && string.IsNullOrEmpty(content))
|
||||
{
|
||||
content = File.ReadAllText(files[0]);
|
||||
}
|
||||
}
|
||||
return content;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 表前缀与命名空间的映射关系
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
private Dictionary<string, string> GetNsMapper()
|
||||
{
|
||||
Dictionary<string, string> nsMapper = new()
|
||||
{
|
||||
{"bas_", "Tnb.BasicData.Entities" },
|
||||
{"prd_", "Tnb.ProductionMgr.Entities" },
|
||||
{"wms_", "Tnb.WarehouseMgr.Entities" },
|
||||
{"eqp_", "Tnb.EquipMgr.Entities" },
|
||||
{"tool_", "Tnb.EquipMgr.Entities" },
|
||||
{"qc_", "Tnb.QcMgr.Entities" },
|
||||
};
|
||||
return nsMapper;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 生成实体类配置
|
||||
/// </summary>
|
||||
/// <param name="db"></param>
|
||||
/// <returns></returns>
|
||||
private IDbFirst GenerateEntityConfig(IDbFirst db)
|
||||
{
|
||||
db.IsCreateAttribute()
|
||||
.StringNullable()
|
||||
.SettingPropertyTemplate((col, tpl, type) =>
|
||||
{
|
||||
string sugarColumnStr = "";
|
||||
List<string> sugarAttrs = new List<string>();
|
||||
if (col.IsPrimarykey)
|
||||
sugarAttrs.Add("IsPrimaryKey=true");
|
||||
if (col.IsIdentity)
|
||||
sugarAttrs.Add("IsIdentity=true");
|
||||
if (sugarAttrs.Count > 0)
|
||||
sugarColumnStr = $"\r\n [SugarColumn({string.Join(", ", sugarAttrs)})]";
|
||||
var txt = tpl.Replace("{PropertyType}", type).Replace("{PropertyName}", col.DbColumnName).Replace("{SugarColumn}", sugarColumnStr);
|
||||
if (col.DbColumnName == "id") txt = txt.TrimEnd('\n', '\r') + " = SnowflakeIdHelper.NextId();\r\n";
|
||||
else if (type == "string") txt = txt.TrimEnd('\n', '\r') + " = string.Empty;\r\n";
|
||||
return txt;
|
||||
})
|
||||
.SettingNamespaceTemplate(tpl => tpl + "using JNPF.Common.Security;\r\n");
|
||||
return db;
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
||||
Reference in New Issue
Block a user