简单基类控制器封装

This commit is contained in:
chenchun
2022-09-10 20:49:46 +08:00
parent b5ad7a1721
commit 0dca7acee6
11 changed files with 172 additions and 144 deletions

View File

@@ -102,6 +102,46 @@
<param name="ids"></param> <param name="ids"></param>
<returns></returns> <returns></returns>
</member> </member>
<member name="T:Yi.Framework.ApiMicroservice.Controllers.BaseSimpleCrudController`1">
<summary>
Json To Sql 类比模式,通用模型
</summary>
<typeparam name="T"></typeparam>
</member>
<member name="M:Yi.Framework.ApiMicroservice.Controllers.BaseSimpleCrudController`1.GetById(System.Int64)">
<summary>
主键查询
</summary>
<param name="id"></param>
<returns></returns>
</member>
<member name="M:Yi.Framework.ApiMicroservice.Controllers.BaseSimpleCrudController`1.GetList(Yi.Framework.Model.Query.QueryCondition)">
<summary>
全部列表查询
</summary>
<returns></returns>
</member>
<member name="M:Yi.Framework.ApiMicroservice.Controllers.BaseSimpleCrudController`1.Add(`0)">
<summary>
添加
</summary>
<param name="entity"></param>
<returns></returns>
</member>
<member name="M:Yi.Framework.ApiMicroservice.Controllers.BaseSimpleCrudController`1.Update(`0)">
<summary>
修改
</summary>
<param name="entity"></param>
<returns></returns>
</member>
<member name="M:Yi.Framework.ApiMicroservice.Controllers.BaseSimpleCrudController`1.DelList(System.Collections.Generic.List{System.Int64})">
<summary>
列表删除
</summary>
<param name="ids"></param>
<returns></returns>
</member>
<member name="M:Yi.Framework.ApiMicroservice.Controllers.DictionaryController.PageList(Yi.Framework.Model.Models.DictionaryEntity,Yi.Framework.Common.Models.PageParModel)"> <member name="M:Yi.Framework.ApiMicroservice.Controllers.DictionaryController.PageList(Yi.Framework.Model.Models.DictionaryEntity,Yi.Framework.Common.Models.PageParModel)">
<summary> <summary>
动态条件分页查询 动态条件分页查询

View File

@@ -1,5 +1,6 @@
using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Localization; using Microsoft.Extensions.Localization;
using Yi.Framework.Common.Helper;
using Yi.Framework.Common.Models; using Yi.Framework.Common.Models;
using Yi.Framework.Interface; using Yi.Framework.Interface;
using Yi.Framework.Language; using Yi.Framework.Language;
@@ -15,7 +16,7 @@ namespace Yi.Framework.ApiMicroservice.Controllers
/// </summary> /// </summary>
/// <typeparam name="T"></typeparam> /// <typeparam name="T"></typeparam>
[ApiController] [ApiController]
public class BaseCrudController<T> : ControllerBase where T : class, IBaseModelEntity,new() public class BaseCrudController<T> : ControllerBase where T : class,new()
{ {
private readonly ILogger<T> _logger; private readonly ILogger<T> _logger;
private IBaseService<T> _baseService; private IBaseService<T> _baseService;
@@ -95,7 +96,7 @@ namespace Yi.Framework.ApiMicroservice.Controllers
[HttpDelete] [HttpDelete]
public virtual async Task<Result> DeleteList(List<long> ids) public virtual async Task<Result> DeleteList(List<long> ids)
{ {
return Result.Success().SetStatus(await _repository.DeleteByLogicAsync(ids)); return Result.Success().SetStatus(await _repository.DeleteByIdAsync(ids.ToDynamicArray()));
} }
} }
} }

View File

