namespace Admin.NET.Core.Service;
///
/// 系统代码生成配置服务
///
[ApiDescriptionSettings(Order = 260)]
public class SysCodeGenConfigService : IDynamicApiController, ITransient
{
private readonly ISqlSugarClient _db;
public SysCodeGenConfigService(ISqlSugarClient db)
{
_db = db;
}
///
/// 获取代码生成配置列表
///
///
///
[DisplayName("获取代码生成配置列表")]
public async Task> GetList([FromQuery] CodeGenConfig input)
{
var whetherCommon = YesNoEnum.Y.ToString();
return await _db.Queryable()
.Where(u => u.CodeGenId == input.CodeGenId && u.WhetherCommon != whetherCommon)
.Select()
.Mapper(u =>
{
u.NetType = (u.EffectType == "EnumSelector" ? u.DictTypeCode : u.NetType);
})
.ToListAsync();
}
///
/// 更新代码生成配置
///
///
///
[ApiDescriptionSettings(Name = "Update"), HttpPost]
[DisplayName("更新代码生成配置")]
public async Task UpdateCodeGenConfig(List inputList)
{
if (inputList == null || inputList.Count < 1) return;
await _db.Updateable(inputList.Adapt>()).ExecuteCommandAsync();
}
///
/// 删除代码生成配置
///
///
///
[ApiDescriptionSettings(false)]
public async Task DeleteCodeGenConfig(long codeGenId)
{
await _db.Deleteable().Where(u => u.CodeGenId == codeGenId).ExecuteCommandAsync();
}
///
/// 获取代码生成配置详情
///
///
///
[DisplayName("获取代码生成配置详情")]
public async Task GetDetail([FromQuery] CodeGenConfig input)
{
return await _db.Queryable().FirstAsync(u => u.Id == input.Id);
}
///
/// 批量增加代码生成配置
///
///
///
[ApiDescriptionSettings(false)]
public void AddList(List tableColumnOuputList, SysCodeGen codeGenerate)
{
if (tableColumnOuputList == null) return;
var codeGenConfigs = new List();
foreach (var tableColumn in tableColumnOuputList)
{
var codeGenConfig = new SysCodeGenConfig();
var YesOrNo = YesNoEnum.Y.ToString();
if (Convert.ToBoolean(tableColumn.ColumnKey))
{
YesOrNo = YesNoEnum.N.ToString();
}
if (CodeGenUtil.IsCommonColumn(tableColumn.ColumnName))
{
codeGenConfig.WhetherCommon = YesNoEnum.Y.ToString();
YesOrNo = YesNoEnum.N.ToString();
}
else
{
codeGenConfig.WhetherCommon = YesNoEnum.N.ToString();
}
codeGenConfig.CodeGenId = codeGenerate.Id;
codeGenConfig.ColumnName = tableColumn.ColumnName;
codeGenConfig.ColumnComment = tableColumn.ColumnComment;
codeGenConfig.NetType = tableColumn.DataType;
codeGenConfig.WhetherRetract = YesNoEnum.N.ToString();
codeGenConfig.WhetherRequired = YesNoEnum.N.ToString();
codeGenConfig.QueryWhether = YesOrNo;
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();
codeGenConfigs.Add(codeGenConfig);
}
// 多库代码生成---这里要切回主库
var provider = _db.AsTenant().GetConnectionScope(SqlSugarConst.ConfigId);
provider.Insertable(codeGenConfigs).ExecuteCommand();
}
///
/// 默认查询类型
///
///
///
private string GetDefaultQueryType(SysCodeGenConfig codeGenConfig)
{
switch (codeGenConfig.NetType?.TrimEnd('?'))
{
case "string":
return "like";
case "DateTime":
return "~";
default:
return "==";
}
}
}