namespace Tiobon.Core.Api.Controllers
{
///
/// 分表demo
///
[Route("api/[controller]/[action]")]
[ApiController]
[Authorize(Permissions.Name), ApiExplorerSettings(GroupName = Grouping.GroupName_Other)]
public class SplitDemoController : ControllerBase
{
readonly ISplitDemoServices splitDemoServices;
readonly IUnitOfWorkManage unitOfWorkManage;
public SplitDemoController(ISplitDemoServices _splitDemoServices, IUnitOfWorkManage _unitOfWorkManage)
{
splitDemoServices = _splitDemoServices;
unitOfWorkManage = _unitOfWorkManage;
}
///
/// 分页获取数据
///
///
///
///
///
///
///
[HttpGet]
[AllowAnonymous]
public async Task>> Get(DateTime beginTime, DateTime endTime, int page = 1, string key = "", int pageSize = 10)
{
if (string.IsNullOrEmpty(key) || string.IsNullOrWhiteSpace(key))
{
key = "";
}
Expression> whereExpression = a => (a.Name != null && a.Name.Contains(key));
var data = await splitDemoServices.QueryPageSplit(whereExpression, beginTime, endTime, page, pageSize, " Id desc ");
return ServiceResult>.OprateSuccess(data.dataCount >= 0, "获取成功", data);
}
///
/// 根据ID获取信息
///
///
///
[HttpGet]
[AllowAnonymous]
public async Task> GetById(long id)
{
var data = new ServiceResult();
var model = await splitDemoServices.QueryByIdSplit(id);
if (model != null)
{
return ServiceResult.OprateSuccess("获取成功", model);
}
else
{
return ServiceResult.OprateFailed("获取失败");
}
}
///
/// 添加一条测试数据
///
///
///
[HttpPost]
[AllowAnonymous]
public async Task> Post([FromBody] SplitDemo splitDemo)
{
var data = new ServiceResult();
//unitOfWorkManage.BeginTran();
var id = (await splitDemoServices.AddSplit(splitDemo));
data.Success = (id == null ? false : true);
try
{
if (data.Success)
{
data.Data = id.FirstOrDefault().ToString();
data.Message = "添加成功";
}
else
{
data.Message = "添加失败";
}
}
catch (Exception)
{
throw;
}
finally
{
//if (data.Success)
// unitOfWorkManage.CommitTran();
//else
// unitOfWorkManage.RollbackTran();
}
return data;
}
///
/// 修改一条测试数据
///
///
///
[HttpPut]
[AllowAnonymous]
public async Task> Put([FromBody] SplitDemo splitDemo)
{
var data = new ServiceResult();
if (splitDemo != null && splitDemo.Id > 0)
{
unitOfWorkManage.BeginTran();
data.Success = await splitDemoServices.UpdateSplit(splitDemo, splitDemo.CreateTime);
try
{
if (data.Success)
{
data.Message = "修改成功";
data.Data = splitDemo?.Id.ObjToString();
}
else
{
data.Message = "修改失败";
}
}
catch (Exception)
{
throw;
}
finally
{
if (data.Success)
unitOfWorkManage.CommitTran();
else
unitOfWorkManage.RollbackTran();
}
}
return data;
}
///
/// 根据id删除数据
///
///
///
[HttpDelete]
[AllowAnonymous]
public async Task> Delete(long id)
{
var data = new ServiceResult();
var model = await splitDemoServices.QueryByIdSplit(id);
if (model != null)
{
unitOfWorkManage.BeginTran();
data.Success = await splitDemoServices.DeleteSplit(model,model.CreateTime);
try
{
data.Data = id.ObjToString();
if (data.Success)
{
data.Message = "删除成功";
}
else
{
data.Message = "删除失败";
}
}
catch (Exception)
{
throw;
}
finally
{
if (data.Success)
unitOfWorkManage.CommitTran();
else
unitOfWorkManage.RollbackTran();
}
}
else
{
data.Message = "不存在";
}
return data;
}
}
}