using Infrastructure;
using Infrastructure.Extensions;
using Microsoft.AspNetCore.Components;
using Newtonsoft.Json.Linq;
using OpenAuth.App.FormScheme.Response;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;

namespace OpenAuth.App.FormScheme.FormHelpers
{
    public static class FormHelper
    {
        /// <summary>
        /// 获取保存sql语句
        /// </summary>
        /// <param name="formSchemeModel"></param>
        /// <param name="dataJson"></param>
        /// <param name="pkey"></param>
        /// <param name="pkeyValue"></param>
        /// <param name="isUpdate"></param>
        /// <returns></returns>
        public static List<FormDbTable> GetSaveSql(FormSchemeModel formSchemeModel, JObject dataJson, string pkey, string pkeyValue, bool isUpdate)
        {
            List<FormDbTable> list = new List<FormDbTable>();

            // 获取主子表
            FormDbTableInfo mainTable = formSchemeModel.Db.Find(t => t.Type == "main");
            List<FormDbTableInfo> childrenTables = formSchemeModel.Db.FindAll(t => t.Type == "chlid");

            // 对表组件按表进行分类
            Dictionary<string, List<FormComponentModel>> tableMap = new Dictionary<string, List<FormComponentModel>>();
            Dictionary<string, FormComponentModel> girdTableMap = new Dictionary<string, FormComponentModel>();

            foreach (var tab in formSchemeModel.FormInfo.TabList)
            {
                foreach (var component in tab.Components)
                {
                    if (string.IsNullOrEmpty(component.Table))
                    {
                        continue;
                    }
                    if (!tableMap.ContainsKey(component.Table))
                    {
                        tableMap[component.Table] = new List<FormComponentModel>();
                    }

                    if (component.Type == "gridtable")
                    {
                        tableMap[component.Table].AddRange(component.children);
                        girdTableMap.Add(component.Table, component);
                    }
                    else
                    {
                        if (component.Prop == pkey)
                        {
                            pkey = component.Field;
                        }
                        tableMap[component.Table].Add(component);
                    }
                }
            }

            if (isUpdate)
            {
                list.Add(GetUpDateSql(mainTable, tableMap, dataJson, pkey, pkeyValue));
                foreach (var childTable in childrenTables)
                {
                    if (tableMap.ContainsKey(childTable.Name))
                    {
                        var rComponent = tableMap[childTable.RelationName].Find(t => t.Field == childTable.RelationField);
                        if (girdTableMap.ContainsKey(childTable.Name))
                        {
                            list.Add(GetDeleteSql(childTable, dataJson, rComponent));
                            // 新增
                            FormComponentModel newComponent = new FormComponentModel();
                            newComponent.Prop = rComponent.Prop;
                            newComponent.Field = childTable.Field;
                            tableMap[childTable.Name].Add(newComponent);
                            List<JObject> girdDataJson = dataJson[girdTableMap[childTable.Name].Prop].ToString().ToObject<List<JObject>>();
                            foreach (var girdData in girdDataJson)
                            {
                                girdData.Add(newComponent.Prop, dataJson[newComponent.Prop]);
                                list.Add(GetInsertSql(childTable, tableMap, girdData));
                            }
                        }
                        else
                        {
                            list.Add(GetUpDateSql(childTable, tableMap, dataJson, childTable.Field, dataJson[rComponent.Prop].ToString()));
                        }
                    }

                }
            }
            else
            {
                list.Add(GetInsertSql(mainTable, tableMap, dataJson));
                foreach (var childTable in childrenTables)
                {
                    var rComponent = tableMap[childTable.RelationName].Find(t => t.Field.ToLower() == childTable.RelationField.ToLower());
                    FormComponentModel newComponent = new FormComponentModel();
                    newComponent.Prop = rComponent.Prop;
                    newComponent.Field = childTable.Field;
                    tableMap[childTable.Name].Add(newComponent);

                    if (girdTableMap.ContainsKey(childTable.Name))
                    {
                        // 编辑表格
                        List<JObject> girdDataJson = dataJson[girdTableMap[childTable.Name].Prop].ToString().ToObject<List<JObject>>();
                        foreach (var girdData in girdDataJson)
                        {
                            girdData.Add(newComponent.Prop, dataJson[newComponent.Prop]);
                            list.Add(GetInsertSql(childTable, tableMap, girdData));
                        }
                    }
                    else
                    {
                        list.Add(GetInsertSql(childTable, tableMap, dataJson));
                    }

                }
            }

            return list;
        }
        /// <summary>
        /// 获取保存sql语句
        /// </summary>
        /// <param name="formSchemeModel"></param>
        /// <param name="dataJson"></param>
        /// <param name="pkey"></param>
        /// <param name="pkeyValue"></param>
        /// <param name="isUpdate"></param>
        /// <returns></returns>
        public static List<FormDbTable> GetSaveSqlNew(FormSchemeNewModel formSchemeModel, JObject dataJson, string pkey, string pkeyValue, bool isUpdate)
        {
            List<FormDbTable> list = new List<FormDbTable>();

            // 获取主子表
            FormDbTableInfo mainTable = formSchemeModel.Db.Find(t => t.Type == "main");
            List<FormDbTableInfo> childrenTables = formSchemeModel.Db.FindAll(t => t.Type == "chlid");

            // 对表组件按表进行分类
            Dictionary<string, List<FormComponentNewModel>> tableMap = new Dictionary<string, List<FormComponentNewModel>>();
            //Dictionary<string, FormComponentNewModel> girdTableMap = new Dictionary<string, FormComponentNewModel>();
            Dictionary<string, FormComponentNewModel> girdTableMap = new Dictionary<string, FormComponentNewModel>();

            foreach (var tab in formSchemeModel.FormInfo.TabList)
            {
                foreach (var component in tab.Schemas)
                {
                    if (component == null || component.ComponentProps == null || string.IsNullOrEmpty(component.ComponentProps.DataTable))
                    {
                        //if(component.Component == "Grid1")
                        //{
                        //    foreach (var column in component.Columns)
                        //    {
                        //        tableMap[component.ComponentProps.DataTable].AddRange(column.children);
                        //    }
                        //}
                        //else
                        //{
                        //    continue;
                        //}
                        continue;
                    }
                    if (!tableMap.ContainsKey(component.ComponentProps.DataTable))
                    {
                        tableMap[component.ComponentProps.DataTable] = new List<FormComponentNewModel>();
                    }

                    if (component.Component == "Grid")
                    {
                        //tableMap[component.ComponentProps.DataTable].AddRange(component.children);
                        //girdTableMap.Add(component.ComponentProps.DataTable, component);

                        foreach (var column in component.Columns)
                        {
                            tableMap[component.ComponentProps.DataTable].AddRange(column.children);
                            //girdTableMap.Add(component.ComponentProps.DataTable, column);
                            if (girdTableMap.ContainsKey(component.ComponentProps.DataTable))
                            {
                                //girdTableMap[component.ComponentProps.DataTable].children.AddRange(column.children);
                            }
                            else
                            {
                                girdTableMap.Add(component.ComponentProps.DataTable, component);
                            }
                        }

                    }
                    else
                    {
                        if (component.Field == pkey)
                        {
                            pkey = component.Field;
                        }
                        tableMap[component.ComponentProps.DataTable].Add(component);
                    }
                }
            }



            if (isUpdate)
            {
                list.Add(GetUpDateSqlNew(mainTable, tableMap, dataJson, pkey, pkeyValue));
                foreach (var childTable in childrenTables)
                {
                    if (tableMap.ContainsKey(childTable.Name))
                    {
                        var rComponent = tableMap[childTable.RelationName].Find(t => t.ComponentProps.FieldName == childTable.RelationField);
                        if (girdTableMap.ContainsKey(childTable.Name))
                        {
                            list.Add(GetDeleteSqlNew(childTable, dataJson, rComponent));
                            // 新增
                            FormComponentNewModel newComponent = new FormComponentNewModel();
                            //newComponent.ComponentProps.FieldName = rComponent.ComponentProps.FieldName;
                            newComponent.ComponentProps = new ComponentPropsModel();
                            newComponent.ComponentProps.DataTable = childTable.Name;
                            newComponent.ComponentProps.FieldName = childTable.Field;
                            //newComponent.Field = childTable.Field;
                            newComponent.Field = rComponent.Field;
                            tableMap[childTable.Name].Add(newComponent);
                            //for (int i = 0; i < girdTableMap[childTable.Name].Count; i++)
                            //{
                            //    // 编辑表格
                            //    List<JObject> girdDataJson = dataJson[girdTableMap[childTable.Name][i].Field].ToString().ToObject<List<JObject>>();
                            //    foreach (var girdData in girdDataJson)
                            //    {
                            //        //girdData.Add(newComponent.ComponentProps.FieldName, dataJson[newComponent.ComponentProps.FieldName]);
                            //        list.Add(GetInsertSqlNew(childTable, tableMap, girdData));
                            //    }
                            //}
                            // List<JObject> girdDataJson = dataJson[girdTableMap[childTable.Name].ComponentProps.FieldName].ToString().ToObject<List<JObject>>();
                            List<JObject> girdDataJson = dataJson[girdTableMap[childTable.Name].Field].ToString().ToObject<List<JObject>>();
                            foreach (var girdData in girdDataJson)
                            {
                                girdData.Add(newComponent.Field, dataJson[newComponent.Field]);
                                list.Add(GetInsertSqlNew(childTable, tableMap, girdData));
                            }
                        }
                        else
                        {
                            list.Add(GetUpDateSqlNew(childTable, tableMap, dataJson, childTable.Field, dataJson[rComponent.Field].ToString()));
                        }
                    }

                }
            }
            else
            {
                list.Add(GetInsertSqlNew(mainTable, tableMap, dataJson));
                foreach (var childTable in childrenTables)
                {
                    var rComponent = tableMap[childTable.RelationName].Find(t => t.ComponentProps.FieldName == childTable.RelationField);
                    FormComponentNewModel newComponent = new FormComponentNewModel() { };
                    newComponent.Field = rComponent.Field;
                    newComponent.ComponentProps = new ComponentPropsModel();
                    //newComponent.ComponentProps.DataTable = rComponent.ComponentProps.DataTable;
                    //newComponent.ComponentProps.FieldName = rComponent.ComponentProps.FieldName;
                    newComponent.ComponentProps.DataTable = childTable.Name;
                    newComponent.ComponentProps.FieldName = childTable.Field;

                    //newComponent.Field = childTable.Field;
                    tableMap[childTable.Name].Add(newComponent);

                    if (girdTableMap.ContainsKey(childTable.Name))
                    {
                        //for(int i=0;i< girdTableMap[childTable.Name].Count; i++)
                        //{
                        //    // 编辑表格
                        //    List<JObject> girdDataJson = dataJson[girdTableMap[childTable.Name][i].Field].ToString().ToObject<List<JObject>>();
                        //    foreach (var girdData in girdDataJson)
                        //    {
                        //        //girdData.Add(newComponent.ComponentProps.FieldName, dataJson[newComponent.ComponentProps.FieldName]);
                        //        list.Add(GetInsertSqlNew(childTable, tableMap, girdData));
                        //    }
                        //}
                        List<JObject> girdDataJson = dataJson[girdTableMap[childTable.Name].Field].ToString().ToObject<List<JObject>>();
                        foreach (var girdData in girdDataJson)
                        {
                            girdData.Add(newComponent.Field, dataJson[newComponent.Field]);
                            list.Add(GetInsertSqlNew(childTable, tableMap, girdData));
                        }
                    }
                    else
                    {
                        list.Add(GetInsertSqlNew(childTable, tableMap, dataJson));
                    }

                }
            }

            return list;
        }

