Esempio n. 1
0
        /// <summary>
        /// 取得规则组的表达式
        /// </summary>
        /// <param name="ruleGroupId">规则组ID</param>
        /// <returns></returns>
        private string GetExpressionStr(Guid ruleGroupId, string userName, string groupId)
        {
            // 规则信息
            var ruleGroup      = _context.Set <RuleGroup>().Find(ruleGroupId);
            var rules          = _context.Set <Rule>().AsNoTracking().Where(rule => rule.RuleGroupId == ruleGroupId);
            var ruleConditions = _context.Set <RuleCondition>().AsNoTracking().Where(condition => condition.RuleGroupId == ruleGroupId);

            // 生成表达式
            var topRule         = rules.Where(rule => rule.UpRuleId.Equals(new Guid())).FirstOrDefault();
            var entityType      = EntityTypeFinder.FindEntityType(ruleGroup.DbContext, ruleGroup.Entity);
            var expressionGroup = new ExpressionGroup(entityType);
            var keyValuePairs   = new Dictionary <string, string> {
            };

            keyValuePairs.Add("UserName", userName);
            keyValuePairs.Add("GroupId", groupId);
            MakeExpressionGroup(topRule, rules, ruleConditions, entityType, keyValuePairs, ref expressionGroup);

            // 生成过滤表达式
            Expression lambda = expressionGroup.GetLambda();

            // 表达式序列化
            var serializer = new ExpressionSerializer(new JsonSerializer())
            {
                AutoAddKnownTypesAsListTypes = true
            };

            serializer.AddKnownType(typeof(Core.Expressions.ExpressionType));
            return(serializer.SerializeText(lambda));
        }
Esempio n. 2
0
        /// <summary>
        /// 修改规则组
        /// </summary>
        /// <param name="rules">规则</param>
        /// <param name="ruleConditions">条件</param>
        /// <param name="ruleGroup">规则组</param>
        public async Task <bool> AddOrUpdateRuleAsync(IEnumerable <RuleEntityResult> rules, IEnumerable <RuleConditionResult> ruleConditions, RuleGroup ruleGroup)
        {
            // 因为有动态参数的存在,表达式暂时不持久化到数据库,暂时删除group的lambda字段
            //// 生成表达式保存到数据库
            //// 获取实体类型
            var topRule         = rules.Where(rule => string.IsNullOrEmpty(rule.UpRuleId)).FirstOrDefault();
            var entityType      = EntityTypeFinder.FindEntityType(ruleGroup.DbContext, ruleGroup.Entity);
            var expressionGroup = new ExpressionGroup(entityType);
            var keyValuePairs   = new Dictionary <string, string> {
            };

            keyValuePairs.Add("UserName", _httpContextAccessor.HttpContext.User.GetClaimValue(CustomClaimTypes.UserName));
            keyValuePairs.Add("GroupId", _httpContextAccessor.HttpContext.User.GetClaimValue(CustomClaimTypes.Group));
            MakeExpressionGroup(topRule, rules, ruleConditions, entityType, keyValuePairs, ref expressionGroup);

            // 用当前用户数据检查表达式是否正确
            try
            {
                expressionGroup.GetLambda();
            }
            catch
            {
                return(false);
            }

            // 开始更新数据
            var group = _ruleGroupRepository.GetByWhereNoTracking(rg => rg.Id == ruleGroup.Id).FirstOrDefault();

            // 已经存在时先删除再插入
            if (group != null)
            {
                // 先删除再插入
                _ruleRepository.DeleteRange(r => r.RuleGroupId == group.Id);
                _ruleConditionRepository.DeleteRange(r => r.RuleGroupId == group.Id);
            }

            // 修改规则组Id
            var groupId = group != null ? group.Id : GuidUtil.NewSquentialGuid();

            ruleGroup.Id = groupId;

            // 修改每项规则的Id和Upid
            foreach (var rule in rules)
            {
                var oldId = rule.Id;

                // 替换前端生成的ID
                var ruleId = GuidUtil.NewSquentialGuid().ToString();
                rule.Id          = ruleId;
                rule.RuleGroupId = groupId.ToString();
                foreach (var r in rules)
                {
                    if (r.UpRuleId == oldId)
                    {
                        r.UpRuleId = ruleId;
                    }
                }
                ;
                foreach (var c in ruleConditions)
                {
                    if (c.RuleId == oldId)
                    {
                        c.RuleId      = ruleId;
                        c.RuleGroupId = groupId.ToString();
                    }
                }
                ;
            }

            // 生成新的Id
            ruleConditions.ToList().ForEach(condition => condition.Id = GuidUtil.NewSquentialGuid().ToString());

            // 保存全部数据
            await _ruleRepository.InsertRangeAsync(_mapper.Map <IEnumerable <RuleEntity> >(rules));

            await _ruleConditionRepository.InsertRangeAsync(_mapper.Map <IEnumerable <RuleCondition> >(ruleConditions));

            // 更新或添加规则组
            if (group == null)
            {
                await _ruleGroupRepository.InsertAsync(ruleGroup);
            }
            else
            {
                _ruleGroupRepository.Update(ruleGroup);
            }

            // 提交事务
            await _unitOfWork.CommitAsync();

            return(true);
        }