using EntrustSettle.Common.Const;
using EntrustSettle.Common.DB.Extension;
using EntrustSettle.Controllers;
using EntrustSettle.Model;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using SqlSugar;
namespace EntrustSettle.Api.Controllers.Systems;
///
/// 动态建表 CURD
///
//[Authorize(Permissions.Name)]
[ApiExplorerSettings(GroupName = ApiGroupNameConst.System)]
public class CodeFirstController : BaseApiController
{
private readonly ISqlSugarClient _db;
public CodeFirstController(ISqlSugarClient db)
{
_db = db;
}
///
/// 动态type
///
///
private Type GetDynamicType()
{
return _db.DynamicBuilder().CreateClass("DynamicTestTable")
//{table} 占位符会自动替换成表名
.CreateIndex(new SugarIndexAttribute("idx_{table}_Code", "Code", OrderByType.Desc))
.CreateProperty("Id", typeof(int), new SugarColumn() {IsPrimaryKey = true, IsIdentity = true})
.CreateProperty("Code", typeof(string), new SugarColumn() {Length = 50})
.CreateProperty("Name", typeof(string), new SugarColumn() {Length = 50})
.WithCache()
.BuilderType();
}
///
/// 动态type 继承BaseEntity
///
///
private Type GetDynamicType2()
{
return _db.DynamicBuilder().CreateClass("DynamicTestTable2", null, typeof(BaseEntity))
//{table} 占位符会自动替换成表名
.CreateIndex(new SugarIndexAttribute("idx_{table}_Code", "Code", OrderByType.Desc))
.CreateProperty("Code", typeof(string), new SugarColumn() {Length = 50})
.CreateProperty("Name", typeof(string), new SugarColumn() {Length = 50})
.WithCache()
.BuilderType();
}
///
/// 测试建表
///
///
[HttpPost]
public MessageModel TestCreateTable()
{
var type = GetDynamicType();
_db.CodeFirst.InitTables(type);
return SuccessMsg();
}
///
/// 测试查询
///
///
[HttpGet]
public MessageModel