        /// <summary>
        /// 获取表单查询方法
        /// </summary>
        /// <param name="formSchemeModel"></param>
        /// <param name="queryJson"></param>
        /// <returns></returns>
        public static FormDbTable GetQuerySql(FormSchemeModel formSchemeModel, string queryJson)
        {
            FormDbTable res = new FormDbTable();
            res.DbParameter = new List<SugarParameter>();

            StringBuilder str = new StringBuilder();
            FormDbTableInfo mainTable = formSchemeModel.Db.Find(t => t.Type == "main");
            int mainTableIndex = formSchemeModel.Db.FindIndex(t => t.Type == "main");

            if (formSchemeModel.FormType == 1)
            {
                str.Append($"select * from ({mainTable.Sql})t ");
            }
            else
            {
                Dictionary<string, List<FormComponentModel>> tableMap = new Dictionary<string, List<FormComponentModel>>();
                foreach (var tab in formSchemeModel.FormInfo.TabList)
                {
                    foreach (var component in tab.Components)
                    {
                        if (component.Type != "gridtable")
                        {
                            if (string.IsNullOrEmpty(component.Table))
                            {
                                continue;
                            }
                            if (!tableMap.ContainsKey(component.Table))
                            {
                                tableMap[component.Table] = new List<FormComponentModel>();
                            }
                            tableMap[component.Table].Add(component);
                        }
                    }
                }


                str.Append("SELECT ");
                var mainComponents = tableMap[mainTable.Name];
                foreach (var component in mainComponents)
                {
                    str.Append(string.Format("t.{1} as {1}{0},", mainTableIndex, component.Field));
                }

                str.Append(GetQueryColumns(formSchemeModel.Db, tableMap, mainTable.Name));
                str.Append("FROM ");
                str.Append(string.Format("{0} t ", mainTable.Name));
                str.Append(GetQueryLeftTable(formSchemeModel.Db, tableMap, mainTable.Name, mainTableIndex));


            }

            str.Append(" WHERE 1=1 {LEARUN_SASSID} ");





            // 添加参数
            // 对表组件按表进行分类
            Dictionary<string, FormComponentModel> componentMap = new Dictionary<string, FormComponentModel>();
            foreach (var tab in formSchemeModel.FormInfo.TabList)
            {
                foreach (var component in tab.Components)
                {
                    if (string.IsNullOrEmpty(component.Table))
                    {
                        continue;
                    }
                    if (!componentMap.ContainsKey(component.Prop))
                    {
                        componentMap.Add(component.Prop, component);
                    }
                }
            }

            var queryParam = queryJson.ToJObject();
            int index = 0;
            foreach (var item in queryParam.Properties())
            {
                componentMap.TryGetValue(item.Name, out var component);
                if (component != null && !string.IsNullOrEmpty(component.Table) && !queryParam[component.Prop].IsEmpty())
                {
                    int tableIndex = formSchemeModel.Db.FindIndex(t => t.Name == component.Table);
                    FormDbTableInfo table = formSchemeModel.Db[tableIndex];

                    string tIndex = tableIndex + "";
                    if (table.Name == mainTable.Name)
                    {
                        tIndex = "";
                    }


                    switch (component.Type)
                    {
                        case "userSelect":
                        case "departmentSelect":
                        case "companySelect":
                        case "company":
                        case "department":
                        case "createuser":
                        case "modifyuser":
                        case "select":
                            str.Append($" AND t{tIndex}.{component.Field} in ('{queryParam[component.Prop].ToString().Replace(",", "','")}') ");
                            break;
                        case "input":
                        case "textarea":
                        case "texteditor":
                            res.DbParameter.Add(new SugarParameter($"p_{index}", $"%{queryParam[component.Prop].ToString()}%"));
                            str.Append($" AND t{tIndex}.{component.Field} like @p_{index} ");
                            break;

                        case "time":
                        case "datetime":
                        case "createtime":
                        case "modifytime":
                            var value = queryParam[component.Prop].ToString().Split(" - ");
                            res.DbParameter.Add(GetMyDbParameter($"p_{index}", value[0], component.CsType));
                            res.DbParameter.Add(GetMyDbParameter($"p_{index + 1}", value[1], component.CsType));
                            str.Append($" AND(t{tIndex}.{component.Field} >= @p_{index} AND t{tIndex}.{component.Field} <= @p_{index + 1} ) ");
                            index++;
                            break;
                        default:
                            res.DbParameter.Add(GetMyDbParameter($"p_{index}", queryParam[component.Prop].ToString(), component.CsType));
                            str.Append($" AND t{tIndex}.{component.Field} = @p_{index} ");
                            break;
                    }

                    index++;
                }
            }

            res.Sql = str.ToString().Replace(",FROM", " FROM");

            return res;
        }
        public static FormDbTable GetQuerySqlNew(FormSchemeNewModel formSchemeModel, string queryJson)
        {
            FormDbTable res = new FormDbTable();
            res.DbParameter = new List<SugarParameter>();

            StringBuilder str = new StringBuilder();
            FormDbTableInfo mainTable = formSchemeModel.Db.Find(t => t.Type == "main");
            int mainTableIndex = formSchemeModel.Db.FindIndex(t => t.Type == "main");

            if (formSchemeModel.FormType == 1)
            {
                str.Append($"select * from ({mainTable.Sql})t ");
            }
            else
            {
                Dictionary<string, List<FormComponentNewModel>> tableMap = new Dictionary<string, List<FormComponentNewModel>>();
                foreach (var tab in formSchemeModel.FormInfo.TabList)
                {
                    foreach (var component in tab.Schemas)
                    {
                        if (component.Component != "Grid")
                        {
                            if (component == null || component.ComponentProps == null || string.IsNullOrEmpty(component.ComponentProps.DataTable))
                            {
                                continue;
                            }
                            if (!tableMap.ContainsKey(component.ComponentProps.DataTable))
                            {
                                tableMap[component.ComponentProps.DataTable] = new List<FormComponentNewModel>();
                            }
                            tableMap[component.ComponentProps.DataTable].Add(component);
                        }
                    }
                }

                str.Append("SELECT ");
                var mainComponents = tableMap[mainTable.Name];
                foreach (var component in mainComponents)
                {
                    if (component.ComponentProps.FieldName == "geom")
                    {
                        str.Append(string.Format("st_astext( t{0}.\"{1}\") as \"{2}\",", mainTableIndex, component.ComponentProps.FieldName, component.Field));
                    }
                    else
                    {
                        str.Append(string.Format("t{0}.\"{1}\" as \"{2}\",", mainTableIndex, component.ComponentProps.FieldName, component.Field));
                    }

                    ////str.Append(string.Format("t{0}.\"{1}\" as \"{1}{0}\",", mainTableIndex, component.ComponentProps.FieldName));
                    //str.Append(string.Format("t{0}.\"{1}\" as \"{2}\",", mainTableIndex, component.ComponentProps.FieldName, component.Field));
                }

                str.Append(GetQueryColumnsNew(formSchemeModel.Db, tableMap, mainTable.Name));
                //str = new StringBuilder(str.ToString().Substring(0, str.Length - 1));
                str.Append("FROM ");
                str.Append(string.Format("\"{0}\" t{1} ", mainTable.Name, mainTableIndex));
                //不查询子表信息
                //str.Append(GetQueryLeftTableNew(formSchemeModel.Db, tableMap, mainTable.Name, mainTableIndex));


            }

            str.Append(" WHERE 1=1 {LEARUN_SASSID} ");





            // 添加参数
            // 对表组件按表进行分类
            Dictionary<string, FormComponentNewModel> componentMap = new Dictionary<string, FormComponentNewModel>();
            foreach (var tab in formSchemeModel.FormInfo.TabList)
            {
                foreach (var component in tab.Schemas)
                {
                    if (component == null || component.ComponentProps == null || string.IsNullOrEmpty(component.ComponentProps.DataTable))
                    {
                        continue;
                    }
                    if (!componentMap.ContainsKey(component.Field))
                    {
                        componentMap.Add(component.Field, component);
                    }
                }
            }



            var queryParam = queryJson.ToJObject();
            int index = 0;
            foreach (var item in queryParam.Properties())
            {
                componentMap.TryGetValue(item.Name, out var component);
                if (component != null && !string.IsNullOrEmpty(component.ComponentProps.DataTable) && !queryParam[component.Field].IsEmpty())
                {
                    //int tableIndex = formSchemeModel.Db.FindIndex(t => t.Name == component.Field);
                    int tableIndex = formSchemeModel.Db.FindIndex(t => t.Name == component.ComponentProps.DataTable);
                    FormDbTableInfo table = formSchemeModel.Db[tableIndex];

                    string tIndex = tableIndex + "";
                    //if (table.Name == mainTable.Name)
                    //{
                    //    tIndex = "";
                    //}


                    switch (component.Component)
                    {
                        case "userSelect":
                        case "departmentSelect":
                        case "companySelect":
                        case "company":
                        case "department":
                        case "createuser":
                        case "modifyuser":
                        case "select":
                            str.Append($" AND t{tIndex}.{component.Field} in ('{queryParam[component.Component].ToString().Replace(",", "','")}') ");
                            break;
                        case "Input":
                        case "InputTextArea":
                        case "texteditor":
                            res.DbParameter.Add(new SugarParameter($"p_{index}", $"%{queryParam[component.Field].ToString()}%"));
                            //str.Append($" AND t{tIndex}.{component.Field} like @p_{index} ");
                            str.Append($" AND t{tIndex}.{component.ComponentProps.FieldName} like @p_{index} ");
                            break;

                        case "time":
                        case "datetime":
                        case "createtime":
                        case "modifytime":
                            var value = queryParam[component.Field].ToString().Split(" - ");
                            res.DbParameter.Add(GetMyDbParameter($"p_{index}", value[0], component.CsType));
                            res.DbParameter.Add(GetMyDbParameter($"p_{index + 1}", value[1], component.CsType));
                            str.Append($" AND(t{tIndex}.{component.ComponentProps.FieldName} >= @p_{index} AND t{tIndex}.{component.ComponentProps.FieldName} <= @p_{index + 1} ) ");
                            index++;
                            break;
                        case "RangePicker":
                            var valuep = queryParam[component.Field].ToString().Split(" - ");
                            res.DbParameter.Add(GetMyDbParameter($"p_{index}", valuep[0], component.CsType));
                            res.DbParameter.Add(GetMyDbParameter($"p_{index + 1}", valuep[1], component.CsType));
                            str.Append($" AND(t{tIndex}.{component.ComponentProps.FieldName} >= @p_{index} AND t{tIndex}.{component.ComponentProps.FieldName} <= @p_{index + 1} ) ");
                            index++;
                            break;
                        default:
                            res.DbParameter.Add(GetMyDbParameter($"p_{index}", queryParam[component.Field].ToString(), component.CsType));
                            str.Append($" AND t{tIndex}.{component.ComponentProps.FieldName} = @p_{index} ");
                            break;
                    }

                    index++;
                }
            }

            res.Sql = str.ToString().Replace(",FROM", " FROM").Replace("{LEARUN_SASSID}", " ");

            return res;
        }

