using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Rhea.Common;
using Tiger.Model;
using Microsoft.AspNetCore.Cors;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Newtonsoft.Json;
using Tiger.IBusiness;
using Tiger.Api.iBiz;
using System.IO;
namespace Tiger.Api.Controllers.Base
{
///
/// Api基础服务
///
[Route("api/[controller]/[action]")]
[EnableCors("Any")]
[ApiController]
public partial class BaseController : ControllerBase
{
#region 添加操作
///
/// Add(ApiAction) :把ApiAction.Data中的单个实体往数据库添加一行数据
///
///
///
[HttpPost]
public async Task Add([FromBody] ApiAction action)
{
ApiAction response;
try
{
response = action.GetResponse(await Biz.Base.AddAsync(action.GetDataEntity(), action.IsAsync));
}
catch (System.Exception ex)
{
response = action.GetResponse().CatchExceptionWithLog(ex);
}
return Ok(response);
}
///
/// AddList(ApiAction) :把ApiAction.Data中的多个实体往数据库批量添加数据
///
///
///
[HttpPost]
public async Task AddList([FromBody] ApiAction action)//>
{
ApiAction response;
try
{
response = action.GetResponse(await Biz.Base.AddListAsync(action.GetDataEntity(), action.IsAsync));
}
catch (System.Exception ex)
{
response = action.GetResponse().CatchExceptionWithLog(ex);
}
return Ok(response);
}
#endregion
#region 保存操作
///
/// Save(ApiAction) :把ApiAction.Data中的单个实体往数据库保存一行数据,如果存在就更新,不存在就插入
///
///
///
[HttpPost]
public async Task Save([FromBody] ApiAction action)
{
ApiAction response;
try
{
response = action.GetResponse(await Biz.Base.SaveAsync(action.GetDataEntity(), action.IsAsync));
}
catch (System.Exception ex)
{
response = action.GetResponse().CatchExceptionWithLog(ex);
}
return Ok(response);
}
///
/// SaveList(ApiAction) :把ApiAction.Data中的多个实体往数据库批量保存数据,如果存在就更新,不存在就插入
///
///
///
[HttpPost]
public async Task SaveList([FromBody] ApiAction action)
{
ApiAction response;
try
{
response = action.GetResponse(await Biz.Base.SaveListAsync(action.GetDataEntity(), action.IsAsync));
}
catch (System.Exception ex)
{
response = action.GetResponse().CatchExceptionWithLog(ex);
}
return Ok(response);
}
#endregion
#region 修改操作
///
/// Update(ApiAction) :从数据库中修改ApiAction.Data中的单个实体
///
///
///
[HttpPost]
public async Task Update([FromBody] ApiAction action)
{
ApiAction response;
try
{
response = action.GetResponse(await Biz.Base.UpdateAsync(action.GetDataEntity(), action.IsAsync));
}
catch (System.Exception ex)
{
response = action.GetResponse().CatchExceptionWithLog(ex);
}
return Ok(response);
}
///
/// UpdateList(ApiAction) :从数据库中修改ApiAction.Data中的多个实体
///
///
///
[HttpPost]
public async Task UpdateList([FromBody] ApiAction action)
{
ApiAction response;
try
{
response = action.GetResponse(await Biz.Base.UpdateAsync(action.GetDataEntity(), action.IsAsync));
}
catch (System.Exception ex)
{
response = action.GetResponse().CatchExceptionWithLog(ex);
}
return Ok(response);
}
///
/// UpdateByColumn(ApiAction) :按ApiAction.Data中UpdateAble对象从数据库中修改多个实体的某些字段
///
///
///
[HttpPost]
public async Task UpdateByColumn([FromBody] ApiAction action)
{
ApiAction response;
try
{
response = action.GetResponse(await Biz.Base.UpdateAsync(action.GetDataEntity(), action.IsAsync));
}
catch (System.Exception ex)
{
response = action.GetResponse().CatchExceptionWithLog(ex);
}
return Ok(response);
}
#endregion
#region 删除操作
///
/// Delete(ApiAction) :从数据库中把ApiAction.Data中的单个实体删除
///
///
///
[HttpPost]
public async Task Delete([FromBody] ApiAction action)
{
ApiAction response;
try
{
response = action.GetResponse(await Biz.Base.DeleteAsync(action.GetDataEntity(), action.IsAsync));
}
catch (System.Exception ex)
{
response = action.GetResponse().CatchExceptionWithLog(ex);
}
return Ok(response);
}
///
/// DeleteList(ApiAction) :从数据库中把ApiAction.Data中的多个实体删除
///
///
///
[HttpPost]
public async Task DeleteList([FromBody] ApiAction action)//>
{
ApiAction response;
try
{
response = action.GetResponse(await Biz.Base.DeleteAsync(action.GetDataEntity(), action.IsAsync));
}
catch (System.Exception ex)
{
response = action.GetResponse().CatchExceptionWithLog(ex);
}
return Ok(response);
}
///
/// DeleteWhere(ApiAction) :根据ApiAction.Data中Where语句从数据库中删除满足条件的Data实体
///
///
///
[HttpPost]
public async Task DeleteWhere([FromBody] ApiAction action)
{
ApiAction response;
try
{
response = action.GetResponse(await Biz.Base.DeleteAsync(action.NewDataEntity(), action.GetDataEntity(), action.IsAsync));
}
catch (System.Exception ex)
{
response = action.GetResponse().CatchExceptionWithLog(ex);
}
return Ok(response);
}
#endregion
#region 查询操作
private T TryDeserializeJson(string json)
{
try
{
return JsonConvert.DeserializeObject(json);
}
catch (Exception)
{
return default(T);
}
}
///
/// Query(ApiAction) :从数据库中查询Data实体的整表数据
///
///
///
[HttpPost]
public async Task Query([FromBody] ApiActionExt action)
{
ApiAction response;
try
{
var options = TryDeserializeJson(action.Options?.ToString());
if (options.IsNullOrEmpty())
{
response = action.ToApiAction().GetResponse(await Biz.Base.QueryAsync(action.GetDataEntity(), action.IsAsync, action.NeedInclude));
}
else
{
response = action.ToApiAction().GetResponse(await Biz.Base.QueryByAuthAsync(action.GetDataEntity(), options, action.IsAsync, action.NeedInclude));
}
}
catch (System.Exception ex)
{
response = action.ToApiAction().GetResponse().CatchExceptionWithLog(ex);
}
return Ok(response);
}
///
/// QueryWhere(ApiAction) :根据ApiAction.Data中Where语句从数据库中查询满足条件的Data实体数据
///
///
///
[HttpPost]
public async Task QueryWhere([FromBody] ApiActionExt action)
{
ApiAction response;
try
{
var options = TryDeserializeJson(action.Options?.ToString());
if (options.IsNullOrEmpty())
{
response = action.ToApiAction().GetResponse(await Biz.Base.QueryAsync(action.NewDataEntity(), action.GetDataEntity(), action.IsAsync, action.NeedInclude));
}
else
{
response = action.ToApiAction().GetResponse(await Biz.Base.QueryByAuthAsync(action.NewDataEntity(), action.GetDataEntity(), options, action.IsAsync, action.NeedInclude));
}
}
catch (System.Exception ex)
{
response = action.ToApiAction().GetResponse().CatchExceptionWithLog(ex);
}
return Ok(response);
}
///
/// QueryPage(ApiAction) :按ApiAction.Data中PageAble对象返回Data实体当前Page的数据
///
///
///
[HttpPost]
public async Task QueryPage([FromBody] ApiActionExt action)
{
ApiAction response;
try
{
var options = TryDeserializeJson(action.Options?.ToString());
if (options.IsNullOrEmpty())
{
response = action.ToApiAction().GetResponse(await Biz.Base.QueryAsync(action.GetDataEntity(), action.IsAsync, action.NeedInclude));
}
else
{
response = action.ToApiAction().GetResponse(await Biz.Base.QueryByAuthAsync(action.GetDataEntity(), options, action.IsAsync, action.NeedInclude));
}
}
catch (System.Exception ex)
{
response = action.ToApiAction().GetResponse().CatchExceptionWithLog(ex);
}
return Ok(response);
}
///
/// 分页直接返回PageAble
///
///
///
[HttpPost]
public async Task QueryCustomPage([FromBody] ApiActionExt action)
{
dynamic response;
try
{
var options = TryDeserializeJson(action.Options?.ToString());
if (options.IsNullOrEmpty())
{
response = action.ToApiAction().GetResponse(await Biz.Base.QueryPageAsync(action.GetDataEntity(), action.IsAsync, action.NeedInclude));
}
else
{
response = action.ToApiAction().GetResponse(await Biz.Base.QueryByAuthPageAsync(action.GetDataEntity(), options, action.IsAsync, action.NeedInclude));
}
}
catch (System.Exception ex)
{
response = action.ToApiAction().GetResponse().CatchExceptionWithLog(ex);
}
return Ok(response);
}
///
/// QueryCustom(ApiAction) :按ApiAction.Data中QueryAble对象从数据库查询满足条件的数据
///
///
///
[HttpPost]
public async Task QueryCustom([FromBody] ApiActionExt action)
{
ApiAction response;
try
{
var options = TryDeserializeJson(action.Options?.ToString());
if (options.IsNullOrEmpty())
{
response = action.ToApiAction().GetResponse(await Biz.Base.QueryAsync(action.GetDataEntity(), action.IsAsync, action.NeedInclude));
}
else
{
response = action.ToApiAction().GetResponse(await Biz.Base.QueryByAuthAsync(action.GetDataEntity(), options, action.IsAsync, action.NeedInclude));
}
}
catch (System.Exception ex)
{
response = action.ToApiAction().GetResponse().CatchExceptionWithLog(ex);
}
return Ok(response);
}
///
/// QuerySQL(ApiAction) :按ApiAction.Data中SQL语句从数据库中查询满足条件的数据
/// 不支持AuthOptions设置
///
///
///
[HttpPost]
public async Task QuerySQL([FromBody] ApiActionExt action)
{
ApiAction response;
try
{
response = action.ToApiAction().GetResponse(await Biz.Base.QuerySqlAsync(action.NewDataEntity(), action.GetDataEntity(), action.IsAsync, action.NeedInclude));
}
catch (System.Exception ex)
{
response = action.ToApiAction().GetResponse().CatchExceptionWithLog(ex);
}
return Ok(response);
}
///
/// QuerySQL(ApiAction) :按ApiAction.Data中SQL语句从数据库中查询并按PageAble对象返回当前Page的数据
/// 不支持AuthOptions设置
///
///
///
[HttpPost]
public async Task QuerySQLPage([FromBody] ApiActionExt action)
{
ApiAction response;
try
{
response = action.ToApiAction().GetResponse(await Biz.Base.QuerySqlAsync(action.GetDataEntity(), action.IsAsync, action.NeedInclude));
}
catch (System.Exception ex)
{
response = action.ToApiAction().GetResponse().CatchExceptionWithLog(ex);
}
return Ok(response);
}
#endregion
#region 查询Count
///
/// Count(ApiAction) :根据ApiAction.Data中Where语句查询满足条件的Data实体的Count数量
///
///
///
[HttpPost]
public async Task Count([FromBody] ApiAction action)
{
ApiAction response;
try
{
var options = TryDeserializeJson(action.Options?.ToString());
if (options.IsNullOrEmpty())
{
response = action.GetResponse(await Biz.Base.CountAsync(action.NewDataEntity(), action.GetDataEntity(), action.IsAsync));
}
else
{
response = action.GetResponse(await Biz.Base.CountByAuthAsync(action.NewDataEntity(), action.GetDataEntity(), options, action.IsAsync));
}
}
catch (System.Exception ex)
{
response = action.GetResponse().CatchExceptionWithLog(ex);
}
return Ok(response);
}
#endregion
#region 是否存在
///
/// IsExist(ApiAction) :根据ApiAction.Data中Where语句查询满足条件的Data实体是否存在
///
///
///
[HttpPost]
public async Task IsExist([FromBody] ApiAction action)
{
ApiAction response;
try
{
var options = TryDeserializeJson(action.Options?.ToString());
if (options.IsNullOrEmpty())
{
response = action.GetResponse(await Biz.Base.IsExistAsync(action.NewDataEntity(), action.GetDataEntity(), action.IsAsync));
}
else
{
response = action.GetResponse(await Biz.Base.IsExistByAuthAsync(action.NewDataEntity(), action.GetDataEntity(), options, action.IsAsync));
}
}
catch (System.Exception ex)
{
response = action.GetResponse().CatchExceptionWithLog(ex);
}
return Ok(response);
}
#endregion
#region 执行SQL
///
/// ExecuteSqlCommand(ApiAction(Data:SQLCmd)) :执行SQL语句返回影响行数
///
///
///
[HttpPost]
public async Task ExecuteSqlCommand([FromBody] ApiAction action)
{
ApiAction response;
try
{
response = action.GetResponse(await Biz.Base.ExecuteSqlCommandAsync(action.Data?.ToString(), action.IsAsync));
}
catch (System.Exception ex)
{
response = action.GetResponse().CatchExceptionWithLog(ex);
}
return Ok(response);
}
#endregion
#region 枚举查询
///
/// QueryEnum(ApiAction) :查询枚举列表
///
///
///
[HttpPost]
public IActionResult QueryEnum([FromBody] ApiAction action)
{
ApiAction response;
try
{
response = string.IsNullOrEmpty(action.Data?.ToString())? action.GetResponse(EnumHelper.GetList(action.NewDataEntity())): action.GetResponse(EnumHelper.GetList(action.NewDataEntity(), action.Data?.ToString()));
}
catch (System.Exception ex)
{
response = action.GetResponse().CatchExceptionWithLog(ex);
}
return Ok(response);
}
#endregion
#region 先删除后新增
[HttpPost]
public async Task AddAfterDeleteAsync([FromBody] ApiAction action)
{
ApiAction response;
try
{
response = action.GetResponse(await Biz.Base.AddAfterDeleteAsync(action.GetDataEntity()));
}
catch (System.Exception ex)
{
response = action.GetResponse().CatchExceptionWithLog(ex);
}
return Ok(response);
}
#endregion
#region 文件上传
///
/// 文件上传
///
///
///
///
[HttpPost]
public async Task Upload([FromForm]string entityName, IFormFile file)
{
if (file == null || file.Length == 0)
return BadRequest("No file uploaded.");
var folderName = entityName == "BAS_LABEL_TEMP" || entityName == "BAS_LABEL_VAR" ? "upload/Template": "upload";
var path = Path.Combine($@"{BizConfig.Configuration["UploadAddress"]}", folderName, file.FileName);
using (var stream = new FileStream(path, FileMode.Create))
{
await file.CopyToAsync(stream);
}
var downloadAddress = entityName == "BAS_LABEL_TEMP" || entityName == "BAS_LABEL_VAR" ? "/Template" : "";
return Ok(new { file.FileName, file.ContentType, file.Length, url= $@"{BizConfig.Configuration["DownloadAddress"]}{downloadAddress}/"+ file.FileName });
}
#endregion
#region 导入Excel
[HttpPost]
public async Task ImportExcel([FromBody] ApiAction action)
{
ApiAction response;
try
{
IImportExcel import = DI.Resolve(action.Data.typeFullName) as IImportExcel;
response = action.GetResponse(await import.Import(action.NewDataEntity(),action.Data));
}
catch (System.Exception ex)
{
response = action.GetResponse().CatchExceptionWithLog(ex);
}
return Ok(response);
}
///
/// 导入Excel前验证
///
///
///
[HttpPost]
public async Task ValidateTableImport([FromBody] ApiAction action)
{
ApiAction response;
try
{
IImportExcel import = DI.Resolve(action.Data.typeFullName) as IImportExcel;
response = action.GetResponse(await import.ValidateTableImport(action.NewDataEntity(), action.Data));
}
catch (System.Exception ex)
{
response = action.GetResponse().CatchExceptionWithLog(ex);
}
return Ok(response);
}
#endregion
}
}