From 0960f8eee70ffb53846b1b5f12bff002f13baa7b Mon Sep 17 00:00:00 2001 From: LemonNoCry Date: Tue, 31 Dec 2024 16:23:57 +0800 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8=E5=A2=9E=E5=8A=A0=E5=88=86=E8=A1=A8?= =?UTF-8?q?=E6=B5=8B=E8=AF=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Controllers/SplitDemoController.cs | 322 ++++++++++-------- 1 file changed, 182 insertions(+), 140 deletions(-) diff --git a/Blog.Core.Api/Controllers/SplitDemoController.cs b/Blog.Core.Api/Controllers/SplitDemoController.cs index f625b202..2f539f49 100644 --- a/Blog.Core.Api/Controllers/SplitDemoController.cs +++ b/Blog.Core.Api/Controllers/SplitDemoController.cs @@ -5,91 +5,174 @@ using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Mvc; using System.Linq.Expressions; +using Blog.Core.Controllers; +using SqlSugar; -namespace Blog.Core.Api.Controllers +namespace Blog.Core.Api.Controllers; + +/// +/// 分表demo +/// +[Route("api/[controller]/[action]")] +[ApiController] +[Authorize(Permissions.Name)] +public class SplitDemoController : BaseApiController { + readonly ISplitDemoServices splitDemoServices; + readonly IUnitOfWorkManage unitOfWorkManage; + private readonly ISqlSugarClient _db; + + public SplitDemoController(ISplitDemoServices _splitDemoServices, IUnitOfWorkManage _unitOfWorkManage, ISqlSugarClient db) + { + splitDemoServices = _splitDemoServices; + unitOfWorkManage = _unitOfWorkManage; + _db = db; + } + + /// + /// 分页获取数据 + /// + /// + /// + /// + /// + /// + /// + [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 MessageModel>.Message(data.dataCount >= 0, "获取成功", data); + } + + [HttpGet] + [AllowAnonymous] + public async Task>> GetSpilt() + { + var data = await _db.Queryable().AS("SplitDemo_20241231").ToListAsync(); + return Success(data); + } + /// - /// 分表demo + /// 根据ID获取信息 /// - [Route("api/[controller]/[action]")] - [ApiController] - [Authorize(Permissions.Name)] - public class SplitDemoController : ControllerBase + /// + /// + [HttpGet] + [AllowAnonymous] + public async Task> GetById(long id) { - readonly ISplitDemoServices splitDemoServices; - readonly IUnitOfWorkManage unitOfWorkManage; - public SplitDemoController(ISplitDemoServices _splitDemoServices, IUnitOfWorkManage _unitOfWorkManage) + var data = new MessageModel(); + var model = await splitDemoServices.QueryByIdSplit(id); + if (model != null) + { + return MessageModel.Success("获取成功", model); + } + else { - splitDemoServices = _splitDemoServices; - unitOfWorkManage = _unitOfWorkManage; + return MessageModel.Fail("获取失败"); } + } - /// - /// 分页获取数据 - /// - /// - /// - /// - /// - /// - /// - [HttpGet] - [AllowAnonymous] - public async Task>> Get(DateTime beginTime, DateTime endTime, int page = 1, string key = "", int pageSize = 10) + [HttpPost] + [AllowAnonymous] + public async Task GenTestData() + { + //帮我生成一个月数据 + for (int i = 0; i < 30; i++) { - if (string.IsNullOrEmpty(key) || string.IsNullOrWhiteSpace(key)) + await splitDemoServices.AddSplit(new SplitDemo() { - key = ""; - } - Expression> whereExpression = a => (a.Name != null && a.Name.Contains(key)); - var data = await splitDemoServices.QueryPageSplit(whereExpression, beginTime, endTime, page, pageSize, " Id desc "); - return MessageModel>.Message(data.dataCount >= 0, "获取成功", data); + Name = "测试数据" + i, + CreateTime = DateTime.Now.AddDays(-i) + }); } - /// - /// 根据ID获取信息 - /// - /// - /// - [HttpGet] - [AllowAnonymous] - public async Task> GetById(long id) + return Success(); + } + + [HttpPost] + [AllowAnonymous] + public async Task InitTable() + { + _db.MappingTables.Add("SplitDemo","SplitDemo_20241231"); + _db.CodeFirst.InitTables(); + await Task.Delay(1); + return Success(); + } + + /// + /// 添加一条测试数据 + /// + /// + /// + [HttpPost] + [AllowAnonymous] + public async Task> Post([FromBody] SplitDemo splitDemo) + { + var data = new MessageModel(); + //unitOfWorkManage.BeginTran(); + var id = (await splitDemoServices.AddSplit(splitDemo)); + data.success = (id == null ? false : true); + try { - var data = new MessageModel(); - var model = await splitDemoServices.QueryByIdSplit(id); - if (model != null) + if (data.success) { - return MessageModel.Success("获取成功", model); + data.response = id.FirstOrDefault().ToString(); + data.msg = "添加成功"; } else { - return MessageModel.Fail("获取失败"); + data.msg = "添加失败"; } } + catch (Exception) + { + throw; + } + finally + { + //if (data.success) + // unitOfWorkManage.CommitTran(); + //else + // unitOfWorkManage.RollbackTran(); + } + + return data; + } - /// - /// 添加一条测试数据 - /// - /// - /// - [HttpPost] - [AllowAnonymous] - public async Task> Post([FromBody] SplitDemo splitDemo) + /// + /// 修改一条测试数据 + /// + /// + /// + [HttpPut] + [AllowAnonymous] + public async Task> Put([FromBody] SplitDemo splitDemo) + { + var data = new MessageModel(); + if (splitDemo != null && splitDemo.Id > 0) { - var data = new MessageModel(); - //unitOfWorkManage.BeginTran(); - var id = (await splitDemoServices.AddSplit(splitDemo)); - data.success = (id == null ? false : true); + unitOfWorkManage.BeginTran(); + data.success = await splitDemoServices.UpdateSplit(splitDemo, splitDemo.CreateTime); try { if (data.success) { - data.response = id.FirstOrDefault().ToString(); - data.msg = "添加成功"; + data.msg = "修改成功"; + data.response = splitDemo?.Id.ObjToString(); } else { - data.msg = "添加失败"; + data.msg = "修改失败"; } } catch (Exception) @@ -98,102 +181,61 @@ public async Task> Post([FromBody] SplitDemo splitDemo) } finally { - //if (data.success) - // unitOfWorkManage.CommitTran(); - //else - // unitOfWorkManage.RollbackTran(); + if (data.success) + unitOfWorkManage.CommitTran(); + else + unitOfWorkManage.RollbackTran(); } - return data; } - /// - /// 修改一条测试数据 - /// - /// - /// - [HttpPut] - [AllowAnonymous] - public async Task> Put([FromBody] SplitDemo splitDemo) + return data; + } + + /// + /// 根据id删除数据 + /// + /// + /// + [HttpDelete] + [AllowAnonymous] + public async Task> Delete(long id) + { + var data = new MessageModel(); + + var model = await splitDemoServices.QueryByIdSplit(id); + if (model != null) { - var data = new MessageModel(); - if (splitDemo != null && splitDemo.Id > 0) + unitOfWorkManage.BeginTran(); + data.success = await splitDemoServices.DeleteSplit(model, model.CreateTime); + try { - unitOfWorkManage.BeginTran(); - data.success = await splitDemoServices.UpdateSplit(splitDemo, splitDemo.CreateTime); - try - { - if (data.success) - { - data.msg = "修改成功"; - data.response = splitDemo?.Id.ObjToString(); - } - else - { - data.msg = "修改失败"; - } - } - catch (Exception) + data.response = id.ObjToString(); + if (data.success) { - throw; + data.msg = "删除成功"; } - finally + else { - if (data.success) - unitOfWorkManage.CommitTran(); - else - unitOfWorkManage.RollbackTran(); + data.msg = "删除失败"; } } - return data; - } - - /// - /// 根据id删除数据 - /// - /// - /// - [HttpDelete] - [AllowAnonymous] - public async Task> Delete(long id) - { - var data = new MessageModel(); - - var model = await splitDemoServices.QueryByIdSplit(id); - if (model != null) + catch (Exception) { - unitOfWorkManage.BeginTran(); - data.success = await splitDemoServices.DeleteSplit(model,model.CreateTime); - try - { - data.response = id.ObjToString(); - if (data.success) - { - data.msg = "删除成功"; - } - else - { - data.msg = "删除失败"; - } - - } - catch (Exception) - { - throw; - } - finally - { - if (data.success) - unitOfWorkManage.CommitTran(); - else - unitOfWorkManage.RollbackTran(); - } + throw; } - else + finally { - data.msg = "不存在"; + if (data.success) + unitOfWorkManage.CommitTran(); + else + unitOfWorkManage.RollbackTran(); } - return data; - } + else + { + data.msg = "不存在"; + } + + return data; } -} +} \ No newline at end of file