        /// <summary>
        /// 获取更新sql语句
        /// </summary>
        /// <param name="table">表</param>
        /// <param name="tableMap">组件集合</param>
        /// <param name="dataJson">表单数据</param>
        /// <param name="pkey">主键</param>
        /// <param name="pkeyValue">主键值</param>
        /// <returns></returns>
        private static FormDbTable GetUpDateSql(FormDbTableInfo table, Dictionary<string, List<FormComponentModel>> tableMap, JObject dataJson, string pkey, string pkeyValue)
        {
            FormDbTable res = new FormDbTable();
            res.DbParameter = new List<SugarParameter>();
            res.TableName = table.Name;
            res.ExecuteType = ExecuteType.Update;
            res.Pkey = pkey.ToUpper();
            bool isPkey = false;
            var formComponents = tableMap[table.Name];

            foreach (var component in formComponents)
            {
                if (!string.IsNullOrEmpty(component.Field))
                {
                    if (res.DbParameter.Find(t => t.ParameterName == component.Field.ToUpper()) != null)
                    {
                        // 参数添加过则不再添加
                        continue;
                    }

                    if (!dataJson[component.Prop].IsEmpty())
                    {
                        res.DbParameter.Add(GetMyDbParameter(component.Field.ToUpper(), dataJson[component.Prop].ToString(), component.CsType));
                    }
                    else
                    {
                        res.DbParameter.Add(new SugarParameter(component.Field.ToUpper(), null));
                    }

                    if (component.Field.ToUpper() == pkey.ToUpper())
                    {
                        isPkey = true;
                        if (res.DbParameter[res.DbParameter.Count - 1].Value == null)
                        {
                            res.DbParameter[res.DbParameter.Count - 1].Value = pkeyValue;
                        }
                    }
                }
            }

            if (!isPkey)
            {
                res.DbParameter.Add(new SugarParameter(pkey.ToUpper(), pkeyValue));
            }

            return res;
        }
        private static FormDbTable GetUpDateSqlNew(FormDbTableInfo table, Dictionary<string, List<FormComponentNewModel>> tableMap, JObject dataJson, string pkey, string pkeyValue)
        {
            FormDbTable res = new FormDbTable();
            res.DbParameter = new List<SugarParameter>();
            res.TableName = table.Name;
            res.ExecuteType = ExecuteType.Update;
            res.Pkey = pkey;
            bool isPkey = false;
            var formComponents = tableMap[table.Name];

            foreach (var component in formComponents)
            {
                if (!string.IsNullOrEmpty(component.ComponentProps.FieldName))
                {
                    if (res.DbParameter.Find(t => t.ParameterName == component.Field) != null)
                    {
                        // 参数添加过则不再添加
                        continue;
                    }

                    //if (dataJson[component.Field] != null && !dataJson[component.Field].IsEmpty())
                    if (!dataJson[component.Field].IsEmpty())
                    {
                        res.DbParameter.Add(GetMyDbParameter(component.ComponentProps.FieldName, dataJson[component.Field].ToString(), component.CsType));
                    }
                    else
                    {
                        //如果传过来的值为""，需要判断下字符类型
                        if (dataJson[component.Field] != null && dataJson[component.Field].ToString() == "" && component.CsType == "string")
                        {
                            res.DbParameter.Add(GetMyDbParameter(component.ComponentProps.FieldName, dataJson[component.Field].ToString(), component.CsType));
                        }
                        //只更新上传的数据，表单其他的不更新
                        //res.DbParameter.Add(new SugarParameter(component.ComponentProps.FieldName, null));
                    }

                    if (component.ComponentProps.FieldName == pkey)
                    {
                        isPkey = true;
                        if (res.DbParameter[res.DbParameter.Count - 1].Value == null)
                        {
                            res.DbParameter[res.DbParameter.Count - 1].Value = pkeyValue;
                        }
                    }
                }
            }

            if (!isPkey)
            {
                res.DbParameter.Add(new SugarParameter(pkey, pkeyValue));
            }

            return res;
        }

