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.ModuleDataAuthorize; using JNPF.Systems.Entitys.Dto.ModuleDataAuthorizeScheme; using JNPF.Systems.Entitys.System; using JNPF.Systems.Interfaces.System; using JNPF.VisualDev.Engine; using JNPF.VisualDev.Engine.Core; using JNPF.VisualDev.Entitys; using Mapster; using Microsoft.AspNetCore.Mvc; using Newtonsoft.Json.Linq; using SqlSugar; namespace JNPF.Systems; /// /// 数据权限 /// 版 本:V3.2 /// 版 权:拓通智联科技有限公司(http://www.tuotong-tech.com) /// 日 期:2021-06-01. /// [ApiDescriptionSettings(Tag = "System", Name = "ModuleDataAuthorize", Order = 214)] [Route("api/system/[controller]")] public class ModuleDataAuthorizeService : IModuleDataAuthorizeService, IDynamicApiController, ITransient { /// /// 服务基础仓储. /// private readonly ISqlSugarRepository _repository; /// /// 用户管理器. /// private readonly IUserManager _userManager; /// /// 初始化一个类型的新实例. /// public ModuleDataAuthorizeService( ISqlSugarRepository repository, IUserManager userManager) { _repository = repository; _userManager = userManager; } #region GET /// /// 列表. /// /// 功能主键. /// 参数. /// [HttpGet("{moduleId}/List")] public async Task GetList(string moduleId, [FromQuery] KeywordInput input) { var list = await _repository.AsSugarClient().Queryable((a, b) => new JoinQueryInfos(JoinType.Left, a.ModuleId == b.Id)) .Where((a, b) => a.ModuleId == moduleId && a.DeleteMark == null && b.DeleteMark == null) .WhereIF(input.keyword.IsNotEmptyOrNull(), a => a.EnCode.Contains(input.keyword) || a.FullName.Contains(input.keyword)) .OrderBy(a => a.SortCode).OrderBy(a => a.CreatorTime, OrderByType.Desc).OrderBy(a => a.LastModifyTime, OrderByType.Desc) .Select((a, b) => new ModuleDataAuthorizeListOutput() { id = a.Id, fullName = a.FullName, type = a.Type, conditionSymbol = a.ConditionSymbol, conditionText = a.ConditionText, conditionSymbolName = SqlFunc.ToString(a.ConditionSymbol).Replace("Equal", "等于").Replace("Included", "包含").Replace("GreaterThan", "大于").Replace("LessThan", "小于").Replace("Not", "不").Replace("Or", ""), bindTable = a.BindTable, fieldRule = a.FieldRule, enCode = SqlFunc.IF(b.Type == 2 && a.FieldRule == 1 && !SqlFunc.IsNullOrEmpty(a.BindTable)).Return(a.EnCode.Replace("jnpf_" + a.BindTable + "_jnpf_", "")) .ElseIF(b.Type == 3 && a.FieldRule == 1).Return(a.EnCode.Replace(a.BindTable + ".", "")) .ElseIF(a.FieldRule == 2).Return(a.EnCode.Replace(a.ChildTableKey + "-", "")).End(a.EnCode), }).ToListAsync(); return new { list = list }; } /// /// 信息. /// /// 主键值. /// [HttpGet("{id}")] public async Task GetInfo_Api(string id) { var data = await _repository.GetFirstAsync(x => x.Id == id && x.DeleteMark == null); if (data.FieldRule == 2 && data.ChildTableKey.IsNotEmptyOrNull()) data.EnCode = data.EnCode.Replace(data.ChildTableKey + "-", string.Empty); var menu = await _repository.AsSugarClient().Queryable().FirstAsync(x => x.Id == data.ModuleId && x.DeleteMark == null); if (menu.IsNotEmptyOrNull() && data.BindTable.IsNotEmptyOrNull() && data.FieldRule == 1) { // 代码生成 if (menu.Type == 2) { data.EnCode = data.EnCode.Replace("jnpf_" + data.BindTable + "_jnpf_", string.Empty); } // 在线开发 if (menu.Type == 3) { data.EnCode = data.EnCode.Replace(data.BindTable + ".", string.Empty); } } return data.Adapt(); } /// /// 字段列表. /// /// 菜单id. /// [HttpGet("{moduleId}/FieldList")] public async Task GetFieldList(string moduleId) { var moduleEntity = await _repository.AsSugarClient().Queryable().FirstAsync(x => x.Id == moduleId && x.DeleteMark == null); var visualDevId = moduleEntity.PropertyJson.ToObject()["moduleId"].ToString(); var visualDevEntity = await _repository.AsSugarClient().Queryable().FirstAsync(x => x.Id == visualDevId && x.DeleteMark == null); var tInfo = new TemplateParsingBase(visualDevEntity); return tInfo.SingleFormData.Where(x => x.__vModel__.IsNotEmptyOrNull()).Select(x => new { field = x.__vModel__, fieldName = x.__config__.label }); } #endregion #region POST /// /// 新建. /// /// 实体对象. /// [HttpPost("")] public async Task Create([FromBody] ModuleDataAuthorizeCrInput input) { var entity = input.Adapt(); if (entity.FieldRule == 2 && input.childTableKey.IsNotEmptyOrNull()) entity.EnCode = input.childTableKey + "-" + entity.EnCode; var menu = await _repository.AsSugarClient().Queryable().FirstAsync(x => x.Id == input.moduleId && x.DeleteMark == null); if (menu.IsNotEmptyOrNull() && entity.BindTable.IsNotEmptyOrNull() && entity.FieldRule == 1) { // 代码生成 if (menu.Type == 2) { entity.EnCode = "jnpf_" + input.bindTable + "_jnpf_" + entity.EnCode; } // 在线开发 if (menu.Type == 3) { entity.EnCode = input.bindTable + "." + input.enCode; } } 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] ModuleDataAuthorizeUpInput input) { var entity = input.Adapt(); if (entity.FieldRule == 2 && input.childTableKey.IsNotEmptyOrNull()) entity.EnCode = input.childTableKey + "-" + entity.EnCode; var menu = await _repository.AsSugarClient().Queryable().FirstAsync(x => x.Id == input.moduleId && x.DeleteMark == null); if (menu.IsNotEmptyOrNull() && entity.BindTable.IsNotEmptyOrNull() && entity.FieldRule == 1) { // 代码生成 if (menu.Type == 2) { entity.EnCode = "jnpf_" + input.bindTable + "_jnpf_" + entity.EnCode; } // 在线开发 if (menu.Type == 3) { entity.EnCode = input.bindTable + "." + input.enCode; } } 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) { if (!await _repository.IsAnyAsync(x => x.Id == id && x.DeleteMark == null)) throw Oops.Oh(ErrorCode.COM1005); await AnyScheme(id); var isOk = await _repository.AsUpdateable().SetColumns(it => new ModuleDataAuthorizeEntity() { DeleteMark = 1, DeleteUserId = _userManager.UserId, DeleteTime = SqlFunc.GetDate() }).Where(it => it.Id == id).ExecuteCommandHasChangeAsync(); if (!isOk) throw Oops.Oh(ErrorCode.COM1002); } #endregion #region PublicMethod /// /// 列表. /// /// 功能主键. /// [NonAction] public async Task> GetList(string? moduleId = default) { return await _repository.AsQueryable().Where(x => x.DeleteMark == null).WhereIF(!moduleId.IsNullOrEmpty(), it => it.ModuleId == moduleId).OrderBy(o => o.SortCode).ToListAsync(); } /// /// 方案中是否存在字段. /// /// /// [NonAction] public async Task AnyScheme(string id) { var moduleDataAuthorizeEntity = await _repository.GetFirstAsync(x => x.Id == id && x.DeleteMark == null); var schemeEntityList = await _repository.AsSugarClient().Queryable().Where(x => x.ModuleId == moduleDataAuthorizeEntity.ModuleId && x.DeleteMark == null).ToListAsync(); var ids = new List(); foreach (var item in schemeEntityList) { if (item.ConditionJson.IsNotEmptyOrNull() && !item.ConditionJson.Equals("[]")) { var conditionJson = item.ConditionJson.ToObject>(); if (conditionJson.Any(x => x.groups.Select(x => x.id).Contains(id))) throw Oops.Oh(ErrorCode.D4010, item.FullName); } } } #endregion }