From 1290ed068e38a014577706b1e95692a933b6b16d Mon Sep 17 00:00:00 2001 From: wintel Date: Sun, 30 Mar 2025 00:24:21 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E4=BB=A3=E7=A0=81=E7=94=9F=E6=88=90?= =?UTF-8?q?=E9=BB=98=E8=AE=A4=E7=94=9F=E6=88=90=E5=9F=BA=E4=BA=8ESqlSugar?= =?UTF-8?q?=E7=9A=84=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Template/SingleTable/BuildApp.html | 52 +++++++++------- .../BuildAppWithDynamicHeader.html | 61 +++++++++++-------- 2 files changed, 64 insertions(+), 49 deletions(-) diff --git a/OpenAuth.WebApi/Template/SingleTable/BuildApp.html b/OpenAuth.WebApi/Template/SingleTable/BuildApp.html index 5c9faa42..a5898441 100644 --- a/OpenAuth.WebApi/Template/SingleTable/BuildApp.html +++ b/OpenAuth.WebApi/Template/SingleTable/BuildApp.html @@ -1,19 +1,18 @@ using System; +using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; using Infrastructure; -using Microsoft.EntityFrameworkCore; using OpenAuth.App.Interface; using OpenAuth.App.Request; using OpenAuth.App.Response; -using OpenAuth.Repository; using OpenAuth.Repository.Domain; -using OpenAuth.Repository.Interface; +using SqlSugar; namespace OpenAuth.App { - public class {ModuleCode} : {BaseAppName}<{ClassName},OpenAuthDBContext> + public class {ModuleCode} : SqlSugarBaseApp<{ClassName}> { /// @@ -24,45 +23,52 @@ namespace OpenAuth.App var loginContext = _auth.GetCurrentUser(); if (loginContext == null) { - throw new CommonException("登录已过期", Define.INVALID_TOKEN); + throw new CommonException("登录已过期", Define.INVALID_TOKEN); } - - + var result = new TableData(); var objs = GetDataPrivilege("u"); if (!string.IsNullOrEmpty(request.key)) { - //增加筛选条件,如: - //objs = objs.Where(u => u.Name.Contains(request.key)); + objs = objs.Where(u => u.Name.Contains(request.key)); } {ForeignKeyTemplate} - + result.data = objs.OrderBy(u => u.Id) - .Skip((request.page - 1) * request.limit) - .Take(request.limit); - result.count =await objs.CountAsync(); + .Skip((request.page - 1) * request.limit) + .Take(request.limit).ToList(); + result.count = await objs.CountAsync(); return result; } - public void Add(AddOrUpdate{ClassName}Req obj) + public void Add(AddOrUpdate{ClassName}Req req) { - //程序类型取入口应用的名称,可以根据自己需要调整 - var addObj = obj.MapTo<{ClassName}>(); - //addObj.Time = DateTime.Now; - Repository.Add(addObj); + var obj = req.MapTo<{ClassName}>(); + obj.Id = Guid.NewGuid().ToString(); + //todo: 根据业务需要调整字段 + obj.Createtime = DateTime.Now; + var user = _auth.GetCurrentUser().User; + obj.Createuserid = user.Id; + obj.Createusername = user.Name; + Repository.Insert(obj); } - + public void Update(AddOrUpdate{ClassName}Req obj) { - UnitWork.Update<{ClassName}>(u => u.Id == obj.Id, u => new {ClassName} + var user = _auth.GetCurrentUser().User; + Repository.Update(u => new {ClassName} { - //todo:要修改的字段赋值 - }); + //todo: 根据业务需要调整字段 + {ClassName} = obj.{ClassName}, + Updatetime = DateTime.Now, + Updateuserid = user.Id, + Updateusername = user.Name + },u => u.Id == obj.Id); } - public {ModuleCode}(IUnitWork unitWork, IRepository<{ClassName},OpenAuthDBContext> repository, IAuth auth) : base(unitWork, repository, auth) + public {ModuleCode}(ISqlSugarClient client, IAuth auth) : base(client, auth) { } } diff --git a/OpenAuth.WebApi/Template/SingleTable/BuildAppWithDynamicHeader.html b/OpenAuth.WebApi/Template/SingleTable/BuildAppWithDynamicHeader.html index a0836a1e..da6ce964 100644 --- a/OpenAuth.WebApi/Template/SingleTable/BuildAppWithDynamicHeader.html +++ b/OpenAuth.WebApi/Template/SingleTable/BuildAppWithDynamicHeader.html @@ -1,19 +1,18 @@ using System; +using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; using Infrastructure; -using Microsoft.EntityFrameworkCore; using OpenAuth.App.Interface; using OpenAuth.App.Request; using OpenAuth.App.Response; -using OpenAuth.Repository; using OpenAuth.Repository.Domain; -using OpenAuth.Repository.Interface; +using SqlSugar; namespace OpenAuth.App { - public class {ModuleCode} : {BaseAppName}<{ClassName},OpenAuthDBContext> + public class {ModuleCode} : SqlSugarBaseApp<{ClassName}> { /// @@ -24,52 +23,62 @@ namespace OpenAuth.App var loginContext = _auth.GetCurrentUser(); if (loginContext == null) { - throw new CommonException("登录已过期", Define.INVALID_TOKEN); + throw new CommonException("登录已过期", Define.INVALID_TOKEN); } - + var columnFields = loginContext.GetTableColumns("{ClassName}"); if (columnFields == null || columnFields.Count == 0) { - throw new Exception("请在代码生成界面配置{ClassName}表的字段属性"); + throw new Exception("请在代码生成界面配置{ClassName}表的字段属性"); } - + var result = new TableData(); var objs = GetDataPrivilege("u"); if (!string.IsNullOrEmpty(request.key)) { - //增加筛选条件,如: - //objs = objs.Where(u => u.Name.Contains(request.key)); + //增加筛选条件,如: + objs = objs.Where(u => u.Name.Contains(request.key)); } {ForeignKeyTemplate} - - var propertyStr = string.Join(',', columnFields.Select(u => u.ColumnName)); + + var columnnames = columnFields.Select(u => u.ColumnName); + var propertyStr = string.Join(',', columnnames); result.columnFields = columnFields; - result.data = objs.OrderBy(u => u.Id) - .Skip((request.page - 1) * request.limit) - .Take(request.limit).Select($"new ({propertyStr})"); - result.count =await objs.CountAsync(); + result.data = objs.OrderByDescending(u => u.Id) + .Skip((request.page - 1) * request.limit) + .Take(request.limit).Select($"{propertyStr}").ToList(); + result.count = await objs.CountAsync(); return result; } - public void Add(AddOrUpdate{ClassName}Req obj) + public void Add(AddOrUpdate{ClassName}Req req) { - //程序类型取入口应用的名称,可以根据自己需要调整 - var addObj = obj.MapTo<{ClassName}>(); - //addObj.Time = DateTime.Now; - Repository.Add(addObj); + var obj = req.MapTo<{ClassName}>(); + obj.Id = Guid.NewGuid().ToString(); + //todo: 根据业务需要调整字段 + obj.Createtime = DateTime.Now; + var user = _auth.GetCurrentUser().User; + obj.Createuserid = user.Id; + obj.Createusername = user.Name; + Repository.Insert(obj); } - + public void Update(AddOrUpdate{ClassName}Req obj) { - UnitWork.Update<{ClassName}>(u => u.Id == obj.Id, u => new {ClassName} + var user = _auth.GetCurrentUser().User; + Repository.Update(u => new {ClassName} { - //todo:要修改的字段赋值 - }); + //todo: 根据业务需要调整字段 + Name = obj.Name, + Updatetime = DateTime.Now, + Updateuserid = user.Id, + Updateusername = user.Name + },u => u.Id == obj.Id); } - public {ModuleCode}(IUnitWork unitWork, IRepository<{ClassName},OpenAuthDBContext> repository, IAuth auth) : base(unitWork, repository, auth) + public {ModuleCode}(ISqlSugarClient client, IAuth auth) : base(client, auth) { } }