        private static FormDbTable GetDeleteSql(FormDbTableInfo table, JObject dataJson, FormComponentModel component)
        {
            FormDbTable res = new FormDbTable();
            res.DbParameter = new List<SugarParameter>();
            res.TableName = table.Name;
            res.ExecuteType = ExecuteType.Delete;

            res.DbParameter.Add(GetMyDbParameter(table.Field.ToUpper(), dataJson[component.Prop].ToString(), component.CsType));
            return res;
        }
        private static FormDbTable GetDeleteSqlNew(FormDbTableInfo table, JObject dataJson, FormComponentNewModel component)
        {
            FormDbTable res = new FormDbTable();
            res.DbParameter = new List<SugarParameter>();
            res.TableName = table.Name;
            res.ExecuteType = ExecuteType.Delete;

            res.DbParameter.Add(GetMyDbParameter(table.Field, dataJson[component.Field].ToString(), component.CsType));
            return res;
        }
        private static void GetDeleteSqlNew(List<FormDbTable> list, List<FormDbTableInfo> db, string pTableName)
        {
            var tables = db.FindAll(t => t.RelationName == pTableName);
            foreach (var table in tables)
            {
                var sql = new StringBuilder();
                sql.Append($" DELETE FROM {table.Name} WHERE {table.Field}= @keyValue ");
                list.Add(new FormDbTable
                {
                    Sql = sql.ToString(),
                    RelationField = table.RelationField,
                    RelationName = table.RelationName,
                    TableName = table.Name
                });
                GetDeleteSqlNew(list, db, table.Name);
            }
        }
        /// <summary>
        /// 获取删除语句
        /// </summary>
        /// <param name="formSchemeModel"></param>
        /// <param name="pkey"></param>
        /// <returns></returns>
        public static List<FormDbTable> GetDeleteSqlNew(FormSchemeNewModel formSchemeModel, string pkey)
        {
            List<FormDbTable> list = new List<FormDbTable>();
            // 获取主子表
            FormDbTableInfo mainTable = formSchemeModel.Db.Find(t => t.Type == "main");

            var sql = new StringBuilder();
            //sql.Append($" DELETE FROM {mainTable.Name} WHERE {pkey}= @keyValue " + "{LEARUN_SASSID_NOTT}");
            sql.Append($" DELETE FROM {mainTable.Name} WHERE \"{pkey}\"= @keyValue ");
            list.Add(new FormDbTable
            {
                Sql = sql.ToString(),
                TableName = mainTable.Name
            });
            GetDeleteSqlNew(list, formSchemeModel.Db, mainTable.Name);
            return list;
        }
        /// <summary>
        /// 获取新增sql语句
        /// </summary>
        /// <param name="table">表</param>
        /// <param name="tableMap">组件集合</param>
        /// <param name="dataJson">表单数据</param>
        /// <returns></returns>
        private static FormDbTable GetInsertSql(FormDbTableInfo table, Dictionary<string, List<FormComponentModel>> tableMap, JObject dataJson)
        {
            FormDbTable res = new FormDbTable();
            res.DbParameter = new List<SugarParameter>();
            res.TableName = table.Name;
            res.ExecuteType = ExecuteType.Insert;
            foreach (var component in tableMap[table.Name])
            {
                if (!string.IsNullOrEmpty(component.Field) && !dataJson[component.Prop].IsEmpty())
                {
                    if (res.DbParameter.Find(t => t.ParameterName == component.Field) != null)
                    {
                        continue;
                    }
                    res.DbParameter.Add(GetMyDbParameter(component.Field, dataJson[component.Prop].ToString(), component.CsType));
                }
            }
            return res;
        }
        /// <summary>
        /// 获取新增sql语句
        /// </summary>
        /// <param name="table">表</param>
        /// <param name="tableMap">组件集合</param>
        /// <param name="dataJson">表单数据</param>
        /// <returns></returns>
        private static FormDbTable GetInsertSqlNew(FormDbTableInfo table, Dictionary<string, List<FormComponentNewModel>> tableMap, JObject dataJson)
        {
            FormDbTable res = new FormDbTable();
            res.DbParameter = new List<SugarParameter>();
            res.TableName = table.Name;
            res.ExecuteType = ExecuteType.Insert;
            foreach (var component in tableMap[table.Name])
            {
                if (!string.IsNullOrEmpty(component.Field) && !dataJson[component.Field].IsEmpty())
                {
                    if (res.DbParameter.Find(t => t.ParameterName == component.Field) != null)
                    {
                        continue;
                    }
                    res.DbParameter.Add(GetMyDbParameter(component.ComponentProps.FieldName, dataJson[component.Field].ToString(), component.CsType));
                }
            }
            return res;
        }

