//平台内置的key 等于0,即为空 , "XuserFromFirstShareId", "XuserFromFirstShareId", "XfromTenantId", "XfromAppId" 

var lstPfKeys = new List<string> { "_userFromFirstShareId", "_userFromFirstShareId", "_fromTenantId", "_fromAppId" };

 if (!dictionary.ContainsKey(key) || dictionary[key] == null || dictionary[key].ToString() == "null" || string.IsNullOrEmpty(dictionary[key].ToString()) || (lstPfKeys.Contains(key) && dictionary[key].ToString() == "0"))
 {
 sql = Regex.Replace(sql, regixStrTemp, string.Empty);
 continue;
 }
            //平台内置的key 等于0,即为空 , "XuserFromFirstShareId", "XuserFromFirstShareId", "XfromTenantId", "XfromAppId" 
            var lstPfKeys = new List<string> { "_userFromFirstShareId", "_userFromFirstShareId", "_fromTenantId", "_fromAppId" };
            foreach (string key in dic.Keys)
            {
                if (string.IsNullOrEmpty(key)) continue;
                bool isS = sql.Contains(RegexConst.FORMAT_S_IF.Replace(RegexConst.FORMAT_IF_NAME, key));
                bool isE = sql.Contains(RegexConst.FORMAT_E_IF.Replace(RegexConst.FORMAT_IF_NAME, key));
                if (!isS && !isE) continue;
                //执行替换
                string regixStr = RegexConst.NO_WHERE.Replace(RegexConst.NO_WHERE_COLUMN, key);
                string regixStrWhere = RegexConst.NO_WHERE_EXISTS.Replace(RegexConst.NO_WHERE_COLUMN, key);
                string regixStrTemp = regixStr;
                MatchCollection mc = Regex.Matches(sql, RegexConst.NO_WHERE_S_IF, RegexOptions.IgnoreCase);
                if (mc.Count == 1)
                {
                    regixStrTemp = regixStrWhere;
                }
                if (!dictionary.ContainsKey(key) || dictionary[key] == null || dictionary[key].ToString() == "null" || string.IsNullOrEmpty(dictionary[key].ToString()) || (lstPfKeys.Contains(key) && dictionary[key].ToString() == "0"))
                {
                    sql = Regex.Replace(sql, regixStrTemp, string.Empty);
                    continue;
                }
                string regixStr_s = RegexConst.NO_WHERE_S.Replace(RegexConst.NO_WHERE_COLUMN, key);
                string regixStr_e = RegexConst.NO_WHERE_E.Replace(RegexConst.NO_WHERE_COLUMN, key);
                // 获取操作的信息
                string regixStrOp_s = RegexConst.FORMAT_WHERE_MERGE_B(regixStr_s);
                string regixStrOp_e = RegexConst.FORMAT_WHERE_MERGE_E(regixStr_e);
                // 提取操作数据
                MatchCollection mcOpS = Regex.Matches(sql, regixStrOp_s, RegexOptions.IgnoreCase);
                if (mcOpS.Count > 0)
                {
                    string paramsValue = dictionary[key].ToString();
                    MatchCollection mcOpE = Regex.Matches(sql, regixStrOp_e, RegexOptions.IgnoreCase);

                    // 移除
                    List<DataKeyValue> lstNull = new List<DataKeyValue>();

                    // 保留
                    List<DataKeyValue> lstYes = new List<DataKeyValue>();

                    foreach (Match mcOpItem in mcOpS)
                    {
                        if (!sql.Contains(mcOpItem.Value)) continue;
                        MatchCollection lstMcOpV = Regex.Matches(mcOpItem.Value, RegexConst.FORMAT_WHERE_COMPARE, RegexOptions.IgnoreCase);
                        string tempValue = null;
                        if (lstMcOpV.Count > 0)
                        {
                            tempValue = lstMcOpV[0].Value.Replace("{op-", "").Trim('}');
                        }
                        if (string.IsNullOrEmpty(tempValue))
                        {
                            throw new BizException(ErrorCodeConst.MAPPER_25001.ErrorCode, $"【{sql}】配置错误,必须包含其中之一(eq,lt,gt,lte,gte)");
                        }
                        string op = tempValue.Split(":")[0];
                        string val = tempValue.Split(":")[1];
                        // 如果满足条件,保留表达式里面内容,否则移除表达式和对应的内容
                        bool isReplaceAll = true;
                        string regixS = $"{regixStr_s}{lstMcOpV[0].Value}";
                        string regixE = $"{lstMcOpV[0].Value}{regixStr_e}";
                        switch (op)
                        {
                            case "neq":
                                isReplaceAll = paramsValue != val;
                                break;
                            case "eq":
                                isReplaceAll = paramsValue == val;
                                break;
                            case "lt":
                                isReplaceAll = Convert.ToDouble(paramsValue) < Convert.ToDouble(val);
                                break;
                            case "gt":
                                isReplaceAll = Convert.ToDouble(paramsValue) > Convert.ToDouble(val);
                                break;
                            case "lte":
                                isReplaceAll = Convert.ToDouble(paramsValue) <= Convert.ToDouble(val);
                                break;
                            case "gte":
                                isReplaceAll = Convert.ToDouble(paramsValue) >= Convert.ToDouble(val);
                                break;
                            default:
                                throw new BizException(ErrorCodeConst.MAPPER_25001.ErrorCode, $"【{op}】未配置,必须是其中之一(eq,lt,gt,lte,gte)");
                        }
                        if (isReplaceAll)
                        {
                            lstYes.Add(new DataKeyValue
                            {
                                Value = regixE,
                                Key = regixS
                            });
                        }
                        else
                        {
                            lstNull.Add(new DataKeyValue
                            {
                                Value = regixE,
                                Key = regixS
                            });

                        }
                    }

                    foreach (var item in lstNull)
                    {
                        sql = Regex.Replace(sql, $"{item.Key}.+{item.Value}", string.Empty);
                    }

                    foreach (var item in lstYes)
                    {
                        sql = Regex.Replace(sql, item.Key, string.Empty);
                        sql = Regex.Replace(sql, item.Value, string.Empty);
                    }
                }
                else
                {
                    sql = Regex.Replace(sql, regixStr_s, string.Empty);
                    sql = Regex.Replace(sql, regixStr_e, string.Empty);
                }
            }
文档更新时间: 2023-05-28 21:50   作者:admin