using JNPF.Common.Core.Manager; using JNPF.Common.Enums; using JNPF.Common.Extension; using JNPF.Common.Filter; using JNPF.Common.Security; using JNPF.DependencyInjection; using JNPF.DynamicApiController; using JNPF.FriendlyException; using JNPF.Systems.Entitys.Dto.Group; using JNPF.Systems.Entitys.Permission; using JNPF.Systems.Entitys.System; using JNPF.Systems.Interfaces.Permission; using Mapster; using Microsoft.AspNetCore.Mvc; using SqlSugar; namespace JNPF.Systems; /// /// 业务实现:分组管理 /// 版 本:V3.3.3 /// 版 权:拓通智联科技有限公司(http://www.tuotong-tech.com) /// 日 期:2022.03.11. /// [ApiDescriptionSettings(Tag = "Permission", Name = "Group", Order = 162)] [Route("api/Permission/[controller]")] public class GroupService : IUserGroupService, IDynamicApiController, ITransient { /// /// 基础仓储. /// private readonly ISqlSugarRepository _repository; /// /// 用户管理器. /// private readonly IUserManager _userManager; /// /// 初始化一个类型的新实例. /// public GroupService( ISqlSugarRepository repository, IUserManager userManager) { _repository = repository; _userManager = userManager; } #region GET /// /// 获取列表. /// /// 参数. /// [HttpGet("")] public async Task GetList([FromQuery] PageInputBase input) { SqlSugarPagedList? data = await _repository.AsSugarClient().Queryable( (a, b) => new JoinQueryInfos(JoinType.Left, a.Type == b.Id && b.DictionaryTypeId == "271905527003350725")) // 关键字(名称、编码) .WhereIF(input.keyword.IsNotEmptyOrNull(), a => a.FullName.Contains(input.keyword) || a.EnCode.Contains(input.keyword)) .Where(a => a.DeleteMark == null).OrderBy(a => a.SortCode).OrderBy(a => a.CreatorTime, OrderByType.Desc).OrderBy(a => a.LastModifyTime, OrderByType.Desc) .Select((a, b) => new GroupListOutput { id = a.Id, fullName = a.FullName, enCode = a.EnCode, type = b.FullName, enabledMark = a.EnabledMark, creatorTime = a.CreatorTime, description = a.Description, sortCode = a.SortCode }).ToPagedListAsync(input.currentPage, input.pageSize); return PageResult.SqlSugarPageResult(data); } /// /// 获取信息. /// /// 主键. /// [HttpGet("{id}")] public async Task GetInfo(string id) { GroupEntity? entity = await _repository.GetSingleAsync(p => p.Id == id); return entity.Adapt(); } /// /// 获取下拉框. /// /// [HttpGet("Selector")] public async Task GetSelector() { // 获取所有分组数据 List? groupList = await _repository.AsQueryable() .Where(t => t.EnabledMark == 1 && t.DeleteMark == null) .OrderBy(o => o.SortCode).OrderBy(a => a.CreatorTime, OrderByType.Desc).OrderBy(a => a.LastModifyTime, OrderByType.Desc).ToListAsync(); // 获取所有分组类型(字典) List? typeList = await _repository.AsSugarClient().Queryable() .Where(x => x.DictionaryTypeId == "271905527003350725" && x.DeleteMark == null && x.EnabledMark == 1).ToListAsync(); List? treeList = new List(); typeList.ForEach(item => { if (groupList.Count(x => x.Type == item.Id) > 0) { treeList.Add(new GroupSelectorOutput() { id = item.Id, parentId = "0", num = groupList.Count(x => x.Type == item.Id), fullName = item.FullName }); } }); groupList.ForEach(item => { treeList.Add( new GroupSelectorOutput { id = item.Id, parentId = item.Type, type = "group", icon = "icon-ym icon-ym-generator-group1", fullName = item.FullName, sortCode = item.SortCode }); }); return treeList.OrderBy(x => x.sortCode).ToList().ToTree("0"); } #endregion #region POST /// /// 新建. /// /// 参数. /// [HttpPost("")] public async Task Create([FromBody] GroupCrInput input) { if (await _repository.IsAnyAsync(p => p.FullName == input.fullName && p.DeleteMark == null)) throw Oops.Oh(ErrorCode.D2402); if (await _repository.IsAnyAsync(p => p.EnCode == input.enCode && p.DeleteMark == null)) throw Oops.Oh(ErrorCode.D2401); GroupEntity? entity = input.Adapt(); int isOk = await _repository.AsInsertable(entity).CallEntityMethod(m => m.Creator()).ExecuteCommandAsync(); if (!(isOk > 0)) throw Oops.Oh(ErrorCode.D2400); } /// /// 删除. /// /// 主键. /// [HttpDelete("{id}")] public async Task Delete(string id) { // 岗位下有用户不能删 if (await _repository.AsSugarClient().Queryable().AnyAsync(u => u.ObjectType == "Group" && u.ObjectId == id)) throw Oops.Oh(ErrorCode.D2406); GroupEntity? entity = await _repository.GetSingleAsync (p => p.Id == id && p.DeleteMark == null); int isOk = await _repository.AsUpdateable(entity).CallEntityMethod(m => m.Delete()).UpdateColumns(it => new { it.DeleteMark, it.DeleteTime, it.DeleteUserId }).ExecuteCommandAsync(); if (!(isOk > 0)) throw Oops.Oh(ErrorCode.D2403); } /// /// 更新. /// /// 主键. /// 参数. /// [HttpPut("{id}")] public async Task Update(string id, [FromBody] GroupUpInput input) { GroupEntity oldEntity = await _repository.GetSingleAsync(it => it.Id == id); if (await _repository.IsAnyAsync(p => p.FullName == input.fullName && p.DeleteMark == null && p.Id != id)) throw Oops.Oh(ErrorCode.D2402); if (await _repository.IsAnyAsync(p => p.EnCode == input.enCode && p.DeleteMark == null && p.Id != id)) throw Oops.Oh(ErrorCode.D2401); GroupEntity entity = input.Adapt(); int isOk = await _repository.AsUpdateable(entity).IgnoreColumns(ignoreAllNullColumns: true).CallEntityMethod(m => m.LastModify()).ExecuteCommandAsync(); if (!(isOk > 0)) throw Oops.Oh(ErrorCode.D2404); } /// /// 更新状态. /// /// 主键. /// [HttpPut("{id}/Actions/State")] public async Task UpdateState(string id) { if (!await _repository.IsAnyAsync(r => r.Id == id && r.DeleteMark == null)) throw Oops.Oh(ErrorCode.D2405); int isOk = await _repository.AsSugarClient().Updateable().UpdateColumns(it => new GroupEntity() { EnabledMark = SqlFunc.IIF(it.EnabledMark == 1, 0, 1), LastModifyUserId = _userManager.UserId, LastModifyTime = SqlFunc.GetDate() }).Where(it => it.Id == id).ExecuteCommandAsync(); if (!(isOk > 0)) throw Oops.Oh(ErrorCode.D6004); } #endregion }