        private static string GetQueryColumns(List<FormDbTableInfo> db, Dictionary<string, List<FormComponentModel>> tableMap, string pTableName)
        {
            StringBuilder str = new StringBuilder();
            var tables = db.FindAll(t => t.RelationName == pTableName && tableMap.ContainsKey(t.Name));
            foreach (var table in tables)
            {
                int tableIndex = db.FindIndex(t => t.Name == table.Name);

                var components = tableMap[table.Name];

                foreach (var component in components)
                {
                    if (!string.IsNullOrEmpty(component.Field))
                    {
                        str.Append(string.Format("t{0}.{1} as {1}{0},", tableIndex, component.Field));
                    }
                }

                str.Append(GetQueryColumns(db, tableMap, table.Name));
            }

            return str.ToString();
        }
        private static string GetQueryColumnsNew(List<FormDbTableInfo> db, Dictionary<string, List<FormComponentNewModel>> tableMap, string pTableName)
        {
            StringBuilder str = new StringBuilder();
            var tables = db.FindAll(t => t.RelationName == pTableName && tableMap.ContainsKey(t.Name));
            foreach (var table in tables)
            {
                //只查主表信息
                if (table.Type == "main")
                {
                    int tableIndex = db.FindIndex(t => t.Name == table.Name);

                    var components = tableMap[table.Name];

                    foreach (var component in components)
                    {
                        if (!string.IsNullOrEmpty(component.ComponentProps.DataTable))
                        {
                            //str.Append(string.Format("t{0}.\"{1}\" as \"{1}{0}\",", tableIndex, component.ComponentProps.FieldName));
                            str.Append(string.Format("t{0}.\"{1}\" as \"{2}\",", tableIndex, component.ComponentProps.FieldName, component.Field));
                        }
                    }

                    str.Append(GetQueryColumnsNew(db, tableMap, table.Name));
                }
                //int tableIndex = db.FindIndex(t => t.Name == table.Name);

                //var components = tableMap[table.Name];

                //foreach (var component in components)
                //{
                //    if (!string.IsNullOrEmpty(component.ComponentProps.DataTable))
                //    {
                //        //str.Append(string.Format("t{0}.\"{1}\" as \"{1}{0}\",", tableIndex, component.ComponentProps.FieldName));
                //        str.Append(string.Format("t{0}.\"{1}\" as \"{2}\",", tableIndex, component.ComponentProps.FieldName, component.Field));
                //    }
                //}

                //str.Append(GetQueryColumnsNew(db, tableMap, table.Name));
            }

            return str.ToString();
        }

