init
This commit is contained in:
75
DouyinApi.Api/Controllers/Systems/CacheManageController.cs
Normal file
75
DouyinApi.Api/Controllers/Systems/CacheManageController.cs
Normal file
@@ -0,0 +1,75 @@
|
||||
using DouyinApi.Common.Caches;
|
||||
using DouyinApi.Common.Caches.Interface;
|
||||
using DouyinApi.Controllers;
|
||||
using DouyinApi.Model;
|
||||
using Microsoft.AspNetCore.Authorization;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
|
||||
namespace DouyinApi.Api.Controllers.Systems;
|
||||
|
||||
/// <summary>
|
||||
/// 缓存管理
|
||||
/// </summary>
|
||||
[Route("api/Systems/[controller]")]
|
||||
[ApiController]
|
||||
[Authorize(Permissions.Name)]
|
||||
public class CacheManageController(ICaching caching) : BaseApiController
|
||||
{
|
||||
/// <summary>
|
||||
/// 获取全部缓存
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
[HttpGet]
|
||||
public MessageModel<List<string>> Get()
|
||||
{
|
||||
return Success(caching.GetAllCacheKeys());
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取缓存
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
[HttpGet("{key}")]
|
||||
public async Task<MessageModel<string>> Get(string key)
|
||||
{
|
||||
return Success<string>(await caching.GetStringAsync(key));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 新增
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
[HttpPost]
|
||||
public async Task<MessageModel> Post([FromQuery] string key, [FromQuery] string value, [FromQuery] int? expire)
|
||||
{
|
||||
if (expire.HasValue)
|
||||
await caching.SetStringAsync(key, value, TimeSpan.FromMilliseconds(expire.Value));
|
||||
else
|
||||
await caching.SetStringAsync(key, value);
|
||||
|
||||
return Success();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 删除全部缓存
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
[HttpDelete]
|
||||
public MessageModel Delete()
|
||||
{
|
||||
caching.RemoveAll();
|
||||
return Success();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 删除缓存
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
[Route("{key}")]
|
||||
[HttpDelete]
|
||||
public async Task<MessageModel> Delete(string key)
|
||||
{
|
||||
await caching.RemoveAsync(key);
|
||||
return Success();
|
||||
}
|
||||
}
|
||||
192
DouyinApi.Api/Controllers/Systems/DataBaseController.cs
Normal file
192
DouyinApi.Api/Controllers/Systems/DataBaseController.cs
Normal file
@@ -0,0 +1,192 @@
|
||||
using System.Diagnostics.CodeAnalysis;
|
||||
using DouyinApi.Common;
|
||||
using DouyinApi.Common.DB;
|
||||
using DouyinApi.Controllers;
|
||||
using DouyinApi.Model;
|
||||
using DouyinApi.Model.Models;
|
||||
using DouyinApi.Model.Systems.DataBase;
|
||||
using DouyinApi.Model.Tenants;
|
||||
using Mapster;
|
||||
using Microsoft.AspNetCore.Authorization;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using SqlSugar;
|
||||
|
||||
namespace DouyinApi.Api.Controllers.Systems;
|
||||
|
||||
/// <summary>
|
||||
/// 数据库管理
|
||||
/// </summary>
|
||||
[Route("api/Systems/[controller]/[action]")]
|
||||
[ApiController]
|
||||
[Authorize(Permissions.Name)]
|
||||
public class DataBaseController : BaseApiController
|
||||
{
|
||||
private readonly ISqlSugarClient _db;
|
||||
|
||||
public DataBaseController(ISqlSugarClient db)
|
||||
{
|
||||
_db = db;
|
||||
}
|
||||
|
||||
[return: NotNull]
|
||||
private ISqlSugarClient GetTenantDb(string configId)
|
||||
{
|
||||
if (!_db.AsTenant().IsAnyConnection(configId))
|
||||
{
|
||||
var tenant = _db.Queryable<SysTenant>().WithCache()
|
||||
.Where(s => s.TenantType == TenantTypeEnum.Db)
|
||||
.Where(s => s.ConfigId == configId)
|
||||
.First();
|
||||
if (tenant != null)
|
||||
{
|
||||
_db.AsTenant().AddConnection(tenant.GetConnectionConfig());
|
||||
}
|
||||
}
|
||||
|
||||
var db = _db.AsTenant().GetConnectionScope(configId);
|
||||
if (db is null)
|
||||
{
|
||||
throw new ApplicationException("无效的数据库配置");
|
||||
}
|
||||
|
||||
return db;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取库配置
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
[HttpGet]
|
||||
public async Task<MessageModel<List<DatabaseOutput>>> GetAllConfig()
|
||||
{
|
||||
//增加多租户的连接
|
||||
var allConfigs = new List<ConnectionConfig>(BaseDBConfig.AllConfigs);
|
||||
var tenants = await _db.Queryable<SysTenant>().WithCache()
|
||||
.Where(s => s.TenantType == TenantTypeEnum.Db)
|
||||
.ToListAsync();
|
||||
if (tenants.Any())
|
||||
{
|
||||
allConfigs.AddRange(tenants.Select(tenant => tenant.GetConnectionConfig()));
|
||||
}
|
||||
|
||||
var configs = await Task.FromResult(allConfigs);
|
||||
return Success(configs.Adapt<List<DatabaseOutput>>());
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取表信息
|
||||
/// </summary>
|
||||
/// <param name="configId">配置Id</param>
|
||||
/// <param name="readType">读取类型</param>
|
||||
/// <returns></returns>
|
||||
[HttpGet]
|
||||
public MessageModel<List<DbTableInfo>> GetTableInfoList(string configId,
|
||||
DataBaseReadType readType = DataBaseReadType.Db)
|
||||
{
|
||||
if (configId.IsNullOrEmpty())
|
||||
{
|
||||
configId = MainDb.CurrentDbConnId;
|
||||
}
|
||||
|
||||
configId = configId.ToLower();
|
||||
|
||||
var provider = GetTenantDb(configId);
|
||||
List<DbTableInfo> data = null;
|
||||
switch (readType)
|
||||
{
|
||||
case DataBaseReadType.Db:
|
||||
data = provider.DbMaintenance.GetTableInfoList(false);
|
||||
break;
|
||||
case DataBaseReadType.Entity:
|
||||
if (EntityUtility.TenantEntitys.TryGetValue(configId, out var types))
|
||||
{
|
||||
data = types.Select(s => provider.EntityMaintenance.GetEntityInfo(s))
|
||||
.Select(s => new {Name = s.DbTableName, Description = s.TableDescription})
|
||||
.Adapt<List<DbTableInfo>>();
|
||||
}
|
||||
|
||||
break;
|
||||
}
|
||||
|
||||
|
||||
return Success(data);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取表字段
|
||||
/// </summary>
|
||||
/// <param name="tableName">表名</param>
|
||||
/// <param name="configId">ConfigId</param>
|
||||
/// <param name="readType">读取类型</param>
|
||||
/// <returns></returns>
|
||||
[HttpGet]
|
||||
public MessageModel<List<DbColumnInfoOutput>> GetColumnInfosByTableName(string tableName, string configId = null,
|
||||
DataBaseReadType readType = DataBaseReadType.Db)
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(tableName))
|
||||
return Failed<List<DbColumnInfoOutput>>("表名不能为空");
|
||||
|
||||
if (configId.IsNullOrEmpty())
|
||||
{
|
||||
configId = MainDb.CurrentDbConnId;
|
||||
}
|
||||
|
||||
configId = configId.ToLower();
|
||||
|
||||
List<DbColumnInfoOutput> data = null;
|
||||
var provider = GetTenantDb(configId);
|
||||
switch (readType)
|
||||
{
|
||||
case DataBaseReadType.Db:
|
||||
data = provider.DbMaintenance.GetColumnInfosByTableName(tableName, false)
|
||||
.Adapt<List<DbColumnInfoOutput>>();
|
||||
break;
|
||||
case DataBaseReadType.Entity:
|
||||
if (EntityUtility.TenantEntitys.TryGetValue(configId, out var types))
|
||||
{
|
||||
var type = types.FirstOrDefault(s => s.Name == tableName);
|
||||
data = provider.EntityMaintenance.GetEntityInfo(type).Columns.Adapt<List<DbColumnInfoOutput>>();
|
||||
}
|
||||
|
||||
break;
|
||||
}
|
||||
|
||||
|
||||
return Success(data);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 编辑表备注
|
||||
/// </summary>
|
||||
/// <param name="input"></param>
|
||||
[HttpPut]
|
||||
public MessageModel PutTableEditRemark([FromBody] EditTableInput input)
|
||||
{
|
||||
var provider = GetTenantDb(input.ConfigId);
|
||||
if (provider.DbMaintenance.IsAnyTableRemark(input.TableName))
|
||||
{
|
||||
provider.DbMaintenance.DeleteTableRemark(input.TableName);
|
||||
}
|
||||
|
||||
provider.DbMaintenance.AddTableRemark(input.TableName, input.Description);
|
||||
return Success();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 编辑列备注
|
||||
/// </summary>
|
||||
/// <param name="input"></param>
|
||||
[HttpPut]
|
||||
public MessageModel PutColumnEditRemark([FromBody] EditColumnInput input)
|
||||
{
|
||||
var provider = GetTenantDb(input.ConfigId);
|
||||
if (provider.DbMaintenance.IsAnyColumnRemark(input.DbColumnName, input.TableName))
|
||||
{
|
||||
provider.DbMaintenance.DeleteColumnRemark(input.DbColumnName, input.TableName);
|
||||
}
|
||||
|
||||
provider.DbMaintenance.AddColumnRemark(input.DbColumnName, input.TableName, input.ColumnDescription);
|
||||
|
||||
return Success();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,96 @@
|
||||
using DouyinApi.Common.DB.Extension;
|
||||
using DouyinApi.Controllers;
|
||||
using DouyinApi.Model;
|
||||
using DouyinApi.Model.Models.RootTkey;
|
||||
using Microsoft.AspNetCore.Authorization;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using NetTaste;
|
||||
using OfficeOpenXml.FormulaParsing.Excel.Functions.Math;
|
||||
using SqlSugar;
|
||||
|
||||
namespace DouyinApi.Api.Controllers.Systems;
|
||||
|
||||
/// <summary>
|
||||
/// 动态建表 CURD
|
||||
/// </summary>
|
||||
[Route("api/Systems/[controller]/[action]")]
|
||||
[ApiController]
|
||||
[Authorize(Permissions.Name)]
|
||||
public class DynamicCodeFirstController : BaseApiController
|
||||
{
|
||||
private readonly ISqlSugarClient _db;
|
||||
|
||||
public DynamicCodeFirstController(ISqlSugarClient db)
|
||||
{
|
||||
_db = db;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 动态type
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
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();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 动态type 继承BaseEntity
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
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();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 测试建表
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
[HttpPost]
|
||||
public MessageModel TestCreateTable()
|
||||
{
|
||||
var type = GetDynamicType();
|
||||
_db.CodeFirst.InitTables(type);
|
||||
return Success();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 测试查询
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
[HttpGet]
|
||||
public MessageModel<object> TestQuery()
|
||||
{
|
||||
var type = GetDynamicType();
|
||||
return Success(_db.QueryableByObject(type).ToList());
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 测试写入
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
[HttpPost]
|
||||
public MessageModel TestInsert(string code, string name)
|
||||
{
|
||||
var type = GetDynamicType();
|
||||
var entity = Activator.CreateInstance(type);
|
||||
type.GetProperty("Code")!.SetValue(entity, code);
|
||||
type.GetProperty("Name")!.SetValue(entity, name);
|
||||
_db.InsertableByObject(entity).ExecuteCommand();
|
||||
return Success();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user