1、根据数据表,删除表数据接口迁移至DataModel 已有接口下
2、将根据表明生成实体类,代码迁移至已有数据接口DataModel下
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
using System.Collections;
|
||||
using System.Data;
|
||||
using System.Text;
|
||||
using JNPF.Common.Configuration;
|
||||
using JNPF.Common.Core.Manager;
|
||||
using JNPF.Common.Core.Manager.Files;
|
||||
using JNPF.Common.Dtos.DataBase;
|
||||
@@ -17,6 +18,7 @@ using JNPF.Systems.Entitys.Model.DataBase;
|
||||
using JNPF.Systems.Entitys.System;
|
||||
using JNPF.Systems.Interfaces.System;
|
||||
using Mapster;
|
||||
using Microsoft.AspNetCore.Authorization;
|
||||
using Microsoft.AspNetCore.Http;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using SqlSugar;
|
||||
@@ -323,16 +325,124 @@ public class DataBaseService : IDynamicApiController, ITransient
|
||||
await Create(linkid, data);
|
||||
}
|
||||
|
||||
#endregion
|
||||
/// <summary>
|
||||
/// 清除表数据
|
||||
/// </summary>
|
||||
/// <param name="linkId">数据库配置主键</param>
|
||||
/// <param name="input"></param>
|
||||
/// <returns></returns>
|
||||
[HttpPost("{linkId}/clean-data")]
|
||||
public async Task<dynamic> CleanTableData(string linkId, DatabaseTableDataCleanInput input)
|
||||
{
|
||||
SqlSugarScope sugarClient = null!;
|
||||
if (linkId == "0")
|
||||
{
|
||||
ConnectionStringsOptions options = App.GetConfig<ConnectionStringsOptions>("ConnectionStrings", true);
|
||||
var connCfg = options.Adapt<ConnectionConfig>();
|
||||
sugarClient = new SqlSugarScope(connCfg);
|
||||
}
|
||||
else
|
||||
{
|
||||
var link = await _dbLinkService.GetInfo(linkId);
|
||||
sugarClient = _dataBaseManager.ChangeDataBase(link);
|
||||
}
|
||||
return await sugarClient.Deleteable<object>().AS(input.TableName).ExecuteCommandAsync();
|
||||
}
|
||||
/// <summary>
|
||||
/// 生成代码
|
||||
/// </summary>
|
||||
/// <param name="linkId">数据库配置表主键</param>
|
||||
/// <param name="input">post输入参数</param>
|
||||
/// <returns></returns>
|
||||
/// <exception cref="ArgumentNullException"></exception>
|
||||
/// <exception cref="ArgumentException"></exception>
|
||||
[HttpPost("{linkId}/gen-code")]
|
||||
public async Task<dynamic> GenerateCode(string linkId,DatabaseTableDataCleanInput input)
|
||||
{
|
||||
if (input.TableName.IsNullOrWhiteSpace())
|
||||
{
|
||||
throw new ArgumentNullException(nameof(input.TableName));
|
||||
}
|
||||
if (!input.TableName.Contains("_"))
|
||||
{
|
||||
throw new ArgumentException($"表【{input.TableName}】,表名之间必须包含_");
|
||||
}
|
||||
var dir = Path.Combine(FileVariable.GenerateCodePath, "DbModel");
|
||||
if (!Directory.Exists(dir))
|
||||
{
|
||||
Directory.CreateDirectory(dir);
|
||||
}
|
||||
SqlSugarScope sugarClient = null!;
|
||||
if (linkId == "0") //默认时,使用当前默认数据库配置
|
||||
{
|
||||
ConnectionStringsOptions options = App.GetConfig<ConnectionStringsOptions>("ConnectionStrings", true);
|
||||
var connCfg = options.Adapt<ConnectionConfig>();
|
||||
sugarClient = new SqlSugarScope(connCfg);
|
||||
}
|
||||
else
|
||||
{
|
||||
var link = await _dbLinkService.GetInfo(linkId);
|
||||
sugarClient = _dataBaseManager.ChangeDataBase(link);
|
||||
}
|
||||
|
||||
#region PrivateMethod
|
||||
string CustomFormatName(string s)
|
||||
{
|
||||
var pos = 0;
|
||||
if ((pos = s.IndexOf("_", StringComparison.Ordinal)) > -1)
|
||||
{
|
||||
var first = s.AsSpan().Slice(0, pos).ToString().ToUpperCase();
|
||||
var second = s.AsSpan().Slice(pos + 1).ToString().ToUpperCase();
|
||||
return $"{first}{second}";
|
||||
}
|
||||
else
|
||||
{
|
||||
return s.ToUpperCase();
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 是否系统表.
|
||||
/// </summary>
|
||||
/// <param name="table"></param>
|
||||
/// <returns></returns>
|
||||
private bool IsSysTable(string table)
|
||||
foreach (var item in sugarClient.DbMaintenance.GetTableInfoList().Where(t => t.Name == input.TableName))
|
||||
{
|
||||
string entityName = CustomFormatName(item.Name);/*实体名首字母大写*/
|
||||
sugarClient.MappingTables.Add(entityName, item.Name);
|
||||
foreach (var col in sugarClient.DbMaintenance.GetColumnInfosByTableName(item.Name))
|
||||
{
|
||||
var colName = CustomFormatName(col.DbColumnName);
|
||||
sugarClient.MappingColumns.Add(colName /*类的属性首字母大写*/, col.DbColumnName, entityName);
|
||||
}
|
||||
}
|
||||
|
||||
var newFileName = "";
|
||||
var pos = input.TableName.IndexOf("_", StringComparison.Ordinal);
|
||||
var first = input.TableName.AsSpan().Slice(0, pos).ToString().ToUpperCase();
|
||||
var second = input.TableName.AsSpan().Slice(pos + 1).ToString().ToUpperCase();
|
||||
newFileName = $"{first}{second}";
|
||||
|
||||
sugarClient.DbFirst.Where(input.TableName)
|
||||
.FormatFileName(CustomFormatName)
|
||||
.IsCreateAttribute()
|
||||
.CreateClassFile(dir, "DbModels");
|
||||
|
||||
var previewContent = "";
|
||||
var codeFile = Path.Combine(dir, $"{CustomFormatName(input.TableName)}.cs");
|
||||
if (File.Exists(codeFile))
|
||||
{
|
||||
using (var sr = File.OpenText(codeFile))
|
||||
{
|
||||
previewContent = await sr.ReadToEndAsync();
|
||||
}
|
||||
}
|
||||
return previewContent;
|
||||
}
|
||||
#endregion
|
||||
|
||||
#region PrivateMethod
|
||||
|
||||
/// <summary>
|
||||
/// 是否系统表.
|
||||
/// </summary>
|
||||
/// <param name="table"></param>
|
||||
/// <returns></returns>
|
||||
private bool IsSysTable(string table)
|
||||
{
|
||||
string[] byoTable =
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user