sqlsugar动态查询

This commit is contained in:
wintel 2023-08-27 00:23:45 +08:00
parent 38fee56701
commit 331bc9d042
4 changed files with 53 additions and 30 deletions

View File

@ -8,45 +8,49 @@ using OpenAuth.App.Request;
using OpenAuth.Repository;
using OpenAuth.Repository.Domain;
using OpenAuth.Repository.Interface;
using SqlSugar;
namespace OpenAuth.App
{
/// <summary>
/// 分类管理
/// 应用管理
/// </summary>
public class AppManager : BaseStringApp<Application, OpenAuthDBContext>
public class AppManager : SqlSugarBaseApp<Application>
{
public AppManager(IUnitWork<OpenAuthDBContext> unitWork, IRepository<Application, OpenAuthDBContext> repository) : base(unitWork, repository, null)
public AppManager(ISqlSugarClient client) : base(client, null)
{
}
public void Add(Application Application)
public void Add(Application application)
{
if (string.IsNullOrEmpty(Application.Id))
if (string.IsNullOrEmpty(application.Id))
{
Application.Id = Guid.NewGuid().ToString();
application.Id = Guid.NewGuid().ToString();
}
Repository.Add(Application);
Repository.Insert(application);
}
public void Update(Application Application)
public void Update(Application application)
{
Repository.Update(Application);
Repository.Update(application);
}
public async Task<List<Application>> GetList(QueryAppListReq request)
{
var applications = UnitWork.Find<Application>(null);
var applications = Repository.GetListAsync();
return await applications.ToListAsync();
return await applications;
}
public Application GetByAppKey(string modelAppKey)
{
return Repository.FirstOrDefault(u => u.AppSecret == modelAppKey);
return Repository.GetFirst(u => u.AppSecret == modelAppKey);
}
public void Delete(string[] ids)
{
Repository.DeleteByIds(ids);
}
}
}

View File

@ -1,23 +1,25 @@
using System;
using System.Linq;
using Infrastructure;
using Microsoft.EntityFrameworkCore;
using OpenAuth.App.Interface;
using OpenAuth.Repository;
using OpenAuth.Repository.Core;
using OpenAuth.Repository.Domain;
using OpenAuth.Repository.Interface;
using SqlSugar;
namespace OpenAuth.App
{
public abstract class SqlSugarBaseApp<T>
public abstract class SqlSugarBaseApp<T> where T : class, new()
{
protected ISqlSugarClient SugarClient;
protected SqlSugarRepository<T> Repository;
protected IAuth _auth;
public SqlSugarBaseApp(ISqlSugarClient client, IAuth auth)
{
Repository = new SqlSugarRepository<T>(client); //这里用new而不用注入可以保证client和repository用的是同一个client
SugarClient = client;
_auth = auth;
}

View File

@ -3,20 +3,18 @@ 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
{
/// <summary>
/// 分类管理
/// </summary>
public class ResourceApp:BaseStringApp<Resource,OpenAuthDBContext>
public class ResourceApp:SqlSugarBaseApp<Resource>
{
private RevelanceManagerApp _revelanceApp;
@ -28,13 +26,13 @@ namespace OpenAuth.App
var user = _auth.GetCurrentUser().User;
obj.CreateUserId = user.Id;
obj.CreateUserName = user.Name;
Repository.Add(obj);
Repository.Insert(obj);
}
public void Update(AddOrUpdateResReq obj)
{
var user = _auth.GetCurrentUser().User;
UnitWork.Update<Resource>(u => u.Id == obj.Id, u => new Resource
Repository.Update(u => new Resource
{
Name = obj.Name,
Disable = obj.Disable,
@ -50,13 +48,13 @@ namespace OpenAuth.App
UpdateUserId = user.Id,
UpdateUserName = user.Name
//todo:要修改的字段赋值
});
},u => u.Id == obj.Id);
}
public IEnumerable<Resource> LoadForRole(string appId, string roleId)
{
var elementIds = _revelanceApp.Get(Define.ROLERESOURCE, true, roleId);
return UnitWork.Find<Resource>(u => elementIds.Contains(u.Id) && (appId == null || appId =="" || u.AppId == appId));
return SugarClient.Queryable<Resource>().Where(u => elementIds.Contains(u.Id) && (appId == null || appId =="" || u.AppId == appId)).ToArray();
}
public async Task<TableData> Load(QueryResourcesReq request)
@ -90,15 +88,18 @@ namespace OpenAuth.App
result.columnFields = columnFields;
result.data = resources.OrderBy(u => u.TypeId)
.Skip((request.page - 1) * request.limit)
.Take(request.limit).Select($"new ({propertyStr})");
.Take(request.limit).Select($"{propertyStr}").ToList();
result.count = await resources.CountAsync();
return result;
}
public ResourceApp(IUnitWork<OpenAuthDBContext> unitWork, IRepository<Resource,OpenAuthDBContext> repository
,RevelanceManagerApp app,IAuth auth) : base(unitWork, repository, auth)
public ResourceApp(ISqlSugarClient client, IAuth auth) : base(client, auth)
{
_revelanceApp = app;
}
public void Delete(string[] ids)
{
Repository.DeleteByIds(ids);
}
}
}

View File

@ -0,0 +1,16 @@
using System.Collections.Generic;
using SqlSugar;
namespace OpenAuth.Repository;
/// <summary>
/// SqlSugar仓储
/// <para>具体用法参考https://www.donet5.com/Home/Doc?typeId=1228</para>
/// </summary>
public class SqlSugarRepository<T>: SimpleClient<T> where T : class, new()
{
public SqlSugarRepository(ISqlSugarClient client)
{
base.Context=client;
}
}