using EntrustSettle.Common.Const; using EntrustSettle.Common.DB; using EntrustSettle.Common.Seed; using EntrustSettle.Model; using Microsoft.AspNetCore.Mvc; using SqlSugar; namespace EntrustSettle.Controllers { [ApiExplorerSettings(GroupName = ApiGroupNameConst.System)] public class DbFirstController : BaseApiController { private readonly SqlSugarScope _sqlSugarClient; private readonly IWebHostEnvironment Env; /// /// 构造函数 /// public DbFirstController(ISqlSugarClient sqlSugarClient, IWebHostEnvironment env) { _sqlSugarClient = sqlSugarClient as SqlSugarScope; Env = env; } /// /// 获取 整体框架 文件(主库)(一般可用第一次生成) /// /// [HttpGet] public MessageModel GetFrameFiles() { var data = new MessageModel() { success = true, msg = "" }; data.data += @"file path is:C:\my-file\"; var isMuti = BaseDBConfig.IsMulti; if (Env.IsDevelopment()) { data.data += $"Controller层生成:{FrameSeed.CreateControllers(_sqlSugarClient)} || "; BaseDBConfig.ValidConfig.ForEach(m => { _sqlSugarClient.ChangeDatabase(m.ConfigId.ToLower()); data.data += $"库{m.ConfigId}-Model层生成:{FrameSeed.CreateModels(_sqlSugarClient, m.ConfigId, isMuti)} || "; data.data += $"库{m.ConfigId}-IRepositorys层生成:{FrameSeed.CreateIRepositorys(_sqlSugarClient, m.ConfigId, isMuti)} || "; data.data += $"库{m.ConfigId}-IServices层生成:{FrameSeed.CreateIServices(_sqlSugarClient, m.ConfigId, isMuti)} || "; data.data += $"库{m.ConfigId}-Repository层生成:{FrameSeed.CreateRepository(_sqlSugarClient, m.ConfigId, isMuti)} || "; data.data += $"库{m.ConfigId}-Services层生成:{FrameSeed.CreateServices(_sqlSugarClient, m.ConfigId, isMuti)} || "; }); // 切回主库 _sqlSugarClient.ChangeDatabase(DBConst.MainDbConfigId.ToLower()); } else { data.success = false; data.msg = "当前不处于开发模式,代码生成不可用!"; } return data; } /// /// 获取仓储层和服务层(需指定表名和数据库) /// /// 数据库链接名称 /// 需要生成的表名 /// [HttpPost] public MessageModel GetFrameFilesByTableNames([FromBody]string[] tableNames, [FromQuery]string ConnID = null) { ConnID = ConnID == null ? DBConst.MainDbConfigId.ToLower() : ConnID; var isMuti = BaseDBConfig.IsMulti; var data = new MessageModel() { success = true, msg = "" }; if (Env.IsDevelopment()) { data.data += $"库{ConnID}-IRepositorys层生成:{FrameSeed.CreateIRepositorys(_sqlSugarClient, ConnID, isMuti, tableNames)} || "; data.data += $"库{ConnID}-IServices层生成:{FrameSeed.CreateIServices(_sqlSugarClient, ConnID, isMuti, tableNames)} || "; data.data += $"库{ConnID}-Repository层生成:{FrameSeed.CreateRepository(_sqlSugarClient, ConnID, isMuti, tableNames)} || "; data.data += $"库{ConnID}-Services层生成:{FrameSeed.CreateServices(_sqlSugarClient, ConnID, isMuti, tableNames)} || "; } else { data.success = false; data.msg = "当前不处于开发模式,代码生成不可用!"; } return data; } /// /// 获取实体(需指定表名和数据库) /// /// 数据库链接名称 /// 需要生成的表名 /// [HttpPost] public MessageModel GetFrameFilesByTableNamesForEntity([FromBody] string[] tableNames, [FromQuery] string ConnID = null) { ConnID = ConnID == null ? DBConst.MainDbConfigId.ToLower() : ConnID; var isMuti = BaseDBConfig.IsMulti; var data = new MessageModel() { success = true, msg = "" }; if (Env.IsDevelopment()) { data.data += $"库{ConnID}-Models层生成:{FrameSeed.CreateModels(_sqlSugarClient, ConnID, isMuti, tableNames)}"; } else { data.success = false; data.msg = "当前不处于开发模式,代码生成不可用!"; } return data; } /// /// 获取控制器(需指定表名和数据库) /// /// 数据库链接名称 /// 需要生成的表名 /// [HttpPost] public MessageModel GetFrameFilesByTableNamesForController([FromBody] string[] tableNames, [FromQuery] string ConnID = null) { ConnID = ConnID == null ? DBConst.MainDbConfigId.ToLower() : ConnID; var isMuti = BaseDBConfig.IsMulti; var data = new MessageModel() { success = true, msg = "" }; if (Env.IsDevelopment()) { data.data += $"库{ConnID}-Controllers层生成:{FrameSeed.CreateControllers(_sqlSugarClient, ConnID, isMuti, tableNames)}"; } else { data.success = false; data.msg = "当前不处于开发模式,代码生成不可用!"; } return data; } /// /// DbFrist 根据数据库表名 生成整体框架,包含Model层(一般可用第一次生成) /// /// 数据库链接名称 /// 需要生成的表名 /// [HttpPost] public MessageModel GetAllFrameFilesByTableNames([FromBody]string[] tableNames, [FromQuery]string ConnID = null) { ConnID = ConnID == null ? DBConst.MainDbConfigId.ToLower() : ConnID; var isMuti = BaseDBConfig.IsMulti; var data = new MessageModel() { success = true, msg = "" }; if (Env.IsDevelopment()) { _sqlSugarClient.ChangeDatabase(ConnID.ToLower()); data.data += $"Controller层生成:{FrameSeed.CreateControllers(_sqlSugarClient, ConnID, isMuti, tableNames)} || "; data.data += $"库{ConnID}-Model层生成:{FrameSeed.CreateModels(_sqlSugarClient, ConnID, isMuti, tableNames)} || "; data.data += $"库{ConnID}-IRepositorys层生成:{FrameSeed.CreateIRepositorys(_sqlSugarClient, ConnID, isMuti, tableNames)} || "; data.data += $"库{ConnID}-IServices层生成:{FrameSeed.CreateIServices(_sqlSugarClient, ConnID, isMuti, tableNames)} || "; data.data += $"库{ConnID}-Repository层生成:{FrameSeed.CreateRepository(_sqlSugarClient, ConnID, isMuti, tableNames)} || "; data.data += $"库{ConnID}-Services层生成:{FrameSeed.CreateServices(_sqlSugarClient, ConnID, isMuti, tableNames)} || "; // 切回主库 _sqlSugarClient.ChangeDatabase(DBConst.MainDbConfigId.ToLower()); } else { data.success = false; data.msg = "当前不处于开发模式,代码生成不可用!"; } return data; } } }