From aaa5eb7108d8cda2a9c5a5d9f890cfd9cc7cd2ae Mon Sep 17 00:00:00 2001 From: cjy Date: Wed, 16 Oct 2024 19:32:08 +0800 Subject: [PATCH] =?UTF-8?q?=E5=AE=A2=E6=88=B7=E6=95=B0=E6=8D=AE=E6=9D=83?= =?UTF-8?q?=E9=99=90=E4=BF=AE=E6=94=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Info/Dtos/ControllerClientRes.cs | 5 + .../DS.WMS.Core/Info/Entity/InfoClient.cs | 6 + .../Sys/Interface/ICommonService.cs | 8 + .../Sys/Method/ClientCommonService.cs | 26 ++- .../DS.WMS.Core/Sys/Method/CommonService.cs | 148 +++++++++++++++++- 5 files changed, 191 insertions(+), 2 deletions(-) diff --git a/ds-wms-service/DS.WMS.Core/Info/Dtos/ControllerClientRes.cs b/ds-wms-service/DS.WMS.Core/Info/Dtos/ControllerClientRes.cs index 09d97e4b..6ebade30 100644 --- a/ds-wms-service/DS.WMS.Core/Info/Dtos/ControllerClientRes.cs +++ b/ds-wms-service/DS.WMS.Core/Info/Dtos/ControllerClientRes.cs @@ -132,5 +132,10 @@ namespace DS.WMS.Core.Info.Dtos /// 服务项目 /// public string? ServiceItem { get; set; } + + /// + /// 干系人列表 + /// + public List UserIds { get; set; } } } diff --git a/ds-wms-service/DS.WMS.Core/Info/Entity/InfoClient.cs b/ds-wms-service/DS.WMS.Core/Info/Entity/InfoClient.cs index 992b510e..63616ea3 100644 --- a/ds-wms-service/DS.WMS.Core/Info/Entity/InfoClient.cs +++ b/ds-wms-service/DS.WMS.Core/Info/Entity/InfoClient.cs @@ -459,4 +459,10 @@ public class InfoClient : SharedOrgModel /// [Navigate(NavigateType.OneToMany, nameof(InfoClientContact.ClientId))] public List? Contacts { get; set; } + + /// + /// 干系人列表 + /// + [SqlSugar.SugarColumn(IsIgnore = true)] + public List UserIds { get; set; } } \ No newline at end of file diff --git a/ds-wms-service/DS.WMS.Core/Sys/Interface/ICommonService.cs b/ds-wms-service/DS.WMS.Core/Sys/Interface/ICommonService.cs index 409fa142..d40747bd 100644 --- a/ds-wms-service/DS.WMS.Core/Sys/Interface/ICommonService.cs +++ b/ds-wms-service/DS.WMS.Core/Sys/Interface/ICommonService.cs @@ -304,4 +304,12 @@ public interface ICommonService /// public Task<(ISugarQueryable queryable, string? ruleScope)> GetVisibleDataRuleFilter(SqlSugarScopeProvider tenantDb); + /// + /// 获取当前用户的可视权限范围 + /// + /// + /// + /// + public Task<(ISugarQueryable queryable, string? ruleScope)> GetVisibleDataRuleFilter(ISugarQueryable queryable, SqlSugarScopeProvider tenantDb); + } \ No newline at end of file diff --git a/ds-wms-service/DS.WMS.Core/Sys/Method/ClientCommonService.cs b/ds-wms-service/DS.WMS.Core/Sys/Method/ClientCommonService.cs index 72593f02..0de93ca2 100644 --- a/ds-wms-service/DS.WMS.Core/Sys/Method/ClientCommonService.cs +++ b/ds-wms-service/DS.WMS.Core/Sys/Method/ClientCommonService.cs @@ -13,6 +13,7 @@ using DS.WMS.Core.Info.Dtos; using DS.WMS.Core.Info.Entity; using DS.WMS.Core.Op.Dtos; using DS.WMS.Core.Op.Entity; +using DS.WMS.Core.Op.Method; using DS.WMS.Core.Sys.Dtos; using DS.WMS.Core.Sys.Entity; using DS.WMS.Core.Sys.Interface; @@ -30,7 +31,7 @@ public class ClientCommonService : IClientCommonService private readonly ISqlSugarClient db; private readonly IUser user; private readonly ISaasDbService saasService; - + private readonly ICommonService commonService; /// /// /// @@ -41,6 +42,7 @@ public class ClientCommonService : IClientCommonService db = _serviceProvider.GetRequiredService(); user = _serviceProvider.GetRequiredService(); saasService = _serviceProvider.GetRequiredService(); + commonService = _serviceProvider.GetRequiredService(); } internal ISugarQueryable DicQueryable(string typeCode, string queryKey) @@ -172,6 +174,27 @@ public class ClientCommonService : IClientCommonService var orglist = db.Queryable().ToList(); + //var query = tenantDb.Queryable() + // .InnerJoin((a, b) => a.Id == b.ClientId) + // .Where((a, b) => a.Status == StatusEnum.Enable.ToEnumInt() && b.IsController == true) + // .WhereIF(!string.IsNullOrEmpty(queryKey), (a, b) => a.CodeName.Contains(queryKey) || a.ShortName.Contains(queryKey) || a.Description.Contains(queryKey)) + // .Select((a, b) => new ControllerClientRes + // { + // Id = a.Id, + // PinYinCode = a.ShortName + "(" + a.CodeName + ")", + // //UserIds = SqlFunc.MappingColumn(sql); + // //SaleOrgName = string.IsNullOrEmpty(a.SaleOrgId) ? "": orglist.Where(x=>x.Id == long.Parse(a.SaleOrgId)).FirstOrDefault().OrgName + // }, true + // ) + // .Mapper(it => + // { + // it.ClientContact = tenantDb.Queryable().Where(x => x.ClientId == it.Id && x.Status == StatusEnum.Enable && x.IsDefault == true) + // .Select().First(); + // //it.UserIds = tenantDb.Queryable().Where(c => c.StartDate <= DateTime.Now && c.EndDate >= DateTime.Now && c.ClientId == it.Id).Select(n => n.CreateBy).ToList(); + // }); + //(query, _) = await commonService.GetVisibleDataRuleFilter(query,tenantDb); + //var data = await query + // .Take(20).WithCache($"{SqlSugarCacheConst.InfoClient}{user.TenantId}", SqlSugarCacheConst.CacheDurationInSeconds).ToListAsync(); var data = await tenantDb.Queryable() .InnerJoin((a, b) => a.Id == b.ClientId) .Where((a, b) => a.Status == StatusEnum.Enable.ToEnumInt() && b.IsController == true) @@ -189,6 +212,7 @@ public class ClientCommonService : IClientCommonService .Select().First(); }) .Take(20).WithCache($"{SqlSugarCacheConst.InfoClient}{user.TenantId}", SqlSugarCacheConst.CacheDurationInSeconds).ToListAsync(); + return await Task.FromResult(DataResult>.Success("获取数据成功!", data, MultiLanguageConst.DataQuerySuccess)); } /// diff --git a/ds-wms-service/DS.WMS.Core/Sys/Method/CommonService.cs b/ds-wms-service/DS.WMS.Core/Sys/Method/CommonService.cs index 978fbe63..9e18052f 100644 --- a/ds-wms-service/DS.WMS.Core/Sys/Method/CommonService.cs +++ b/ds-wms-service/DS.WMS.Core/Sys/Method/CommonService.cs @@ -27,6 +27,8 @@ using DS.Module.Core.Constants; using DS.WMS.Core.Op.Entity; using DS.Module.Core.Data; using DS.WMS.Core.TaskPlat.Entity; +using DS.WMS.Core.Invoice.Dtos; +using DS.WMS.Core.Op.Method; namespace DS.WMS.Core.Sys.Method; @@ -42,7 +44,6 @@ public class CommonService : ICommonService private readonly IHttpContextAccessor IhttpContext; private readonly IWebHostEnvironment _environment; private readonly ILogger _logger; - /// /// 任务台基础表自定义列对应的Code /// @@ -1582,6 +1583,151 @@ public class CommonService : ICommonService } } + + /// + /// 获取当前用户的可视权限范围 + /// + /// + /// + /// + public async Task<(ISugarQueryable queryable, string? ruleScope)> GetVisibleDataRuleFilter(ISugarQueryable queryable,SqlSugarScopeProvider tenantDb) + { + // 因为TaskBaseAllocation不能继承IOrgId,所以在这里根据条件,在需要的时候手动添加IOrgId查询筛选器 + bool isTask = typeof(T) == typeof(TaskBaseAllocation); + + var userInfo = db.Queryable().First(x => x.Id == long.Parse(user.UserId)); + if (userInfo.UserType == 0) + { + if (isTask) queryable.Filter(typeof(IOrgId)); + + return (queryable, "all"); //超级管理员特权 + } + + var moduleName = typeof(T).Name.ToLower(); + + //var rule = tenantDb.Queryable() + // .First(u => u.UserId == long.Parse(user.UserId) && u.PermissionEntity.ToLower() == moduleName && u.Status == StatusEnum.Enable); + var rule = tenantDb.Queryable() + .First(u => u.UserId == long.Parse(user.UserId) && u.PermissionEntity.ToLower() == "infoclient" && u.Status == StatusEnum.Enable); + if (rule.IsNull()) + { + return (queryable, null); //默认查询 + } + else + { + var contidions = new List(); + #region 可视权限 + + if ((long)rule.VisibleTemplateId == 0) + { + contidions.Add(new ConditionalModel { FieldName = "CreateBy", ConditionalType = ConditionalType.Equal, FieldValue = user.UserId }); + return (queryable.Where(contidions), "self"); //查询自身 + } + + var ruleInfo = tenantDb.Queryable().First(x => x.Id == (long)rule.VisibleTemplateId); + if (ruleInfo.RuleScope == "all") + { + return (queryable.ClearFilter(), ruleInfo.RuleScope); //查询全部 + } + else if (ruleInfo.RuleScope == "none") + { + if (isTask) + { return (queryable, ruleInfo.RuleScope); } + else + { + SqlSugar.Check.ExceptionEasy("NO Operation", "没有数据查看权限!");//返回提示 + } + } + else if (ruleInfo.RuleScope == "self_org") + { + if (isTask) queryable.Filter(typeof(IOrgId)); + var conditionalModels = ConvertConditionalModel(ruleInfo.DataRules); + if (conditionalModels.Count > 0) + { + return (queryable.Where(conditionalModels), ruleInfo.RuleScope);//自定义查询 + } + else + { + return (queryable, ruleInfo.RuleScope);//默认查询 + } + } + else if (ruleInfo.RuleScope == "self_org_with_child") // 当前机构及下属机构 + { + List orgList = await db.Queryable().ToChildListAsync(s => s.ParentId, user.OrgId); + IEnumerable orgIdList = orgList.Select(x => x.Id); + contidions.Add(new ConditionalModel { FieldName = "OrgId", ConditionalType = ConditionalType.In, FieldValue = string.Join(",", orgIdList) }); + + return (queryable.Where(contidions), ruleInfo.RuleScope); + } + else if (ruleInfo.RuleScope == "self") + { + var conditionalModels = ConvertConditionalModel(ruleInfo.DataRules); + if (conditionalModels.Count > 0) + { + return (queryable.Where(conditionalModels), ruleInfo.RuleScope);//自定义查询 + } + else + { + contidions.Add(new ConditionalModel { FieldName = "CreateBy", ConditionalType = ConditionalType.Equal, FieldValue = user.UserId }); + return (queryable.Where(contidions), ruleInfo.RuleScope); //查询创建人自身信息 + } + + } + else if (ruleInfo.RuleScope == "self_dept") + { + if (String.IsNullOrEmpty(userInfo.DeptName)) + { + if (!isTask) + { + SqlSugar.Check.ExceptionEasy("NO Operation", "当前员工没有默认归属部门!"); + } + } + var conditionalModels = ConvertConditionalModel(ruleInfo.DataRules); + if (conditionalModels.Count > 0) + { + return (queryable.Where(conditionalModels), ruleInfo.RuleScope);//自定义查询 + } + else + { + var deptUsers = db.Queryable().Where(x => x.DeptId == userInfo.DeptId).Select(x => x.Id).ToList(); + contidions.Add(new ConditionalModel { FieldName = "CreateBy", ConditionalType = ConditionalType.In, FieldValue = string.Join(",", deptUsers) }); + return (queryable.Where(contidions), ruleInfo.RuleScope); //查询本部门 + } + } + else if (ruleInfo.RuleScope == "select_org") + { + var conditionalModels = ConvertConditionalModel(ruleInfo.DataRules); + //var conditions = ruleInfo.DataRules.ConvertSqlSugarExpression(); + //var conditionalModels = + // db.ConfigQuery.Context.Utilities.JsonToConditionalModels( + // JsonConvert.SerializeObject(conditions)); + return (queryable.ClearFilter().Where(conditionalModels), ruleInfo.RuleScope); //查询指定机构 + } + else if (ruleInfo.RuleScope == "select_user") + { + //var conditionalModels = ConvertConditionalModel(ruleInfo.DataRules); + var conditionalModels = ConvertConditionalModel(ruleInfo.DataRules); + return (queryable.ClearFilter().Where(conditionalModels), ruleInfo.RuleScope);//查询指定人员 + } + + else if (ruleInfo.RuleScope == "customize") + { + //var conditionalModels = ConvertConditionalModel(ruleInfo.DataRules); + var conditionalModels = ConvertConditionalModel(ruleInfo.DataRules); + return (queryable.Where(conditionalModels), ruleInfo.RuleScope);//自定义查询 + } + else if (ruleInfo.DataRules.IsNull()) + { + contidions.Add(new ConditionalModel { FieldName = "CreateBy", ConditionalType = ConditionalType.Equal, FieldValue = user.UserId }); + return (queryable.Where(contidions), ruleInfo.RuleScope); //没有设置数据规则,那么视为该资源允许被创建人查看 + } + + return (queryable, ruleInfo.RuleScope); + + #endregion + } + } + private List ConvertConditionalModel(string rules) {