@@ -0,0 +1,91 @@
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Localization;
using Yi.Framework.Common.Helper;
using Yi.Framework.Common.Models;
using Yi.Framework.Interface;
using Yi.Framework.Language;
using Yi.Framework.Model.Models;
using Yi.Framework.Model.Query;
using Yi.Framework.Repository;
using Yi.Framework.WebCore.AttributeExtend;
namespace Yi.Framework.ApiMicroservice.Controllers
{
/// <summary>
/// Json To Sql 类比模式,通用模型
/// </summary>
/// <typeparam name="T"></typeparam>
[ApiController]
public class BaseSimpleCrudController<T> : ControllerBase where T : class, new()
{
private readonly ILogger<T> _logger;
private IBaseService<T> _baseService;
private IRepository<T> _repository;
public BaseSimpleCrudController(ILogger<T> logger, IBaseService<T> iBaseService)
{
_logger = logger;
_baseService = iBaseService;
_repository = iBaseService._repository;
}
/// <summary>
/// 主键查询
/// </summary>
/// <param name="id"></param>
/// <returns></returns>
[Permission($"{nameof(T)}:get")]
[HttpGet]
public virtual async Task<Result> GetById(long id)
{
return Result.Success().SetData(await _repository.GetByIdAsync(id));
}
/// <summary>
/// 全部列表查询
/// </summary>
/// <returns></returns>
[Permission($"{nameof(T)}:get")]
[HttpPost]
public virtual async Task<Result> GetList()
{
return Result.Success().SetData(await _repository.GetListAsync());
}
/// <summary>
/// 添加
/// </summary>
/// <param name="entity"></param>
/// <returns></returns>
[Permission($"{nameof(T)}:add")]
[HttpPost]
public virtual async Task<Result> Add(T entity)
{
return Result.Success().SetData(await _repository.InsertReturnSnowflakeIdAsync(entity));
}
/// <summary>
/// 修改
/// </summary>
/// <param name="entity"></param>
/// <returns></returns>
[Permission($"{nameof(T)}:update")]
[HttpPut]
public virtual async Task<Result> Update(T entity)
{
return Result.Success().SetStatus(await _repository.UpdateIgnoreNullAsync(entity));
}
/// <summary>
/// 列表删除
/// </summary>
/// <param name="ids"></param>
/// <returns></returns>
[Permission($"{nameof(T)}:del")]
[HttpDelete]
public virtual async Task<Result> DelList(List<long> ids)
{
return Result.Success().SetStatus(await _repository.DeleteByIdsAsync(ids.ToDynamicArray()));
}
}
}

View File

@@ -19,14 +19,12 @@ namespace Yi.Framework.ApiMicroservice.Controllers
{ {
[ApiController] [ApiController]
[Route("api/[controller]/[action]")] [Route("api/[controller]/[action]")]
public class DictionaryController public class DictionaryController :BaseSimpleCrudController<DictionaryEntity>
{ {
private IDictionaryService _iDictionaryService; private IDictionaryService _iDictionaryService;
private IDictionaryInfoService _iDictionaryInfoService; public DictionaryController(ILogger<DictionaryEntity> logger, IDictionaryService iDictionaryService):base(logger, iDictionaryService)
public DictionaryController(ILogger<DictionaryEntity> logger, IDictionaryService iDictionaryService, IDictionaryInfoService iDictionaryInfoService)
{ {
_iDictionaryService = iDictionaryService; _iDictionaryService = iDictionaryService;
_iDictionaryInfoService = iDictionaryInfoService;
} }
/// <summary> /// <summary>
@@ -40,63 +38,5 @@ namespace Yi.Framework.ApiMicroservice.Controllers
{ {
return Result.Success().SetData(await _iDictionaryService.SelctPageList(dic, page)); return Result.Success().SetData(await _iDictionaryService.SelctPageList(dic, page));
} }
/// <summary>
/// 添加字典表
/// </summary>
/// <param name="dic"></param>
/// <returns></returns>
[HttpPost]
public async Task<Result> Add(DictionaryEntity dic)
{
return Result.Success().SetData(await _iDictionaryService._repository.InsertReturnSnowflakeIdAsync(dic));
}
/// <summary>
/// 根据字典id获取字典表
/// </summary>
/// <param name="id"></param>
/// <returns></returns>
[HttpGet]
[Route("{id}")]
public async Task<Result> GetById(long id)
{
return Result.Success().SetData(await _iDictionaryService._repository.GetByIdAsync(id));
}
/// <summary>
/// 获取全部字典表
/// </summary>
/// <returns></returns>
[HttpGet]
public async Task<Result> GetList()
{
return Result.Success().SetData(await _iDictionaryService._repository.GetListAsync());
}
/// <summary>
/// id范围删除
/// </summary>
/// <param name="ids"></param>
/// <returns></returns>
[HttpDelete]
public async Task<Result> DelList(List<long> ids)
{
return Result.Success().SetStatus(await _iDictionaryService._repository.DeleteByIdsAsync(ids.ToDynamicArray()));
}
/// <summary>
/// 更新
/// </summary>
/// <param name="dic"></param>
/// <returns></returns>
[HttpPut]
public async Task<Result> Update(DictionaryEntity dic)
{
return Result.Success().SetStatus(await _iDictionaryService._repository.UpdateIgnoreNullAsync(dic));
}
} }
} }

View File

