using JNPF.Common.Core.Manager; using JNPF.Common.Enums; using JNPF.Common.Extension; using JNPF.Common.Filter; using JNPF.DependencyInjection; using JNPF.DynamicApiController; using JNPF.FriendlyException; using JNPF.LinqBuilder; using JNPF.Systems.Entitys.Dto.System.System; using JNPF.Systems.Entitys.Permission; using JNPF.Systems.Entitys.System; using Mapster; using Microsoft.AspNetCore.Mvc; using SqlSugar; namespace JNPF.Systems.System; /// /// 系统功能. /// [ApiDescriptionSettings(Tag = "System", Name = "System", Order = 200)] [Route("api/system/[controller]")] public class SystemService : IDynamicApiController, ITransient { /// /// 系统功能表仓储. /// private readonly ISqlSugarRepository _repository; /// /// 用户管理. /// private readonly IUserManager _userManager; /// /// 初始化一个类型的新实例. /// public SystemService( ISqlSugarRepository repository, IUserManager userManager) { _repository = repository; _userManager = userManager; } #region Get /// /// 列表. /// /// 参数. /// [HttpGet("")] public async Task GetList([FromQuery] KeywordInput input) { var authorIds = await _repository.AsSugarClient().Queryable() .Where(x => x.ItemType.Equals("system") && x.ObjectType.Equals("Role") && _userManager.Roles.Contains(x.ObjectId)).Select(x => x.ItemId).ToListAsync(); var whereLambda = LinqExpression.And(); whereLambda = whereLambda.And(x => x.DeleteMark == null); if (!_userManager.IsAdministrator) whereLambda = whereLambda.And(x => authorIds.Contains(x.Id)); if (input.keyword.IsNotEmptyOrNull()) whereLambda = whereLambda.And(x => x.FullName.Contains(input.keyword) || x.EnCode.Contains(input.keyword)); var output = (await _repository.AsQueryable().Where(whereLambda).OrderBy(a => a.SortCode).OrderByDescending(a => a.CreatorTime).ToListAsync()).Adapt>(); return new { list = output }; } /// /// 获取信息. /// /// 主键id. /// [HttpGet("{id}")] public async Task GetInfo(string id) { var data = await _repository.GetFirstAsync(x => x.Id == id && x.DeleteMark == null); return data.Adapt(); } #endregion #region Post /// /// 新建. /// /// 实体对象. /// [HttpPost("")] public async Task Create([FromBody] SystemCrInput input) { if (await _repository.IsAnyAsync(x => (x.EnCode == input.enCode || x.FullName == input.fullName) && x.DeleteMark == null)) throw Oops.Oh(ErrorCode.COM1004); var entity = input.Adapt(); var isOk = await _repository.AsInsertable(entity).IgnoreColumns(ignoreNullColumn: true).CallEntityMethod(m => m.Creator()).ExecuteCommandAsync(); if (isOk < 1) throw Oops.Oh(ErrorCode.COM1000); } /// /// 修改. /// /// 主键值. /// 实体对象. /// [HttpPut("{id}")] public async Task Update(string id, [FromBody] SystemCrInput input) { if (await _repository.IsAnyAsync(x => x.Id != id && (x.EnCode == input.enCode || x.FullName == input.fullName) && x.DeleteMark == null)) throw Oops.Oh(ErrorCode.COM1004); var entity = input.Adapt(); var isOk = await _repository.AsUpdateable(entity).IgnoreColumns(ignoreAllNullColumns: true).CallEntityMethod(m => m.LastModify()).ExecuteCommandHasChangeAsync(); if (!isOk) throw Oops.Oh(ErrorCode.COM1001); } /// /// 删除. /// /// 主键. /// [HttpDelete("{id}")] public async Task Delete(string id) { var entity = await _repository.GetFirstAsync(x => x.Id == id && x.DeleteMark == null); if (entity == null) throw Oops.Oh(ErrorCode.COM1005); var isOk = await _repository.AsUpdateable(entity).CallEntityMethod(m => m.Delete()).UpdateColumns(it => new { it.DeleteMark, it.DeleteTime, it.DeleteUserId }).ExecuteCommandHasChangeAsync(); if (!isOk) throw Oops.Oh(ErrorCode.COM1002); } #endregion }