        private static string GetQueryLeftTable(List<FormDbTableInfo> db, Dictionary<string, List<FormComponentModel>> tableMap, string pTableName, int pTableIndex)
        {
            StringBuilder str = new StringBuilder();
            var tables = db.FindAll(t => t.RelationName == pTableName && tableMap.ContainsKey(t.Name));
            foreach (var table in tables)
            {

                int tableIndex = db.FindIndex(t => t.Name == table.Name);
                str.Append(string.Format(" LEFT JOIN {0} t{1} ON t{1}.{2} = t{3}.{4} ", table.Name, tableIndex, table.Field, pTableIndex, table.RelationField));
                str.Append(GetQueryLeftTable(db, tableMap, table.Name, tableIndex));
            }

            return str.ToString();
        }
        private static string GetQueryLeftTableNew(List<FormDbTableInfo> db, Dictionary<string, List<FormComponentNewModel>> tableMap, string pTableName, int pTableIndex)
        {
            StringBuilder str = new StringBuilder();
            var tables = db.FindAll(t => t.RelationName == pTableName && tableMap.ContainsKey(t.Name));
            foreach (var table in tables)
            {

                int tableIndex = db.FindIndex(t => t.Name == table.Name);
                str.Append(string.Format(" LEFT JOIN \"{0}\" t{1} ON t{1}.\"{2}\" = t{3}.\"{4}\" ", table.Name, tableIndex, table.Field, pTableIndex, table.RelationField));
                str.Append(GetQueryLeftTableNew(db, tableMap, table.Name, tableIndex));
            }

            return str.ToString();
        }
        public static bool IsEmpty(this object value)
        {
            if (value != null && !string.IsNullOrEmpty(value.ToString()))
            {
                return false;
            }

            return true;
        }
        public static SugarParameter GetMyDbParameter(string parameterName, string value, string csType)
        {
            string _csType = csType;
            switch (csType)
            {
                case "int":
                    _csType = "System.Int32";
                    break;
                case "long":
                    _csType = "System.Int64";
                    break;
                case "short":
                    _csType = "System.Int16";
                    break;
                case "bool":
                    _csType = "System.Boolean";
                    break;
                case "float":
                    _csType = "System.Single";
                    break;
                case "text":
                    _csType = "";
                    break;
                default:
                    if (!string.IsNullOrEmpty(csType))
                    {
                        _csType = $"System.{StringExtension.FirstUpper(csType)}";
                    }
                    break;
            }
            if (_csType == "System.TimeSpan")
            {
                return new SugarParameter(parameterName, TimeSpan.Parse(value));
            }
            else if (_csType == "System.Guid")
            {
                return new SugarParameter(parameterName, Guid.Parse(value));
            }
            else
            {
                return new SugarParameter(parameterName, string.IsNullOrEmpty(_csType) ? value : Convert.ChangeType(value, System.Type.GetType(_csType)));
            }
        }
        /// <summary>
        /// 获取表单查询方法
        /// </summary>
        /// <param name="formSchemeModel"></param>
        /// <param name="pkey"></param>
        /// <returns></returns>
        public static List<FormDbTable> GetQuery(FormSchemeModel formSchemeModel, string pkey)
        {
            List<FormDbTable> list = new List<FormDbTable>();

            StringBuilder str = new StringBuilder();
            FormDbTableInfo mainTable = formSchemeModel.Db.Find(t => t.Type == "main");
            // 对表组件按表进行分类
            Dictionary<string, List<FormComponentModel>> tableMap = new Dictionary<string, List<FormComponentModel>>();
            Dictionary<string, FormComponentModel> girdTableMap = new Dictionary<string, FormComponentModel>();

            foreach (var tab in formSchemeModel.FormInfo.TabList)
            {
                foreach (var component in tab.Components)
                {
                    if (string.IsNullOrEmpty(component.Table))
                    {
                        continue;
                    }
                    if (!tableMap.ContainsKey(component.Table))
                    {
                        tableMap[component.Table] = new List<FormComponentModel>();
                    }

                    if (component.Type == "gridtable")
                    {
                        tableMap[component.Table].AddRange(component.children);
                        girdTableMap.Add(component.Table, component);
                    }
                    else
                    {
                        if (component.Prop == pkey)
                        {
                            pkey = component.Field;
                        }
                        tableMap[component.Table].Add(component);
                    }
                }
            }


            str.Append("SELECT ");
            var mainComponents = tableMap[mainTable.Name];
            foreach (var component in mainComponents)
            {
                str.Append($" {component.Field},");
            }
            str.Append($"FROM {mainTable.Name} t where {pkey} = @keyValue " + "{LEARUN_SASSID}");

            list.Add(new FormDbTable
            {
                Sql = str.ToString().Replace(",FROM", " FROM"),
                TableName = mainTable.Name
            });


            GetQuery(list, formSchemeModel.Db, mainTable.Name, tableMap, girdTableMap);

            return list;
        }
        /// <summary>
        /// 获取表单查询方法
        /// </summary>
        /// <param name="formSchemeModel"></param>
        /// <param name="pkey"></param>
        /// <returns></returns>
        public static List<FormDbTable> GetQueryNew(FormSchemeNewModel formSchemeModel, string pkey)
        {
            List<FormDbTable> list = new List<FormDbTable>();

            StringBuilder str = new StringBuilder();
            // 主数据库
            FormDbTableInfo mainTable = formSchemeModel.Db.Find(t => t.Type == "main");
            // 对表组件按表进行分类
            Dictionary<string, List<FormComponentNewModel>> tableMap = new Dictionary<string, List<FormComponentNewModel>>();
            Dictionary<string, FormComponentNewModel> girdTableMap = new Dictionary<string, FormComponentNewModel>();

            foreach (var tab in formSchemeModel.FormInfo.TabList)
            {
                foreach (var component in tab.Schemas)
                {
                    if (component == null || component.ComponentProps == null || string.IsNullOrEmpty(component.ComponentProps.DataTable))
                    {
                        //if(component.Component == "Grid1")
                        //{
                        //    foreach (var column in component.Columns)
                        //    {
                        //        tableMap[component.ComponentProps.DataTable].AddRange(column.children);
                        //    }
                        //}
                        //else
                        //{
                        //    continue;
                        //}
                        continue;
                    }
                    if (!tableMap.ContainsKey(component.ComponentProps.DataTable))
                    {
                        tableMap[component.ComponentProps.DataTable] = new List<FormComponentNewModel>();
                    }

                    if (component.Component == "Grid")
                    {
                        //tableMap[component.ComponentProps.DataTable].AddRange(component.children);
                        //girdTableMap.Add(component.ComponentProps.DataTable, component);
                        foreach (var column in component.Columns)
                        {
                            tableMap[component.ComponentProps.DataTable].AddRange(column.children);
                            if (girdTableMap.ContainsKey(component.ComponentProps.DataTable))
                            {
                                //girdTableMap[component.ComponentProps.DataTable].AddRange(column);
                            }
                            else
                            {
                                girdTableMap.Add(component.ComponentProps.DataTable, column);
                            }

                        }
                    }
                    else
                    {
                        if (component.ComponentProps.FieldName == pkey)
                        {
                            pkey = component.ComponentProps.FieldName;
                        }
                        //添加关联键
                        tableMap[component.ComponentProps.DataTable].Add(component);
                    }
                }
            }




            str.Append("SELECT ");
            var mainComponents = tableMap[mainTable.Name];
            foreach (var component in mainComponents)
            {
                if (component.ComponentProps.FieldName == "geom")
                {
                    str.Append($" st_astext(\"{component.ComponentProps.FieldName}\")  as " + component.Field + ",");
                }
                else
                {
                    str.Append($" \"{component.ComponentProps.FieldName}\" as " + component.Field + ",");
                }
                //str.Append($" \"{component.ComponentProps.FieldName}\" as " + component.Field + ",");

            }
            //pkey = mainComponents.Where(t => t.ComponentProps.FieldName == pkey).First()?.Field;
            pkey = mainComponents.Where(t => t.ComponentProps.FieldName == pkey).First()?.ComponentProps.FieldName;
            //str.Append($"FROM \"{mainTable.Name}\" t where \"{pkey}\" = @keyValue " + "{LEARUN_SASSID}");
            str.Append($"FROM \"{mainTable.Name}\" t where \"{pkey}\" = @keyValue ");

            list.Add(new FormDbTable
            {
                Sql = str.ToString().Replace(",FROM", " FROM"),
                TableName = mainTable.Name
            });


            GetQueryNew(list, formSchemeModel.Db, mainTable.Name, tableMap, girdTableMap);

            return list;
        }