@@ -19,13 +19,11 @@ namespace Yi.Framework.ApiMicroservice.Controllers
{ {
[ApiController] [ApiController]
[Route("api/[controller]/[action]")] [Route("api/[controller]/[action]")]
public class DictionaryInfoController public class DictionaryInfoController:BaseSimpleCrudController<DictionaryInfoEntity>
{ {
private IDictionaryService _iDictionaryService;
private IDictionaryInfoService _iDictionaryInfoService; private IDictionaryInfoService _iDictionaryInfoService;
public DictionaryInfoController(ILogger<DictionaryEntity> logger, IDictionaryService iDictionaryService, IDictionaryInfoService iDictionaryInfoService) public DictionaryInfoController(ILogger<DictionaryInfoEntity> logger, IDictionaryInfoService iDictionaryInfoService):base(logger, iDictionaryInfoService)
{ {
_iDictionaryService = iDictionaryService;
_iDictionaryInfoService = iDictionaryInfoService; _iDictionaryInfoService = iDictionaryInfoService;
} }
@@ -41,17 +39,6 @@ namespace Yi.Framework.ApiMicroservice.Controllers
return Result.Success().SetData(await _iDictionaryInfoService.SelctPageList(dicInfo, page)); return Result.Success().SetData(await _iDictionaryInfoService.SelctPageList(dicInfo, page));
} }
/// <summary>
/// 添加字典信息表
/// </summary>
/// <param name="dicInfo"></param>
/// <returns></returns>
[HttpPost]
public async Task<Result> Add(DictionaryInfoEntity dicInfo)
{
return Result.Success().SetData(await _iDictionaryInfoService._repository.InsertReturnSnowflakeIdAsync(dicInfo));
}
/// <summary> /// <summary>
/// 根据字典类别获取字典信息 /// 根据字典类别获取字典信息
/// </summary> /// </summary>
@@ -63,42 +50,5 @@ namespace Yi.Framework.ApiMicroservice.Controllers
{ {
return Result.Success().SetData(await _iDictionaryInfoService._repository.GetListAsync(u=>u.DictType==type&&u.IsDeleted==false)); return Result.Success().SetData(await _iDictionaryInfoService._repository.GetListAsync(u=>u.DictType==type&&u.IsDeleted==false));
} }
/// <summary>
/// id范围删除
/// </summary>
/// <param name="ids"></param>
/// <returns></returns>
[HttpDelete]
public async Task<Result> DelList(List<long> ids)
{
return Result.Success().SetStatus(await _iDictionaryInfoService._repository.DeleteByIdsAsync(ids.ToDynamicArray()));
}
/// <summary>
/// 更新
/// </summary>
/// <param name="dicInfo"></param>
/// <returns></returns>
[HttpPut]
public async Task<Result> Update(DictionaryInfoEntity dicInfo)
{
return Result.Success().SetStatus(await _iDictionaryInfoService._repository.UpdateIgnoreNullAsync(dicInfo));
}
/// <summary>
/// 根据字典id获取字典信息表
/// </summary>
/// <param name="id"></param>
/// <returns></returns>
[HttpGet]
[Route("{id}")]
public async Task<Result> GetById(long id)
{
return Result.Success().SetData(await _iDictionaryInfoService._repository.GetByIdAsync(id));
}
} }
} }

View File

