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.
192 lines
6.9 KiB
192 lines
6.9 KiB
using System.Collections.Generic;
|
|
using System.Reflection;
|
|
using Tiobon.Core.Model;
|
|
|
|
namespace Tiobon.Core.Controllers
|
|
{
|
|
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<ServiceResult<PageModel<TEntityDto>>> QueryByFilter([FromFilter] QueryFilter filter)
|
|
{
|
|
//var response = await _service.QueryFilterPage(filter);
|
|
var response = (await InvokeServiceAsync("QueryFilterPage", [filter])) as PageModel<TEntityDto>;
|
|
return new ServiceResult<PageModel<TEntityDto>>() { msg = "获取成功", success = true, response = response };
|
|
}
|
|
|
|
/// <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]));
|
|
|
|
//var entity = ObjectConverter.ConvertToEntity<TEntityDto>(obj);
|
|
//var entity = await _service.QueryById(Id);
|
|
if (entity == null)
|
|
return ServiceResult<object>.OprateFailed("获取失败");
|
|
else
|
|
return new ServiceResult<object>() { msg = "获取成功", success = true, response = entity };
|
|
}
|
|
#endregion
|
|
|
|
#region 新增
|
|
/// <summary>
|
|
/// Ghra_Grade -- 新增数据
|
|
/// </summary>
|
|
/// <param name="insertModel"></param>
|
|
/// <returns></returns>
|
|
[HttpPost]
|
|
public async Task<ServiceResult<string>> Post([FromBody] TEditDto insertModel)
|
|
{
|
|
var data = ServiceResult<string>.OprateSuccess("获取成功", null);
|
|
var id = Convert.ToInt64(await InvokeServiceAsync("Add", [insertModel]));
|
|
data.success = id > 0;
|
|
if (data.success)
|
|
data.response = id.ObjToString();
|
|
|
|
return data;
|
|
}
|
|
#endregion
|
|
|
|
#region 更新
|
|
/// <summary>
|
|
/// Ghra_Grade -- 更新数据
|
|
/// </summary>
|
|
/// <param name="Id"></param>
|
|
/// <param name="editModel"></param>
|
|
/// <returns></returns>
|
|
[HttpPut("{Id}")]
|
|
public async Task<ServiceResult> Put(long Id, [FromBody] TEditDto editModel)
|
|
{
|
|
var data = ServiceResult.OprateSuccess("更新成功");
|
|
var id = Convert.ToInt64(await InvokeServiceAsync("Update", [editModel]));
|
|
if (!data.success)
|
|
data.msg = "更新失败";
|
|
|
|
return data;
|
|
}
|
|
#endregion
|
|
|
|
#region 删除
|
|
/// <summary>
|
|
/// Ghra_Grade -- 删除数据
|
|
/// </summary>
|
|
/// <param name="Id"></param>
|
|
/// <returns></returns>
|
|
[HttpDelete("{Id}")]
|
|
public async Task<ServiceResult> Delete(long Id)
|
|
{
|
|
var data = ServiceResult.OprateSuccess("删除成功");
|
|
var entity = await QueryById(Id);
|
|
if (entity == null)
|
|
return ServiceResult.OprateFailed("删除失败");
|
|
|
|
//entity.IsEnable = 0;
|
|
data.success = Convert.ToBoolean(await InvokeServiceAsync("Update", [entity]));
|
|
if (!data.success)
|
|
data.msg = "删除失败";
|
|
return data;
|
|
}
|
|
#endregion
|
|
|
|
#endregion
|
|
|
|
/// <summary>
|
|
/// 反射调用service方法
|
|
/// </summary>
|
|
/// <param name="methodName"></param>
|
|
/// <param name="parameters"></param>
|
|
/// <returns></returns>
|
|
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>
|
|
private object InvokeService(string methodName, Type[] types, object[] parameters) => _service.GetType().GetMethod(methodName, types).Invoke(_service, parameters);
|
|
|
|
|
|
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 msg = "成功")
|
|
{
|
|
return new ServiceResult<T>() { success = true, msg = msg, response = 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 msg = "成功")
|
|
{
|
|
return new ServiceResult() { success = true, msg = msg, response = null, };
|
|
}
|
|
|
|
[NonAction]
|
|
public ServiceResult<string> Failed(string msg = "失败", int status = 500)
|
|
{
|
|
return new ServiceResult<string>() { success = false, status = status, msg = msg, response = null, };
|
|
}
|
|
|
|
[NonAction]
|
|
public ServiceResult<T> Failed<T>(string msg = "失败", int status = 500)
|
|
{
|
|
return new ServiceResult<T>() { success = false, status = status, msg = msg, response = default, };
|
|
}
|
|
|
|
[NonAction]
|
|
public ServiceResult<PageModel<T>> SuccessPage<T>(int page, int dataCount, int pageSize, List<T> data, int pageCount, string msg = "获取成功")
|
|
{
|
|
return new ServiceResult<PageModel<T>>() { success = true, msg = msg, response = new PageModel<T>(page, dataCount, pageSize, data) };
|
|
}
|
|
|
|
[NonAction]
|
|
public ServiceResult<PageModel<T>> SuccessPage<T>(PageModel<T> pageModel, string msg = "获取成功")
|
|
{
|
|
return new ServiceResult<PageModel<T>>() { success = true, msg = msg, response = pageModel };
|
|
}
|
|
}
|
|
} |