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
|
{
|
/// <summary>
|
/// Api基础服务
|
/// </summary>
|
[Route("api/[controller]/[action]")]
|
[EnableCors("Any")]
|
[ApiController]
|
public partial class BaseController : ControllerBase
|
{
|
#region 添加操作
|
/// <summary>
|
/// Add(ApiAction) :把ApiAction.Data中的单个实体往数据库添加一行数据
|
/// </summary>
|
/// <param name="action"></param>
|
/// <returns></returns>
|
[HttpPost]
|
public async Task<IActionResult> 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);
|
}
|
|
/// <summary>
|
/// AddList(ApiAction) :把ApiAction.Data中的多个实体往数据库批量添加数据
|
/// </summary>
|
/// <param name="action"></param>
|
/// <returns></returns>
|
[HttpPost]
|
public async Task<IActionResult> AddList([FromBody] ApiAction action)//<List<object>>
|
{
|
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 保存操作
|
/// <summary>
|
/// Save(ApiAction) :把ApiAction.Data中的单个实体往数据库保存一行数据,如果存在就更新,不存在就插入
|
/// </summary>
|
/// <param name="action"></param>
|
/// <returns></returns>
|
[HttpPost]
|
public async Task<IActionResult> 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);
|
}
|
|
/// <summary>
|
/// SaveList(ApiAction) :把ApiAction.Data中的多个实体往数据库批量保存数据,如果存在就更新,不存在就插入
|
/// </summary>
|
/// <param name="action"></param>
|
/// <returns></returns>
|
[HttpPost]
|
public async Task<IActionResult> 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 修改操作
|
/// <summary>
|
/// Update(ApiAction) :从数据库中修改ApiAction.Data中的单个实体
|
/// </summary>
|
/// <param name="action"></param>
|
/// <returns></returns>
|
[HttpPost]
|
public async Task<IActionResult> 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);
|
}
|
|
/// <summary>
|
/// UpdateList(ApiAction) :从数据库中修改ApiAction.Data中的多个实体
|
/// </summary>
|
/// <param name="action"></param>
|
/// <returns></returns>
|
[HttpPost]
|
public async Task<IActionResult> 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);
|
}
|
|
/// <summary>
|
/// UpdateByColumn(ApiAction) :按ApiAction.Data中UpdateAble对象从数据库中修改多个实体的某些字段
|
/// </summary>
|
/// <param name="action"></param>
|
/// <returns></returns>
|
[HttpPost]
|
public async Task<IActionResult> 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 删除操作
|
/// <summary>
|
/// Delete(ApiAction) :从数据库中把ApiAction.Data中的单个实体删除
|
/// </summary>
|
/// <param name="action"></param>
|
/// <returns></returns>
|
[HttpPost]
|
public async Task<IActionResult> 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);
|
}
|
|
/// <summary>
|
/// DeleteList(ApiAction) :从数据库中把ApiAction.Data中的多个实体删除
|
/// </summary>
|
/// <param name="action"></param>
|
/// <returns></returns>
|
[HttpPost]
|
public async Task<IActionResult> DeleteList([FromBody] ApiAction action)//<List<object>>
|
{
|
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);
|
}
|
|
/// <summary>
|
/// DeleteWhere(ApiAction) :根据ApiAction.Data中Where语句从数据库中删除满足条件的Data实体
|
/// </summary>
|
/// <param name="action"></param>
|
/// <returns></returns>
|
[HttpPost]
|
public async Task<IActionResult> 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<T>(string json)
|
{
|
try
|
{
|
return JsonConvert.DeserializeObject<T>(json);
|
}
|
catch (Exception)
|
{
|
return default(T);
|
}
|
}
|
|
/// <summary>
|
/// Query(ApiAction) :从数据库中查询Data实体的整表数据
|
/// </summary>
|
/// <param name="action"></param>
|
/// <returns></returns>
|
[HttpPost]
|
public async Task<IActionResult> Query([FromBody] ApiActionExt action)
|
{
|
ApiAction response;
|
try
|
{
|
var options = TryDeserializeJson<AuthOption>(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);
|
}
|
|
/// <summary>
|
/// QueryWhere(ApiAction) :根据ApiAction.Data中Where语句从数据库中查询满足条件的Data实体数据
|
/// </summary>
|
/// <param name="action"></param>
|
/// <returns></returns>
|
[HttpPost]
|
public async Task<IActionResult> QueryWhere([FromBody] ApiActionExt action)
|
{
|
ApiAction response;
|
try
|
{
|
var options = TryDeserializeJson<AuthOption>(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);
|
}
|
|
/// <summary>
|
/// QueryPage(ApiAction) :按ApiAction.Data中PageAble对象返回Data实体当前Page的数据
|
/// </summary>
|
/// <param name="action"></param>
|
/// <returns></returns>
|
[HttpPost]
|
public async Task<IActionResult> QueryPage([FromBody] ApiActionExt action)
|
{
|
ApiAction response;
|
try
|
{
|
var options = TryDeserializeJson<AuthOption>(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);
|
}
|
|
/// <summary>
|
/// 分页直接返回PageAble
|
/// </summary>
|
/// <param name="action"></param>
|
/// <returns></returns>
|
[HttpPost]
|
public async Task<IActionResult> QueryCustomPage([FromBody] ApiActionExt action)
|
{
|
dynamic response;
|
try
|
{
|
var options = TryDeserializeJson<AuthOption>(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);
|
}
|
|
/// <summary>
|
/// QueryCustom(ApiAction) :按ApiAction.Data中QueryAble对象从数据库查询满足条件的数据
|
/// </summary>
|
/// <param name="action"></param>
|
/// <returns></returns>
|
[HttpPost]
|
public async Task<IActionResult> QueryCustom([FromBody] ApiActionExt action)
|
{
|
ApiAction response;
|
try
|
{
|
var options = TryDeserializeJson<AuthOption>(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);
|
}
|
|
/// <summary>
|
/// QuerySQL(ApiAction) :按ApiAction.Data中SQL语句从数据库中查询满足条件的数据
|
/// 不支持AuthOptions设置
|
/// </summary>
|
/// <param name="action"></param>
|
/// <returns></returns>
|
[HttpPost]
|
public async Task<IActionResult> 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);
|
}
|
|
/// <summary>
|
/// QuerySQL(ApiAction) :按ApiAction.Data中SQL语句从数据库中查询并按PageAble对象返回当前Page的数据
|
/// 不支持AuthOptions设置
|
/// </summary>
|
/// <param name="action"></param>
|
/// <returns></returns>
|
[HttpPost]
|
public async Task<IActionResult> 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
|
/// <summary>
|
/// Count(ApiAction) :根据ApiAction.Data中Where语句查询满足条件的Data实体的Count数量
|
/// </summary>
|
/// <param name="action"></param>
|
/// <returns></returns>
|
[HttpPost]
|
public async Task<IActionResult> Count([FromBody] ApiAction action)
|
{
|
ApiAction response;
|
try
|
{
|
var options = TryDeserializeJson<AuthOption>(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 是否存在
|
/// <summary>
|
/// IsExist(ApiAction) :根据ApiAction.Data中Where语句查询满足条件的Data实体是否存在
|
/// </summary>
|
/// <param name="action"></param>
|
/// <returns></returns>
|
[HttpPost]
|
public async Task<IActionResult> IsExist([FromBody] ApiAction action)
|
{
|
ApiAction response;
|
try
|
{
|
var options = TryDeserializeJson<AuthOption>(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
|
/// <summary>
|
/// ExecuteSqlCommand(ApiAction(Data:SQLCmd)) :执行SQL语句返回影响行数
|
/// </summary>
|
/// <param name="action"></param>
|
/// <returns></returns>
|
[HttpPost]
|
public async Task<IActionResult> 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 枚举查询
|
/// <summary>
|
/// QueryEnum(ApiAction) :查询枚举列表
|
/// </summary>
|
/// <param name="action"></param>
|
/// <returns></returns>
|
[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<IActionResult> 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 文件上传
|
/// <summary>
|
/// 文件上传
|
/// </summary>
|
/// <param name="file"></param>
|
/// <param name="entityName"></param>
|
/// <returns></returns>
|
[HttpPost]
|
public async Task<IActionResult> 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);
|
}
|
|
return Ok(new { file.FileName, file.ContentType, file.Length, url= $@"{BizConfig.Configuration["DownloadAddress"]}/"+ file.FileName });
|
}
|
|
#endregion
|
}
|
}
|