更新基类控制器
This commit is contained in:
@@ -0,0 +1,49 @@
|
|||||||
|
using Microsoft.AspNetCore.Mvc;
|
||||||
|
using Yi.Framework.Common.Models;
|
||||||
|
using Yi.Framework.Model.Query;
|
||||||
|
using Yi.Framework.Repository;
|
||||||
|
|
||||||
|
namespace Yi.Framework.ApiMicroservice.Controllers
|
||||||
|
{
|
||||||
|
[ApiController]
|
||||||
|
[Route("api/[controller]/[action]")]
|
||||||
|
public class BaseCrudController<T> : ControllerBase where T : class,new()
|
||||||
|
{
|
||||||
|
private readonly ILogger<T> _logger;
|
||||||
|
|
||||||
|
public IRepository<T> _iRepository;
|
||||||
|
public BaseCrudController(ILogger<T> logger, IRepository<T> iRepository)
|
||||||
|
{
|
||||||
|
_logger = logger;
|
||||||
|
_iRepository = iRepository;
|
||||||
|
}
|
||||||
|
|
||||||
|
[HttpGet]
|
||||||
|
public async Task<Result> Get()
|
||||||
|
{
|
||||||
|
return Result.Success().SetData(await _iRepository.GetListAsync());
|
||||||
|
}
|
||||||
|
|
||||||
|
[HttpPost]
|
||||||
|
public async Task<Result> Page(QueryCondition queryCondition)
|
||||||
|
{
|
||||||
|
return Result.Success().SetData(_iRepository.CommonPage(queryCondition));
|
||||||
|
}
|
||||||
|
|
||||||
|
[HttpPost]
|
||||||
|
public async Task<Result> Add(T entity)
|
||||||
|
{
|
||||||
|
return Result.Success().SetData(await _iRepository.InsertReturnEntityAsync(entity));
|
||||||
|
}
|
||||||
|
[HttpPut]
|
||||||
|
public async Task<Result> Update(T entity)
|
||||||
|
{
|
||||||
|
return Result.Success().SetStatus(await _iRepository.UpdateAsync(entity));
|
||||||
|
}
|
||||||
|
[HttpDelete]
|
||||||
|
public async Task<Result> Delete(object[] ids)
|
||||||
|
{
|
||||||
|
return Result.Success().SetStatus(await _iRepository.DeleteByIdsAsync(ids));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -8,6 +8,7 @@ using System.Threading.Tasks;
|
|||||||
using Yi.Framework.Common.Models;
|
using Yi.Framework.Common.Models;
|
||||||
using Yi.Framework.Interface;
|
using Yi.Framework.Interface;
|
||||||
using Yi.Framework.Model.Models;
|
using Yi.Framework.Model.Models;
|
||||||
|
using Yi.Framework.Repository;
|
||||||
using Yi.Framework.WebCore;
|
using Yi.Framework.WebCore;
|
||||||
using Yi.Framework.WebCore.AuthorizationPolicy;
|
using Yi.Framework.WebCore.AuthorizationPolicy;
|
||||||
|
|
||||||
@@ -15,26 +16,16 @@ namespace Yi.Framework.ApiMicroservice.Controllers
|
|||||||
{
|
{
|
||||||
[ApiController]
|
[ApiController]
|
||||||
[Route("api/[controller]/[action]")]
|
[Route("api/[controller]/[action]")]
|
||||||
public class UserController : ControllerBase
|
public class UserController : BaseCrudController<UserEntity>
|
||||||
{
|
{
|
||||||
private readonly ILogger<UserController> _logger;
|
public UserController(ILogger<UserEntity> logger, IUserService iUserService) : base(logger, iUserService)
|
||||||
|
|
||||||
private IUserService _iUserService;
|
|
||||||
public UserController(ILogger<UserController> logger, IUserService iUserService)
|
|
||||||
{
|
{
|
||||||
_logger = logger;
|
|
||||||
_iUserService = iUserService;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
[HttpGet]
|
[HttpGet]
|
||||||
public async Task<Result> Get()
|
public async Task<IActionResult> Test()
|
||||||
{
|
{
|
||||||
return Result.Success().SetData(await _iUserService.GetListAsync());
|
return Ok(await _iRepository.GetListAsync());
|
||||||
}
|
|
||||||
[HttpPost]
|
|
||||||
public async Task<Result> Add(UserEntity userEntity)
|
|
||||||
{
|
|
||||||
return Result.Success().SetData(await _iUserService.InsertAsync(userEntity));
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
18
Yi.Framework.Net6/Yi.Framework.Model/Query/QueryCondition.cs
Normal file
18
Yi.Framework.Net6/Yi.Framework.Model/Query/QueryCondition.cs
Normal file
@@ -0,0 +1,18 @@
|
|||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Text;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
|
namespace Yi.Framework.Model.Query
|
||||||
|
{
|
||||||
|
public class QueryCondition
|
||||||
|
{
|
||||||
|
public int Index { get; set; }
|
||||||
|
public int Size { get; set; }
|
||||||
|
public int Count { get; set; }
|
||||||
|
public List<QueryParameter> Parameters { get; set; } = new List<QueryParameter>();
|
||||||
|
public List<string> OrderBys { get; set; } = new List<string>();
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
16
Yi.Framework.Net6/Yi.Framework.Model/Query/QueryParameter.cs
Normal file
16
Yi.Framework.Net6/Yi.Framework.Model/Query/QueryParameter.cs
Normal file
@@ -0,0 +1,16 @@
|
|||||||
|
using SqlSugar;
|
||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Text;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
|
namespace Yi.Framework.Model.Query
|
||||||
|
{
|
||||||
|
public class QueryParameter
|
||||||
|
{
|
||||||
|
public string FieldName { get; set; }
|
||||||
|
public string FieldValue { get; set; }
|
||||||
|
public ConditionalType ConditionalType { get; set; } = ConditionalType.Like;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,42 @@
|
|||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Text;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
|
namespace Yi.Framework.Model.Query
|
||||||
|
{
|
||||||
|
public static class QueryParametersExtensions
|
||||||
|
{
|
||||||
|
|
||||||
|
//public static QueryParameters SetParameters(this QueryParameters queryParameters, Dictionary<string, string> dic, bool IsTenant = true)
|
||||||
|
//{
|
||||||
|
// //var httpcontext = ServiceLocator.Instance.GetService<IHttpContextAccessor>().HttpContext;
|
||||||
|
// queryParameters.OrderBys = new List<string> { "CreateTime" };
|
||||||
|
// foreach (var p in dic)
|
||||||
|
// {
|
||||||
|
// QueryParameter qp = null;
|
||||||
|
// if (p.Key == "IsDeleted" || p.Key == "Id")
|
||||||
|
// {
|
||||||
|
// qp = new QueryParameter() { FieldName = p.Key, FieldValue = p.Value, ConditionalType = ConditionalType.Equal };
|
||||||
|
|
||||||
|
// }
|
||||||
|
// else
|
||||||
|
// {
|
||||||
|
// qp = new QueryParameter() { FieldName = p.Key, FieldValue = p.Value };
|
||||||
|
|
||||||
|
// }
|
||||||
|
// queryParameters.Parameters.Add(qp);
|
||||||
|
// }
|
||||||
|
// if (IsTenant)
|
||||||
|
// {
|
||||||
|
// //if (httpcontext.Request.Headers["TenantLevel"].ToString() == "0")
|
||||||
|
// //{
|
||||||
|
// // queryParameters.Parameters.Add(new QueryParameter() { ConditionalType = ConditionalType.Equal, FieldName = "TenantId", FieldValue = httpcontext.Request.Headers["TenantId"].ToString() });
|
||||||
|
// //}
|
||||||
|
// }
|
||||||
|
|
||||||
|
// return queryParameters;
|
||||||
|
//}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,20 +0,0 @@
|
|||||||
using System;
|
|
||||||
using System.Collections.Generic;
|
|
||||||
using System.Text;
|
|
||||||
|
|
||||||
namespace Yi.Framework.Model.Search
|
|
||||||
{
|
|
||||||
public class PageResult<T>
|
|
||||||
{
|
|
||||||
public static readonly long serialVersionUID = 4612105649493688532L;
|
|
||||||
public long total; // 总记录数
|
|
||||||
public int totalPages; //总页数
|
|
||||||
public List<T> rows; // 每页显示的数据集合
|
|
||||||
|
|
||||||
public PageResult(long total, List<T> rows)
|
|
||||||
{
|
|
||||||
this.total = total;
|
|
||||||
this.rows = rows;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,39 +0,0 @@
|
|||||||
using System;
|
|
||||||
using System.Collections.Generic;
|
|
||||||
using System.Linq;
|
|
||||||
using System.Text;
|
|
||||||
using System.Threading.Tasks;
|
|
||||||
|
|
||||||
namespace Yi.Framework.Model.Search
|
|
||||||
{
|
|
||||||
public class SearchRequest
|
|
||||||
{
|
|
||||||
public static readonly int DEFAULT_PAGE = 1;
|
|
||||||
public static readonly int DEFAULT_SIZE = 20;
|
|
||||||
public string key { get; set; }
|
|
||||||
public int page { get; set; }
|
|
||||||
//排序字段
|
|
||||||
public string sortBy { get; set; }
|
|
||||||
//是否降序
|
|
||||||
public bool descending { get; set; }
|
|
||||||
//过滤字段
|
|
||||||
public Dictionary<string, string> filter = new Dictionary<string, string>();
|
|
||||||
|
|
||||||
public int getPage()
|
|
||||||
{
|
|
||||||
if (page == 0)
|
|
||||||
{
|
|
||||||
return DEFAULT_PAGE;
|
|
||||||
}
|
|
||||||
// 获取页码时做一些校验,不能小于1
|
|
||||||
return Math.Max(DEFAULT_PAGE, page);
|
|
||||||
}
|
|
||||||
|
|
||||||
public int getSize()
|
|
||||||
{
|
|
||||||
return DEFAULT_SIZE;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,4 +1,5 @@
|
|||||||
using SqlSugar;
|
using SqlSugar;
|
||||||
|
using Yi.Framework.Common.Models;
|
||||||
|
|
||||||
namespace Yi.Framework.Repository
|
namespace Yi.Framework.Repository
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -5,27 +5,14 @@ using System.Linq;
|
|||||||
using System.Linq.Expressions;
|
using System.Linq.Expressions;
|
||||||
using System.Text;
|
using System.Text;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
|
using Yi.Framework.Model.Query;
|
||||||
|
|
||||||
namespace Yi.Framework.Repository
|
namespace Yi.Framework.Repository
|
||||||
{
|
{
|
||||||
public interface IRepository<T> : ISimpleClient<T> where T : class, new()
|
public interface IRepository<T> : ISimpleClient<T> where T : class, new()
|
||||||
{
|
{
|
||||||
public Task<T> InsertReturnEntityAsync(T entity);
|
public Task<T> InsertReturnEntityAsync(T entity);
|
||||||
public object CommonPage(QueryParameters pars, int pageIndex, int pageSize);
|
|
||||||
public object CommonPage(QueryParameters pars, int pageIndex, int pageSize, bool whereBool, Expression<Func<T, bool>> where);
|
|
||||||
|
|
||||||
public object CommonPageMapper<T2, TT>(Expression<Func<TT, ManyToMany>> expression, QueryParameters pars, int pageIndex, int pageSize, bool whereBool, Expression<Func<T, bool>> where);
|
|
||||||
|
|
||||||
public Task<T> FirstMapperAsync<T2, TT>(Expression<Func<TT, ManyToMany>> expression, bool isTenant = true);
|
|
||||||
|
|
||||||
public Task<List<T>> ToListMapperAsync<T2, TT>(Expression<Func<TT, ManyToMany>> expression, bool isTenant = true);
|
|
||||||
|
|
||||||
public Task<List<T>> ToListMapperAsync<T2, TT>(Expression<Func<TT, ManyToMany>> expression, bool whereBool, Expression<Func<T, bool>> where, bool isTenant = true);
|
|
||||||
|
|
||||||
public Task<List<T>> GetListAsync(Expression<Func<T, bool>> whereExpression, bool whereBool, Expression<Func<T, bool>> where, bool isTenant = true);
|
|
||||||
|
|
||||||
public Task<List<T>> GetListAsync(bool whereBool, Expression<Func<T, bool>> where, bool isTenant = true);
|
|
||||||
public Task<List<S>> StoreAsync<S>(string storeName, object para);
|
public Task<List<S>> StoreAsync<S>(string storeName, object para);
|
||||||
|
public object CommonPage(QueryCondition queryCondition);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
using SqlSugar;
|
using SqlSugar;
|
||||||
using System.Data;
|
using System.Data;
|
||||||
using System.Linq.Expressions;
|
using System.Linq.Expressions;
|
||||||
using static Yi.Framework.Repository.QueryParametersExtensions;
|
using Yi.Framework.Model.Query;
|
||||||
|
|
||||||
/***这里面写的代码不会给覆盖,如果要重新生成请删除 Repository.cs ***/
|
/***这里面写的代码不会给覆盖,如果要重新生成请删除 Repository.cs ***/
|
||||||
namespace Yi.Framework.Repository
|
namespace Yi.Framework.Repository
|
||||||
@@ -10,7 +10,7 @@ namespace Yi.Framework.Repository
|
|||||||
/// 仓储模式
|
/// 仓储模式
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <typeparam name="T"></typeparam>
|
/// <typeparam name="T"></typeparam>
|
||||||
public class Repository<T> : DataContext<T> ,IRepository<T> where T : class, new()
|
public class Repository<T> : DataContext<T>, IRepository<T> where T : class, new()
|
||||||
{
|
{
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
@@ -25,119 +25,34 @@ namespace Yi.Framework.Repository
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 添加返回实体
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="entity"></param>
|
||||||
|
/// <returns></returns>
|
||||||
public async Task<T> InsertReturnEntityAsync(T entity)
|
public async Task<T> InsertReturnEntityAsync(T entity)
|
||||||
{
|
{
|
||||||
return await Db.Insertable(entity).ExecuteReturnEntityAsync();
|
return await Db.Insertable(entity).ExecuteReturnEntityAsync();
|
||||||
}
|
}
|
||||||
/// <summary>
|
|
||||||
/// whereif与where混搭,多租户
|
|
||||||
/// </summary>
|
|
||||||
/// <param name="whereExpression"></param>
|
|
||||||
/// <param name="whereBool"></param>
|
|
||||||
/// <param name="where"></param>
|
|
||||||
/// <returns></returns>
|
|
||||||
public async Task<List<T>> GetListAsync(Expression<Func<T, bool>> whereExpression, bool whereBool, Expression<Func<T, bool>> where, bool isTenant = true)
|
|
||||||
{
|
|
||||||
return await Db.Queryable<T>().WhereIF(whereBool, where).Where(whereExpression).WhereTenant(isTenant).ToListAsync();
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// where重载,多租户
|
/// 调用存储过程
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="whereBool"></param>
|
/// <typeparam name="S"></typeparam>
|
||||||
/// <param name="where"></param>
|
/// <param name="storeName"></param>
|
||||||
|
/// <param name="para"></param>
|
||||||
/// <returns></returns>
|
/// <returns></returns>
|
||||||
public async Task<List<T>> GetListAsync(bool whereBool, Expression<Func<T, bool>> where, bool isTenant = true)
|
|
||||||
{
|
|
||||||
return await Db.Queryable<T>().WhereIF(whereBool, where).WhereTenant(isTenant). ToListAsync();
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// 左连接,三表连接,返回最右边的列表,多租户
|
|
||||||
/// </summary>
|
|
||||||
/// <typeparam name="M"></typeparam>
|
|
||||||
/// <typeparam name="R"></typeparam>
|
|
||||||
/// <param name="joinQueryable1"></param>
|
|
||||||
/// <param name="joinQueryable12"></param>
|
|
||||||
/// <param name="whereLambda"></param>
|
|
||||||
/// <param name="selectLambda"></param>
|
|
||||||
/// <returns></returns>
|
|
||||||
public async Task<List<R>> LeftJoinListAsync<M, R>(Expression<Func<T, M, bool>> joinQueryable1, Expression<Func<T, M, R, bool>> joinQueryable12, Expression<Func<T, bool>> whereLambda, Expression<Func<T, M, R>> selectLambda, bool isTenant = true)
|
|
||||||
{
|
|
||||||
return await Db.Queryable<T>().LeftJoin<M>(joinQueryable1)
|
|
||||||
.LeftJoin<R>(joinQueryable12)
|
|
||||||
.Where(whereLambda)
|
|
||||||
.WhereTenant(isTenant)
|
|
||||||
.Select(selectLambda)
|
|
||||||
.ToListAsync();
|
|
||||||
}
|
|
||||||
|
|
||||||
public async Task<List<S>> StoreAsync<S>(string storeName, object para)
|
public async Task<List<S>> StoreAsync<S>(string storeName, object para)
|
||||||
{
|
{
|
||||||
return await Db.Ado.UseStoredProcedure().SqlQueryAsync<S>(storeName, para);
|
return await Db.Ado.UseStoredProcedure().SqlQueryAsync<S>(storeName, para);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// 调用sql
|
|
||||||
/// </summary>
|
|
||||||
/// <param name="sql"></param>
|
|
||||||
/// <param name="para"></param>
|
|
||||||
/// <returns></returns>
|
|
||||||
public async Task<DataTable> SqlDataTableAsync(string sql, object para = null)
|
|
||||||
{
|
|
||||||
return await Db.Ado.GetDataTableAsync(sql, para);
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// 导航属性mapper返回一个,多租户
|
|
||||||
/// </summary>
|
|
||||||
/// <typeparam name="T2"></typeparam>
|
|
||||||
/// <typeparam name="TT"></typeparam>
|
|
||||||
/// <param name="expression"></param>
|
|
||||||
/// <returns></returns>
|
|
||||||
public async Task<T> FirstMapperAsync<T2, TT>(Expression<Func<TT, ManyToMany>> expression,bool isTenant=true)
|
|
||||||
{
|
|
||||||
return await Db.Queryable<T>().Mapper<T, T2, TT>(expression).WhereTenant(isTenant).FirstAsync();
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// 导航属性mapper返回一组,多租户
|
|
||||||
/// </summary>
|
|
||||||
/// <typeparam name="T2"></typeparam>
|
|
||||||
/// <typeparam name="TT"></typeparam>
|
|
||||||
/// <param name="expression"></param>
|
|
||||||
/// <returns></returns>
|
|
||||||
public async Task<List<T>> ToListMapperAsync<T2, TT>(Expression<Func<TT, ManyToMany>> expression, bool isTenant = true)
|
|
||||||
{
|
|
||||||
return await Db.Queryable<T>() .Mapper<T, T2, TT>(expression).WhereTenant(isTenant).ToListAsync();
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// 导航属性mapper返回一组.同时添加条件,多租户
|
|
||||||
/// </summary>
|
|
||||||
/// <typeparam name="T2"></typeparam>
|
|
||||||
/// <typeparam name="TT"></typeparam>
|
|
||||||
/// <param name="expression"></param>
|
|
||||||
/// <param name="where"></param>
|
|
||||||
/// <returns></returns>
|
|
||||||
public async Task<List<T>> ToListMapperAsync<T2, TT>(Expression<Func<TT, ManyToMany>> expression, bool whereBool, Expression<Func<T, bool>> where, bool isTenant = true)
|
|
||||||
{
|
|
||||||
return await Db.Queryable<T>().Mapper<T, T2, TT>(expression).WhereIF(whereBool,where).WhereTenant(isTenant).ToListAsync();
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 仓储扩展方法:单表查询通用分页
|
/// 仓储扩展方法:单表查询通用分页
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <returns></returns>
|
/// <returns></returns>
|
||||||
public object CommonPage(QueryParameters pars, int pageIndex, int pageSize)
|
public object CommonPage(QueryCondition pars)
|
||||||
{
|
{
|
||||||
int tolCount = 0;
|
int tolCount = 0;
|
||||||
var sugarParamters = pars.Parameters.Select(it => (IConditionalModel)new ConditionalModel()
|
var sugarParamters = pars.Parameters.Select(it => (IConditionalModel)new ConditionalModel()
|
||||||
@@ -154,168 +69,14 @@ namespace Yi.Framework.Repository
|
|||||||
query.OrderBy(item.ToSqlFilter());//格式 id asc或者 id desc
|
query.OrderBy(item.ToSqlFilter());//格式 id asc或者 id desc
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
var result = query.Where(sugarParamters).ToPageList(pageIndex, pageSize, ref tolCount);
|
var result = query.Where(sugarParamters).ToPageList(pars.Index, pars.Size, ref tolCount);
|
||||||
return new
|
return new
|
||||||
{
|
{
|
||||||
count = tolCount,
|
count = tolCount,
|
||||||
data = result
|
data = result
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// 额外添加动态条件拼接
|
|
||||||
/// </summary>
|
|
||||||
/// <returns></returns>
|
|
||||||
public object CommonPage(QueryParameters pars, int pageIndex, int pageSize, bool whereBool, Expression<Func<T, bool>> where)
|
|
||||||
{
|
|
||||||
int tolCount = 0;
|
|
||||||
var sugarParamters = pars.Parameters.Select(it => (IConditionalModel)new ConditionalModel()
|
|
||||||
{
|
|
||||||
ConditionalType = it.ConditionalType,
|
|
||||||
FieldName = it.FieldName,
|
|
||||||
FieldValue = it.FieldValue
|
|
||||||
}).ToList();
|
|
||||||
var query = Db.Queryable<T>();
|
|
||||||
if (pars.OrderBys != null)
|
|
||||||
{
|
|
||||||
foreach (var item in pars.OrderBys)
|
|
||||||
{
|
|
||||||
query.OrderBy(item.ToSqlFilter());//格式 id asc或者 id desc
|
|
||||||
}
|
|
||||||
}
|
|
||||||
var result = query.WhereIF(whereBool, where).Where(sugarParamters).ToPageList(pageIndex, pageSize, ref tolCount);
|
|
||||||
return new
|
|
||||||
{
|
|
||||||
count = tolCount,
|
|
||||||
data = result
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// 导航属性mapper分页多条件
|
|
||||||
/// </summary>
|
|
||||||
/// <typeparam name="T2"></typeparam>
|
|
||||||
/// <typeparam name="TT"></typeparam>
|
|
||||||
/// <param name="expression"></param>
|
|
||||||
/// <returns></returns>
|
|
||||||
public object CommonPageMapper<T2, TT>(Expression<Func<TT, ManyToMany>> expression, QueryParameters pars, int pageIndex, int pageSize,bool whereBool, Expression<Func<T, bool>> where)
|
|
||||||
{
|
|
||||||
int tolCount = 0;
|
|
||||||
var sugarParamters = pars.Parameters.Select(it => (IConditionalModel)new ConditionalModel()
|
|
||||||
{
|
|
||||||
ConditionalType = it.ConditionalType,
|
|
||||||
FieldName = it.FieldName,
|
|
||||||
FieldValue = it.FieldValue
|
|
||||||
}).ToList();
|
|
||||||
var query = Db.Queryable<T>();
|
|
||||||
if (pars.OrderBys != null)
|
|
||||||
{
|
|
||||||
foreach (var item in pars.OrderBys)
|
|
||||||
{
|
|
||||||
query.OrderBy(item.ToSqlFilter());//格式 id asc或者 id desc
|
|
||||||
}
|
|
||||||
}
|
|
||||||
var result = query.Mapper < T, T2, TT>(expression).WhereIF(whereBool, where). Where(sugarParamters).ToPageList(pageIndex, pageSize, ref tolCount);
|
|
||||||
return new
|
|
||||||
{
|
|
||||||
count = tolCount,
|
|
||||||
data = result
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// 通用查询参数
|
|
||||||
/// </summary>
|
|
||||||
public class QueryParameters
|
|
||||||
{
|
|
||||||
public List<QueryParameter> Parameters { get; set; } = new List<QueryParameter>();
|
|
||||||
public List<string> OrderBys { get; set; } = new List<string>();
|
|
||||||
}
|
|
||||||
|
|
||||||
public static class QueryParametersExtensions
|
|
||||||
{
|
|
||||||
|
|
||||||
public static ISugarQueryable<T,M,R> WhereTenant<T, M, R>(this ISugarQueryable<T, M, R> db, bool isTenant = true)
|
|
||||||
{
|
|
||||||
if (isTenant)
|
|
||||||
{
|
|
||||||
var sugarParamters = new QueryParameters().SetParameters(new Dictionary<string, string>()).Parameters.Select(it => (IConditionalModel)new ConditionalModel()
|
|
||||||
{
|
|
||||||
ConditionalType = it.ConditionalType,
|
|
||||||
FieldName = it.FieldName,
|
|
||||||
FieldValue = it.FieldValue
|
|
||||||
}).ToList();
|
|
||||||
return db.Where(sugarParamters);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
return db;
|
|
||||||
|
|
||||||
}
|
|
||||||
public static ISugarQueryable<T> WhereTenant<T>(this ISugarQueryable<T> db, bool isTenant = true)
|
|
||||||
{
|
|
||||||
if (isTenant)
|
|
||||||
{
|
|
||||||
var sugarParamters = new QueryParameters().SetParameters(new Dictionary<string, string>()).Parameters.Select(it => (IConditionalModel)new ConditionalModel()
|
|
||||||
{
|
|
||||||
ConditionalType = it.ConditionalType,
|
|
||||||
FieldName = it.FieldName,
|
|
||||||
FieldValue = it.FieldValue
|
|
||||||
}).ToList();
|
|
||||||
return db.Where(sugarParamters);
|
|
||||||
}
|
|
||||||
|
|
||||||
return db;
|
|
||||||
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
public static QueryParameters SetParameters(this QueryParameters queryParameters, Dictionary<string, string> dic,bool IsTenant=true)
|
|
||||||
{
|
|
||||||
//var httpcontext = ServiceLocator.Instance.GetService<IHttpContextAccessor>().HttpContext;
|
|
||||||
queryParameters.OrderBys = new List<string> { "CreateTime" };
|
|
||||||
|
|
||||||
|
|
||||||
foreach (var p in dic)
|
|
||||||
{
|
|
||||||
QueryParameter qp = null;
|
|
||||||
if (p.Key == "IsDeleted" || p.Key=="Id")
|
|
||||||
{
|
|
||||||
qp= new QueryParameter() { FieldName = p.Key, FieldValue = p.Value, ConditionalType = ConditionalType.Equal };
|
|
||||||
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
qp= new QueryParameter() { FieldName = p.Key, FieldValue = p.Value };
|
|
||||||
|
|
||||||
}
|
|
||||||
queryParameters.Parameters.Add(qp);
|
|
||||||
}
|
|
||||||
if (IsTenant)
|
|
||||||
{
|
|
||||||
//if (httpcontext.Request.Headers["TenantLevel"].ToString() == "0")
|
|
||||||
//{
|
|
||||||
// queryParameters.Parameters.Add(new QueryParameter() { ConditionalType = ConditionalType.Equal, FieldName = "TenantId", FieldValue = httpcontext.Request.Headers["TenantId"].ToString() });
|
|
||||||
//}
|
|
||||||
}
|
|
||||||
|
|
||||||
return queryParameters;
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// 通用查询参数
|
|
||||||
/// </summary>
|
|
||||||
public class QueryParameter
|
|
||||||
{
|
|
||||||
public string FieldName { get; set; }
|
|
||||||
public string FieldValue { get; set; }
|
|
||||||
public ConditionalType ConditionalType { get; set; } = ConditionalType.Like;
|
|
||||||
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
@@ -6,6 +6,10 @@
|
|||||||
<Nullable>disable</Nullable>
|
<Nullable>disable</Nullable>
|
||||||
</PropertyGroup>
|
</PropertyGroup>
|
||||||
|
|
||||||
|
<ItemGroup>
|
||||||
|
<PackageReference Include="Microsoft.AspNetCore.Http.Abstractions" Version="2.2.0" />
|
||||||
|
</ItemGroup>
|
||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<ProjectReference Include="..\Yi.Framework.Model\Yi.Framework.Model.csproj" />
|
<ProjectReference Include="..\Yi.Framework.Model\Yi.Framework.Model.csproj" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|||||||
Reference in New Issue
Block a user