feat: 代码生成默认生成基于SqlSugar的代码

This commit is contained in:
wintel 2025-03-30 00:24:21 +08:00
parent bb87b4fed7
commit 1290ed068e
2 changed files with 64 additions and 49 deletions

View File

@ -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}>
{
/// <summary>
@ -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<OpenAuthDBContext> unitWork, IRepository<{ClassName},OpenAuthDBContext> repository, IAuth auth) : base(unitWork, repository, auth)
public {ModuleCode}(ISqlSugarClient client, IAuth auth) : base(client, auth)
{
}
}

View File

@ -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}>
{
/// <summary>
@ -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<OpenAuthDBContext> unitWork, IRepository<{ClassName},OpenAuthDBContext> repository, IAuth auth) : base(unitWork, repository, auth)
public {ModuleCode}(ISqlSugarClient client, IAuth auth) : base(client, auth)
{
}
}