using JNPF.Apps.Entitys; using JNPF.Apps.Entitys.Dto; using JNPF.Apps.Interfaces; 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.Permission; using JNPF.Systems.Entitys.System; using JNPF.WorkFlow.Entitys.Dto.FlowEngine; using JNPF.WorkFlow.Entitys.Entity; using JNPF.WorkFlow.Interfaces.Service; using Mapster; using Microsoft.AspNetCore.Mvc; using SqlSugar; namespace JNPF.Apps; /// /// App常用数据 /// 版 本:V3.2 /// 版 权:拓通智联科技有限公司(http://www.tuotong-tech.com) /// 日 期:2021-06-01 . /// [ApiDescriptionSettings(Tag = "App", Name = "Data", Order = 800)] [Route("api/App/[controller]")] public class AppDataService : IAppDataService, IDynamicApiController, ITransient { /// /// 服务基础仓储. /// private readonly ISqlSugarRepository _repository; // App常用数据 /// /// 用户管理. /// private readonly IUserManager _userManager; /// /// 流程管理. /// private readonly IFlowTemplateService _flowTemplateService; /// /// 构造. /// /// /// /// public AppDataService( ISqlSugarRepository repository, IUserManager userManager, IFlowTemplateService flowTemplateService) { _repository = repository; _userManager = userManager; _flowTemplateService = flowTemplateService; } #region Get /// /// 常用数据. /// /// /// [HttpGet("")] public async Task GetList([FromQuery] string type) { List? list = await GetListByType(type); List? output = list.Adapt>(); if (type.Equals("1")) { foreach (var item in output) { var flowJson = _repository.AsSugarClient().Queryable().First(x => x.TemplateId == item.objectId && x.EnabledMark == 1 && x.DeleteMark == null); if (flowJson != null) { item.objectId = flowJson.Id; } } } return new { list = output }; } /// /// 所有流程. /// /// [HttpGet("getFlowList")] public async Task GetFlowList([FromQuery] FlowEngineListInput input) { var list = await _repository.AsSugarClient().Queryable() .Where(a => a.DeleteMark == null && a.EnabledMark == 1 && a.Type == 0) .WhereIF(input.category.IsNotEmptyOrNull(), a => a.Category == input.category) .WhereIF(input.keyword.IsNotEmptyOrNull(), a => a.FullName.Contains(input.keyword) || a.EnCode.Contains(input.keyword)) .OrderBy(a => a.SortCode).OrderBy(a => a.CreatorTime, OrderByType.Desc) .OrderBy(a => a.LastModifyTime, OrderByType.Desc) .Select(a => new AppFlowListAllOutput { id = a.Id, icon = a.Icon, enCode = a.EnCode, fullName = a.FullName, iconBackground = a.IconBackground, isData = SqlFunc.Subqueryable().Where(x => x.ObjectType == "1" && x.CreatorUserId == _userManager.UserId && x.ObjectId == a.Id && x.DeleteMark == null).Any(), }).ToPagedListAsync(input.currentPage, input.pageSize); return PageResult.SqlSugarPageResult(list); } /// /// 所有流程. /// /// [HttpGet("getDataList")] public async Task GetDataList(string keyword) { List? list = (await GetAppMenuList(keyword)).Adapt>(); foreach (AppDataListAllOutput? item in list) { item.isData = _repository.IsAny(x => x.ObjectType == "2" && x.CreatorUserId == _userManager.UserId && x.ObjectId == item.id && x.DeleteMark == null); } List? output = list.ToTree("-1"); return new { list = output }; } #endregion #region Post /// /// 新增. /// /// /// [HttpPost("")] public async Task Create([FromBody] AppDataCrInput input) { AppDataEntity? entity = input.Adapt(); int isOk = await _repository.AsInsertable(entity).IgnoreColumns(ignoreNullColumn: true).CallEntityMethod(m => m.Creator()).ExecuteCommandAsync(); if (isOk < 1) throw Oops.Oh(ErrorCode.COM1000); } /// /// 删除. /// /// /// [HttpDelete("{objectId}")] public async Task Delete(string objectId) { AppDataEntity? entity = await _repository.GetSingleAsync(x => x.ObjectId == objectId && x.CreatorUserId == _userManager.UserId && x.DeleteMark == null); 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 #region PrivateMethod /// /// 列表. /// /// /// private async Task> GetListByType(string type) { return await _repository.AsQueryable().Where(x => x.ObjectType == type && x.CreatorUserId == _userManager.UserId && x.DeleteMark == null).ToListAsync(); } /// /// 菜单列表. /// /// [NonAction] public async Task> GetAppMenuList(string keyword) { List? menuList = new List(); if (_userManager.IsAdministrator) { menuList = await _repository.AsSugarClient().Queryable() .Where(x => x.EnabledMark == 1 && x.Category == "App" && x.DeleteMark == null && x.SystemId == _userManager.User.SystemId) .WhereIF(!string.IsNullOrEmpty(keyword), x => x.FullName.Contains(keyword) || x.ParentId == "-1") .OrderBy(o => o.SortCode) .ToListAsync(); } else { string[]? objectIds = _userManager.Roles.ToArray(); if (objectIds.Length == 0) return menuList; List? ids = await _repository.AsSugarClient().Queryable() .Where(x => objectIds.Contains(x.ObjectId) && x.ObjectType == "Role" && x.ItemType == "module").Select(x => x.ItemId).ToListAsync(); if (ids.Count == 0) return menuList; menuList = await _repository.AsSugarClient().Queryable() .Where(x => ids.Contains(x.Id) && x.EnabledMark == 1 && x.Category == "App" && x.DeleteMark == null && x.SystemId == _userManager.User.SystemId) .WhereIF(!string.IsNullOrEmpty(keyword), x => x.FullName.Contains(keyword) || x.ParentId == "-1") .OrderBy(o => o.SortCode) .ToListAsync(); } return menuList; } #endregion }