@@ -8,7 +8,7 @@ using Yi.Framework.Repository;
namespace Yi.Framework.Interface namespace Yi.Framework.Interface
{ {
public interface IBaseService<T> where T: class, IBaseModelEntity,new() public interface IBaseService<T> where T: class,new()
{ {
public IRepository<T> _repository { get; set; } public IRepository<T> _repository { get; set; }
} }

View File

@@ -6,7 +6,7 @@
<ItemGroup> <ItemGroup>
<PackageReference Include="NEST" Version="7.16.0" /> <PackageReference Include="NEST" Version="7.16.0" />
<PackageReference Include="SqlSugarCore" Version="5.0.7.8" /> <PackageReference Include="SqlSugarCore" Version="5.1.2.7" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>

View File

@@ -11,16 +11,16 @@ using Yi.Framework.Model.Query;
namespace Yi.Framework.Repository namespace Yi.Framework.Repository
{ {
public interface IRepository<T> : ISimpleClient<T> where T : class, IBaseModelEntity, new() public interface IRepository<T> : ISimpleClient<T> where T : class, new()
{ {
public ISugarQueryable<T> _DbQueryable { get; set; } public ISugarQueryable<T> _DbQueryable { get; set; }
public ISqlSugarClient _Db { get; set; } public ISqlSugarClient _Db { get; set; }
public Task<bool> UseTranAsync(Func<Task> func); public Task<bool> UseTranAsync(Func<Task> func);
public Task<T> InsertReturnEntityAsync(T entity); //public Task<T> InsertReturnEntityAsync(T entity);
public Task<List<S>> StoreAsync<S>(string storeName, object para); public Task<List<S>> StoreAsync<S>(string storeName, object para);
public Task<PageModel<List<T>>> CommonPageAsync(QueryPageCondition pars); public Task<PageModel<List<T>>> CommonPageAsync(QueryPageCondition pars);
public Task<List<T>> GetListAsync(QueryCondition pars); public Task<List<T>> GetListAsync(QueryCondition pars);
public Task<bool> DeleteByLogicAsync(List<long> ids); //public Task<bool> DeleteByLogicAsync(List<long> ids);
public Task<bool> UpdateIgnoreNullAsync(T entity); public Task<bool> UpdateIgnoreNullAsync(T entity);
public Task<List<S>> UseSqlAsync<S>(string sql, object parameters = null); public Task<List<S>> UseSqlAsync<S>(string sql, object parameters = null);
public Task<bool> UseSqlAsync(string sql, object parameters = null); public Task<bool> UseSqlAsync(string sql, object parameters = null);

View File

@@ -12,7 +12,7 @@ namespace Yi.Framework.Repository
/// 仓储模式 /// 仓储模式
/// </summary> /// </summary>
/// <typeparam name="T"></typeparam> /// <typeparam name="T"></typeparam>
public class Repository<T> : SimpleClient<T>, IRepository<T> where T : class, IBaseModelEntity, new() public class Repository<T> : SimpleClient<T>, IRepository<T> where T : class, new()
{ {
public ISugarQueryable<T> _DbQueryable { get { return base.Context.Queryable<T>(); } set { } } public ISugarQueryable<T> _DbQueryable { get { return base.Context.Queryable<T>(); } set { } }
@@ -59,16 +59,16 @@ namespace Yi.Framework.Repository
return await _Db.Ado.ExecuteCommandAsync(sql, parameters) >0; return await _Db.Ado.ExecuteCommandAsync(sql, parameters) >0;
} }
/// <summary> ///// <summary>
/// 添加返回实体 ///// 添加返回实体
/// </summary> ///// </summary>
/// <param name="entity"></param> ///// <param name="entity"></param>
/// <returns></returns> ///// <returns></returns>
public async Task<T> InsertReturnEntityAsync(T entity) //public async Task<T> InsertReturnEntityAsync(T entity)
{ //{
entity.Id =SnowFlakeSingle.instance.getID(); // entity.Id =SnowFlakeSingle.instance.getID();
return await _Db.Insertable(entity).ExecuteReturnEntityAsync(); // return await _Db.Insertable(entity).ExecuteReturnEntityAsync();
} //}
/// <summary> /// <summary>
/// 更新忽略空值 /// 更新忽略空值
@@ -81,16 +81,16 @@ namespace Yi.Framework.Repository
} }
/// <summary> ///// <summary>
/// 逻辑多删除 ///// 逻辑多删除
/// </summary> ///// </summary>
/// <returns></returns> ///// <returns></returns>
public async Task<bool> DeleteByLogicAsync(List<long> ids) //public async Task<bool> DeleteByLogicAsync(List<long> ids)
{ //{
var entitys = await _Db.Queryable<T>().Where(u => ids.Contains(u.Id)).ToListAsync(); // var entitys = await _Db.Queryable<T>().Where(u => ids.Contains(u.Id)).ToListAsync();
entitys.ForEach(u=>u.IsDeleted=true); // entitys.ForEach(u=>u.IsDeleted=true);
return await _Db.Updateable(entitys).ExecuteCommandAsync()>0; // return await _Db.Updateable(entitys).ExecuteCommandAsync()>0;
} //}
/// <summary> /// <summary>

View File

@@ -9,7 +9,7 @@ using Yi.Framework.Repository;
namespace Yi.Framework.Service namespace Yi.Framework.Service
{ {
public class BaseService<T>:IBaseService<T> where T:class, IBaseModelEntity,new() public class BaseService<T>:IBaseService<T> where T:class,new()
{ {
public IRepository<T> _repository { get; set; } public IRepository<T> _repository { get; set; }
public BaseService(IRepository<T> iRepository) public BaseService(IRepository<T> iRepository)

View File

@@ -64,7 +64,13 @@ namespace Yi.Framework.Service
{ {
user.UserName = userEntity.UserName; user.UserName = userEntity.UserName;
user.BuildPassword(); user.BuildPassword();
userAction.Invoke(await _repository.InsertReturnEntityAsync(user)); //等待老杰哥更新杰哥漏了雪花id呀嘿嘿
//var entity = await _repository.InsertReturnSnowflakeEntityAsync(user);
//userAction.Invoke(await _repository.GetByIdAsync(entity));
//await _repository.InsertReturnSnowflakeEntityAsync(user);
//userAction.Invoke(await _repository.GetByIdAsync(user));
return true; return true;
} }
return false; return false;