diff --git a/Yi.Framework.Net6/Yi.Framework.ApiMicroservice/Controllers/BaseCrudController.cs b/Yi.Framework.Net6/Yi.Framework.ApiMicroservice/Controllers/BaseCrudController.cs new file mode 100644 index 00000000..a47868d3 --- /dev/null +++ b/Yi.Framework.Net6/Yi.Framework.ApiMicroservice/Controllers/BaseCrudController.cs @@ -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 : ControllerBase where T : class,new() + { + private readonly ILogger _logger; + + public IRepository _iRepository; + public BaseCrudController(ILogger logger, IRepository iRepository) + { + _logger = logger; + _iRepository = iRepository; + } + + [HttpGet] + public async Task Get() + { + return Result.Success().SetData(await _iRepository.GetListAsync()); + } + + [HttpPost] + public async Task Page(QueryCondition queryCondition) + { + return Result.Success().SetData(_iRepository.CommonPage(queryCondition)); + } + + [HttpPost] + public async Task Add(T entity) + { + return Result.Success().SetData(await _iRepository.InsertReturnEntityAsync(entity)); + } + [HttpPut] + public async Task Update(T entity) + { + return Result.Success().SetStatus(await _iRepository.UpdateAsync(entity)); + } + [HttpDelete] + public async Task Delete(object[] ids) + { + return Result.Success().SetStatus(await _iRepository.DeleteByIdsAsync(ids)); + } + } +} diff --git a/Yi.Framework.Net6/Yi.Framework.ApiMicroservice/Controllers/UserController.cs b/Yi.Framework.Net6/Yi.Framework.ApiMicroservice/Controllers/UserController.cs index 57f9e3d7..b601f1a9 100644 --- a/Yi.Framework.Net6/Yi.Framework.ApiMicroservice/Controllers/UserController.cs +++ b/Yi.Framework.Net6/Yi.Framework.ApiMicroservice/Controllers/UserController.cs @@ -8,6 +8,7 @@ using System.Threading.Tasks; using Yi.Framework.Common.Models; using Yi.Framework.Interface; using Yi.Framework.Model.Models; +using Yi.Framework.Repository; using Yi.Framework.WebCore; using Yi.Framework.WebCore.AuthorizationPolicy; @@ -15,26 +16,16 @@ namespace Yi.Framework.ApiMicroservice.Controllers { [ApiController] [Route("api/[controller]/[action]")] - public class UserController : ControllerBase + public class UserController : BaseCrudController { - private readonly ILogger _logger; - - private IUserService _iUserService; - public UserController(ILogger logger, IUserService iUserService) + public UserController(ILogger logger, IUserService iUserService) : base(logger, iUserService) { - _logger = logger; - _iUserService = iUserService; + } - [HttpGet] - public async Task Get() + public async Task Test() { - return Result.Success().SetData(await _iUserService.GetListAsync()); - } - [HttpPost] - public async Task Add(UserEntity userEntity) - { - return Result.Success().SetData(await _iUserService.InsertAsync(userEntity)); + return Ok(await _iRepository.GetListAsync()); } } } diff --git a/Yi.Framework.Net6/Yi.Framework.Model/Query/QueryCondition.cs b/Yi.Framework.Net6/Yi.Framework.Model/Query/QueryCondition.cs new file mode 100644 index 00000000..e3875eac --- /dev/null +++ b/Yi.Framework.Net6/Yi.Framework.Model/Query/QueryCondition.cs @@ -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 Parameters { get; set; } = new List(); + public List OrderBys { get; set; } = new List(); + + } +} diff --git a/Yi.Framework.Net6/Yi.Framework.Model/Query/QueryParameter.cs b/Yi.Framework.Net6/Yi.Framework.Model/Query/QueryParameter.cs new file mode 100644 index 00000000..e46520c1 --- /dev/null +++ b/Yi.Framework.Net6/Yi.Framework.Model/Query/QueryParameter.cs @@ -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; + } +} diff --git a/Yi.Framework.Net6/Yi.Framework.Model/Query/QueryParametersExtensions.cs b/Yi.Framework.Net6/Yi.Framework.Model/Query/QueryParametersExtensions.cs new file mode 100644 index 00000000..bac5176c --- /dev/null +++ b/Yi.Framework.Net6/Yi.Framework.Model/Query/QueryParametersExtensions.cs @@ -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 dic, bool IsTenant = true) + //{ + // //var httpcontext = ServiceLocator.Instance.GetService().HttpContext; + // queryParameters.OrderBys = new List { "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; + //} + } +} \ No newline at end of file diff --git a/Yi.Framework.Net6/Yi.Framework.Model/Search/PageResult.cs b/Yi.Framework.Net6/Yi.Framework.Model/Search/PageResult.cs deleted file mode 100644 index 52fde5cb..00000000 --- a/Yi.Framework.Net6/Yi.Framework.Model/Search/PageResult.cs +++ /dev/null @@ -1,20 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Text; - -namespace Yi.Framework.Model.Search -{ - public class PageResult - { - public static readonly long serialVersionUID = 4612105649493688532L; - public long total; // 总记录数 - public int totalPages; //总页数 - public List rows; // 每页显示的数据集合 - - public PageResult(long total, List rows) - { - this.total = total; - this.rows = rows; - } - } -} diff --git a/Yi.Framework.Net6/Yi.Framework.Model/Search/SearchRequest.cs b/Yi.Framework.Net6/Yi.Framework.Model/Search/SearchRequest.cs deleted file mode 100644 index a3853d37..00000000 --- a/Yi.Framework.Net6/Yi.Framework.Model/Search/SearchRequest.cs +++ /dev/null @@ -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 filter = new Dictionary(); - - public int getPage() - { - if (page == 0) - { - return DEFAULT_PAGE; - } - // 获取页码时做一些校验,不能小于1 - return Math.Max(DEFAULT_PAGE, page); - } - - public int getSize() - { - return DEFAULT_SIZE; - } - - - } -} diff --git a/Yi.Framework.Net6/Yi.Framework.Repository/DataContext.cs b/Yi.Framework.Net6/Yi.Framework.Repository/DataContext.cs index 1af51e10..1f01d075 100644 --- a/Yi.Framework.Net6/Yi.Framework.Repository/DataContext.cs +++ b/Yi.Framework.Net6/Yi.Framework.Repository/DataContext.cs @@ -1,4 +1,5 @@ using SqlSugar; +using Yi.Framework.Common.Models; namespace Yi.Framework.Repository { diff --git a/Yi.Framework.Net6/Yi.Framework.Repository/IRepository.cs b/Yi.Framework.Net6/Yi.Framework.Repository/IRepository.cs index 6efecffd..8d7c1107 100644 --- a/Yi.Framework.Net6/Yi.Framework.Repository/IRepository.cs +++ b/Yi.Framework.Net6/Yi.Framework.Repository/IRepository.cs @@ -5,27 +5,14 @@ using System.Linq; using System.Linq.Expressions; using System.Text; using System.Threading.Tasks; +using Yi.Framework.Model.Query; namespace Yi.Framework.Repository { public interface IRepository : ISimpleClient where T : class, new() { public Task InsertReturnEntityAsync(T entity); - public object CommonPage(QueryParameters pars, int pageIndex, int pageSize); - public object CommonPage(QueryParameters pars, int pageIndex, int pageSize, bool whereBool, Expression> where); - - public object CommonPageMapper(Expression> expression, QueryParameters pars, int pageIndex, int pageSize, bool whereBool, Expression> where); - - public Task FirstMapperAsync(Expression> expression, bool isTenant = true); - - public Task> ToListMapperAsync(Expression> expression, bool isTenant = true); - - public Task> ToListMapperAsync(Expression> expression, bool whereBool, Expression> where, bool isTenant = true); - - public Task> GetListAsync(Expression> whereExpression, bool whereBool, Expression> where, bool isTenant = true); - - public Task> GetListAsync(bool whereBool, Expression> where, bool isTenant = true); public Task> StoreAsync(string storeName, object para); - + public object CommonPage(QueryCondition queryCondition); } } diff --git a/Yi.Framework.Net6/Yi.Framework.Repository/Repository.cs b/Yi.Framework.Net6/Yi.Framework.Repository/Repository.cs index f3ceccf9..39f4c57f 100644 --- a/Yi.Framework.Net6/Yi.Framework.Repository/Repository.cs +++ b/Yi.Framework.Net6/Yi.Framework.Repository/Repository.cs @@ -1,7 +1,7 @@ using SqlSugar; using System.Data; using System.Linq.Expressions; -using static Yi.Framework.Repository.QueryParametersExtensions; +using Yi.Framework.Model.Query; /***这里面写的代码不会给覆盖,如果要重新生成请删除 Repository.cs ***/ namespace Yi.Framework.Repository @@ -10,7 +10,7 @@ namespace Yi.Framework.Repository /// 仓储模式 /// /// - public class Repository : DataContext ,IRepository where T : class, new() + public class Repository : DataContext, IRepository where T : class, new() { /// @@ -25,119 +25,34 @@ namespace Yi.Framework.Repository } } - + /// + /// 添加返回实体 + /// + /// + /// public async Task InsertReturnEntityAsync(T entity) { return await Db.Insertable(entity).ExecuteReturnEntityAsync(); } - /// - /// whereif与where混搭,多租户 - /// - /// - /// - /// - /// - public async Task> GetListAsync(Expression> whereExpression, bool whereBool, Expression> where, bool isTenant = true) - { - return await Db.Queryable().WhereIF(whereBool, where).Where(whereExpression).WhereTenant(isTenant).ToListAsync(); - } /// - /// where重载,多租户 + /// 调用存储过程 /// - /// - /// + /// + /// + /// /// - public async Task> GetListAsync(bool whereBool, Expression> where, bool isTenant = true) - { - return await Db.Queryable().WhereIF(whereBool, where).WhereTenant(isTenant). ToListAsync(); - } - - - - /// - /// 左连接,三表连接,返回最右边的列表,多租户 - /// - /// - /// - /// - /// - /// - /// - /// - public async Task> LeftJoinListAsync(Expression> joinQueryable1, Expression> joinQueryable12, Expression> whereLambda, Expression> selectLambda, bool isTenant = true) - { - return await Db.Queryable().LeftJoin(joinQueryable1) - .LeftJoin(joinQueryable12) - .Where(whereLambda) - .WhereTenant(isTenant) - .Select(selectLambda) - .ToListAsync(); - } - public async Task> StoreAsync(string storeName, object para) { return await Db.Ado.UseStoredProcedure().SqlQueryAsync(storeName, para); } - /// - /// 调用sql - /// - /// - /// - /// - public async Task SqlDataTableAsync(string sql, object para = null) - { - return await Db.Ado.GetDataTableAsync(sql, para); - } - - /// - /// 导航属性mapper返回一个,多租户 - /// - /// - /// - /// - /// - public async Task FirstMapperAsync(Expression> expression,bool isTenant=true) - { - return await Db.Queryable().Mapper(expression).WhereTenant(isTenant).FirstAsync(); - } - - /// - /// 导航属性mapper返回一组,多租户 - /// - /// - /// - /// - /// - public async Task> ToListMapperAsync(Expression> expression, bool isTenant = true) - { - return await Db.Queryable() .Mapper(expression).WhereTenant(isTenant).ToListAsync(); - } - - - - - /// - /// 导航属性mapper返回一组.同时添加条件,多租户 - /// - /// - /// - /// - /// - /// - public async Task> ToListMapperAsync(Expression> expression, bool whereBool, Expression> where, bool isTenant = true) - { - return await Db.Queryable().Mapper(expression).WhereIF(whereBool,where).WhereTenant(isTenant).ToListAsync(); - } - - /// /// 仓储扩展方法:单表查询通用分页 /// /// - public object CommonPage(QueryParameters pars, int pageIndex, int pageSize) + public object CommonPage(QueryCondition pars) { int tolCount = 0; 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 } } - var result = query.Where(sugarParamters).ToPageList(pageIndex, pageSize, ref tolCount); + var result = query.Where(sugarParamters).ToPageList(pars.Index, pars.Size, ref tolCount); return new { count = tolCount, data = result }; } - - - /// - /// 额外添加动态条件拼接 - /// - /// - public object CommonPage(QueryParameters pars, int pageIndex, int pageSize, bool whereBool, Expression> 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(); - 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 - }; - } - - - - /// - /// 导航属性mapper分页多条件 - /// - /// - /// - /// - /// - public object CommonPageMapper(Expression> expression, QueryParameters pars, int pageIndex, int pageSize,bool whereBool, Expression> 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(); - 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 - }; - } - } - /// - /// 通用查询参数 - /// - public class QueryParameters - { - public List Parameters { get; set; } = new List(); - public List OrderBys { get; set; } = new List(); - } - - public static class QueryParametersExtensions - { - - public static ISugarQueryable WhereTenant(this ISugarQueryable db, bool isTenant = true) - { - if (isTenant) - { - var sugarParamters = new QueryParameters().SetParameters(new Dictionary()).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 WhereTenant(this ISugarQueryable db, bool isTenant = true) - { - if (isTenant) - { - var sugarParamters = new QueryParameters().SetParameters(new Dictionary()).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 dic,bool IsTenant=true) - { - //var httpcontext = ServiceLocator.Instance.GetService().HttpContext; - queryParameters.OrderBys = new List { "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; - } - - /// - /// 通用查询参数 - /// - public class QueryParameter - { - public string FieldName { get; set; } - public string FieldValue { get; set; } - public ConditionalType ConditionalType { get; set; } = ConditionalType.Like; - - } - } + } \ No newline at end of file diff --git a/Yi.Framework.Net6/Yi.Framework.Repository/Yi.Framework.Repository.csproj b/Yi.Framework.Net6/Yi.Framework.Repository/Yi.Framework.Repository.csproj index 4ba9aaad..239ac7ef 100644 --- a/Yi.Framework.Net6/Yi.Framework.Repository/Yi.Framework.Repository.csproj +++ b/Yi.Framework.Net6/Yi.Framework.Repository/Yi.Framework.Repository.csproj @@ -6,6 +6,10 @@ disable + + + +