using Admin.Core.Common;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Mvc;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
namespace Admin.Core.Api
{
///
/// Controller代码生成
///
[ApiController]
[Route("api/[controller]/[action]")]
[AllowAnonymous]
public class SeedController : ControllerBase
{
private readonly SqlSugarClient _sqlSugarClient;
private readonly IWebHostEnvironment Env;
///
/// 构造方法
///
///
///
public SeedController(ISqlSugarClient sqlSugarClient, IWebHostEnvironment env)
{
_sqlSugarClient = sqlSugarClient as SqlSugarClient;
Env = env;
}
///
/// 获取控制器(需指定表名和数据库)
///
/// 数据库链接名称
/// 需要生成的表名
///
[HttpPost]
public string GetFrameFilesByTableNamesForController([FromBody] string[] tableNames, [FromQuery] string ConnID = null)
{
ConnID = ConnID == null ? MainDb.CurrentDbConnId.ToLower() : ConnID;
var isMuti = Appsettings.app(new string[] { "MutiDBEnabled" }).ObjToBool();
var seed = FrameSeed.CreateControllers(_sqlSugarClient, ConnID, isMuti, tableNames);
var data = $"库{ConnID}-Controllers层生成:{seed}";
return data;
}
///
/// 获取实体(需指定表名和数据库)
///
/// 数据库链接名称
/// 需要生成的表名
///
[HttpPost]
public string GetFrameFilesByTableNamesForEntity([FromBody] string[] tableNames, [FromQuery] string ConnID = null)
{
ConnID = ConnID == null ? MainDb.CurrentDbConnId.ToLower() : ConnID;
var isMuti = Appsettings.app(new string[] { "MutiDBEnabled" }).ObjToBool();
var data = $"库{ConnID}-Models层生成:{FrameSeed.CreateModels(_sqlSugarClient, ConnID, isMuti, tableNames)}";
return data;
}
///
/// 获取控制器、服务、仓储、实体(需指定表名和数据库)
///
/// 数据库链接名称
/// 需要生成的表名
///
[HttpPost]
public string GetFrameFilesByTableNames([FromBody] string[] tableNames, [FromQuery] string ConnID = null)
{
try
{
ConnID = ConnID == null ? MainDb.CurrentDbConnId.ToLower() : ConnID;
var isMuti = Appsettings.app(new string[] { "MutiDBEnabled" }).ObjToBool();
FrameSeed.CreateControllers(_sqlSugarClient, ConnID, isMuti, tableNames);
FrameSeed.CreateIServices(_sqlSugarClient, ConnID, isMuti, tableNames);
FrameSeed.CreateServices(_sqlSugarClient, ConnID, isMuti, tableNames);
FrameSeed.CreateIRepositorys(_sqlSugarClient, ConnID, isMuti, tableNames);
FrameSeed.CreateRepository(_sqlSugarClient, ConnID, isMuti, tableNames);
FrameSeed.CreateModels(_sqlSugarClient, ConnID, isMuti, tableNames);
var data = $"库{ConnID}-控制器、服务、仓储、实体层生成";
return data;
}
catch (Exception)
{
throw;
}
}
}
}