using SqlSugar;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
namespace Admin.Core.Common
{
public class FrameSeed
{
///
/// 生成Controller层
///
/// sqlsugar实例
/// 数据库链接ID
/// 数据库表名数组,默认空,生成所有表
///
///
public static bool CreateControllers(SqlSugarClient sqlSugarClient, string ConnId = null, bool isMuti = false, string[] tableNames = null)
{
Create_Controller_ClassFileByDBTalbe(sqlSugarClient, ConnId, $@"E:\my-file\Admin.Core.Api.Controllers", "Admin.Core.Api", tableNames, "", isMuti);
return true;
}
///
/// 生成Model层
///
/// sqlsugar实例
/// 数据库链接ID
/// 数据库表名数组,默认空,生成所有表
///
///
public static bool CreateModels(SqlSugarClient sqlSugarClient, string ConnId, bool isMuti = false, string[] tableNames = null)
{
Create_Model_ClassFileByDBTalbe(sqlSugarClient, ConnId, $@"E:\my-file\Admin.Core.Model", "Admin.Core.Model", tableNames, "", isMuti);
return true;
}
///
/// 生成IRepository层
///
/// sqlsugar实例
/// 数据库链接ID
///
/// 数据库表名数组,默认空,生成所有表
///
public static bool CreateIRepositorys(SqlSugarClient sqlSugarClient, string ConnId, bool isMuti = false, string[] tableNames = null)
{
Create_IRepository_ClassFileByDBTalbe(sqlSugarClient, ConnId, $@"E:\my-file\Admin.Core.IRepository", "Admin.Core.IRepository", tableNames, "", isMuti);
return true;
}
///
/// 生成 IService 层
///
/// sqlsugar实例
/// 数据库链接ID
///
/// 数据库表名数组,默认空,生成所有表
///
public static bool CreateIServices(SqlSugarClient sqlSugarClient, string ConnId, bool isMuti = false, string[] tableNames = null)
{
Create_IServices_ClassFileByDBTalbe(sqlSugarClient, ConnId, $@"E:\my-file\Admin.Core.IService", "Admin.Core.IService", tableNames, "", isMuti);
return true;
}
///
/// 生成 Repository 层
///
/// sqlsugar实例
/// 数据库链接ID
///
/// 数据库表名数组,默认空,生成所有表
///
public static bool CreateRepository(SqlSugarClient sqlSugarClient, string ConnId, bool isMuti = false, string[] tableNames = null)
{
Create_Repository_ClassFileByDBTalbe(sqlSugarClient, ConnId, $@"E:\my-file\Admin.Core.Repository", "Admin.Core.Repository", tableNames, "", isMuti);
return true;
}
///
/// 生成 Service 层
///
/// sqlsugar实例
/// 数据库链接ID
///
/// 数据库表名数组,默认空,生成所有表
///
public static bool CreateServices(SqlSugarClient sqlSugarClient, string ConnId, bool isMuti = false, string[] tableNames = null)
{
Create_Services_ClassFileByDBTalbe(sqlSugarClient, ConnId, $@"E:\my-file\Admin.Core.Servic", "Admin.Core.Service", tableNames, "", isMuti);
return true;
}
#region 根据数据库表生产Controller层
///
/// 功能描述:根据数据库表生产Controller层
/// 作 者:Admin.Core
///
///
/// 数据库链接ID
/// 实体类存放路径
/// 命名空间
/// 生产指定的表
/// 实现接口
///
/// 是否序列化
private static void Create_Controller_ClassFileByDBTalbe(
SqlSugarClient sqlSugarClient,
string ConnId,
string strPath,
string strNameSpace,
string[] lstTableNames,
string strInterface,
bool isMuti = false,
bool blnSerializable = false)
{
var IDbFirst = sqlSugarClient.DbFirst;
if (lstTableNames != null && lstTableNames.Length > 0)
{
IDbFirst = IDbFirst.Where(lstTableNames);
}
var ls = IDbFirst.IsCreateDefaultValue().IsCreateAttribute()
.SettingClassTemplate(p => p =
@"using Admin.Core.IService;
using Admin.Core.Service;
using Admin.Core.IService.ISys;
using Admin.Core.Model;
using Admin.Core.Common.Resource;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Linq.Expressions;
using System.Threading.Tasks;
using Admin.Core.Common;
namespace " + strNameSpace + @"
{
///
/// {ClassName}Controller
///
[Route(""api/[controller]/[action]"")]
[ApiController]
[Authorize(Permissions.Name)]
public class {ClassName}Controller : BaseApiUserController
{
///
/// _{ClassName}Service
///
private readonly I{ClassName}Services _{ClassName}Service;
///
/// 构造方法
///
///
public {ClassName}Controller(I{ClassName}Services {ClassName}Services,ISysUserService sysUserService) : base(sysUserService)
{
_sysUserService = sysUserService;
_{ClassName}Service = {ClassName}Services;
}
///
/// 分页查询
///
/// 第几页
///
/// 每页大小
///
[HttpGet]
public MessageModel> GetByPage(int page = 1, string key = """",int intPageSize = 50)
{
if (string.IsNullOrEmpty(key) || string.IsNullOrWhiteSpace(key))
{
key = """";
}
Expression> whereExpression = a => true;
var data = _{ClassName}Service.QueryPageAsync(whereExpression, page, intPageSize, ""UpdateTime"").Result;
return SuccessPage(data);
}
///
/// 根据ID查询
///
/// 主键
///
[HttpGet]
public async Task> GetByID(int id)
{
return Success(await _{ClassName}Service.QueryByIdAsync(id));
}
///
/// 新增一条数据
///
/// {ClassName}
///
[HttpPost]
public MessageModel Add([FromBody] {ClassName} request)
{
var data = new MessageModel();
var RData = _{ClassName}Service.AddAsync(request).Result;
data.success = RData > 0;
if (data.success)
{
data.data = RData;
data.msg = Resource_SysBase.OprateSuccess;
}
else data.msg = Resource_SysBase.OprateFail;
return data;
}
///
/// 更新一条数据
///
/// {ClassName}
///
[HttpPost]
public MessageModel Update([FromBody] {ClassName} request)
{
var data = new MessageModel();
var RData = _{ClassName}Service.UpdateAsync(request).Result;
data.success = RData;
if (data.success)
{
data.data = RData;
data.msg = Resource_SysBase.OprateSuccess;
}
else data.msg = Resource_SysBase.OprateFail;
return data;
}
///
/// 删除一条数据
///
/// 主键
///
[HttpPost]
public MessageModel Del(int id)
{
var data = new MessageModel();
var RData = _{ClassName}Service.DeleteByIdAsync(id).Result;
data.success = RData;
if (data.success)
{
data.data = RData;
data.msg = Resource_SysBase.OprateSuccess;
}
else data.msg = Resource_SysBase.OprateFail;
return data;
}
}
}")
.ToClassStringList(strNameSpace);
Dictionary newdic = new Dictionary();
//循环处理 首字母小写 并插入新的 Dictionary
foreach (KeyValuePair item in ls)
{
string newkey = "_" + item.Key.First().ToString().ToLower() + item.Key.Substring(1);
string newvalue = item.Value.Replace("_" + item.Key, newkey);
newdic.Add(item.Key, newvalue);
}
CreateFilesByClassStringList(newdic, strPath, "{0}Controller");
}
#endregion
#region 根据数据库表生产Model层
///
/// 功能描述:根据数据库表生产Model层
/// 作 者:Admin.Core
///
///
/// 数据库链接ID
/// 实体类存放路径
/// 命名空间
/// 生产指定的表
/// 实现接口
///
/// 是否序列化
private static void Create_Model_ClassFileByDBTalbe(
SqlSugarClient sqlSugarClient,
string ConnId,
string strPath,
string strNameSpace,
string[] lstTableNames,
string strInterface,
bool isMuti = false,
bool blnSerializable = false)
{
//多库文件分离
if (isMuti)
{
strPath = strPath + @"\Models\" + ConnId;
strNameSpace = strNameSpace + "." + ConnId;
}
var IDbFirst = sqlSugarClient.DbFirst;
if (lstTableNames != null && lstTableNames.Length > 0)
{
IDbFirst = IDbFirst.Where(lstTableNames);
}
var ls = IDbFirst.IsCreateDefaultValue().IsCreateAttribute()
.SettingClassTemplate(p => p =
@"{using}
namespace " + strNameSpace + @"
{
{ClassDescription}
[SugarTable( ""{ClassName}"", """ + ConnId + @""")]" + (blnSerializable ? "\n [Serializable]" : "") + @"
public class {ClassName}" + (string.IsNullOrEmpty(strInterface) ? "" : (" : " + strInterface)) + @"
{
public {ClassName}()
{
}
{PropertyName}
}
}")
//.SettingPropertyDescriptionTemplate(p => p = string.Empty)
.SettingPropertyTemplate(p => p =
@"{SugarColumn}
public {PropertyType} {PropertyName} { get; set; }")
//.SettingConstructorTemplate(p => p = " this._{PropertyName} ={DefaultValue};")
.ToClassStringList(strNameSpace);
CreateFilesByClassStringList(ls, strPath, "{0}");
}
#endregion
#region 根据数据库表生产IRepository层
///
/// 功能描述:根据数据库表生产IRepository层
/// 作 者:Admin.Core
///
///
/// 数据库链接ID
/// 实体类存放路径
/// 命名空间
/// 生产指定的表
/// 实现接口
///
private static void Create_IRepository_ClassFileByDBTalbe(
SqlSugarClient sqlSugarClient,
string ConnId,
string strPath,
string strNameSpace,
string[] lstTableNames,
string strInterface,
bool isMuti = false
)
{
//多库文件分离
if (isMuti)
{
strPath = strPath + @"\" + ConnId;
strNameSpace = strNameSpace + "." + ConnId;
}
var IDbFirst = sqlSugarClient.DbFirst;
if (lstTableNames != null && lstTableNames.Length > 0)
{
IDbFirst = IDbFirst.Where(lstTableNames);
}
var ls = IDbFirst.IsCreateDefaultValue().IsCreateAttribute()
.SettingClassTemplate(p => p =
@"using Admin.Core.Model" + (isMuti ? "." + ConnId + "" : "") + @";
namespace " + strNameSpace + @"
{
///
/// I{ClassName}Repository
///
public interface I{ClassName}Repository : IBaseRepository<{ClassName}>" + (string.IsNullOrEmpty(strInterface) ? "" : (" , " + strInterface)) + @"
{
}
}")
.ToClassStringList(strNameSpace);
CreateFilesByClassStringList(ls, strPath, "I{0}Repository");
}
#endregion
#region 根据数据库表生产IService层
///
/// 功能描述:根据数据库表生产IService层
/// 作 者:Admin.Core
///
///
/// 数据库链接ID
/// 实体类存放路径
/// 命名空间
/// 生产指定的表
/// 实现接口
///
private static void Create_IServices_ClassFileByDBTalbe(
SqlSugarClient sqlSugarClient,
string ConnId,
string strPath,
string strNameSpace,
string[] lstTableNames,
string strInterface,
bool isMuti = false)
{
//多库文件分离
if (isMuti)
{
strPath = strPath + @"\" + ConnId;
strNameSpace = strNameSpace + "." + ConnId;
}
var IDbFirst = sqlSugarClient.DbFirst;
if (lstTableNames != null && lstTableNames.Length > 0)
{
IDbFirst = IDbFirst.Where(lstTableNames);
}
var ls = IDbFirst.IsCreateDefaultValue().IsCreateAttribute()
.SettingClassTemplate(p => p =
@"using Admin.Core.IService;
using Admin.Core.Model" + (isMuti ? "." + ConnId + "" : "") + @";
namespace " + strNameSpace + @"
{
///
/// I{ClassName}Services
///
public interface I{ClassName}Services :IBaseServices<{ClassName}>" + (string.IsNullOrEmpty(strInterface) ? "" : (" , " + strInterface)) + @"
{
}
}")
.ToClassStringList(strNameSpace);
CreateFilesByClassStringList(ls, strPath, "I{0}Services");
}
#endregion
#region 根据数据库表生产 Repository 层
///
/// 功能描述:根据数据库表生产 Repository 层
/// 作 者:Admin.Core
///
///
/// 数据库链接ID
/// 实体类存放路径
/// 命名空间
/// 生产指定的表
/// 实现接口
///
private static void Create_Repository_ClassFileByDBTalbe(
SqlSugarClient sqlSugarClient,
string ConnId,
string strPath,
string strNameSpace,
string[] lstTableNames,
string strInterface,
bool isMuti = false)
{
//多库文件分离
if (isMuti)
{
strPath = strPath + @"\" + ConnId;
strNameSpace = strNameSpace + "." + ConnId;
}
var IDbFirst = sqlSugarClient.DbFirst;
if (lstTableNames != null && lstTableNames.Length > 0)
{
IDbFirst = IDbFirst.Where(lstTableNames);
}
var ls = IDbFirst.IsCreateDefaultValue().IsCreateAttribute()
.SettingClassTemplate(p => p =
@"using Admin.Core.IRepository" + (isMuti ? "." + ConnId + "" : "") + @";
using Admin.Core.Model" + (isMuti ? "." + ConnId + "" : "") + @";
namespace " + strNameSpace + @"
{
///
/// {ClassName}Repository
///
public class {ClassName}Repository : BaseRepository<{ClassName}>, I{ClassName}Repository" + (string.IsNullOrEmpty(strInterface) ? "" : (" , " + strInterface)) + @"
{
public {ClassName}Repository(IUnitOfWork unitOfWork) : base(unitOfWork)
{
}
}
}")
.ToClassStringList(strNameSpace);
CreateFilesByClassStringList(ls, strPath, "{0}Repository");
}
#endregion
#region 根据数据库表生产 Service 层
///
/// 功能描述:根据数据库表生产 Service 层
/// 作 者:Admin.Core
///
///
/// 数据库链接ID
/// 实体类存放路径
/// 命名空间
/// 生产指定的表
/// 实现接口
///
private static void Create_Services_ClassFileByDBTalbe(
SqlSugarClient sqlSugarClient,
string ConnId,
string strPath,
string strNameSpace,
string[] lstTableNames,
string strInterface,
bool isMuti = false)
{
//多库文件分离
if (isMuti)
{
strPath = strPath + @"\" + ConnId;
strNameSpace = strNameSpace + "." + ConnId;
}
var IDbFirst = sqlSugarClient.DbFirst;
if (lstTableNames != null && lstTableNames.Length > 0)
{
IDbFirst = IDbFirst.Where(lstTableNames);
}
var ls = IDbFirst.IsCreateDefaultValue().IsCreateAttribute()
.SettingClassTemplate(p => p =
@"using Admin.Core.IRepository;
using Admin.Core.Service;
using Admin.Core.IService" + (isMuti ? "." + ConnId + "" : "") + @";
using Admin.Core.Model" + (isMuti ? "." + ConnId + "" : "") + @";
namespace " + strNameSpace + @"
{
public class {ClassName}Services : BaseServices<{ClassName}>, I{ClassName}Services" + (string.IsNullOrEmpty(strInterface) ? "" : (" , " + strInterface)) + @"
{
private readonly IBaseRepository<{ClassName}> _dal;
public {ClassName}Services(IBaseRepository<{ClassName}> dal)
{
this._dal = dal;
base.BaseDal = dal;
}
}
}")
.ToClassStringList(strNameSpace);
CreateFilesByClassStringList(ls, strPath, "{0}Services");
}
#endregion
#region 根据模板内容批量生成文件
///
/// 根据模板内容批量生成文件
///
/// 类文件字符串list
/// 生成路径
/// 文件名格式模板
private static void CreateFilesByClassStringList(Dictionary ls, string strPath, string fileNameTp)
{
foreach (var item in ls)
{
var fileName = $"{string.Format(fileNameTp, item.Key)}.cs";
var fileFullPath = Path.Combine(strPath, fileName);
if (!Directory.Exists(strPath))
{
Directory.CreateDirectory(strPath);
}
File.WriteAllText(fileFullPath, item.Value, Encoding.UTF8);
}
}
#endregion
}
}