2024-06-15 13:02:35 +08:00
|
|
|
|
// Admin.NET 项目的版权、商标、专利和其他相关权利均受相应法律法规的保护。使用本项目应遵守相关法律法规和许可证的要求。
|
|
|
|
|
|
//
|
|
|
|
|
|
// 本项目主要遵循 MIT 许可证和 Apache 许可证(版本 2.0)进行分发和使用。许可证位于源代码树根目录中的 LICENSE-MIT 和 LICENSE-APACHE 文件。
|
|
|
|
|
|
//
|
|
|
|
|
|
// 不得利用本项目从事危害国家安全、扰乱社会秩序、侵犯他人合法权益等法律法规禁止的活动!任何基于本项目二次开发而产生的一切法律纠纷和责任,我们不承担任何责任!
|
|
|
|
|
|
|
|
|
|
|
|
namespace Admin.NET.Core.Service;
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 系统代码生成配置服务 🧩
|
|
|
|
|
|
/// </summary>
|
2024-09-05 14:08:51 +08:00
|
|
|
|
[ApiDescriptionSettings(Order = 260, Description = "代码生成配置")]
|
2024-06-15 13:02:35 +08:00
|
|
|
|
public class SysCodeGenConfigService : IDynamicApiController, ITransient
|
|
|
|
|
|
{
|
|
|
|
|
|
private readonly ISqlSugarClient _db;
|
2025-01-22 23:07:18 +08:00
|
|
|
|
private readonly CodeGenOptions _codeGenOptions;
|
|
|
|
|
|
private readonly DbConnectionOptions _dbConnectionOptions;
|
2024-06-15 13:02:35 +08:00
|
|
|
|
|
2025-01-22 23:07:18 +08:00
|
|
|
|
public SysCodeGenConfigService(ISqlSugarClient db,
|
2025-04-06 15:47:51 +08:00
|
|
|
|
IOptions<CodeGenOptions> codeGenOptions,
|
|
|
|
|
|
IOptions<DbConnectionOptions> dbConnectionOptions)
|
2024-06-15 13:02:35 +08:00
|
|
|
|
{
|
|
|
|
|
|
_db = db;
|
2025-01-22 23:07:18 +08:00
|
|
|
|
_codeGenOptions = codeGenOptions.Value;
|
2025-04-06 15:47:51 +08:00
|
|
|
|
_dbConnectionOptions = dbConnectionOptions.Value;
|
2025-01-22 23:07:18 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 获取数据表列(实体属性)集合
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <returns></returns>
|
2025-04-19 01:05:36 +08:00
|
|
|
|
[DisplayName("获取数据表列(实体属性)集合")]
|
2025-01-22 23:07:18 +08:00
|
|
|
|
public List<ColumnOuput> GetColumnList([FromQuery] AddCodeGenInput input)
|
|
|
|
|
|
{
|
|
|
|
|
|
return GetColumnList(input.TableName, input.ConfigId);
|
|
|
|
|
|
}
|
2025-01-23 13:11:06 +08:00
|
|
|
|
|
2025-01-22 23:07:18 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 获取数据表列(实体属性)集合
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="EntityName"></param>
|
|
|
|
|
|
/// <param name="ConfigId"></param>
|
|
|
|
|
|
/// <returns></returns>
|
2025-04-19 01:05:36 +08:00
|
|
|
|
[DisplayName("获取数据表列(实体属性)集合")]
|
2025-01-22 23:07:18 +08:00
|
|
|
|
public List<ColumnOuput> GetColumnList(string EntityName, string ConfigId)
|
|
|
|
|
|
{
|
|
|
|
|
|
var entityType = GetEntityInfos().GetAwaiter().GetResult().FirstOrDefault(u => u.EntityName == EntityName);
|
2025-01-23 13:11:06 +08:00
|
|
|
|
if (entityType == null) return null;
|
2025-01-22 23:07:18 +08:00
|
|
|
|
|
|
|
|
|
|
var config = _dbConnectionOptions.ConnectionConfigs.FirstOrDefault(u => u.ConfigId.ToString() == ConfigId);
|
|
|
|
|
|
var dbTableName = config!.DbSettings.EnableUnderLine ? UtilMethods.ToUnderLine(entityType.DbTableName) : entityType.DbTableName;
|
|
|
|
|
|
int bracketIndex = dbTableName.IndexOf('{');
|
|
|
|
|
|
if (bracketIndex != -1)
|
|
|
|
|
|
{
|
2025-01-23 13:11:06 +08:00
|
|
|
|
dbTableName = dbTableName[..bracketIndex];
|
2025-01-22 23:07:18 +08:00
|
|
|
|
var dbTableInfos = _db.AsTenant().GetConnectionScope(ConfigId).DbMaintenance.GetTableInfoList(false);
|
2025-01-23 13:11:06 +08:00
|
|
|
|
var table = dbTableInfos.FirstOrDefault(u => u.Name.StartsWith(config.DbSettings.EnableUnderLine ? UtilMethods.ToUnderLine(dbTableName) : dbTableName, StringComparison.CurrentCultureIgnoreCase));
|
2025-01-22 23:07:18 +08:00
|
|
|
|
if (table != null)
|
|
|
|
|
|
dbTableName = table.Name;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 切库---多库代码生成用
|
|
|
|
|
|
var provider = _db.AsTenant().GetConnectionScope(!string.IsNullOrEmpty(ConfigId) ? ConfigId : SqlSugarConst.MainConfigId);
|
|
|
|
|
|
|
2025-01-23 19:49:02 +08:00
|
|
|
|
var entityBasePropertyNames = _codeGenOptions.EntityBaseColumn[nameof(EntityTenantBaseData)];
|
2025-01-22 23:07:18 +08:00
|
|
|
|
var columnInfos = provider.DbMaintenance.GetColumnInfosByTableName(dbTableName, false);
|
|
|
|
|
|
var result = columnInfos.Select(u => new ColumnOuput
|
|
|
|
|
|
{
|
|
|
|
|
|
// 转下划线后的列名需要再转回来(暂时不转)
|
|
|
|
|
|
//ColumnName = config.DbSettings.EnableUnderLine ? CodeGenUtil.CamelColumnName(u.DbColumnName, entityBasePropertyNames) : u.DbColumnName,
|
|
|
|
|
|
ColumnName = u.DbColumnName,
|
|
|
|
|
|
ColumnLength = u.Length,
|
|
|
|
|
|
IsPrimarykey = u.IsPrimarykey,
|
|
|
|
|
|
IsNullable = u.IsNullable,
|
|
|
|
|
|
ColumnKey = u.IsPrimarykey.ToString(),
|
|
|
|
|
|
NetType = CodeGenUtil.ConvertDataType(u, provider.CurrentConnectionConfig.DbType),
|
|
|
|
|
|
DataType = u.DataType,
|
|
|
|
|
|
ColumnComment = string.IsNullOrWhiteSpace(u.ColumnDescription) ? u.DbColumnName : u.ColumnDescription,
|
|
|
|
|
|
DefaultValue = u.DefaultValue,
|
|
|
|
|
|
}).ToList();
|
|
|
|
|
|
|
|
|
|
|
|
// 获取实体的属性信息,赋值给PropertyName属性(CodeFirst模式应以PropertyName为实际使用名称)
|
|
|
|
|
|
var entityProperties = entityType.Type.GetProperties();
|
|
|
|
|
|
for (int i = result.Count - 1; i >= 0; i--)
|
|
|
|
|
|
{
|
|
|
|
|
|
var columnOutput = result[i];
|
|
|
|
|
|
// 先找自定义字段名的,如果找不到就再找自动生成字段名的(并且过滤掉没有SugarColumn的属性)
|
|
|
|
|
|
var propertyInfo = entityProperties.FirstOrDefault(u => (u.GetCustomAttribute<SugarColumn>()?.ColumnName ?? "").ToLower() == columnOutput.ColumnName.ToLower()) ??
|
|
|
|
|
|
entityProperties.FirstOrDefault(u => u.GetCustomAttribute<SugarColumn>() != null && u.Name.ToLower() == (config.DbSettings.EnableUnderLine
|
|
|
|
|
|
? CodeGenUtil.CamelColumnName(columnOutput.ColumnName, entityBasePropertyNames).ToLower()
|
|
|
|
|
|
: columnOutput.ColumnName.ToLower()));
|
|
|
|
|
|
if (propertyInfo != null)
|
|
|
|
|
|
{
|
|
|
|
|
|
columnOutput.PropertyName = propertyInfo.Name;
|
|
|
|
|
|
columnOutput.ColumnComment = propertyInfo.GetCustomAttribute<SugarColumn>()!.ColumnDescription;
|
|
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
|
|
|
|
|
result.RemoveAt(i); // 移除没有定义此属性的字段
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
return result;
|
2024-06-15 13:02:35 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2025-01-22 23:07:18 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 获取库表信息
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="excludeSysTable">是否排除带SysTable属性的表</param>
|
|
|
|
|
|
/// <returns></returns>
|
2025-04-19 01:05:36 +08:00
|
|
|
|
[DisplayName("获取库表信息")]
|
2025-01-22 23:07:18 +08:00
|
|
|
|
public async Task<IEnumerable<EntityInfo>> GetEntityInfos(bool excludeSysTable = false)
|
|
|
|
|
|
{
|
|
|
|
|
|
var types = new List<Type>();
|
|
|
|
|
|
if (_codeGenOptions.EntityAssemblyNames != null)
|
|
|
|
|
|
{
|
|
|
|
|
|
var assemblies = AppDomain.CurrentDomain.GetAssemblies();
|
|
|
|
|
|
foreach (var assembly in assemblies)
|
|
|
|
|
|
{
|
|
|
|
|
|
var assemblyName = assembly.GetName().Name!;
|
|
|
|
|
|
if (_codeGenOptions.EntityAssemblyNames.Contains(assemblyName) || _codeGenOptions.EntityAssemblyNames.Any(name => assemblyName.Contains(name)))
|
|
|
|
|
|
{
|
|
|
|
|
|
Assembly asm = Assembly.Load(assemblyName);
|
|
|
|
|
|
types.AddRange(asm.GetExportedTypes().ToList());
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2025-06-05 02:18:36 +08:00
|
|
|
|
//var sugarTableType = typeof(SugarTable);
|
|
|
|
|
|
//bool IsMyAttribute(Attribute[] o)
|
|
|
|
|
|
//{
|
|
|
|
|
|
// foreach (Attribute a in o)
|
|
|
|
|
|
// {
|
|
|
|
|
|
// if (a.GetType() == sugarTableType)
|
|
|
|
|
|
// return true;
|
|
|
|
|
|
// }
|
|
|
|
|
|
// return false;
|
|
|
|
|
|
//}
|
|
|
|
|
|
Type[] cosType = types.Where(u => u.IsDefined(typeof(SugarTable), false) && !u.GetCustomAttributes<IgnoreTableAttribute>().Any()).ToArray();
|
2025-01-22 23:07:18 +08:00
|
|
|
|
|
|
|
|
|
|
var entityInfos = new List<EntityInfo>();
|
|
|
|
|
|
foreach (var ct in cosType)
|
|
|
|
|
|
{
|
|
|
|
|
|
// 若实体贴[SysTable]特性,则禁止显示系统自带的
|
|
|
|
|
|
if (excludeSysTable && ct.IsDefined(typeof(SysTableAttribute), false))
|
|
|
|
|
|
continue;
|
|
|
|
|
|
|
|
|
|
|
|
var des = ct.GetCustomAttributes(typeof(DescriptionAttribute), false);
|
|
|
|
|
|
var description = des.Length > 0 ? ((DescriptionAttribute)des[0]).Description : "";
|
2025-06-05 02:18:36 +08:00
|
|
|
|
var sugarAttribute = ct.GetCustomAttributes(typeof(SugarTable), true).FirstOrDefault();
|
2025-01-22 23:07:18 +08:00
|
|
|
|
|
|
|
|
|
|
entityInfos.Add(new EntityInfo()
|
|
|
|
|
|
{
|
|
|
|
|
|
EntityName = ct.Name,
|
|
|
|
|
|
DbTableName = sugarAttribute == null ? ct.Name : ((SugarTable)sugarAttribute).TableName,
|
|
|
|
|
|
TableDescription = sugarAttribute == null ? description : ((SugarTable)sugarAttribute).TableDescription,
|
|
|
|
|
|
Type = ct
|
|
|
|
|
|
});
|
|
|
|
|
|
}
|
|
|
|
|
|
return await Task.FromResult(entityInfos);
|
|
|
|
|
|
}
|
2025-01-23 13:11:06 +08:00
|
|
|
|
|
2024-06-15 13:02:35 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 获取代码生成配置列表 🔖
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="input"></param>
|
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
|
[DisplayName("获取代码生成配置列表")]
|
|
|
|
|
|
public async Task<List<CodeGenConfig>> GetList([FromQuery] CodeGenConfig input)
|
|
|
|
|
|
{
|
2025-01-23 13:11:06 +08:00
|
|
|
|
// 获取主表
|
|
|
|
|
|
var codeGenTable = _db.Queryable<SysCodeGen>().Single(u => u.Id == input.CodeGenId);
|
|
|
|
|
|
// 获取配置的字段
|
|
|
|
|
|
var genConfigColumnList = await _db.Queryable<SysCodeGenConfig>().Where(u => u.CodeGenId == input.CodeGenId).ToListAsync();
|
|
|
|
|
|
// 获取实体所有字段
|
|
|
|
|
|
var tableColumnList = GetColumnList(codeGenTable.TableName, codeGenTable.ConfigId);
|
|
|
|
|
|
// 获取新增的字段
|
|
|
|
|
|
var addColumnList = tableColumnList.Where(u => !genConfigColumnList.Select(d => d.ColumnName).Contains(u.ColumnName)).ToList();
|
|
|
|
|
|
// 获取删除的字段
|
|
|
|
|
|
var delColumnList = genConfigColumnList.Where(u => !tableColumnList.Select(d => d.ColumnName).Contains(u.ColumnName)).ToList();
|
|
|
|
|
|
// 获取更新的字段
|
|
|
|
|
|
var updateColumnList = new List<SysCodeGenConfig>();
|
|
|
|
|
|
foreach (var column in genConfigColumnList)
|
2025-01-22 23:07:18 +08:00
|
|
|
|
{
|
2025-01-23 13:11:06 +08:00
|
|
|
|
// 获取没有增减的
|
|
|
|
|
|
if (tableColumnList.Any(u => u.ColumnName == column.ColumnName))
|
2025-01-22 23:07:18 +08:00
|
|
|
|
{
|
2025-01-23 13:11:06 +08:00
|
|
|
|
var nmd = tableColumnList.Single(u => u.ColumnName == column.ColumnName);
|
|
|
|
|
|
// 如果数据库类型或者长度改变
|
2025-01-22 23:07:18 +08:00
|
|
|
|
if (nmd.NetType != column.NetType || nmd.ColumnLength != column.ColumnLength || nmd.ColumnComment != column.ColumnComment)
|
|
|
|
|
|
{
|
|
|
|
|
|
column.NetType = nmd.NetType;
|
|
|
|
|
|
column.ColumnLength = nmd.ColumnLength;
|
|
|
|
|
|
column.ColumnComment = nmd.ColumnComment;
|
|
|
|
|
|
updateColumnList.Add(column);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2025-01-23 13:11:06 +08:00
|
|
|
|
// 增加新增的
|
|
|
|
|
|
if (addColumnList.Count > 0) AddList(addColumnList, codeGenTable);
|
|
|
|
|
|
// 删除没有的
|
|
|
|
|
|
if (delColumnList.Count > 0) await _db.Deleteable(delColumnList).ExecuteCommandAsync();
|
|
|
|
|
|
// 更新配置
|
2025-01-22 23:07:18 +08:00
|
|
|
|
if (updateColumnList.Count > 0) await _db.Updateable(updateColumnList).ExecuteCommandAsync();
|
2025-01-23 13:11:06 +08:00
|
|
|
|
// 重新获取配置
|
2024-06-15 13:02:35 +08:00
|
|
|
|
return await _db.Queryable<SysCodeGenConfig>()
|
|
|
|
|
|
.Where(u => u.CodeGenId == input.CodeGenId)
|
|
|
|
|
|
.Select<CodeGenConfig>()
|
|
|
|
|
|
.Mapper(u =>
|
|
|
|
|
|
{
|
2025-01-17 09:28:55 +08:00
|
|
|
|
u.NetType = (u.EffectType is "EnumSelector" or "ConstSelector" ? u.DictTypeCode : u.NetType);
|
2024-06-15 13:02:35 +08:00
|
|
|
|
})
|
2024-07-25 17:08:49 +08:00
|
|
|
|
.OrderBy(u => new { u.OrderNo, u.Id })
|
2024-06-15 13:02:35 +08:00
|
|
|
|
.ToListAsync();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 更新代码生成配置 🔖
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="inputList"></param>
|
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
|
[ApiDescriptionSettings(Name = "Update"), HttpPost]
|
|
|
|
|
|
[DisplayName("更新代码生成配置")]
|
|
|
|
|
|
public async Task UpdateCodeGenConfig(List<CodeGenConfig> inputList)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (inputList == null || inputList.Count < 1) return;
|
2025-01-23 13:11:06 +08:00
|
|
|
|
|
2024-06-15 13:02:35 +08:00
|
|
|
|
await _db.Updateable(inputList.Adapt<List<SysCodeGenConfig>>())
|
|
|
|
|
|
.IgnoreColumns(u => new { u.ColumnLength, u.ColumnName, u.PropertyName })
|
|
|
|
|
|
.ExecuteCommandAsync();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 删除代码生成配置
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="codeGenId"></param>
|
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
|
[NonAction]
|
|
|
|
|
|
public async Task DeleteCodeGenConfig(long codeGenId)
|
|
|
|
|
|
{
|
|
|
|
|
|
await _db.Deleteable<SysCodeGenConfig>().Where(u => u.CodeGenId == codeGenId).ExecuteCommandAsync();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 获取代码生成配置详情 🔖
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="input"></param>
|
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
|
[DisplayName("获取代码生成配置详情")]
|
|
|
|
|
|
public async Task<SysCodeGenConfig> GetDetail([FromQuery] CodeGenConfig input)
|
|
|
|
|
|
{
|
2024-09-01 11:35:08 +08:00
|
|
|
|
return await _db.Queryable<SysCodeGenConfig>().SingleAsync(u => u.Id == input.Id);
|
2024-06-15 13:02:35 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 批量增加代码生成配置
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="tableColumnOutputList"></param>
|
|
|
|
|
|
/// <param name="codeGenerate"></param>
|
|
|
|
|
|
[NonAction]
|
|
|
|
|
|
public void AddList(List<ColumnOuput> tableColumnOutputList, SysCodeGen codeGenerate)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (tableColumnOutputList == null) return;
|
|
|
|
|
|
|
|
|
|
|
|
var codeGenConfigs = new List<SysCodeGenConfig>();
|
2024-10-31 15:36:42 +08:00
|
|
|
|
var orderNo = 1;
|
2024-06-15 13:02:35 +08:00
|
|
|
|
foreach (var tableColumn in tableColumnOutputList)
|
|
|
|
|
|
{
|
2024-08-17 15:07:57 +08:00
|
|
|
|
if (_db.Queryable<SysCodeGenConfig>().Any(u => u.ColumnName == tableColumn.ColumnName && u.CodeGenId == codeGenerate.Id))
|
|
|
|
|
|
continue;
|
|
|
|
|
|
|
2024-06-15 13:02:35 +08:00
|
|
|
|
var codeGenConfig = new SysCodeGenConfig();
|
|
|
|
|
|
|
|
|
|
|
|
var YesOrNo = YesNoEnum.Y.ToString();
|
|
|
|
|
|
if (Convert.ToBoolean(tableColumn.ColumnKey))
|
|
|
|
|
|
{
|
|
|
|
|
|
YesOrNo = YesNoEnum.N.ToString();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if (CodeGenUtil.IsCommonColumn(tableColumn.PropertyName))
|
|
|
|
|
|
{
|
|
|
|
|
|
codeGenConfig.WhetherCommon = YesNoEnum.Y.ToString();
|
|
|
|
|
|
YesOrNo = YesNoEnum.N.ToString();
|
|
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
|
|
|
|
|
codeGenConfig.WhetherCommon = YesNoEnum.N.ToString();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
codeGenConfig.CodeGenId = codeGenerate.Id;
|
|
|
|
|
|
codeGenConfig.ColumnName = tableColumn.ColumnName; // 字段名
|
|
|
|
|
|
codeGenConfig.PropertyName = tableColumn.PropertyName;// 实体属性名
|
|
|
|
|
|
codeGenConfig.ColumnLength = tableColumn.ColumnLength;// 长度
|
|
|
|
|
|
codeGenConfig.ColumnComment = tableColumn.ColumnComment;
|
2024-06-18 01:58:17 +08:00
|
|
|
|
codeGenConfig.NetType = tableColumn.NetType;
|
2024-06-15 13:02:35 +08:00
|
|
|
|
codeGenConfig.WhetherRetract = YesNoEnum.N.ToString();
|
|
|
|
|
|
|
|
|
|
|
|
// 生成代码时,主键并不是必要输入项,故一定要排除主键字段
|
2024-08-27 22:04:07 +08:00
|
|
|
|
//codeGenConfig.WhetherRequired = (tableColumn.IsNullable || tableColumn.IsPrimarykey) ? YesNoEnum.N.ToString() : YesNoEnum.Y.ToString();
|
2024-08-28 11:37:37 +08:00
|
|
|
|
|
2024-08-27 22:04:07 +08:00
|
|
|
|
#region 添加校验规则
|
2024-08-28 11:37:37 +08:00
|
|
|
|
|
2024-12-30 02:20:39 +08:00
|
|
|
|
// 添加校验规则
|
2024-08-27 22:04:07 +08:00
|
|
|
|
codeGenConfig.Id = YitIdHelper.NextId();
|
2024-12-30 02:20:39 +08:00
|
|
|
|
// 验证规则
|
|
|
|
|
|
List<VerifyRuleItem> ruleItems = [];
|
2024-08-27 22:04:07 +08:00
|
|
|
|
if (!tableColumn.IsNullable && !tableColumn.IsPrimarykey)
|
|
|
|
|
|
{
|
|
|
|
|
|
ruleItems.Add(new VerifyRuleItem()
|
|
|
|
|
|
{
|
|
|
|
|
|
Key = codeGenConfig.Id,
|
|
|
|
|
|
Type = "required",
|
|
|
|
|
|
Message = $"{tableColumn.ColumnComment}不能为空",
|
|
|
|
|
|
});
|
|
|
|
|
|
}
|
|
|
|
|
|
codeGenConfig.WhetherRequired = ruleItems.Any(t => t.Type == "required") ? YesNoEnum.Y.ToString() : YesNoEnum.N.ToString();
|
2024-08-28 11:37:37 +08:00
|
|
|
|
codeGenConfig.Rules = ruleItems.ToJson();
|
|
|
|
|
|
|
|
|
|
|
|
#endregion 添加校验规则
|
2024-08-27 22:04:07 +08:00
|
|
|
|
|
2024-08-29 14:28:23 +08:00
|
|
|
|
codeGenConfig.QueryWhether = YesNoEnum.N.ToString();
|
2024-06-15 13:02:35 +08:00
|
|
|
|
codeGenConfig.WhetherAddUpdate = YesOrNo;
|
|
|
|
|
|
codeGenConfig.WhetherTable = YesOrNo;
|
|
|
|
|
|
|
|
|
|
|
|
codeGenConfig.ColumnKey = tableColumn.ColumnKey;
|
|
|
|
|
|
|
|
|
|
|
|
codeGenConfig.DataType = tableColumn.DataType;
|
|
|
|
|
|
codeGenConfig.EffectType = CodeGenUtil.DataTypeToEff(codeGenConfig.NetType);
|
|
|
|
|
|
codeGenConfig.QueryType = GetDefaultQueryType(codeGenConfig); // QueryTypeEnum.eq.ToString();
|
|
|
|
|
|
codeGenConfig.OrderNo = orderNo;
|
2024-08-28 22:07:24 +08:00
|
|
|
|
codeGenConfig.DefaultValue = GetDefaultValue(tableColumn.DefaultValue);
|
2024-06-15 13:02:35 +08:00
|
|
|
|
codeGenConfigs.Add(codeGenConfig);
|
|
|
|
|
|
|
2024-10-31 15:36:42 +08:00
|
|
|
|
orderNo += 1; // 每个配置排序间隔1
|
2024-06-15 13:02:35 +08:00
|
|
|
|
}
|
|
|
|
|
|
// 多库代码生成---这里要切回主库
|
2024-08-20 21:01:15 +08:00
|
|
|
|
var provider = _db.AsTenant().GetConnectionScope(SqlSugarConst.MainConfigId);
|
2024-06-15 13:02:35 +08:00
|
|
|
|
provider.Insertable(codeGenConfigs).ExecuteCommand();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 默认查询类型
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="codeGenConfig"></param>
|
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
|
private static string GetDefaultQueryType(SysCodeGenConfig codeGenConfig)
|
|
|
|
|
|
{
|
|
|
|
|
|
return (codeGenConfig.NetType?.TrimEnd('?')) switch
|
|
|
|
|
|
{
|
|
|
|
|
|
"string" => "like",
|
|
|
|
|
|
"DateTime" => "~",
|
|
|
|
|
|
_ => "==",
|
|
|
|
|
|
};
|
|
|
|
|
|
}
|
2024-08-28 22:07:24 +08:00
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 获取默认值
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="dataValue"></param>
|
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
|
private static string? GetDefaultValue(string dataValue)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (dataValue == null) return null;
|
|
|
|
|
|
// 正则表达式模式
|
|
|
|
|
|
// \( 和 \) 用来匹配字面量的括号
|
|
|
|
|
|
// .+ 用来匹配一个或多个任意字符,但不包括换行符
|
|
|
|
|
|
string pattern = @"\((.+)\)";//适合MSSQL其他数据库没有测试
|
|
|
|
|
|
|
|
|
|
|
|
// 使用 Regex 类进行匹配
|
|
|
|
|
|
Match match = Regex.Match(dataValue, pattern);
|
|
|
|
|
|
|
2024-12-30 02:20:39 +08:00
|
|
|
|
string value;
|
2024-08-28 22:07:24 +08:00
|
|
|
|
// 如果找到了匹配项
|
|
|
|
|
|
if (match.Success)
|
|
|
|
|
|
{
|
|
|
|
|
|
// 提取括号内的值
|
|
|
|
|
|
value = match.Groups[1].Value.Trim('\'');
|
|
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
|
|
|
|
|
value = dataValue;
|
|
|
|
|
|
}
|
|
|
|
|
|
return value;
|
|
|
|
|
|
}
|
2024-06-15 13:02:35 +08:00
|
|
|
|
}
|