1、根据数据表,删除表数据接口迁移至DataModel 已有接口下
2、将根据表明生成实体类,代码迁移至已有数据接口DataModel下
This commit is contained in:
@@ -1,7 +0,0 @@
|
|||||||
namespace Tnb.BasicData.Entitys
|
|
||||||
{
|
|
||||||
public class Class1
|
|
||||||
{
|
|
||||||
|
|
||||||
}
|
|
||||||
}
|
|
||||||
19
Tnb.BasicData.Entitys/Mapper/Mapper.cs
Normal file
19
Tnb.BasicData.Entitys/Mapper/Mapper.cs
Normal file
@@ -0,0 +1,19 @@
|
|||||||
|
using JNPF.Systems.Entitys.System;
|
||||||
|
using Mapster;
|
||||||
|
using SqlSugar;
|
||||||
|
|
||||||
|
namespace Tnb.BasicData.Entitys.Mapper
|
||||||
|
{
|
||||||
|
public class Mapper : IRegister
|
||||||
|
{
|
||||||
|
public void Register(TypeAdapterConfig config)
|
||||||
|
{
|
||||||
|
config.ForType<ConnectionStringsOptions, ConnectionConfig>()
|
||||||
|
.Map(dest => dest.ConnectionString, src => src.DefaultConnection)
|
||||||
|
.AfterMapping((src, dest) =>
|
||||||
|
{
|
||||||
|
dest.ConnectionString = string.Format(src.DefaultConnection, src.Host, src.Port, src.DBName, src.UserName, src.Password);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -7,6 +7,7 @@
|
|||||||
</PropertyGroup>
|
</PropertyGroup>
|
||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
|
<ProjectReference Include="..\common\Tnb.Common.Core\Tnb.Common.Core.csproj" />
|
||||||
<ProjectReference Include="..\common\Tnb.Common\Tnb.Common.csproj" />
|
<ProjectReference Include="..\common\Tnb.Common\Tnb.Common.csproj" />
|
||||||
<ProjectReference Include="..\common\Tnb.SqlSugar\Tnb.SqlSugar.csproj" />
|
<ProjectReference Include="..\common\Tnb.SqlSugar\Tnb.SqlSugar.csproj" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|||||||
@@ -1,72 +0,0 @@
|
|||||||
using System;
|
|
||||||
using System.Collections.Generic;
|
|
||||||
using System.Linq;
|
|
||||||
using System.Text;
|
|
||||||
using System.Threading.Tasks;
|
|
||||||
using JNPF.Common.Configuration;
|
|
||||||
using JNPF.Common.Core.Manager;
|
|
||||||
using JNPF.DependencyInjection;
|
|
||||||
using JNPF.DynamicApiController;
|
|
||||||
using JNPF.Systems.Entitys.System;
|
|
||||||
using JNPF.Systems.Interfaces.System;
|
|
||||||
using Microsoft.AspNetCore.Mvc;
|
|
||||||
using Org.BouncyCastle.Bcpg.OpenPgp;
|
|
||||||
using Senparc.Weixin.MP.AdvancedAPIs.NewTmpl.NewTmplJson;
|
|
||||||
using SqlSugar;
|
|
||||||
using Tnb.BasicData.Entitys.Dto.DataModeing;
|
|
||||||
|
|
||||||
namespace Tnb.BasicData
|
|
||||||
{
|
|
||||||
/// <summary>
|
|
||||||
/// 数据库表操作服务
|
|
||||||
/// </summary>
|
|
||||||
[ApiDescriptionSettings(Tag = "BasicData", Name = "Table", Order = 701)]
|
|
||||||
[Route("api/basic/db/[controller]")]
|
|
||||||
public class DataTableOperateService : IDynamicApiController, ITransient
|
|
||||||
{
|
|
||||||
private readonly DataBaseManager _dataBaseManager;
|
|
||||||
private readonly ISqlSugarRepository<DbLinkEntity> _repository;
|
|
||||||
private readonly IDbLinkService _dbLinkService;
|
|
||||||
public DataTableOperateService(
|
|
||||||
DataBaseManager dataBaseManager,
|
|
||||||
ISqlSugarRepository<DbLinkEntity> repository,
|
|
||||||
IDbLinkService dbLinkService
|
|
||||||
)
|
|
||||||
{
|
|
||||||
_dataBaseManager = dataBaseManager;
|
|
||||||
_repository = repository;
|
|
||||||
_dbLinkService = dbLinkService;
|
|
||||||
}
|
|
||||||
/// <summary>
|
|
||||||
/// 清除表数据
|
|
||||||
/// </summary>
|
|
||||||
/// <param name="input"></param>
|
|
||||||
/// <returns></returns>
|
|
||||||
[HttpPost("clean-data")]
|
|
||||||
public async Task<dynamic> CleanTableData(CleanTableDataInput input)
|
|
||||||
{
|
|
||||||
var link = await _dbLinkService.GetInfo(input.LinkId);
|
|
||||||
var sugarClient = _dataBaseManager.ChangeDataBase(link);
|
|
||||||
return await sugarClient.Deleteable<object>().AS(input.TableName).ExecuteCommandAsync();
|
|
||||||
}
|
|
||||||
|
|
||||||
public async Task<dynamic> GenerateCode(CleanTableDataInput input)
|
|
||||||
{
|
|
||||||
var dir = Path.Combine(FileVariable.GenerateCodePath, "DbModel");
|
|
||||||
if (!Directory.Exists(dir))
|
|
||||||
{
|
|
||||||
Directory.CreateDirectory(dir);
|
|
||||||
}
|
|
||||||
var link = await _dbLinkService.GetInfo(input.LinkId);
|
|
||||||
var sugarClient = _dataBaseManager.ChangeDataBase(link);
|
|
||||||
var pos = input.TableName.IndexOf("_", StringComparison.Ordinal);
|
|
||||||
|
|
||||||
|
|
||||||
//sugarClient.DynamicBuilder().CreateClass();
|
|
||||||
sugarClient.DbFirst.Where(input.TableName).CreateClassFile(FileVariable.GenerateCodePath, "DbModels");
|
|
||||||
|
|
||||||
return null;
|
|
||||||
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -5,15 +5,15 @@ using System.Text;
|
|||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
using JNPF.DependencyInjection;
|
using JNPF.DependencyInjection;
|
||||||
|
|
||||||
namespace Tnb.BasicData.Entitys.Dto.DataModeing
|
namespace JNPF.Systems.Entitys.Dto.Database
|
||||||
{
|
{
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 清除表数据输入参数
|
/// 清除表数据输入参数
|
||||||
/// </summary>
|
/// </summary>
|
||||||
[SuppressSniffer]
|
[SuppressSniffer]
|
||||||
public class CleanTableDataInput
|
public class DatabaseTableDataCleanInput
|
||||||
{
|
{
|
||||||
public string LinkId { get; set; }
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 数据库名称
|
/// 数据库名称
|
||||||
/// </summary>
|
/// </summary>
|
||||||
24
system/Tnb.Systems.Entitys/Mapper/ConnectionMapper.cs
Normal file
24
system/Tnb.Systems.Entitys/Mapper/ConnectionMapper.cs
Normal file
@@ -0,0 +1,24 @@
|
|||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Text;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
using Mapster;
|
||||||
|
using SqlSugar;
|
||||||
|
|
||||||
|
namespace Tnb.Systems.Entitys.Mapper
|
||||||
|
{
|
||||||
|
public class ConnectionMapper : IRegister
|
||||||
|
{
|
||||||
|
public void Register(TypeAdapterConfig config)
|
||||||
|
{
|
||||||
|
config.ForType<ConnectionStringsOptions, ConnectionConfig>()
|
||||||
|
.Map(dest => dest.ConnectionString, src => src.DefaultConnection)
|
||||||
|
.AfterMapping((src, dest) =>
|
||||||
|
{
|
||||||
|
dest.ConnectionString = string.Format(src.DefaultConnection, src.Host, src.Port, src.DBName, src.UserName, src.Password);
|
||||||
|
});
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,6 +1,7 @@
|
|||||||
using System.Collections;
|
using System.Collections;
|
||||||
using System.Data;
|
using System.Data;
|
||||||
using System.Text;
|
using System.Text;
|
||||||
|
using JNPF.Common.Configuration;
|
||||||
using JNPF.Common.Core.Manager;
|
using JNPF.Common.Core.Manager;
|
||||||
using JNPF.Common.Core.Manager.Files;
|
using JNPF.Common.Core.Manager.Files;
|
||||||
using JNPF.Common.Dtos.DataBase;
|
using JNPF.Common.Dtos.DataBase;
|
||||||
@@ -17,6 +18,7 @@ using JNPF.Systems.Entitys.Model.DataBase;
|
|||||||
using JNPF.Systems.Entitys.System;
|
using JNPF.Systems.Entitys.System;
|
||||||
using JNPF.Systems.Interfaces.System;
|
using JNPF.Systems.Interfaces.System;
|
||||||
using Mapster;
|
using Mapster;
|
||||||
|
using Microsoft.AspNetCore.Authorization;
|
||||||
using Microsoft.AspNetCore.Http;
|
using Microsoft.AspNetCore.Http;
|
||||||
using Microsoft.AspNetCore.Mvc;
|
using Microsoft.AspNetCore.Mvc;
|
||||||
using SqlSugar;
|
using SqlSugar;
|
||||||
@@ -323,16 +325,124 @@ public class DataBaseService : IDynamicApiController, ITransient
|
|||||||
await Create(linkid, data);
|
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>
|
foreach (var item in sugarClient.DbMaintenance.GetTableInfoList().Where(t => t.Name == input.TableName))
|
||||||
/// 是否系统表.
|
{
|
||||||
/// </summary>
|
string entityName = CustomFormatName(item.Name);/*实体名首字母大写*/
|
||||||
/// <param name="table"></param>
|
sugarClient.MappingTables.Add(entityName, item.Name);
|
||||||
/// <returns></returns>
|
foreach (var col in sugarClient.DbMaintenance.GetColumnInfosByTableName(item.Name))
|
||||||
private bool IsSysTable(string table)
|
{
|
||||||
|
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 =
|
string[] byoTable =
|
||||||
{
|
{
|
||||||
|
|||||||
Reference in New Issue
Block a user