        private static void GetQuery(List<FormDbTable> list, List<FormDbTableInfo> db, string pTableName, Dictionary<string, List<FormComponentModel>> tableMap, Dictionary<string, FormComponentModel> girdTableMap)
        {
            var tables = db.FindAll(t => t.RelationName == pTableName);
            foreach (var table in tables)
            {
                StringBuilder str = new StringBuilder();
                str.Append("SELECT ");
                var components = tableMap[table.Name];
                foreach (var column in components)
                {
                    str.Append($" {column.Field},");
                }
                str.Append($"FROM {table.Name} where {table.Field} = @keyValue ");

                if (girdTableMap.ContainsKey(table.Name))
                {
                    var componentModel = girdTableMap[table.Name];
                    if (!string.IsNullOrEmpty(componentModel.OrderId))
                    {
                        if (componentModel.isDESC)
                        {
                            str.Append($" order by {componentModel.OrderId} DESC ");
                        }
                        else
                        {
                            str.Append($" order by {componentModel.OrderId} ");
                        }
                    }
                }

                list.Add(new FormDbTable
                {
                    Sql = str.ToString().Replace(",FROM", " FROM"),
                    RelationField = table.RelationField,
                    RelationName = table.RelationName,
                    TableName = table.Name
                });

                GetQuery(list, db, table.Name, tableMap, girdTableMap);
            }


        }
        private static void GetQueryNew(List<FormDbTable> list, List<FormDbTableInfo> db, string pTableName, Dictionary<string, List<FormComponentNewModel>> tableMap, Dictionary<string, FormComponentNewModel> girdTableMap)
        {
            var tables = db.FindAll(t => t.RelationName == pTableName);
            foreach (var table in tables)
            {
                StringBuilder str = new StringBuilder();
                str.Append("SELECT ");
                var components = tableMap[table.Name];
                foreach (var column in components)
                {
                    //新加
                    var field = components.Where(t => t.ComponentProps.FieldName == column.ComponentProps.FieldName).First();
                    if (field.ComponentProps.FieldName == "geom")
                    {
                        str.Append($" st_astext(\"{column.ComponentProps.FieldName}\")  as " + field.Field + ",");
                    }
                    else
                    {
                        str.Append($" \"{column.ComponentProps.FieldName}\" as " + field.Field + ",");
                    }
                    //str.Append($" \"{column.ComponentProps.FieldName}\" as " + field.Field + ",");
                }
                // var ffield = components.Where(t => t.ComponentProps.FieldName == table.Field).First()?.Field;
                //str.Append($"FROM \"{table.Name}\" where \"{table.Field}\" = @keyValue ");
                str.Append($"FROM \"{table.Name}\" where \"{table.Field}\" = @keyValue ");

                if (girdTableMap.ContainsKey(table.Name))
                {
                    var componentModel = girdTableMap[table.Name];
                    if (!string.IsNullOrEmpty(componentModel.OrderId))
                    {
                        if (componentModel.isDESC)
                        {
                            str.Append($" order by \"{componentModel.OrderId}\" DESC ");
                        }
                        else
                        {
                            str.Append($" order by \"{componentModel.OrderId}\" ");
                        }
                    }
                }

                list.Add(new FormDbTable
                {
                    Sql = str.ToString().Replace(",FROM", " FROM"),
                    //RelationField = table.RelationField,
                    RelationField = table.Field,
                    RelationName = table.RelationName,
                    TableName = table.Name
                });

                GetQueryNew(list, db, table.Name, tableMap, girdTableMap);
            }


        }
        public static string GetCsType(DbType dbtype, string dbType)
        {
            string csType = "string";
            switch (dbType.ToLower())
            {
                case "varchar":
                    csType = "string";
                    break;
                case "varchar2":
                    csType = "string";
                    break;
                case "char":
                case "nchar":
                case "nvarchar":
                case "nvarchar2":
                    csType = "string";
                    break;
                case "text":
                    csType = "string";
                    break;
                case "ntext":
                case "longtext":
                case "clob":
                case "nclog":
                    csType = "text";
                    break;
                case "int2":
                    csType = "int";
                    break;
                case "integer":
                    csType = "int";
                    break;
                case "int4":
                    csType = "int";
                    break;
                case "mediumint":// mysql
                case "year":
                    csType = "int";
                    break;
                case "bigint":
                    csType = "long";
                    break;
                case "smallint":
                    csType = "short";
                    break;
                case "bit":
                    csType = "bool";
                    break;
                case "tinyint":
                    csType = "byte";
                    break;
                case "decimal":
                    csType = "decimal";
                    break;
                case "numeric":
                    csType = "decimal";
                    break;
                case "number":
                    csType = "int";
                    break;
                case "number(8,2)":
                    csType = "double";
                    break;
                case "money":
                case "bool":
                    csType = "bool";
                    break;
                case "smallmoney":
                    csType = "decimal";
                    break;
                case "real":
                case "double":
                    if (dbtype == DbType.MySql)
                    {
                        csType = "double";
                    }
                    else
                    {
                        csType = "float";
                    }
                    break;
                case "float":
                    if (dbtype == DbType.MySql)
                    {
                        csType = "float";
                    }
                    else
                    {
                        csType = "double";
                    }
                    break;
                case "float4":
                    csType = "double";
                    break;
                case "float8":
                    csType = "double";
                    break;
                case "date":
                    csType = "DateTime";
                    break;
                case "datetime":
                    csType = "DateTime";
                    break;
                case "datetime2":
                    csType = "DateTime";
                    break;
                case "smalldatetime":
                    csType = "DateTime";
                    break;
                case "datetimeoffset":
                    csType = "DateTimeOffset";
                    break;
                case "time":
                    //csType = "TimeSpan";
                    //break;
                    csType = "DateTime";
                    break;
                case "timestamp":
                    if (dbtype == DbType.MySql)
                    {
                        csType = "DateTime";

                    }
                    else
                    {
                        csType = "DateTime";
                    }
                    break;
                case "binary":
                case "varbinary":
                case "image":

                case "tinyblob":
                case "blob":
                case "mediumblob":
                case "longblob":
                    csType = "byte[]";
                    break;
                case "uniqueidentifier":
                    csType = "Guid";
                    break;
                case "variant":
                    csType = "Object";
                    break;
                case "timestamp without time zone":
                    csType = "TimeSpan";
                    break;
                // mysql
                case "set":
                case "enum":
                    csType = "Enum";
                    break;
                case "point":
                case "linestring":
                case "polygon":
                case "geometry":
                case "multipoint":
                case "multilinestring":
                case "multipolygon":
                //case "geometrycollection":
                //    csType = "MygisGeometry";
                case "geometrycollection":
                    csType = "string";
                    break;
            }

            return csType;
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="source"></param>
        /// <returns></returns>
        public static string SqlFilters(string source)
        {
            if (string.IsNullOrEmpty(source))
            {
                return source;
            }

            //去除执行SQL语句的命令关键字
            // source = Regex.Replace(source, "select", "", RegexOptions.IgnoreCase);
            source = Regex.Replace(source, "insert", "", RegexOptions.IgnoreCase);
            source = Regex.Replace(source, "update", "", RegexOptions.IgnoreCase);
            source = Regex.Replace(source, "delete", "", RegexOptions.IgnoreCase);
            source = Regex.Replace(source, "drop", "", RegexOptions.IgnoreCase);
            source = Regex.Replace(source, "truncate", "", RegexOptions.IgnoreCase);
            source = Regex.Replace(source, "declare", "", RegexOptions.IgnoreCase);
            source = Regex.Replace(source, "xp_cmdshell", "", RegexOptions.IgnoreCase);
            source = Regex.Replace(source, "/add", "", RegexOptions.IgnoreCase);
            source = Regex.Replace(source, "net user", "", RegexOptions.IgnoreCase);
            //去除执行存储过程的命令关键字 
            source = Regex.Replace(source, "exec", "", RegexOptions.IgnoreCase);
            source = Regex.Replace(source, "execute", "", RegexOptions.IgnoreCase);
            //去除系统存储过程或扩展存储过程关键字
            source = Regex.Replace(source, "xp_", "x p_", RegexOptions.IgnoreCase);
            source = Regex.Replace(source, "sp_", "s p_", RegexOptions.IgnoreCase);
            //防止16进制注入
            source = Regex.Replace(source, "0x", "0 x", RegexOptions.IgnoreCase);
            return source;
        }
    }
}
