You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
196 lines
6.2 KiB
196 lines
6.2 KiB
using System.Reflection;
|
|
using Confluent.Kafka;
|
|
|
|
namespace Tiobon.Core.Controllers;
|
|
|
|
/// <summary>
|
|
/// 增删改查基础服务
|
|
/// </summary>
|
|
/// <typeparam name="IServiceBase"></typeparam>
|
|
/// <typeparam name="TEntity"></typeparam>
|
|
/// <typeparam name="TEntityDto"></typeparam>
|
|
/// <typeparam name="TInsertDto"></typeparam>
|
|
/// <typeparam name="TEditDto"></typeparam>
|
|
public class BaseController<IServiceBase, TEntity, TEntityDto, TInsertDto, TEditDto> : Controller
|
|
{
|
|
#region 初始化
|
|
protected IServiceBase _service;
|
|
/// <summary>
|
|
/// 初始化 (注入)
|
|
/// </summary>
|
|
public BaseController(IServiceBase service)
|
|
{
|
|
_service = service;
|
|
}
|
|
#endregion
|
|
|
|
|
|
#region 基础接口
|
|
|
|
#region 查询
|
|
/// <summary>
|
|
/// Ghra_Grade -- 根据条件查询数据
|
|
/// </summary>
|
|
/// <param name="filter">条件</param>
|
|
/// <returns></returns>
|
|
[HttpGet]
|
|
public async Task<ServicePageResult<TEntityDto>> QueryByFilter([FromFilter] QueryFilter filter)
|
|
{
|
|
var data = (await InvokeServiceAsync("QueryFilterPage", [filter])) as ServicePageResult<TEntityDto>;
|
|
return data;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Ghra_Grade -- 根据Id查询数据
|
|
/// </summary>
|
|
/// <param name="Id">主键ID</param>
|
|
/// <returns></returns>
|
|
[HttpGet("{Id}")]
|
|
public async Task<ServiceResult<object>> QueryById(long Id)
|
|
{
|
|
var entity = await InvokeServiceAsync("QueryById", [Id]);
|
|
if (entity == null)
|
|
return Failed<object>("获取失败");
|
|
else
|
|
return Success(entity, "获取成功");
|
|
}
|
|
#endregion
|
|
|
|
#region 新增
|
|
/// <summary>
|
|
/// Ghra_Grade -- 新增数据
|
|
/// </summary>
|
|
/// <param name="insertModel"></param>
|
|
/// <returns></returns>
|
|
[HttpPost]
|
|
public async Task<ServiceResult<string>> Insert([FromBody] TInsertDto insertModel)
|
|
{
|
|
var data = Success<string>(null, "新增成功");
|
|
var id = Convert.ToInt64(await InvokeServiceAsync("Add", [insertModel]));
|
|
data.Success = id > 0;
|
|
if (data.Success)
|
|
data.Data = id.ObjToString();
|
|
else
|
|
return Failed<string>("新增失败");
|
|
|
|
return data;
|
|
}
|
|
#endregion
|
|
|
|
#region 更新
|
|
/// <summary>
|
|
/// Ghra_Grade -- 更新数据
|
|
/// </summary>
|
|
/// <param name="Id">主键ID</param>
|
|
/// <param name="editModel"></param>
|
|
/// <returns></returns>
|
|
[HttpPut("{Id}")]
|
|
public async Task<ServiceResult> Put(long Id, [FromBody] TEditDto editModel)
|
|
{
|
|
var data = Success("更新成功");
|
|
var flag = Convert.ToBoolean(await InvokeServiceAsync("Update", [Id, editModel]));
|
|
if (!flag)
|
|
return Failed("更新失败");
|
|
return data;
|
|
}
|
|
#endregion
|
|
|
|
#region 删除
|
|
/// <summary>
|
|
/// Ghra_Grade -- 删除数据
|
|
/// </summary>
|
|
/// <param name="Id">主键ID</param>
|
|
/// <returns></returns>
|
|
[HttpDelete("{Id}")]
|
|
public async Task<ServiceResult> Delete(long Id)
|
|
{
|
|
var data = Success("删除成功");
|
|
var isExist = Convert.ToBoolean(await InvokeServiceAsync("AnyAsync", [Id]));
|
|
if (!isExist)
|
|
return Failed("删除失败");
|
|
data.Success = Convert.ToBoolean(await InvokeServiceAsync("DeleteById1", [Id]));
|
|
if (!data.Success)
|
|
return Failed("删除失败");
|
|
return data;
|
|
}
|
|
#endregion
|
|
|
|
#endregion
|
|
|
|
/// <summary>
|
|
/// 反射调用service方法
|
|
/// </summary>
|
|
/// <param name="methodName"></param>
|
|
/// <param name="parameters"></param>
|
|
/// <returns></returns>
|
|
[NonAction]
|
|
private object InvokeService(string methodName, object[] parameters)
|
|
{
|
|
return _service.GetType().GetMethod(methodName).Invoke(_service, parameters);
|
|
}
|
|
/// <summary>
|
|
/// 反射调用service方法
|
|
/// </summary>
|
|
/// <param name="methodName"></param>
|
|
/// <param name="types">为要调用重载的方法参数类型:new Type[] { typeof(SaveDataModel)</param>
|
|
/// <param name="parameters"></param>
|
|
/// <returns></returns>
|
|
[NonAction]
|
|
private object InvokeService(string methodName, Type[] types, object[] parameters) => _service.GetType().GetMethod(methodName, types).Invoke(_service, parameters);
|
|
|
|
|
|
[NonAction]
|
|
private async Task<object> InvokeServiceAsync(string methodName, object[] parameters)
|
|
{
|
|
var task = _service.GetType().InvokeMember(methodName, BindingFlags.InvokeMethod, null, _service, parameters) as Task;
|
|
if (task != null) await task;
|
|
var result = task?.GetType().GetProperty("Result")?.GetValue(task);
|
|
return result;
|
|
}
|
|
|
|
[NonAction]
|
|
public ServiceResult<T> Success<T>(T data, string message = "成功")
|
|
{
|
|
return new ServiceResult<T>() { Success = true, Message = message, Data = data, };
|
|
}
|
|
|
|
// [NonAction]
|
|
//public ServiceResult<T> Success<T>(T data, string msg = "成功",bool success = true)
|
|
//{
|
|
// return new ServiceResult<T>()
|
|
// {
|
|
// success = success,
|
|
// msg = msg,
|
|
// response = data,
|
|
// };
|
|
//}
|
|
[NonAction]
|
|
public ServiceResult Success(string message = "成功")
|
|
{
|
|
return new ServiceResult() { Success = true, Message = message, Data = null, };
|
|
}
|
|
|
|
[NonAction]
|
|
public ServiceResult Failed(string message = "失败", int status = 500)
|
|
{
|
|
return new ServiceResult() { Success = false, Status = status, Message = message, Data = null, };
|
|
}
|
|
|
|
[NonAction]
|
|
public ServiceResult<T> Failed<T>(string message = "失败", int status = 500)
|
|
{
|
|
return new ServiceResult<T>() { Success = false, Status = status, Message = message, Data = default, };
|
|
}
|
|
|
|
[NonAction]
|
|
public ServiceResult<PageModel<T>> SuccessPage<T>(int page, int dataCount, int pageSize, List<T> data, int pageCount, string message = "获取成功")
|
|
{
|
|
return new ServiceResult<PageModel<T>>() { Success = true, Message = message, Data = new PageModel<T>(page, dataCount, pageSize, data) };
|
|
}
|
|
|
|
[NonAction]
|
|
public ServiceResult<PageModel<T>> SuccessPage<T>(PageModel<T> pageModel, string message = "获取成功")
|
|
{
|
|
return new ServiceResult<PageModel<T>>() { Success = true, Message = message, Data = pageModel };
|
|
}
|
|
} |