using APT.BaseData.Domain.ApiModel;
using APT.BaseData.Domain.Entities;
using APT.BaseData.Domain.Entities.FM;
using APT.BaseData.Domain.Entities.PF;
using APT.BaseData.Domain.Enums;
using APT.BaseData.Domain.IServices;
using APT.Infrastructure.Core;
using APT.Utility;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Linq.Expressions;
using System.Reflection;
using System.Text;
using APT.Infrastructure.Api;
namespace APT.BaseData.Services.DomainServices
{
    public class PFFormService : CommonService, IPFFormService
    {
        /// <summary>数据库关键字</summary>
        private readonly string[] DB_KEYWORDS = new string[] { "ORDER", "DESC", "TEXT", "GROUP", "ASC", "MIN", "MAX", "SUM", "DATE", "DATA", "DEC", "CASE", "CAST" };

        public PFFormService(IRepository repository)
            : base(repository)
        {
        }

        /// <summary>
        /// 获取实体语法
        /// </summary>
        /// <param name="dataBaseType"></param>
        /// <param name="obj"></param>
        /// <param name="list"></param>
        /// <param name="dstOrgId"></param>
        /// <param name="sqlType">获取语法类型 0-update 和insert  1-仅获取update 2-仅获取insert</param>
        public void GetSqlByObject(DataBaseType dataBaseType, object obj, List<string> list, string dstOrgId, int sqlType)
        {
            this.DoGetSqlByEntity(dataBaseType, obj, list, dstOrgId, sqlType);
        }

        /// <summary>
        /// 创建表单设置版本信息
        /// </summary>
        /// <param name="configEnum">当前设置类型</param>
        /// <param name="ids">ID列表 逗号隔开</param>
        public void CreateFormConfigVersion(PFFormConfigVersionEnum configEnum, string ids)
        {

            //List<Guid> arrays = string.IsNullOrEmpty(ids) ? null : ids.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries).Select(t => new Guid(t)).ToList();
            //if (arrays == null || !arrays.Any()) return;

            //Guid? orgId = null;

            //List<string> cookies = new List<string>();

            //switch (configEnum)
            //{
            //    case PFFormConfigVersionEnum.Form:
            //        var forms = this.GetEntities<T_PF_FORM>(t => arrays.Contains(t.ID), new BaseFilter()).ToList();
            //        foreach (var form in forms)
            //        {
            //            orgId = form.ORG_ID;
            //            cookies.Add(string.Format("form_config_form_{0}_{1}", form.CODE, form.ORG_ID));
            //        }
            //        break;
            //    case PFFormConfigVersionEnum.Btn:
            //        var btns = this.GetEntities<T_PF_BTN>(t => arrays.Contains(t.ID), new BaseFilter()).ToList();
            //        foreach (var btn in btns)
            //        {
            //            orgId = btn.ORG_ID;
            //            if (btn.PAGE_CUSTOM_FORM_ID != null)
            //            {
            //                var form = this.GetEntity<T_PF_FORM>(btn.PAGE_CUSTOM_FORM_ID.ToString());
            //                if (form != null)
            //                    cookies.Add(string.Format("form_config_custom_{0}_{1}", form.CODE, btn.ORG_ID));
            //            }
            //            else if (btn.PAGE_TABLE_ID != null)
            //            {
            //                cookies.Add(string.Format("form_config_tablebase_{0}_{1}", btn.PAGE_TABLE_ID, btn.ORG_ID));
            //            }
            //            else if (btn.PAGE_EDIT_ID != null)
            //            {

            //                var pageEdit = this.GetEntity<T_PF_PAGE_EDIT>(btn.PAGE_EDIT_ID.ToString(), new string[] { "Nav_PageForm" });
            //                if (pageEdit != null)
            //                {
            //                    if (pageEdit.Nav_PageForm != null)
            //                        cookies.Add(string.Format("form_config_edit_{0}_{1}", pageEdit.Nav_PageForm.CODE, pageEdit.ORG_ID));
            //                }
            //                else
            //                {
            //                    var pageTree = this.GetEntity<T_PF_PAGE_TREE>(btn.PAGE_EDIT_ID.ToString(), new string[] { "Nav_PageForm" });
            //                    if (pageTree.Nav_PageForm != null)
            //                        cookies.Add(string.Format("form_config_tree_{0}_{1}", pageTree.Nav_PageForm.CODE, pageTree.ORG_ID));
            //                }
            //            }
            //        }
            //        break;
            //    case PFFormConfigVersionEnum.Column:
            //        var columns = this.GetEntities<T_PF_COLUMN>(t => arrays.Contains(t.ID), new BaseFilter()).ToList();
            //        foreach (var column in columns)
            //        {

            //            orgId = column.ORG_ID;
            //            cookies.Add(string.Format("form_config_tablebase_{0}_{1}", column.PAGE_TABLE_ID, column.ORG_ID));

            //        }
            //        break;
            //    case PFFormConfigVersionEnum.EditColumn:
            //        var editColumns = this.GetEntities<T_PF_EDIT_COLUMN>(t => arrays.Contains(t.ID), new BaseFilter(), new string[] { "Nav_PageEdit.Nav_PageForm" }).ToList();
            //        foreach (var editColumn in editColumns)
            //        {

            //            orgId = editColumn.ORG_ID;
            //            if (editColumn.Nav_PageEdit != null && editColumn.Nav_PageEdit.Nav_PageForm != null)
            //                cookies.Add(string.Format("form_config_edit_{0}_{1}", editColumn.Nav_PageEdit.Nav_PageForm.CODE, editColumn.ORG_ID));

            //        }
            //        break;
            //    case PFFormConfigVersionEnum.EditColumnFillMap:
            //        var editColumnFillMaps = this.GetEntities<T_PF_EDIT_COLUMN_FILL_MAP>(t => arrays.Contains(t.ID), new BaseFilter(),
            //            new string[] { "Nav_EditColumn.Nav_PageEdit.Nav_PageForm" }).ToList();
            //        foreach (var editColumnFillMap in editColumnFillMaps)
            //        {

            //            orgId = editColumnFillMap.ORG_ID;
            //            if (editColumnFillMap.Nav_EditColumn != null && editColumnFillMap.Nav_EditColumn.Nav_PageEdit != null
            //                && editColumnFillMap.Nav_EditColumn.Nav_PageEdit.Nav_PageForm != null)
            //                cookies.Add(string.Format("form_config_edit_{0}_{1}",
            //                    editColumnFillMap.Nav_EditColumn.Nav_PageEdit.Nav_PageForm.CODE, editColumnFillMap.ORG_ID));

            //        }
            //        break;
            //    case PFFormConfigVersionEnum.EditColumnFillMapDetail:
            //        var editColumnFillMapDetails = this.GetEntities<T_PF_EDIT_COLUMN_FILL_MAP_D>(t => arrays.Contains(t.ID),
            //            new BaseFilter(), new string[] { "Nav_EditColumnFillMap.Nav_EditColumn.Nav_PageEdit.Nav_PageForm" }).ToList();
            //        foreach (var editColumnFillMapDetail in editColumnFillMapDetails)
            //        {

            //            orgId = editColumnFillMapDetail.ORG_ID;
            //            if (editColumnFillMapDetail.Nav_EditColumnFillMap != null && editColumnFillMapDetail.Nav_EditColumnFillMap.Nav_EditColumn != null
            //                && editColumnFillMapDetail.Nav_EditColumnFillMap.Nav_EditColumn.Nav_PageEdit != null
            //                && editColumnFillMapDetail.Nav_EditColumnFillMap.Nav_EditColumn.Nav_PageEdit.Nav_PageForm != null)
            //                cookies.Add(string.Format("form_config_edit_{0}_{1}",
            //                    editColumnFillMapDetail.Nav_EditColumnFillMap.Nav_EditColumn.Nav_PageEdit.Nav_PageForm.CODE,
            //                    editColumnFillMapDetail.ORG_ID));

            //        }
            //        break;
            //    case PFFormConfigVersionEnum.EditColumnFilter:
            //        var editColumnFilters = this.GetEntities<T_PF_EDIT_COLUMN_FILTER>(t => arrays.Contains(t.ID),
            //                    new BaseFilter(), new string[] { "Nav_EditColumn.Nav_PageEdit.Nav_PageForm" }).ToList();
            //        foreach (var editColumnFilter in editColumnFilters)
            //        {

            //            orgId = editColumnFilter.ORG_ID;
            //            if (editColumnFilter.Nav_EditColumn != null && editColumnFilter.Nav_EditColumn.Nav_PageEdit != null
            //                && editColumnFilter.Nav_EditColumn.Nav_PageEdit.Nav_PageForm != null)
            //                cookies.Add(string.Format("form_config_edit_{0}_{1}",
            //                    editColumnFilter.Nav_EditColumn.Nav_PageEdit.Nav_PageForm.CODE, editColumnFilter.ORG_ID));

            //        }
            //        break;
            //    case PFFormConfigVersionEnum.PageCustom:
            //        var pageCustoms = this.GetEntities<T_PF_PAGE_CUSTOM>(t => arrays.Contains(t.ID),
            //                        new BaseFilter(), new string[] { "Nav_PageForm" }).ToList();
            //        foreach (var pageCustom in pageCustoms)
            //        {

            //            orgId = pageCustom.ORG_ID;
            //            if (pageCustom.Nav_PageForm != null)
            //                cookies.Add(string.Format("form_config_custom_{0}_{1}", pageCustom.Nav_PageForm.CODE, pageCustom.ORG_ID));

            //        }
            //        break;
            //    case PFFormConfigVersionEnum.PageEdit:
            //        var pageEdits = this.GetEntities<T_PF_PAGE_EDIT>(t => arrays.Contains(t.ID),
            //                new BaseFilter(), new string[] { "Nav_PageForm" }).ToList();
            //        foreach (var pageEdit in pageEdits)
            //        {

            //            orgId = pageEdit.ORG_ID;
            //            if (pageEdit.Nav_PageForm != null)
            //                cookies.Add(string.Format("form_config_edit_{0}_{1}", pageEdit.Nav_PageForm.CODE, pageEdit.ORG_ID));

            //        }
            //        break;
            //    case PFFormConfigVersionEnum.PageTable:
            //        var pageTables = this.GetEntities<T_PF_PAGE_TABLE>(t => arrays.Contains(t.ID),
            //                        new BaseFilter(), new string[] { "Nav_PageForm" }).ToList();
            //        foreach (var pageTable in pageTables)
            //        {

            //            orgId = pageTable.ORG_ID;
            //            if (pageTable.Nav_PageForm != null)
            //            {
            //                cookies.Add(string.Format("form_config_table_{0}_{1}_{2}", pageTable.Nav_PageForm.CODE, pageTable.ORG_ID, (pageTable.PARENT_ID == null ? "" : pageTable.PARENT_ID.ToString())));
            //                cookies.Add(string.Format("form_config_table_{0}_{1}_{2}_{3}", pageTable.Nav_PageForm.CODE, pageTable.ORG_ID, (pageTable.PARENT_ID == null ? "" : pageTable.PARENT_ID.ToString()),
            //                    pageTable.ID));
            //            }
            //            cookies.Add(string.Format("form_config_tablebase_{0}_{1}", pageTable.ID, pageTable.ORG_ID));

            //        }
            //        break;
            //    case PFFormConfigVersionEnum.PageTree:
            //        var pageTrees = this.GetEntities<T_PF_PAGE_TREE>(t => arrays.Contains(t.ID),
            //             new BaseFilter(), new string[] { "Nav_PageForm" }).ToList();
            //        foreach (var pageTree in pageTrees)
            //        {

            //            orgId = pageTree.ORG_ID;
            //            if (pageTree.Nav_PageForm != null)
            //                cookies.Add(string.Format("form_config_tree_{0}_{1}", pageTree.Nav_PageForm.CODE, pageTree.ORG_ID));

            //        }
            //        break;
            //    case PFFormConfigVersionEnum.Query:
            //        var querys = this.GetEntities<T_PF_QUERY>(t => arrays.Contains(t.ID),
            //            new BaseFilter(), new string[] { "Nav_PageForm" }).ToList();
            //        foreach (var query in querys)
            //        {

            //            orgId = query.ORG_ID;
            //            cookies.Add(string.Format("form_config_search_{0}_{1}", query.PAGE_FORM_ID, query.ORG_ID));
            //            if (query.Nav_PageForm != null)
            //                cookies.Add(string.Format("form_config_search_{0}_{1}", query.Nav_PageForm.CODE, query.ORG_ID));

            //        }
            //        break;
            //    case PFFormConfigVersionEnum.TableParams:
            //        var tableParams = this.GetEntities<T_PF_TABLE_PARAMS>(t => arrays.Contains(t.ID), new BaseFilter()).ToList();
            //        foreach (var tableParam in tableParams)
            //        {
            //            orgId = tableParam.ORG_ID;
            //            cookies.Add(string.Format("form_config_tablebase_{0}_{1}", tableParam.PAGE_TABLE_ID, tableParam.ORG_ID));

            //        }
            //        break;
            //    case PFFormConfigVersionEnum.TreeColumn:
            //        var treeColumns = this.GetEntities<T_PF_TREE_COLUMN>(t => arrays.Contains(t.ID),
            //            new BaseFilter(), new string[] { "Nav_PageTree.Nav_PageForm" }).ToList();
            //        foreach (var treeColumn in treeColumns)
            //        {

            //            orgId = treeColumn.ORG_ID;
            //            if (treeColumn.Nav_PageTree != null && treeColumn.Nav_PageTree.Nav_PageForm != null)
            //                cookies.Add(string.Format("form_config_tree_{0}_{1}", treeColumn.Nav_PageTree.Nav_PageForm.CODE, treeColumn.ORG_ID));

            //        }
            //        break;
            //    case PFFormConfigVersionEnum.PageEditPanel:
            //        var pageEditPanels = this.GetEntities<T_PF_PAGE_EDIT_PANEL>(t => arrays.Contains(t.ID),
            //                new BaseFilter(), new string[] { "Nav_PageEdit.Nav_PageForm" }).ToList();
            //        foreach (var pageEditPanel in pageEditPanels)
            //        {

            //            orgId = pageEditPanel.ORG_ID;
            //            if (pageEditPanel.Nav_PageEdit != null && pageEditPanel.Nav_PageEdit.Nav_PageForm != null)
            //                cookies.Add(string.Format("form_config_edit_{0}_{1}", pageEditPanel.Nav_PageEdit.Nav_PageForm.CODE, pageEditPanel.ORG_ID));

            //        }
            //        break;
            //    case PFFormConfigVersionEnum.PageChart:
            //        var pageCharts = this.GetEntities<T_PF_PAGE_CHART>(t => arrays.Contains(t.ID),
            //                new BaseFilter(), new string[] { "Nav_PageForm" }).ToList();
            //        foreach (var pageChart in pageCharts)
            //        {
            //            orgId = pageChart.ORG_ID;
            //            if (pageChart.Nav_PageForm != null)
            //                cookies.Add(string.Format("form_config_chart_{0}_{1}", pageChart.Nav_PageForm.CODE, pageChart.ORG_ID));
            //            cookies.Add(string.Format("form_config_chart_{0}_{1}", pageChart.PAGE_FORM_ID, pageChart.ORG_ID));
            //        }
            //        break;
            //    case PFFormConfigVersionEnum.PageChartParam:
            //        var pageChartParams = this.GetEntities<T_PF_PAGE_CHART_PARAM>(t => arrays.Contains(t.ID),
            //                new BaseFilter(), new string[] { "Nav_PageChart.Nav_PageForm" }).ToList();
            //        foreach (var pageChartParam in pageChartParams)
            //        {
            //            orgId = pageChartParam.ORG_ID;
            //            if (pageChartParam.Nav_PageChart != null && pageChartParam.Nav_PageChart.Nav_PageForm != null)
            //                cookies.Add(string.Format("form_config_chart_{0}_{1}", pageChartParam.Nav_PageChart.Nav_PageForm.CODE, pageChartParam.ORG_ID));
            //            cookies.Add(string.Format("form_config_chart_{0}_{1}", pageChartParam.Nav_PageChart.PAGE_FORM_ID, pageChartParam.ORG_ID));
            //        }
            //        break;
            //    case PFFormConfigVersionEnum.ColumnFilter:
            //        var columnFilters = this.GetEntities<T_PF_COLUMN_FILTER>(t => arrays.Contains(t.ID), new BaseFilter(), new string[] { "Nav_Column" }).ToList();
            //        foreach (var columnFilter in columnFilters)
            //        {
            //            orgId = columnFilter.ORG_ID;
            //            if (columnFilter.Nav_Column != null)
            //                cookies.Add(string.Format("form_config_tablebase_{0}_{1}", columnFilter.Nav_Column.PAGE_TABLE_ID, columnFilter.ORG_ID));
            //        }
            //        break;
            //    case PFFormConfigVersionEnum.FormQuery:
            //        var formQuerys = this.GetEntities<T_PF_FORM_QUERY>(t => arrays.Contains(t.ID),
            //            new BaseFilter(), new string[] { "Nav_PageForm" }).ToList();
            //        foreach (var formQuery in formQuerys)
            //        {

            //            orgId = formQuery.ORG_ID;
            //            cookies.Add(string.Format("form_config_search_{0}_{1}", formQuery.PAGE_FORM_ID, formQuery.ORG_ID));
            //            if (formQuery.Nav_PageForm != null)
            //                cookies.Add(string.Format("form_config_search_{0}_{1}", formQuery.Nav_PageForm.CODE, formQuery.ORG_ID));

            //        }
            //        break;
            //    case PFFormConfigVersionEnum.PageTablePanel:
            //        var pageTabePanels = this.GetEntities<T_PF_PAGE_TABLE_PANEL>(t => arrays.Contains(t.ID),
            //                new BaseFilter(), new string[] { "Nav_PageTable.Nav_PageForm" }).ToList();
            //        foreach (var pageTablePanel in pageTabePanels)
            //        {
            //            orgId = pageTablePanel.ORG_ID;
            //            cookies.Add(string.Format("form_config_tablebase_{0}_{1}", pageTablePanel.PAGE_TABLE_ID, pageTablePanel.ORG_ID));
            //        }
            //        break;
            //    case PFFormConfigVersionEnum.ParamItem:
            //        var schemeIds = this.GetEntities<T_PF_PARAM_SCHEME_DETAIL>(t => arrays.Contains(t.PARAM_ITEM_ID), new BaseFilter()).Select(t => t.PARAM_SCHEME_ID).ToList();
            //        if (schemeIds.Any())
            //            this.CreateFormConfigVersion(PFFormConfigVersionEnum.ParamScheme, string.Join(",", schemeIds));
            //        break;
            //    case PFFormConfigVersionEnum.ParamScheme:
            //        var tempPageTableIds = this.GetEntities<T_PF_PAGE_TABLE>(t => arrays.Contains(t.PARAM_SCHEME_ID ?? Guid.Empty), new BaseFilter()).Select(t => t.ID).ToList();
            //        var tempPageEditIds = this.GetEntities<T_PF_PAGE_EDIT>(t => arrays.Contains(t.PARAM_SCHEME_ID ?? Guid.Empty), new BaseFilter()).Select(t => t.ID).ToList();
            //        if (tempPageTableIds.Any())
            //            this.CreateFormConfigVersion(PFFormConfigVersionEnum.PageTable, string.Join(",", tempPageTableIds));
            //        if (tempPageEditIds.Any())
            //            this.CreateFormConfigVersion(PFFormConfigVersionEnum.PageEdit, string.Join(",", tempPageEditIds));
            //        break;

            //}
            //if (orgId != null && cookies.Count > 0)
            //{
            //    decimal version = decimal.Zero;
            //    BaseFilter filter = new BaseFilter();
            //    filter.OrgId = orgId;
            //    filter.Order = DbOrder.DESC;
            //    filter.Sort = "VERSION";
            //    filter.OrgType = FilterOrgTypeEnum.仅本组织;
            //    var lastMaxVersion = this.GetOrderEntities<T_PF_FORM_CONFIG_VERSION>(null, filter).FirstOrDefault();
            //    if (lastMaxVersion != null)
            //        version = lastMaxVersion.VERSION;
            //    version += 1;
            //    List<T_PF_FORM_CONFIG_VERSION_D> details = new List<T_PF_FORM_CONFIG_VERSION_D>();
            //    T_PF_FORM_CONFIG_VERSION formConfigVersion = new T_PF_FORM_CONFIG_VERSION();
            //    formConfigVersion.ORG_ID = orgId;
            //    formConfigVersion.VERSION = version;
            //    foreach (var item in cookies)
            //    {
            //        T_PF_FORM_CONFIG_VERSION_D formConfigVersionDetail = new T_PF_FORM_CONFIG_VERSION_D();
            //        formConfigVersionDetail.ORG_ID = orgId;
            //        formConfigVersionDetail.REMOVE_COOKIE_KEY = item;
            //        formConfigVersionDetail.FORM_CONFIG_VERSION_ID = formConfigVersion.ID;
            //        details.Add(formConfigVersionDetail);
            //    }
            //    this.AddEntity(formConfigVersion);
            //    this.BantchAddEntity(details);
            //}

        }

        /// <summary>
        /// 依据表单编号获取更新表单配置语法
        /// </summary>
        /// <param name="orgId"></param>
        /// <param name="formCodes"></param>
        /// <param name="dstOrgId"></param>
        /// <param name="dstFormConfigVersion"></param>
        /// <param name="dstDataBaseType"></param>
        /// <param name="sqlType"></param>
        /// <param name="startTime">开始时间  从此时间之后的有异动的配置信息都会获取到</param>
        /// <returns></returns>
        public List<string> GetSqlsByFormCode(Guid orgId, List<string> formCodes, string dstOrgId, decimal dstFormConfigVersion, DataBaseType dstDataBaseType, int sqlType, DateTime? startTime, bool isInitOrg)
        {
            var forms = this.GetEntities<T_PF_FORM>(t => formCodes.Contains(t.CODE), new BaseFilter(orgId, FilterOrgTypeEnum.仅本组织)).ToList();
            return GetSqlsByFormId(orgId, forms.Select(t => t.ID).ToList(), dstOrgId, dstFormConfigVersion, dstDataBaseType, sqlType, startTime, isInitOrg, 1);
        }
        /// <summary>
        /// 依据表单ID获取更新表单配置语法
        /// </summary>
        /// <param name="orgId"></param>
        /// <param name="formIds"></param>
        /// <param name="dstOrgId"></param>
        /// <param name="dstFormConfigVersion"></param>
        /// <param name="dstDataBaseType"></param>
        /// <param name="sqlType"></param>
        /// <param name="startTime">开始时间  从此时间之后的有异动的配置信息都会获取到</param>
        /// <returns></returns>
        public List<string> GetSqlsByFormId(Guid orgId, List<Guid> formIds, string dstOrgId, decimal dstFormConfigVersion, DataBaseType dstDataBaseType, int sqlType, DateTime? startTime, bool isInitOrg, int formType)
        {
            var filiterThisDep = new BaseFilter(orgId, FilterOrgTypeEnum.仅本组织);
            filiterThisDep.IgnoreDataRule = true;

            #region 生成菜单SQL
            if (formType == 2)
            {
                return GetSqlsByFormType(orgId, formIds, dstOrgId, dstFormConfigVersion, dstDataBaseType, sqlType, startTime, isInitOrg, formType);
            }
            #endregion
            #region 跑批队列SQL
            else if (formType == 3)
            {
                return GetSqlsByFormType(orgId, formIds, dstOrgId, dstFormConfigVersion, dstDataBaseType, sqlType, startTime, isInitOrg, formType);
            }
            #endregion
            #region 生成表单SQL
            else
            {
                string dstOrgIdGuidStr = dstOrgId;
                if (dstDataBaseType == DataBaseType.Oracle && !string.IsNullOrEmpty(dstOrgId))
                    dstOrgIdGuidStr = Utility.DataHelper.OracleToDotNet(dstOrgId);
                if (string.IsNullOrEmpty(dstOrgIdGuidStr))
                    dstOrgIdGuidStr = orgId.ToString();
                bool isCheckParent = formIds != null && formIds.Any();

                List<string> cookies = new List<string>();
                List<string> result = new List<string>();
                List<Guid> tempFormIds = new List<Guid>();
                var paramStartTime = DateTime.Now.AddDays(-7);
                if (startTime != null)
                    paramStartTime = startTime.Value;
                #region 系统参数模板
                Expression<Func<T_PF_PARAM_ITEM, bool>> paramItemExpression = t => isInitOrg || t.CREATE_TIME > paramStartTime
                    || t.MODIFY_TIME > paramStartTime;
                if (startTime != null)
                    paramItemExpression = paramItemExpression.Or(t => t.CREATE_TIME > startTime || t.MODIFY_TIME > startTime);


                var paramItems = this.GetEntities<T_PF_PARAM_ITEM>(paramItemExpression, filiterThisDep).ToList();
                paramItems.ForEach(t =>
                {
                    DoGetSqlByEntity(dstDataBaseType, t, result, dstOrgId, sqlType);
                });

                Expression<Func<T_PF_PARAM_SCHEME, bool>> paramSchemeExpression = t => isInitOrg || t.CREATE_TIME > paramStartTime
                    || t.MODIFY_TIME > paramStartTime;
                if (startTime != null)
                    paramSchemeExpression = paramSchemeExpression.Or(t => t.CREATE_TIME > startTime || t.MODIFY_TIME > startTime);
                var paramSchemes = this.GetEntities<T_PF_PARAM_SCHEME>(paramSchemeExpression, filiterThisDep).ToList();
                paramSchemes.ForEach(t =>
                {
                    DoGetSqlByEntity(dstDataBaseType, t, result, dstOrgId, sqlType);
                });
                Expression<Func<T_PF_PARAM_SCHEME_DETAIL, bool>> paramSchemeDetailExpression = t => isInitOrg || t.CREATE_TIME > paramStartTime
                    || t.MODIFY_TIME > paramStartTime;
                if (startTime != null)
                    paramSchemeDetailExpression = paramSchemeDetailExpression.Or(t => t.CREATE_TIME > startTime || t.MODIFY_TIME > startTime);
                var paramDetails = this.GetEntities<T_PF_PARAM_SCHEME_DETAIL>(paramSchemeDetailExpression, filiterThisDep).ToList();
                paramDetails.ForEach(t =>
                {
                    DoGetSqlByEntity(dstDataBaseType, t, result, dstOrgId, sqlType);
                });

                List<Guid> itemIds = paramItems.Select(t => t.ID).ToList();
                List<Guid> schemeIds = paramSchemes.Select(t => t.ID).ToList();
                List<Guid> schemeDetailIds = paramDetails.Select(t => t.ID).ToList();

                var allParamSchemeIds = this.GetEntities<T_PF_PARAM_SCHEME>(t => schemeIds.Contains(t.ID) ||
                      t.Nav_Details.Any(t1 => schemeDetailIds.Contains(t1.ID)) ||
                      t.Nav_Details.Any(t1 => itemIds.Contains(t1.PARAM_ITEM_ID)), new BaseFilter(orgId)).Select(t => t.ID).ToList();
                if (allParamSchemeIds.Any())
                {
                    var temppageTables = this.GetEntities<T_PF_PAGE_TABLE>(t => allParamSchemeIds.Contains(t.PARAM_SCHEME_ID ?? Guid.Empty), new BaseFilter(orgId)).ToList();
                    temppageTables.ForEach(t =>
                    {
                        if (t.PAGE_FORM_ID != null && !tempFormIds.Contains(t.PAGE_FORM_ID.Value))
                            tempFormIds.Add(t.PAGE_FORM_ID.Value);
                        cookies.Add(string.Format("form_config_tablebase_{0}_{1}", t.ID, dstOrgIdGuidStr));
                        if (t.Nav_PageForm != null)
                        {
                            cookies.Add(string.Format("form_config_table_{0}_{1}_{2}", t.Nav_PageForm.CODE, t.ORG_ID, (t.PARENT_ID == null ? "" : t.PARENT_ID.ToString())));
                            cookies.Add(string.Format("form_config_table_{0}_{1}_{2}_{3}", t.Nav_PageForm.CODE, t.ORG_ID, (t.PARENT_ID == null ? "" : t.PARENT_ID.ToString()),
                                t.ID));
                        }
                    });
                    var temppageEdits = this.GetEntities<T_PF_PAGE_EDIT>(t => allParamSchemeIds.Contains(t.PARAM_SCHEME_ID ?? Guid.Empty), new BaseFilter(orgId)).ToList();
                    temppageEdits.ForEach(t =>
                    {
                        if (t.PAGE_FORM_ID != null && !tempFormIds.Contains(t.PAGE_FORM_ID.Value))
                            tempFormIds.Add(t.PAGE_FORM_ID.Value);
                    });
                }
                #endregion

                Expression<Func<T_PF_FORM, bool>> formExpression = t => isInitOrg || formIds.Contains(t.ID);
                if (startTime != null)
                    formExpression = formExpression.Or(t => t.CREATE_TIME > startTime || t.MODIFY_TIME > startTime);
                var forms = this.GetEntities<T_PF_FORM>(formExpression, filiterThisDep).ToList();

                forms.ForEach(t =>
                {
                    DoGetSqlByEntity(dstDataBaseType, t, result, dstOrgId, sqlType);
                    tempFormIds.Add(t.ID);
                });

                List<Guid> tmpFormIds = forms.Select(t => t.ID).ToList();
                #region 图片配置
                Expression<Func<T_PF_PIC_FILTER, bool>> picExpression = t => isInitOrg || isCheckParent && tmpFormIds.Contains(t.PAGE_FORM_ID);
                if (startTime != null)
                    picExpression = picExpression.Or(t => t.CREATE_TIME > startTime || t.MODIFY_TIME > startTime);
                var pics = this.GetEntities<T_PF_PIC_FILTER>(picExpression, filiterThisDep).ToList();
                pics.ForEach(t =>
                {
                    DoGetSqlByEntity(dstDataBaseType, t, result, dstOrgId, sqlType);
                    if (t.PAGE_FORM_ID != null && !tempFormIds.Contains(t.PAGE_FORM_ID))
                        tempFormIds.Add(t.PAGE_FORM_ID);
                });
                List<Guid> picIds = pics.Select(t => t.ID).ToList();
                Expression<Func<T_PF_PIC_FILTER_DETAIL, bool>> picDetailExpression = t => isInitOrg || isCheckParent && picIds.Contains(t.PIC_FILTER_ID);
                if (startTime != null)
                    picDetailExpression = picDetailExpression.Or(t => t.CREATE_TIME > startTime || t.MODIFY_TIME > startTime);
                var picDetails = this.GetEntities<T_PF_PIC_FILTER_DETAIL>(picDetailExpression, filiterThisDep, new string[] { "Nav_PicFilter" }).ToList();
                picDetails.ForEach(t =>
                {
                    DoGetSqlByEntity(dstDataBaseType, t, result, dstOrgId, sqlType);
                    if (t.Nav_PicFilter != null && t.Nav_PicFilter.PAGE_FORM_ID != null && !tempFormIds.Contains(t.Nav_PicFilter.PAGE_FORM_ID))
                        tempFormIds.Add(t.Nav_PicFilter.PAGE_FORM_ID);
                });
                #endregion
                #region 表单关联
                Expression<Func<T_PF_FORM_RELATION, bool>> formRelationExpression = t => isInitOrg || tmpFormIds.Contains(t.PAGE_FORM_ID);
                if (startTime != null)
                    formRelationExpression = formRelationExpression.Or(t => t.CREATE_TIME > startTime || t.MODIFY_TIME > startTime);
                var formRelations = this.GetEntities<T_PF_FORM_RELATION>(formRelationExpression, filiterThisDep).ToList();

                formRelations.ForEach(t =>
                {
                    DoGetSqlByEntity(dstDataBaseType, t, result, dstOrgId, sqlType);
                    if (t.PAGE_FORM_ID != null && !tempFormIds.Contains(t.PAGE_FORM_ID))
                        tempFormIds.Add(t.PAGE_FORM_ID);
                });
                #endregion
                #region 编辑页
                Expression<Func<T_PF_PAGE_EDIT, bool>> editExpression = t => isInitOrg || isCheckParent && tmpFormIds.Contains(t.PAGE_FORM_ID ?? Guid.Empty);
                if (startTime != null)
                    editExpression = editExpression.Or(t => t.CREATE_TIME > startTime || t.MODIFY_TIME > startTime);
                var pageEdits = this.GetEntities<T_PF_PAGE_EDIT>(editExpression, filiterThisDep).ToList();
                pageEdits.ForEach(t =>
                {
                    DoGetSqlByEntity(dstDataBaseType, t, result, dstOrgId, sqlType);
                    if (t.PAGE_FORM_ID != null && !tempFormIds.Contains(t.PAGE_FORM_ID.Value))
                        tempFormIds.Add(t.PAGE_FORM_ID.Value);
                });

                List<Guid> pageEditIds = pageEdits.Select(t => t.ID).ToList();
                Expression<Func<T_PF_PAGE_EDIT_PANEL, bool>> editPanelExpression = t => isInitOrg || isCheckParent && pageEditIds.Contains(t.PAGE_EDIT_ID);
                if (startTime != null)
                    editPanelExpression = editPanelExpression.Or(t => t.CREATE_TIME > startTime || t.MODIFY_TIME > startTime);
                var editPanels = this.GetEntities<T_PF_PAGE_EDIT_PANEL>(editPanelExpression, filiterThisDep, new string[] { "Nav_PageEdit" }).ToList();
                editPanels.ForEach(t =>
                {
                    DoGetSqlByEntity(dstDataBaseType, t, result, dstOrgId, sqlType);
                    if (t.Nav_PageEdit != null && t.Nav_PageEdit.PAGE_FORM_ID != null && !tempFormIds.Contains(t.Nav_PageEdit.PAGE_FORM_ID.Value))
                        tempFormIds.Add(t.Nav_PageEdit.PAGE_FORM_ID.Value);
                });
                Expression<Func<T_PF_EDIT_COLUMN, bool>> editColumnExpression = t => isInitOrg || isCheckParent && pageEditIds.Contains(t.PAGE_EDIT_ID);
                if (startTime != null)
                    editColumnExpression = editColumnExpression.Or(t => t.CREATE_TIME > startTime || t.MODIFY_TIME > startTime);

                var editColumns = this.GetEntities<T_PF_EDIT_COLUMN>(editColumnExpression, filiterThisDep, new string[] { "Nav_PageEdit" }).ToList();
                editColumns.ForEach(t =>
                {
                    DoGetSqlByEntity(dstDataBaseType, t, result, dstOrgId, sqlType);
                    if (t.Nav_PageEdit != null && t.Nav_PageEdit.PAGE_FORM_ID != null && !tempFormIds.Contains(t.Nav_PageEdit.PAGE_FORM_ID.Value))
                        tempFormIds.Add(t.Nav_PageEdit.PAGE_FORM_ID.Value);
                });

                List<Guid> editColumnIds = editColumns.Select(t => t.ID).ToList();
                Expression<Func<T_PF_EDIT_COLUMN_FILTER, bool>> editColumnFilterExpression = t => isInitOrg || isCheckParent && editColumnIds.Contains(t.EDIT_COLUMN_ID);
                if (startTime != null)
                    editColumnFilterExpression = editColumnFilterExpression.Or(t => t.CREATE_TIME > startTime || t.MODIFY_TIME > startTime);
                var editColumnFilters = this.GetEntities<T_PF_EDIT_COLUMN_FILTER>(editColumnFilterExpression, filiterThisDep, new string[] { "Nav_EditColumn.Nav_PageEdit" }).ToList();
                editColumnFilters.ForEach(t =>
                {
                    DoGetSqlByEntity(dstDataBaseType, t, result, dstOrgId, sqlType);
                    if (t.Nav_EditColumn != null && t.Nav_EditColumn.Nav_PageEdit != null && t.Nav_EditColumn.Nav_PageEdit.PAGE_FORM_ID != null && !tempFormIds.Contains(t.Nav_EditColumn.Nav_PageEdit.PAGE_FORM_ID.Value))
                        tempFormIds.Add(t.Nav_EditColumn.Nav_PageEdit.PAGE_FORM_ID.Value);
                });
                Expression<Func<T_PF_EDIT_COLUMN_FILL_MAP, bool>> editColumnFillMapExpression = t => isInitOrg || isCheckParent && editColumnIds.Contains(t.EDIT_COLUMN_ID);
                if (startTime != null)
                    editColumnFillMapExpression = editColumnFillMapExpression.Or(t => t.CREATE_TIME > startTime || t.MODIFY_TIME > startTime);

                var editColumnFillMaps = this.GetEntities<T_PF_EDIT_COLUMN_FILL_MAP>(editColumnFillMapExpression, filiterThisDep, new string[] { "Nav_EditColumn.Nav_PageEdit" }).ToList();
                editColumnFillMaps.ForEach(t =>
                {
                    DoGetSqlByEntity(dstDataBaseType, t, result, dstOrgId, sqlType);
                    if (t.Nav_EditColumn != null && t.Nav_EditColumn.Nav_PageEdit != null && t.Nav_EditColumn.Nav_PageEdit.PAGE_FORM_ID != null && !tempFormIds.Contains(t.Nav_EditColumn.Nav_PageEdit.PAGE_FORM_ID.Value))
                        tempFormIds.Add(t.Nav_EditColumn.Nav_PageEdit.PAGE_FORM_ID.Value);
                });


                var editColumnFillMapIds = editColumnFillMaps.Select(t => t.ID).ToList();
                Expression<Func<T_PF_EDIT_COLUMN_FILL_MAP_D, bool>> editColumnFillMapDExpression = t => isInitOrg || isCheckParent && editColumnFillMapIds.Contains(t.EDIT_COLUMN_FILL_MAP_ID);
                if (startTime != null)
                    editColumnFillMapDExpression = editColumnFillMapDExpression.Or(t => t.CREATE_TIME > startTime || t.MODIFY_TIME > startTime);

                var editColumnFillMapDetails = this.GetEntities<T_PF_EDIT_COLUMN_FILL_MAP_D>(editColumnFillMapDExpression, filiterThisDep, new string[] { "Nav_EditColumnFillMap.Nav_EditColumn.Nav_PageEdit" }).ToList();
                editColumnFillMapDetails.ForEach(t =>
                {
                    DoGetSqlByEntity(dstDataBaseType, t, result, dstOrgId, sqlType);
                    if (t.Nav_EditColumnFillMap != null && t.Nav_EditColumnFillMap.Nav_EditColumn != null && t.Nav_EditColumnFillMap.Nav_EditColumn.Nav_PageEdit != null
                        && t.Nav_EditColumnFillMap.Nav_EditColumn.Nav_PageEdit.PAGE_FORM_ID != null && !tempFormIds.Contains(t.Nav_EditColumnFillMap.Nav_EditColumn.Nav_PageEdit.PAGE_FORM_ID.Value))
                        tempFormIds.Add(t.Nav_EditColumnFillMap.Nav_EditColumn.Nav_PageEdit.PAGE_FORM_ID.Value);
                });

                #endregion

                #region 自定义页
                Expression<Func<T_PF_PAGE_CUSTOM, bool>> customExpression = t => isInitOrg || isCheckParent && tmpFormIds.Contains(t.PAGE_FORM_ID ?? Guid.Empty);
                if (startTime != null)
                    customExpression = customExpression.Or(t => t.CREATE_TIME > startTime || t.MODIFY_TIME > startTime);
                var pageCustoms = this.GetEntities<T_PF_PAGE_CUSTOM>(customExpression, filiterThisDep).ToList();
                pageCustoms.ForEach(t =>
                {
                    DoGetSqlByEntity(dstDataBaseType, t, result, dstOrgId, sqlType);
                    if (t.PAGE_FORM_ID != null && !tempFormIds.Contains(t.PAGE_FORM_ID.Value))
                        tempFormIds.Add(t.PAGE_FORM_ID.Value);
                });
                #endregion

                #region 图表页
                Expression<Func<T_PF_PAGE_CHART, bool>> chartExpression = t => isInitOrg || isCheckParent && tmpFormIds.Contains(t.PAGE_FORM_ID ?? Guid.Empty);
                if (startTime != null)
                    chartExpression = chartExpression.Or(t => t.CREATE_TIME > startTime || t.MODIFY_TIME > startTime);
                var pageCharts = this.GetEntities<T_PF_PAGE_CHART>(chartExpression, filiterThisDep).ToList();
                pageCharts.ForEach(t =>
                {
                    DoGetSqlByEntity(dstDataBaseType, t, result, dstOrgId, sqlType);
                    if (t.PAGE_FORM_ID != null && !tempFormIds.Contains(t.PAGE_FORM_ID.Value))
                        tempFormIds.Add(t.PAGE_FORM_ID.Value);
                });
                List<Guid> pageChartIds = pageCharts.Select(t => t.ID).ToList();
                Expression<Func<T_PF_PAGE_CHART_PARAM, bool>> chartParamExpression = t => isInitOrg || isCheckParent && pageChartIds.Contains(t.PAGE_CHART_ID);
                if (startTime != null)
                    chartParamExpression = chartParamExpression.Or(t => t.CREATE_TIME > startTime || t.MODIFY_TIME > startTime);
                var pageChartParams = this.GetEntities<T_PF_PAGE_CHART_PARAM>(chartParamExpression, filiterThisDep, new string[] { "Nav_PageChart" }).ToList();
                pageChartParams.ForEach(t =>
                {
                    DoGetSqlByEntity(dstDataBaseType, t, result, dstOrgId, sqlType);
                    if (t.Nav_PageChart != null && t.Nav_PageChart.PAGE_FORM_ID != null && !tempFormIds.Contains(t.Nav_PageChart.PAGE_FORM_ID.Value))
                        tempFormIds.Add(t.Nav_PageChart.PAGE_FORM_ID.Value);
                });

                #endregion


                #region 列表页
                Expression<Func<T_PF_PAGE_TABLE, bool>> tableExpression = t => isInitOrg || isCheckParent && tmpFormIds.Contains(t.PAGE_FORM_ID ?? Guid.Empty);
                if (startTime != null)
                    tableExpression = tableExpression.Or(t => t.CREATE_TIME > startTime || t.MODIFY_TIME > startTime);
                var pageTables = this.GetEntities<T_PF_PAGE_TABLE>(tableExpression, filiterThisDep, new string[] { "Nav_PageForm" }).ToList();
                pageTables.ForEach(t =>
                {
                    DoGetSqlByEntity(dstDataBaseType, t, result, dstOrgId, sqlType);
                    cookies.Add(string.Format("form_config_tablebase_{0}_{1}", t.ID, dstOrgIdGuidStr));
                    if (t.PAGE_FORM_ID != null && !tempFormIds.Contains(t.PAGE_FORM_ID.Value))
                        tempFormIds.Add(t.PAGE_FORM_ID.Value);
                    if (t.Nav_PageForm != null)
                    {
                        cookies.Add(string.Format("form_config_table_{0}_{1}_{2}", t.Nav_PageForm.CODE, t.ORG_ID, (t.PARENT_ID == null ? "" : t.PARENT_ID.ToString())));
                        cookies.Add(string.Format("form_config_table_{0}_{1}_{2}_{3}", t.Nav_PageForm.CODE, t.ORG_ID, (t.PARENT_ID == null ? "" : t.PARENT_ID.ToString()),
                            t.ID));
                    }
                });

                List<Guid> pageTableIds = pageTables.Select(t => t.ID).ToList();
                Expression<Func<T_PF_PAGE_TABLE_PANEL, bool>> tablePanelExpression = t => isInitOrg || isCheckParent && pageTableIds.Contains(t.PAGE_TABLE_ID);
                if (startTime != null)
                    tablePanelExpression = tablePanelExpression.Or(t => t.CREATE_TIME > startTime || t.MODIFY_TIME > startTime);
                var tablePanels = this.GetEntities<T_PF_PAGE_TABLE_PANEL>(tablePanelExpression, filiterThisDep).ToList();
                tablePanels.ForEach(t =>
                {
                    DoGetSqlByEntity(dstDataBaseType, t, result, dstOrgId, sqlType);
                    cookies.Add(string.Format("form_config_tablebase_{0}_{1}", t.PAGE_TABLE_ID, t.ORG_ID));
                });
                Expression<Func<T_PF_COLUMN, bool>> columnExpression = t => isInitOrg || isCheckParent && pageTableIds.Contains(t.PAGE_TABLE_ID ?? Guid.Empty);
                if (startTime != null)
                    columnExpression = columnExpression.Or(t => t.CREATE_TIME > startTime || t.MODIFY_TIME > startTime);
                var columns = this.GetEntities<T_PF_COLUMN>(columnExpression, filiterThisDep).ToList();
                var columnIds = columns.Select(t => t.ID).ToList();
                columns.ForEach(t =>
                {
                    DoGetSqlByEntity(dstDataBaseType, t, result, dstOrgId, sqlType);
                    cookies.Add(string.Format("form_config_tablebase_{0}_{1}", t.PAGE_TABLE_ID, t.ORG_ID));
                });
                Expression<Func<T_PF_COLUMN_FILTER, bool>> columnFilterExpression = t => isInitOrg || isCheckParent && columnIds.Contains(t.COLUMN_ID);
                if (startTime != null)
                    columnFilterExpression = columnFilterExpression.Or(t => t.CREATE_TIME > startTime || t.MODIFY_TIME > startTime);
                var columnFilters = this.GetEntities<T_PF_COLUMN_FILTER>(columnFilterExpression, filiterThisDep, new string[] { "Nav_Column" }).ToList();

                var tempTableIds = columns.Where(t => t.PAGE_TABLE_ID != null).Select(t => t.PAGE_TABLE_ID.Value).ToList();
                columnFilters.ForEach(t =>
                {
                    DoGetSqlByEntity(dstDataBaseType, t, result, dstOrgId, sqlType);
                    if (t.Nav_Column != null && t.Nav_Column.PAGE_TABLE_ID != null && tempFormIds.Contains(t.Nav_Column.PAGE_TABLE_ID.Value))
                        tempTableIds.Add(t.Nav_Column.PAGE_TABLE_ID.Value);
                });
                if (tempTableIds.Any())
                {
                    var tempPageTables = this.GetEntities<T_PF_PAGE_TABLE>(t => tempTableIds.Contains(t.ID), filiterThisDep, new string[] { "Nav_PageForm" }).ToList();
                    tempPageTables.ForEach(t =>
                    {
                        if (t.Nav_PageForm != null)
                        {
                            cookies.Add(string.Format("form_config_table_{0}_{1}_{2}", t.Nav_PageForm.CODE, t.ORG_ID, (t.PARENT_ID == null ? "" : t.PARENT_ID.ToString())));
                            cookies.Add(string.Format("form_config_table_{0}_{1}_{2}_{3}", t.Nav_PageForm.CODE, t.ORG_ID, (t.PARENT_ID == null ? "" : t.PARENT_ID.ToString()),
                                t.ID));
                        }
                    });
                    var tempColFormIds = tempPageTables.Where(t => t.PAGE_FORM_ID != null).Select(t => t.PAGE_FORM_ID.Value).ToList();
                    tempColFormIds.ForEach(t =>
                    {
                        if (!tempFormIds.Contains(t))
                            tempFormIds.Add(t);
                    });
                }

                Expression<Func<T_PF_TABLE_PARAMS, bool>> tableParamsExpression = t => isInitOrg || isCheckParent && pageTableIds.Contains(t.PAGE_TABLE_ID);
                if (startTime != null)
                    tableParamsExpression = tableParamsExpression.Or(t => t.CREATE_TIME > startTime || t.MODIFY_TIME > startTime);
                var tableParams = this.GetEntities<T_PF_TABLE_PARAMS>(tableParamsExpression, filiterThisDep, new string[] { "Nav_PageTable" }).ToList();
                tableParams.ForEach(t =>
                {
                    DoGetSqlByEntity(dstDataBaseType, t, result, dstOrgId, sqlType);
                    if (t.Nav_PageTable != null && t.Nav_PageTable.PAGE_FORM_ID != null && !tempFormIds.Contains(t.Nav_PageTable.PAGE_FORM_ID.Value))
                        tempFormIds.Add(t.Nav_PageTable.PAGE_FORM_ID.Value);
                });

                #endregion
                #region 查询
                Expression<Func<T_PF_FORM_QUERY, bool>> formQueryExpression = t => isInitOrg || isCheckParent && tmpFormIds.Contains(t.PAGE_FORM_ID ?? Guid.Empty);
                if (startTime != null)
                    formQueryExpression = formQueryExpression.Or(t => t.CREATE_TIME > startTime || t.MODIFY_TIME > startTime);
                var formQuerys = this.GetEntities<T_PF_FORM_QUERY>(formQueryExpression, filiterThisDep, new string[] { "Nav_PageForm" }).ToList();
                formQuerys.ForEach(t =>
                {
                    DoGetSqlByEntity(dstDataBaseType, t, result, dstOrgId, sqlType);
                    if (t.PAGE_FORM_ID != null && !tempFormIds.Contains(t.PAGE_FORM_ID.Value))
                        tempFormIds.Add(t.PAGE_FORM_ID.Value);
                });
                var formQueryIds = formQuerys.Select(t => t.ID).ToList();
                Expression<Func<T_PF_QUERY, bool>> queryExpression = t => isInitOrg || isCheckParent && formQueryIds.Contains(t.PAGE_FORM_QUERY_ID ?? Guid.Empty);
                if (startTime != null)
                    queryExpression = queryExpression.Or(t => t.CREATE_TIME > startTime || t.MODIFY_TIME > startTime);
                var querys = this.GetEntities<T_PF_QUERY>(queryExpression, filiterThisDep).ToList();
                querys.ForEach(t =>
                {
                    DoGetSqlByEntity(dstDataBaseType, t, result, dstOrgId, sqlType);
                    if (t.PAGE_FORM_ID != null && !tempFormIds.Contains(t.PAGE_FORM_ID.Value))
                        tempFormIds.Add(t.PAGE_FORM_ID.Value);
                });
                #endregion
                #region 树页面
                Expression<Func<T_PF_PAGE_TREE, bool>> treeExpression = t => isInitOrg || isCheckParent && tmpFormIds.Contains(t.PAGE_FORM_ID ?? Guid.Empty);
                if (startTime != null)
                    treeExpression = treeExpression.Or(t => t.CREATE_TIME > startTime || t.MODIFY_TIME > startTime);
                var pageTrees = this.GetEntities<T_PF_PAGE_TREE>(treeExpression, filiterThisDep).ToList();
                pageTrees.ForEach(t =>
                {
                    DoGetSqlByEntity(dstDataBaseType, t, result, dstOrgId, sqlType);
                    if (t.PAGE_FORM_ID != null && !tempFormIds.Contains(t.PAGE_FORM_ID.Value))
                        tempFormIds.Add(t.PAGE_FORM_ID.Value);
                });
                List<Guid> pageTreeIds = pageTrees.Select(t => t.ID).ToList();
                Expression<Func<T_PF_TREE_COLUMN, bool>> treeColumnExpression = t => isInitOrg || isCheckParent && pageTreeIds.Contains(t.PAGE_TREE_ID);
                if (startTime != null)
                    treeColumnExpression = treeColumnExpression.Or(t => t.CREATE_TIME > startTime || t.MODIFY_TIME > startTime);
                var treeColumns = this.GetEntities<T_PF_TREE_COLUMN>(treeColumnExpression, filiterThisDep, new string[] { "Nav_PageTree" }).ToList();
                treeColumns.ForEach(t =>
                {
                    DoGetSqlByEntity(dstDataBaseType, t, result, dstOrgId, sqlType);
                    if (t.Nav_PageTree != null && t.Nav_PageTree.PAGE_FORM_ID != null && !tempFormIds.Contains(t.Nav_PageTree.PAGE_FORM_ID.Value))
                        tempFormIds.Add(t.Nav_PageTree.PAGE_FORM_ID.Value);
                });

                #endregion

                #region 按钮
                Expression<Func<T_PF_BTN, bool>> btnExpression = t => isInitOrg || isCheckParent && (tmpFormIds.Contains(t.PAGE_CUSTOM_FORM_ID ?? Guid.Empty) || pageTableIds.Contains(t.PAGE_TABLE_ID ?? Guid.Empty)
                        || pageTreeIds.Contains(t.PAGE_EDIT_ID ?? Guid.Empty) || pageEditIds.Contains(t.PAGE_EDIT_ID ?? Guid.Empty));
                if (startTime != null)
                    btnExpression = btnExpression.Or(t => t.CREATE_TIME > startTime || t.MODIFY_TIME > startTime);

                var btns = this.GetEntities<T_PF_BTN>(btnExpression, filiterThisDep).ToList();
                btns.ForEach(t =>
                {
                    DoGetSqlByEntity(dstDataBaseType, t, result, dstOrgId, sqlType);
                });

                var tempBtnFormIds = btns.Where(t => t.PAGE_CUSTOM_FORM_ID != null).Select(t => t.PAGE_CUSTOM_FORM_ID.Value).ToList();
                var tempBtnTableIds = btns.Where(t => t.PAGE_TABLE_ID != null).Select(t => t.PAGE_TABLE_ID.Value).ToList();
                var tempBtnEditIds = btns.Where(t => t.PAGE_EDIT_ID != null).Select(t => t.PAGE_EDIT_ID.Value).ToList();
                if (tempBtnTableIds.Any())
                {
                    var tempPageTables = this.GetEntities<T_PF_PAGE_TABLE>(t => tempBtnTableIds.Contains(t.ID), filiterThisDep).ToList();
                    var tempTableFormIds = tempPageTables.Where(t => t.PAGE_FORM_ID != null).Select(t => t.PAGE_FORM_ID.Value).ToList();
                    tempTableFormIds.ForEach(t =>
                    {
                        if (!tempBtnFormIds.Contains(t))
                            tempBtnFormIds.Add(t);
                    });
                }
                if (tempBtnEditIds.Any())
                {
                    var tempPageTrees = this.GetEntities<T_PF_PAGE_TREE>(t => tempBtnEditIds.Contains(t.ID), filiterThisDep).ToList();
                    var tempTreeFormIds = tempPageTrees.Where(t => t.PAGE_FORM_ID != null).Select(t => t.PAGE_FORM_ID.Value).ToList();
                    tempTreeFormIds.ForEach(t =>
                    {
                        if (!tempBtnFormIds.Contains(t))
                            tempBtnFormIds.Add(t);
                    });
                    var tempPageEdits = this.GetEntities<T_PF_PAGE_EDIT>(t => tempBtnEditIds.Contains(t.ID), filiterThisDep).ToList();
                    var tempEditFormIds = tempPageEdits.Where(t => t.PAGE_FORM_ID != null).Select(t => t.PAGE_FORM_ID.Value).ToList();
                    tempEditFormIds.ForEach(t =>
                    {
                        if (!tempBtnFormIds.Contains(t))
                            tempBtnFormIds.Add(t);
                    });
                }
                tempBtnFormIds.ForEach(t =>
                {
                    if (!tempFormIds.Contains(t))
                        tempFormIds.Add(t);
                });

                #endregion

                #region 版本
                if (dstFormConfigVersion > decimal.Zero && tempFormIds.Any())
                {
                    var versionForms = this.GetEntities<T_PF_FORM>(t => tempFormIds.Contains(t.ID), new BaseFilter(orgId)).ToList();
                    versionForms.ForEach(t =>
                    {
                        cookies.Add(string.Format("form_config_form_{0}_{1}", t.CODE, dstOrgIdGuidStr));
                        cookies.Add(string.Format("form_config_custom_{0}_{1}", t.CODE, dstOrgIdGuidStr));
                        cookies.Add(string.Format("form_config_edit_{0}_{1}", t.CODE, dstOrgIdGuidStr));
                        cookies.Add(string.Format("form_config_tree_{0}_{1}", t.CODE, dstOrgIdGuidStr));
                        cookies.Add(string.Format("form_config_table_{0}_{1}", t.CODE, dstOrgIdGuidStr));
                        cookies.Add(string.Format("form_config_search_{0}_{1}", t.ID, dstOrgIdGuidStr));
                        cookies.Add(string.Format("form_config_search_{0}_{1}", t.CODE, dstOrgIdGuidStr));
                        cookies.Add(string.Format("form_config_chart_{0}_{1}", t.CODE, dstOrgIdGuidStr));
                        cookies.Add(string.Format("form_config_chart_{0}_{1}", t.ID, dstOrgIdGuidStr));
                    });
                    dstFormConfigVersion++;
                    T_PF_FORM_CONFIG_VERSION formConfigVersion = new T_PF_FORM_CONFIG_VERSION();
                    formConfigVersion.VERSION = dstFormConfigVersion;
                    DoGetSqlByEntity(dstDataBaseType, formConfigVersion, result, dstOrgId, sqlType);
                    foreach (var item in cookies)
                    {
                        T_PF_FORM_CONFIG_VERSION_D formConfigVersionDetail = new T_PF_FORM_CONFIG_VERSION_D();
                        formConfigVersionDetail.REMOVE_COOKIE_KEY = item;
                        formConfigVersionDetail.FORM_CONFIG_VERSION_ID = formConfigVersion.ID;
                        DoGetSqlByEntity(dstDataBaseType, formConfigVersionDetail, result, dstOrgId, sqlType);
                    }
                }
                #endregion

                return result;
            }
            #endregion
        }
        /// <summary>
        /// 生成菜单/跑批SQL
        /// </summary>
        /// <param name="orgId"></param>
        /// <param name="formIds"></param>
        /// <param name="dstOrgId"></param>
        /// <param name="dstFormConfigVersion"></param>
        /// <param name="dstDataBaseType"></param>
        /// <param name="sqlType"></param>
        /// <param name="startTime"></param>
        /// <param name="isInitOrg"></param>
        /// <param name="formType"></param>
        /// <returns></returns>
        public List<string> GetSqlsByFormType(Guid orgId, List<Guid> formIds, string dstOrgId, decimal dstFormConfigVersion, DataBaseType dstDataBaseType, int sqlType, DateTime? startTime, bool isInitOrg, int formType)
        {
            List<string> result = new List<string>();
            string dstOrgIdGuidStr = dstOrgId;
            if (dstDataBaseType == DataBaseType.Oracle && !string.IsNullOrEmpty(dstOrgId))
                dstOrgIdGuidStr = Utility.DataHelper.OracleToDotNet(dstOrgId);
            if (string.IsNullOrEmpty(dstOrgIdGuidStr))
                dstOrgIdGuidStr = orgId.ToString();
            bool isCheckParent = formIds != null && formIds.Any();//是否勾选数据

            var filiterThisDep = new BaseFilter(orgId, FilterOrgTypeEnum.仅本组织);
            filiterThisDep.IgnoreDataRule = true;
            if (formType == 2)//菜单
            {
                Expression<Func<T_PF_MENU, bool>> editExpression = t => isInitOrg || formIds.Contains((Guid)t.MENU_FORM_ID);
                if (startTime != null)
                    editExpression = editExpression.Or(t => t.CREATE_TIME > startTime || t.MODIFY_TIME > startTime);
                var menus = this.GetEntities<T_PF_MENU>(editExpression, filiterThisDep).OrderBy(x => x.MODIFY_TIME).ToList();
                List<T_PF_MENU> orderMenus = new List<T_PF_MENU>();
                foreach (var menu in menus)
                {
                    GetOrderMenus(menu, menus, orderMenus);
                }

                orderMenus.ForEach(t =>
                {
                    DoGetSqlByEntity(dstDataBaseType, t, result, dstOrgId, sqlType);

                });
            }
            else//跑批
            {
                Expression<Func<T_FM_SYNC_TASK, bool>> editExpression = t => isInitOrg;
                if (startTime != null)
                    editExpression = editExpression.Or(t => t.CREATE_TIME > startTime || t.MODIFY_TIME > startTime);
                var tasks = this.GetEntities<T_FM_SYNC_TASK>(editExpression, filiterThisDep).OrderBy(x => x.CREATE_TIME).ToList();
                tasks.ForEach(t =>
                {
                    DoGetSqlByEntity(dstDataBaseType, t, result, dstOrgId, sqlType);

                });
            }
            return result;
        }
        private void GetOrderMenus(T_PF_MENU currentMenu, List<T_PF_MENU> menus, List<T_PF_MENU> orderMenus)
        {
            var parentId = currentMenu.PARENT_ID;
            if (parentId != null && menus.Where(x => x.ID == parentId) != null && menus.Where(x => x.ID == parentId).Any())
            {
                GetOrderMenus(menus.FirstOrDefault(x => x.ID == parentId), menus, orderMenus);
                if (!orderMenus.Where(x => x.ID == currentMenu.ID).Any())
                {
                    orderMenus.Add(currentMenu);
                }
            }
            else
            {
                if (!orderMenus.Where(x => x.ID == currentMenu.ID).Any())
                {
                    orderMenus.Add(currentMenu);
                }
            }
        }
        /// <summary>
        /// 获取实体表语法
        /// </summary>
        /// <param name="orgId"></param>
        /// <param name="dataBaseType"></param>
        /// <param name="entityNames"></param>
        /// <param name="dstOrgId"></param>
        /// <returns></returns>
        public List<string> GetSqlByEntity(DataBaseType dataBaseType, Guid orgId, string entityNames, string dstOrgId, int sqlType)
        {
            var temps = entityNames.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries).ToList();
            if (!temps.Any()) return null;

            List<string> list = new List<string>();
            foreach (var item in temps)
            {
                Type type = DataHelper.GetTypeByEntityName(item);
                MethodInfo methodInfo = this.GetType().GetMethod("GetEntitisSql", BindingFlags.Instance | BindingFlags.Public | BindingFlags.Static);
                methodInfo.MakeGenericMethod(new Type[] { type }).
                            Invoke(this, new object[] { dataBaseType, orgId, list, dstOrgId, sqlType });
            }
            return list;
        }


        /// <summary>
        ///根据来源组织初始化目的组织 配置数据
        /// </summary>
        /// <param name="srcOrgCode">来源组织编号</param>
        /// <param name="destOrgCode">目的组织编号</param>
        /// <param name="destOrgName">目的组织名称</param>
        public void InitOrgBySrcOrg(string srcOrgCode, string destOrgCode, string destOrgName)
        {
            if (string.IsNullOrEmpty(destOrgCode))
                throw new Exception("目标组织编号不允许为空");
            if (string.IsNullOrEmpty(destOrgName))
                throw new Exception("目标组织名称不允许为空");

            var srcOrg = this.GetEntity<T_FM_ORGANIZATION>(t => t.CODE == srcOrgCode);
            if (srcOrg == null)
                throw new Exception("来源组织不存在，请确认");
            var destOrg = this.GetEntity<T_FM_ORGANIZATION>(t => t.CODE == destOrgCode);
            if (destOrg != null)
                throw new Exception("已存在编号" + destOrgCode + "的组织，不允许执行此操作");


            destOrg = new T_FM_ORGANIZATION();
            destOrg.CODE = destOrgCode;
            destOrg.NAME = destOrgName;
            this.AddEntity(destOrg);

            List<Guid> destOrgIds = new List<Guid>();
            destOrgIds.Add(destOrg.ID);

            this.InitOrgByOrg(srcOrg.ID, destOrgIds, null);
        }

        /// <summary>
        /// 初始化组织
        /// </summary>
        /// <param name="srcOrgId"></param>
        /// <param name="destOrgIds"></param>
        /// <param name="startTime"></param>
        public void InitOrgByOrg(Guid srcOrgId, List<Guid> destOrgIds, DateTime? startTime)
        {
            List<Guid> destInitOrgIds = new List<Guid>();
            List<Guid> destSynchOrgIds = new List<Guid>();
            var destOrgs = this.GetEntities<T_FM_ORGANIZATION>(t => destOrgIds.Contains(t.ID), new BaseFilter()).ToList();
            foreach (var destOrg in destOrgs)
            {
                var isExistForm = this.GetEntities<T_PF_FORM>(null, new BaseFilter(destOrg.ID)).Any();
                if (isExistForm)
                {
                    if (startTime == null)
                        throw new Exception("已进行初始化组织，开始时间不允许为空");
                    destSynchOrgIds.Add(destOrg.ID);
                }
                else
                {
                    destInitOrgIds.Add(destOrg.ID);
                }
            }
            if (destInitOrgIds.Any())
                this.DoSynchFormConfigByOrg(srcOrgId, destInitOrgIds, null);
            if (destSynchOrgIds.Any())
                this.DoSynchFormConfigByOrg(srcOrgId, destSynchOrgIds, startTime);

        }

        /// <summary>
        /// 同步表单配置信息
        /// </summary>
        /// <param name="srcOrgId"></param>
        /// <param name="destOrgIds"></param>
        /// <param name="startTime"></param>
        public void SynchFormConfigByOrg(Guid srcOrgId, List<Guid> destOrgIds, DateTime? startTime)
        {
            this.DoSynchFormConfigByOrg(srcOrgId, destOrgIds, startTime);
        }

        /// <summary>
        /// 执行语法并且获取执行结果
        /// </summary>
        /// <param name="sql"></param>
        /// <returns></returns>
        public ExecuteSqlResult ExcuteSqlAndGetResult(string sql)
        {
            ExecuteSqlResult executeSqlResult = new ExecuteSqlResult();
            if (string.IsNullOrEmpty(sql)) return executeSqlResult;
            int tableIndex = 1;

            var arr = sql.Split(new char[] { ';' }, StringSplitOptions.RemoveEmptyEntries).ToList();
            foreach (var s in arr)
                DoExcuteSqlAndGetResult(s, executeSqlResult, ref tableIndex);

            foreach (var table in executeSqlResult.Tables)
            {
                if (!table.IsSelectTable) continue;
                ExecuteSqlColumn column = new ExecuteSqlColumn()
                {
                    FieldName = "RowNo",
                    Label = "序号"
                };
                table.Columns.Insert(0, column);
                int rowIndex = 1;
                foreach (var row in table.Rows)
                {
                    ExecuteSqlRowColumn executeSqlRowColumn = new ExecuteSqlRowColumn()
                    {
                        ColumnId = column.ID,
                        Value = rowIndex.ToString()
                    };
                    row.Datas.Add(executeSqlRowColumn);
                    rowIndex++;
                }
            }

            return executeSqlResult;
        }



        private void GetConfigBySrc<T>(List<T> srcEntities,
            Guid destOrgId, Dictionary<Guid, Guid> entityRelations,
            List<T> destAddEntities, List<T> destUpdateEntities, string relationFieldName, DateTime? startTime,
            Action<T, T, Dictionary<Guid, Guid>> copyFieldAction,
            Func<T, T> getEntityFunc,
            Func<T, bool> vilFunc,
                Action<T> handleNewEntityAction)
            where T : EntityBase<Guid>, new()
        {
            if (srcEntities == null || !srcEntities.Any()) return;
            srcEntities.ForEach(t =>
            {
                bool isAdd = false;
                T destEntity = null;
                if (getEntityFunc != null)
                    destEntity = getEntityFunc(t);
                if (destEntity == null)
                {
                    isAdd = true;
                    destEntity = new T();
                }
                entityRelations[t.ID] = destEntity.ID;
                if (startTime != null)
                {
                    if (t.CREATE_TIME < startTime || t.MODIFY_TIME < startTime)
                        return;
                }
                if (vilFunc != null)
                {
                    bool isVil = vilFunc(t);
                    if (!isVil) return;
                }
                CopyUtils.CopyObject(destEntity, t, new string[] { "CREATER_ID", "MODIFIER_ID" });
                destEntity.ORG_ID = destOrgId;
                if (!string.IsNullOrEmpty(relationFieldName) && entityRelations != null)
                {
                    Type srcType = t.GetType();
                    Type destType = destEntity.GetType();
                    var srcP = srcType.GetProperty(relationFieldName);
                    var destP = destType.GetProperty(relationFieldName);
                    if (srcP != null && srcP.PropertyType == destP.PropertyType)
                    {
                        var value = LibUtils.ToGuid(srcP.GetValue(t, null));
                        try
                        {
                            Guid destId;
                            if (value != null && entityRelations.TryGetValue(value.Value, out destId))
                                destP.SetValue(destEntity, destId, null);
                        }
                        catch
                        { }
                    }
                }
                if (copyFieldAction != null)
                    copyFieldAction(t, destEntity, entityRelations);
                if (isAdd)
                    destAddEntities.Add(destEntity);
                else
                    destUpdateEntities.Add(destEntity);
                if (handleNewEntityAction != null)
                    handleNewEntityAction(destEntity);
            });
        }

        private void GetConfigBySrcOnTree<T>(List<T> srcEntities, Guid? srcParentId, Guid? destParentId,
                Guid destOrgId, Dictionary<Guid, Guid> entityRelations,
                List<T> destAddEntities, List<T> destUpdateEntities, string relationFieldName, DateTime? startTime,
                Action<T, T, Dictionary<Guid, Guid>> copyFieldAction,
                Func<T, Guid?, T> getEntityFunc,
                Func<T, bool> vilFunc,
                Action<T> handleNewEntityAction)
            where T : MesTreeEntityBase, new()
        {
            if (srcEntities == null || !srcEntities.Any()) return;
            var temps = srcEntities.Where(t => t.PARENT_ID == srcParentId).ToList();
            if (!temps.Any()) return;
            temps.ForEach(t =>
            {
                T destEntity = null;
                if (getEntityFunc != null)
                    destEntity = getEntityFunc(t, destParentId);
                bool isAdd = false;
                if (destEntity == null)
                {
                    destEntity = new T();
                    isAdd = true;
                }
                entityRelations[t.ID] = destEntity.ID;
                bool isCopy = true;
                if (startTime != null)
                {
                    if (t.CREATE_TIME < startTime || t.MODIFY_TIME < startTime)
                        isCopy = false;
                }
                if (vilFunc != null)
                {
                    bool isVil = vilFunc(t);
                    if (!isVil) isCopy = false;
                }
                if (isCopy)
                {
                    CopyUtils.CopyObject(destEntity, t, new string[] { "CREATER_ID", "MODIFIER_ID" });
                    destEntity.ORG_ID = destOrgId;
                    if (!string.IsNullOrEmpty(relationFieldName) && entityRelations != null)
                    {
                        Type srcType = t.GetType();
                        Type destType = destEntity.GetType();
                        var srcP = srcType.GetProperty(relationFieldName);
                        var destP = destType.GetProperty(relationFieldName);
                        if (srcP != null && srcP.PropertyType == destP.PropertyType)
                        {
                            var value = LibUtils.ToGuid(srcP.GetValue(t, null));
                            try
                            {
                                Guid destId;
                                if (value != null && entityRelations.TryGetValue(value.Value, out destId))
                                    destP.SetValue(destEntity, destId, null);

                            }
                            catch
                            { }
                        }
                    }
                    copyFieldAction?.Invoke(t, destEntity, entityRelations);
                    if (destParentId != null && isAdd)
                        destEntity.PARENT_ID = destParentId;
                    if (isAdd)
                        destAddEntities.Add(destEntity);
                    else
                        destUpdateEntities.Add(destEntity);
                    handleNewEntityAction?.Invoke(destEntity);
                }
                GetConfigBySrcOnTree(srcEntities, t.ID, destEntity.ID, destOrgId,
                    entityRelations, destAddEntities, destUpdateEntities, relationFieldName, startTime, copyFieldAction,
                    getEntityFunc, vilFunc, handleNewEntityAction);
            });
        }








        /// <summary>
        /// 
        /// </summary>
        /// <param name="dataBaseType"></param>
        /// <param name="obj"></param>
        /// <param name="result"></param>
        /// <param name="dstOrgId"></param>
        /// <param name="sqlType">获取语法类型 0-update 和insert  1-仅获取update 2-仅获取insert</param>
        private void DoGetSqlByEntity(DataBaseType dataBaseType, object obj, List<string> result, string dstOrgId, int sqlType)
        {
            Type objType = obj.GetType();

            Dictionary<string, string> fields = new Dictionary<string, string>();
            string MODIFY_TIME = DateTime.Now.ToString("yyyy-MM-dd 00:00:00");
            foreach (var mi in objType.GetProperties())
            {
                if (string.Compare(mi.Name, "NAV_PROPERTY", true) != 0 && mi.Name.StartsWith("Nav_", StringComparison.OrdinalIgnoreCase) ||
                    mi.Name.StartsWith("VIR_", StringComparison.OrdinalIgnoreCase) ||
                    mi.Name.StartsWith("VIRTUAL_", StringComparison.OrdinalIgnoreCase) ||
                    mi.Name.ToUpper() != mi.Name)
                {
                    if (mi.DeclaringType.Name == "T_PF_MENU" && mi.Name == "MineType")
                    { 
                    }
                    else
                    {
                        //wyw 菜单导出 功能优化
                        continue;
                    }
                }

                string fieldName = mi.Name;
                string value = string.Empty;
                if (string.Compare(mi.Name, "CREATER_ID", true) == 0 || string.Compare(mi.Name, "MODIFIER_ID", true) == 0)
                    value = "NULL";
                else if (string.Compare(mi.Name, "MODIFY_TIME", true) == 0 || string.Compare(mi.Name, "MODIFY_TIME", true) == 0)//更新时间变成操作当天
                    value = MODIFY_TIME;
                else if (string.Compare(mi.Name, "ORG_ID", true) == 0)
                {
                    if (string.IsNullOrEmpty(dstOrgId))
                    {
                        var tmpObj = mi.GetValue(obj, null);
                        if (tmpObj != null)
                        {
                            value = tmpObj.ToString().ToUpper();
                            if (dataBaseType == DataBaseType.Oracle)
                                value = Utility.DataHelper.DotNetToOracle(value);
                            else if (dataBaseType == DataBaseType.MySQL || dataBaseType == DataBaseType.Postgresql)
                                value = tmpObj.ToString().ToLower();
                        }
                    }
                    else
                        value = dstOrgId;
                }
                else
                {
                    string propertyTypeName = mi.PropertyType.Name;
                    string fullTypeName = mi.PropertyType.FullName;
                    if (string.Compare(propertyTypeName, "GUID", true) == 0 || fullTypeName.IndexOf("Guid", StringComparison.OrdinalIgnoreCase) > -1)
                    {
                        var tmpObj = mi.GetValue(obj, null);
                        if (tmpObj != null)
                        {
                            value = tmpObj.ToString().ToUpper();
                            if (dataBaseType == DataBaseType.Oracle)
                                value = Utility.DataHelper.DotNetToOracle(value);
                            else if (dataBaseType == DataBaseType.MySQL || dataBaseType == DataBaseType.Postgresql)
                                value = tmpObj.ToString().ToLower();
                        }
                    }
                    else if (string.Compare(propertyTypeName, "DateTime", true) == 0 || fullTypeName.IndexOf("DateTime", StringComparison.OrdinalIgnoreCase) > -1)
                    {
                        var tmpObj = mi.GetValue(obj, null);
                        if (tmpObj != null)
                        {
                            if (dataBaseType == DataBaseType.Oracle)
                                value = "to_date('" + ((DateTime)tmpObj).ToString("yyyy/MM/dd hh:mm:ss") + "','yyyy-mm-dd hh24-mi-ss')";
                            else if (dataBaseType == DataBaseType.Postgresql)
                                value = ((DateTime)tmpObj).ToString("yyyy-MM-dd HH:mm:ss");
                            else
                                value = tmpObj.ToString();
                        }
                    }
                    else if (string.Compare(propertyTypeName, "bool", true) == 0 || string.Compare(propertyTypeName, "boolean", true) == 0 || fullTypeName.IndexOf("bool", StringComparison.OrdinalIgnoreCase) > -1)
                    {
                        var tmpObj = mi.GetValue(obj, null);
                        if (tmpObj != null)
                            value = ((bool)tmpObj) ? "1" : "0";
                    }
                    else
                    {
                        var tmpObj = mi.GetValue(obj, null);
                        if (tmpObj != null)
                            value = tmpObj.ToString();
                    }

                    if (string.IsNullOrEmpty(value))
                        value = "NULL";
                }
                if (!string.IsNullOrEmpty(value))
                    fields[fieldName] = value;
            }

            if (fields.Count > 0)
            {
                string tableName = objType.Name;
                if (string.Compare(objType.Namespace, "System.Data.Entity.DynamicProxies", true) == 0)
                    tableName = objType.BaseType.Name;
                if (dataBaseType == DataBaseType.Postgresql)
                    tableName = "\"" + tableName + "\"";
                StringBuilder upBuilder = new StringBuilder();
                StringBuilder upFieldBuilder = new StringBuilder();
                StringBuilder insertBuilder = new StringBuilder();
                upBuilder.Append("UPDATE " + tableName + " SET ");
                insertBuilder.Append("INSERT INTO " + tableName + "(");
                foreach (var item in fields)
                {
                    string field = item.Key;
                    if (dataBaseType == DataBaseType.Postgresql)
                        field = "\"" + field + "\"";
                    else if (DB_KEYWORDS.Any(t => (string.Compare(t, field, true) == 0)))//关键字加引号
                        field = (dataBaseType == DataBaseType.MySQL ? "`" : "\"") + field + (dataBaseType == DataBaseType.MySQL ? "`" : "\"");
                    insertBuilder.Append(field);
                    insertBuilder.Append(",");
                }
                insertBuilder.Length--;
                insertBuilder.Append(") VALUES(");
                string id = string.Empty;
                foreach (var item in fields)
                {
                    string field = item.Key;
                    string value = item.Value;
                    if (string.Compare(value, "NULL") != 0 && value.IndexOf("to_date", StringComparison.OrdinalIgnoreCase) == -1)
                        value = "'" + value + "'";
                    if (dataBaseType == DataBaseType.Postgresql)
                        field = "\"" + field + "\"";
                    else if (DB_KEYWORDS.Any(t => (string.Compare(t, field, true) == 0)))//关键字加引号
                        field = (dataBaseType == DataBaseType.MySQL ? "`" : "\"") + field + (dataBaseType == DataBaseType.MySQL ? "`" : "\"");

                    if (string.Compare(field, (dataBaseType == DataBaseType.Postgresql ? "\"ID\"" : "ID"), true) == 0)
                        id = value;
                    else
                    {
                        upBuilder.Append(field + "=" + value + ",");
                        upFieldBuilder.Append(field + "=" + value + ",");
                    }

                    insertBuilder.Append(value + ",");
                }
                upBuilder.Length--;
                insertBuilder.Length--;
                upFieldBuilder.Length--;

                insertBuilder.Append(")");
                if (dataBaseType == DataBaseType.Postgresql)
                    upBuilder.Append(" WHERE \"ID\"=" + id);
                else
                    upBuilder.Append(" WHERE ID=" + id);

                if (sqlType == 1)
                {
                    result.Add(upBuilder.ToString() + ";");
                }
                else if (sqlType == 2)
                {
                    result.Add(insertBuilder.ToString() + ";");
                }
                else if (dataBaseType == DataBaseType.Oracle)
                    result.Add(string.Format(" {0} ; if sql % notfound then {1} ; end if; ", upBuilder.ToString(), insertBuilder.ToString()));
                else if (dataBaseType == DataBaseType.MySQL)
                    result.Add(string.Format(" {0}  ON DUPLICATE KEY UPDATE {1}; ", insertBuilder.ToString(), upFieldBuilder.ToString()));
                else if (dataBaseType == DataBaseType.Postgresql)
                    result.Add(string.Format(@" do language plpgsql $$ declare v_rowCount int:= 0; begin {0}; GET DIAGNOSTICS v_rowCount = ROW_COUNT; if (v_rowCount = 0) then {1}; end if; end $$;",
                            upBuilder.ToString(), insertBuilder.ToString()));
                else
                    result.Add(string.Format("begin {0}; if @@RowCount = 0 begin {1} ; end  end ", upBuilder.ToString(), insertBuilder.ToString()));
            }
        }



        private void DoSynchFormConfigByOrg(Guid srcOrgId, List<Guid> destOrgIds, DateTime? startTime)
        {
            List<Guid> orgIds = new List<Guid>();
            orgIds.Add(srcOrgId);
            orgIds.AddRange(destOrgIds);

            List<T_PF_FORM> destAddForms = new List<T_PF_FORM>();
            List<T_PF_PAGE_EDIT> destAddEdits = new List<T_PF_PAGE_EDIT>();
            List<T_PF_PAGE_EDIT_PANEL> destAddEditPanels = new List<T_PF_PAGE_EDIT_PANEL>();

            List<T_PF_PARAM_ITEM> destAddParamItems = new List<T_PF_PARAM_ITEM>();
            List<T_PF_PARAM_SCHEME> destAddParamSchemes = new List<T_PF_PARAM_SCHEME>();
            List<T_PF_PARAM_SCHEME_DETAIL> destAddParamSchemeDetails = new List<T_PF_PARAM_SCHEME_DETAIL>();
            List<T_PF_PAGE_TABLE_PANEL> destAddTablePanels = new List<T_PF_PAGE_TABLE_PANEL>();
            List<T_PF_EDIT_COLUMN> destAddEditColumns = new List<T_PF_EDIT_COLUMN>();
            List<T_PF_EDIT_COLUMN_FILTER> destAddEditColumnFilters = new List<T_PF_EDIT_COLUMN_FILTER>();
            List<T_PF_EDIT_COLUMN_FILL_MAP> destAddEditColumnFillMaps = new List<T_PF_EDIT_COLUMN_FILL_MAP>();
            List<T_PF_EDIT_COLUMN_FILL_MAP_D> destAddEditColumnFillMapDs = new List<T_PF_EDIT_COLUMN_FILL_MAP_D>();
            List<T_PF_PAGE_CUSTOM> destAddCustoms = new List<T_PF_PAGE_CUSTOM>();
            List<T_PF_PAGE_CHART> destAddCharts = new List<T_PF_PAGE_CHART>();
            List<T_PF_PAGE_CHART_PARAM> destAddChartParams = new List<T_PF_PAGE_CHART_PARAM>();
            List<T_PF_PAGE_TABLE> destAddTables = new List<T_PF_PAGE_TABLE>();
            List<T_PF_COLUMN> destAddColums = new List<T_PF_COLUMN>();
            List<T_PF_COLUMN_FILTER> destAddColumFilters = new List<T_PF_COLUMN_FILTER>();
            List<T_PF_TABLE_PARAMS> destAddTableParams = new List<T_PF_TABLE_PARAMS>();
            List<T_PF_QUERY> destAddQuerys = new List<T_PF_QUERY>();
            List<T_PF_FORM_QUERY> destAddFormQuerys = new List<T_PF_FORM_QUERY>();
            List<T_PF_PAGE_TREE> destAddTrees = new List<T_PF_PAGE_TREE>();
            List<T_PF_TREE_COLUMN> destAddTreeColumns = new List<T_PF_TREE_COLUMN>();
            List<T_PF_BTN> destAddBtns = new List<T_PF_BTN>();

            List<T_PF_MENU> destAddMenus = new List<T_PF_MENU>();
            List<T_PF_APPMENU> destAddAppMenus = new List<T_PF_APPMENU>();
            List<T_PF_CLIENT_MENU> destAddClientMenus = new List<T_PF_CLIENT_MENU>();

            List<T_PF_CODE_RULE> destAddCodeRules = new List<T_PF_CODE_RULE>();

            List<T_PF_FORM> destUpdateForms = new List<T_PF_FORM>();
            List<T_PF_PAGE_EDIT> destUpdateEdits = new List<T_PF_PAGE_EDIT>();
            List<T_PF_PAGE_EDIT_PANEL> destUpdateEditPanels = new List<T_PF_PAGE_EDIT_PANEL>();

            List<T_PF_PARAM_ITEM> destUpdateParamItems = new List<T_PF_PARAM_ITEM>();
            List<T_PF_PARAM_SCHEME> destUpdateParamSchemes = new List<T_PF_PARAM_SCHEME>();
            List<T_PF_PARAM_SCHEME_DETAIL> destUpdateParamSchemeDetails = new List<T_PF_PARAM_SCHEME_DETAIL>();
            List<T_PF_PAGE_TABLE_PANEL> destUpdateTablePanels = new List<T_PF_PAGE_TABLE_PANEL>();
            List<T_PF_EDIT_COLUMN> destUpdateEditColumns = new List<T_PF_EDIT_COLUMN>();
            List<T_PF_EDIT_COLUMN_FILTER> destUpdateEditColumnFilters = new List<T_PF_EDIT_COLUMN_FILTER>();
            List<T_PF_EDIT_COLUMN_FILL_MAP> destUpdateEditColumnFillMaps = new List<T_PF_EDIT_COLUMN_FILL_MAP>();
            List<T_PF_EDIT_COLUMN_FILL_MAP_D> destUpdateEditColumnFillMapDs = new List<T_PF_EDIT_COLUMN_FILL_MAP_D>();
            List<T_PF_PAGE_CUSTOM> destUpdateCustoms = new List<T_PF_PAGE_CUSTOM>();
            List<T_PF_PAGE_CHART> destUpdateCharts = new List<T_PF_PAGE_CHART>();
            List<T_PF_PAGE_CHART_PARAM> destUpdateChartParams = new List<T_PF_PAGE_CHART_PARAM>();
            List<T_PF_PAGE_TABLE> destUpdateTables = new List<T_PF_PAGE_TABLE>();
            List<T_PF_COLUMN> destUpdateColums = new List<T_PF_COLUMN>();
            List<T_PF_COLUMN_FILTER> destUpdateColumFilters = new List<T_PF_COLUMN_FILTER>();
            List<T_PF_TABLE_PARAMS> destUpdateTableParams = new List<T_PF_TABLE_PARAMS>();
            List<T_PF_QUERY> destUpdateQuerys = new List<T_PF_QUERY>();
            List<T_PF_FORM_QUERY> destUpdateFormQuerys = new List<T_PF_FORM_QUERY>();
            List<T_PF_PAGE_TREE> destUpdateTrees = new List<T_PF_PAGE_TREE>();
            List<T_PF_TREE_COLUMN> destUpdateTreeColumns = new List<T_PF_TREE_COLUMN>();
            List<T_PF_BTN> destUpdateBtns = new List<T_PF_BTN>();

            List<T_PF_MENU> destUpdateMenus = new List<T_PF_MENU>();
            List<T_PF_APPMENU> destUpdateAppMenus = new List<T_PF_APPMENU>();
            List<T_PF_CLIENT_MENU> destUpdateClientMenus = new List<T_PF_CLIENT_MENU>();

            List<T_PF_CODE_RULE> destUpdateCodeRules = new List<T_PF_CODE_RULE>();

            Dictionary<Guid, Dictionary<Guid, Guid>> entityRelations = new Dictionary<Guid, Dictionary<Guid, Guid>>();
            Dictionary<Guid, Guid> tempRels = null;
            List<Guid> updateFormIds = new List<Guid>();

            #region 系统参数模板

            var paramItems = this.GetEntities<T_PF_PARAM_ITEM>(t => orgIds.Contains(t.ORG_ID ?? Guid.Empty), new BaseFilter()).ToList();
            var srcParamItems = paramItems.Where(t => t.ORG_ID == srcOrgId).ToList();
            List<Guid> paramItemIds = new List<Guid>();
            foreach (var item in destOrgIds)
            {
                if (!entityRelations.TryGetValue(item, out tempRels))
                {
                    tempRels = new Dictionary<Guid, Guid>();
                    entityRelations.Add(item, tempRels);
                }

                GetConfigBySrc<T_PF_PARAM_ITEM>(srcParamItems, item, tempRels, destAddParamItems, destUpdateParamItems, string.Empty, startTime, null,
                (t) =>
                {
                    return paramItems.FirstOrDefault(t1 => t1.ORG_ID == item && t1.FIELD_NAME == t.FIELD_NAME);
                },
                null,
                (t) =>
                {
                    if (!paramItemIds.Contains(t.ID))
                        paramItemIds.Add(t.ID);
                    if (!paramItems.Any(t1 => t1.ID == t.ID))
                        paramItems.Add(t);
                });
            }

            var paramSchemes = this.GetEntities<T_PF_PARAM_SCHEME>(t => orgIds.Contains(t.ORG_ID ?? Guid.Empty), new BaseFilter()).ToList();
            var srcParamSchemes = paramSchemes.Where(t => t.ORG_ID == srcOrgId).ToList();
            List<Guid> paramSchemeIds = new List<Guid>();
            foreach (var item in destOrgIds)
            {
                if (!entityRelations.TryGetValue(item, out tempRels))
                {
                    tempRels = new Dictionary<Guid, Guid>();
                    entityRelations.Add(item, tempRels);
                }

                GetConfigBySrc<T_PF_PARAM_SCHEME>(srcParamSchemes, item, tempRels, destAddParamSchemes, destUpdateParamSchemes, string.Empty, startTime, null,
                (t) =>
                {
                    return paramSchemes.FirstOrDefault(t1 => t1.ORG_ID == item && t1.CODE == t.CODE);
                },
                null,
                (t) =>
                {
                    if (!paramSchemeIds.Contains(t.ID))
                        paramSchemeIds.Add(t.ID);
                    if (!paramSchemes.Any(t1 => t1.ID == t.ID))
                        paramSchemes.Add(t);
                });
            }

            var paramSchemeDetails = this.GetEntities<T_PF_PARAM_SCHEME_DETAIL>(t => orgIds.Contains(t.ORG_ID ?? Guid.Empty), new BaseFilter()).ToList();
            var srcParamSchemeDetails = paramSchemeDetails.Where(t => t.ORG_ID == srcOrgId).ToList();
            List<Guid> paramSchemeDetailIds = new List<Guid>();
            foreach (var item in destOrgIds)
            {
                if (!entityRelations.TryGetValue(item, out tempRels))
                {
                    tempRels = new Dictionary<Guid, Guid>();
                    entityRelations.Add(item, tempRels);
                }

                GetConfigBySrc<T_PF_PARAM_SCHEME_DETAIL>(srcParamSchemeDetails, item, tempRels, destAddParamSchemeDetails, destUpdateParamSchemeDetails, "PARAM_SCHEME_ID", startTime, (src, dest, rels) =>
                {
                    var srcItem = paramItems.FirstOrDefault(t1 => t1.ID == src.PARAM_ITEM_ID);
                    if (srcItem != null)
                    {
                        var desItem = paramItems.FirstOrDefault(t => t.ORG_ID == item && srcItem.CODE == t.CODE);
                        if (desItem != null)
                            dest.PARAM_ITEM_ID = desItem.ID;
                    }
                },
                (t) =>
                {
                    var srcItem = paramItems.FirstOrDefault(t1 => t1.ID == t.PARAM_ITEM_ID);
                    List<Guid> destSchemeIds = new List<Guid>();
                    if (srcItem != null)
                        destSchemeIds = paramItems.Where(t1 => t1.CODE == srcItem.CODE).Select(t1 => t1.ID).ToList();
                    return paramSchemeDetails.FirstOrDefault(t1 => t1.ORG_ID == item && destSchemeIds.Contains(t1.ID));
                },
                null,
                (t) =>
                {
                    if (!paramSchemeDetailIds.Contains(t.ID))
                        paramSchemeDetailIds.Add(t.ID);
                    if (!paramSchemeDetails.Any(t1 => t1.ID == t.ID))
                        paramSchemeDetails.Add(t);
                });
            }


            #endregion

            #region 表单配置信息
            //表单
            var forms = this.GetEntities<T_PF_FORM>(t => orgIds.Contains(t.ORG_ID ?? Guid.Empty), new BaseFilter()).ToList();
            var srcForms = forms.Where(t => t.ORG_ID == srcOrgId).ToList();
            foreach (var item in destOrgIds)
            {
                if (!entityRelations.TryGetValue(item, out tempRels))
                {
                    tempRels = new Dictionary<Guid, Guid>();
                    entityRelations.Add(item, tempRels);
                }

                GetConfigBySrc<T_PF_FORM>(srcForms, item, tempRels, destAddForms, destUpdateForms, string.Empty, startTime, null,
                (t) =>
                {
                    return forms.FirstOrDefault(t1 => t1.ORG_ID == item && t1.CODE == t.CODE);
                },
                null,
                (t) =>
                {
                    if (!updateFormIds.Contains(t.ID))
                        updateFormIds.Add(t.ID);
                    if (!forms.Any(t1 => t1.ID == t.ID))
                        forms.Add(t);
                });
            }

            #region 编辑页			
            var pageEdits = this.GetEntities<T_PF_PAGE_EDIT>(t => orgIds.Contains(t.ORG_ID ?? Guid.Empty), new BaseFilter()).ToList();
            var srcPageEdits = pageEdits.Where(t => t.ORG_ID == srcOrgId).ToList();
            foreach (var item in destOrgIds)
            {
                if (!entityRelations.TryGetValue(item, out tempRels))
                {
                    tempRels = new Dictionary<Guid, Guid>();
                    entityRelations.Add(item, tempRels);
                }

                GetConfigBySrcOnTree<T_PF_PAGE_EDIT>(srcPageEdits, null, null, item, tempRels, destAddEdits, destUpdateEdits, "PAGE_FORM_ID", startTime,
                    (src, dest, rels) =>
                    {
                        dest.NAV_PROPERTY = src.NAV_PROPERTY;
                        T_PF_PARAM_SCHEME destParamScheme = null;
                        if (src.PARAM_SCHEME_ID != null)
                        {
                            var srcScheme = paramSchemes.FirstOrDefault(t1 => t1.ID == src.PARAM_SCHEME_ID);
                            if (srcScheme != null)
                                destParamScheme = paramSchemes.FirstOrDefault(t1 => t1.ORG_ID == dest.ORG_ID && t1.CODE == srcScheme.CODE);
                        }
                        if (destParamScheme != null)
                            dest.PARAM_SCHEME_ID = destParamScheme.ID;
                        else
                            dest.PARAM_SCHEME_ID = null;
                    },
                    (src, destParentId) =>
                    {
                        Guid destPageFormId;
                        if (src.PAGE_FORM_ID != null && tempRels.TryGetValue(src.PAGE_FORM_ID.Value, out destPageFormId))
                        {
                            return pageEdits.FirstOrDefault(t1 => t1.PAGE_FORM_ID == destPageFormId &&
                                    t1.ORG_ID == item && t1.CODE == src.CODE && t1.PARENT_ID == destParentId);
                        }
                        return null;
                    },
                    null,
                    t =>
                    {
                        if (t.PAGE_FORM_ID != null && !updateFormIds.Contains(t.PAGE_FORM_ID.Value))
                            updateFormIds.Add(t.PAGE_FORM_ID.Value);
                        if (!pageEdits.Any(t1 => t1.ID == t.ID))
                            pageEdits.Add(t);
                    });
            }


            var editPanels = this.GetEntities<T_PF_PAGE_EDIT_PANEL>(t => orgIds.Contains(t.ORG_ID ?? Guid.Empty), new BaseFilter()).ToList();
            var srcEditPanels = editPanels.Where(t => t.ORG_ID == srcOrgId).ToList();
            foreach (var item in destOrgIds)
            {
                if (!entityRelations.TryGetValue(item, out tempRels))
                {
                    tempRels = new Dictionary<Guid, Guid>();
                    entityRelations.Add(item, tempRels);
                }
                GetConfigBySrc<T_PF_PAGE_EDIT_PANEL>(srcEditPanels, item, tempRels, destAddEditPanels, destUpdateEditPanels, "PAGE_EDIT_ID", startTime, null,
                    t =>
                    {
                        Guid destId;
                        if (tempRels.TryGetValue(t.PAGE_EDIT_ID, out destId))
                        {
                            return editPanels.FirstOrDefault(t1 => t1.PAGE_EDIT_ID == destId &&
                                    t1.ORG_ID == item && t1.LABEL == t.LABEL);
                        }
                        return null;
                    },
                    null,
                    t =>
                    {
                        var pageEdit = pageEdits.FirstOrDefault(t1 => t1.ID == t.PAGE_EDIT_ID);
                        if (pageEdit != null)
                        {
                            if (pageEdit.PAGE_FORM_ID != null && !updateFormIds.Contains(pageEdit.PAGE_FORM_ID.Value))
                                updateFormIds.Add(pageEdit.PAGE_FORM_ID.Value);
                        }
                        if (!editPanels.Any(t1 => t1.ID == t.ID))
                            editPanels.Add(t);
                    });
            }


            var editColumns = this.GetEntities<T_PF_EDIT_COLUMN>(t => orgIds.Contains(t.ORG_ID ?? Guid.Empty), new BaseFilter()).ToList();
            var srcEditColumns = editColumns.Where(t => t.ORG_ID == srcOrgId).ToList();
            foreach (var item in destOrgIds)
            {
                if (!entityRelations.TryGetValue(item, out tempRels))
                {
                    tempRels = new Dictionary<Guid, Guid>();
                    entityRelations.Add(item, tempRels);
                }
                GetConfigBySrc<T_PF_EDIT_COLUMN>(srcEditColumns, item, tempRels, destAddEditColumns, destUpdateEditColumns, "PAGE_EDIT_ID", startTime,
                    (src, dest, rel) =>
                    {
                        Guid temp;
                        if (src.PAGE_EDIT_PANEL_ID != null && rel.TryGetValue(src.PAGE_EDIT_PANEL_ID.Value, out temp))
                            dest.PAGE_EDIT_PANEL_ID = temp;
                    },
                    (t) =>
                    {
                        Guid destId;
                        if (tempRels.TryGetValue(t.PAGE_EDIT_ID, out destId))
                        {
                            return editColumns.FirstOrDefault(t1 => t1.ORG_ID == item && t1.FIELD_NAME == t.FIELD_NAME
                            && t1.PAGE_EDIT_ID == destId);
                        }
                        return null;
                    },
                    null,
                    (t) =>
                    {
                        var pageEdit = pageEdits.FirstOrDefault(t1 => t1.ID == t.PAGE_EDIT_ID);
                        if (pageEdit != null)
                        {
                            if (pageEdit.PAGE_FORM_ID != null && !updateFormIds.Contains(pageEdit.PAGE_FORM_ID.Value))
                                updateFormIds.Add(pageEdit.PAGE_FORM_ID.Value);
                        }
                        if (!editColumns.Any(t1 => t1.ID == t.ID))
                            editColumns.Add(t);
                    });
            }

            var editColumnFilters = this.GetEntities<T_PF_EDIT_COLUMN_FILTER>(t => orgIds.Contains(t.ORG_ID ?? Guid.Empty), new BaseFilter()).ToList();
            var srcEditColumnFilters = editColumnFilters.Where(t => t.ORG_ID == srcOrgId).ToList();
            foreach (var item in destOrgIds)
            {
                if (!entityRelations.TryGetValue(item, out tempRels))
                {
                    tempRels = new Dictionary<Guid, Guid>();
                    entityRelations.Add(item, tempRels);
                }
                GetConfigBySrc<T_PF_EDIT_COLUMN_FILTER>(srcEditColumnFilters, item, tempRels, destAddEditColumnFilters, destUpdateEditColumnFilters, "EDIT_COLUMN_ID", startTime, null,
                    t =>
                    {
                        Guid destId;
                        if (tempRels.TryGetValue(t.EDIT_COLUMN_ID, out destId))
                        {
                            return editColumnFilters.FirstOrDefault(t1 => t1.ORG_ID == item && t1.FIELD_NAME == t.FIELD_NAME
                            && t1.OPERATION == t.OPERATION && t1.VALUE == t.VALUE && t1.IS_CUSTOM == t.IS_CUSTOM
                            && t1.EDIT_COLUMN_ID == destId);
                        }
                        return null;
                    },
                    null,
                    t =>
                    {
                        var editColumn = editColumns.FirstOrDefault(t1 => t1.ID == t.EDIT_COLUMN_ID);
                        if (editColumn != null)
                        {
                            var pageEdit = pageEdits.FirstOrDefault(t1 => t1.ID == editColumn.PAGE_EDIT_ID);
                            if (pageEdit != null)
                            {
                                if (pageEdit.PAGE_FORM_ID != null && !updateFormIds.Contains(pageEdit.PAGE_FORM_ID.Value))
                                    updateFormIds.Add(pageEdit.PAGE_FORM_ID.Value);
                            }
                        }

                        if (!editColumnFilters.Any(t1 => t1.ID == t.ID))
                            editColumnFilters.Add(t);
                    });
            }


            var editColumnFillMaps = this.GetEntities<T_PF_EDIT_COLUMN_FILL_MAP>(t => orgIds.Contains(t.ORG_ID ?? Guid.Empty), new BaseFilter()).ToList();
            var srcEditColumnFillMaps = editColumnFillMaps.Where(t => t.ORG_ID == srcOrgId).ToList();
            foreach (var item in destOrgIds)
            {
                if (!entityRelations.TryGetValue(item, out tempRels))
                {
                    tempRels = new Dictionary<Guid, Guid>();
                    entityRelations.Add(item, tempRels);
                }
                GetConfigBySrc<T_PF_EDIT_COLUMN_FILL_MAP>(srcEditColumnFillMaps, item, tempRels, destAddEditColumnFillMaps, destUpdateEditColumnFillMaps, "EDIT_COLUMN_ID", startTime, null,
                    t =>
                    {
                        Guid destId;
                        if (tempRels.TryGetValue(t.EDIT_COLUMN_ID, out destId))
                        {
                            return editColumnFillMaps.FirstOrDefault(t1 => t1.ORG_ID == item && t1.SRC_NAV == t.SRC_NAV
                                && t1.EDIT_COLUMN_ID == destId);
                        }
                        return null;
                    },
                    null,
                    t =>
                    {
                        var editColumn = editColumns.FirstOrDefault(t1 => t1.ID == t.EDIT_COLUMN_ID);
                        if (editColumn != null)
                        {
                            var pageEdit = pageEdits.FirstOrDefault(t1 => t1.ID == editColumn.PAGE_EDIT_ID);
                            if (pageEdit != null)
                            {
                                if (pageEdit.PAGE_FORM_ID != null && !updateFormIds.Contains(pageEdit.PAGE_FORM_ID.Value))
                                    updateFormIds.Add(pageEdit.PAGE_FORM_ID.Value);
                            }
                        }

                        if (!editColumnFillMaps.Any(t1 => t1.ID == t.ID))
                            editColumnFillMaps.Add(t);
                    });
            }

            var editColumnFillMapDetails = this.GetEntities<T_PF_EDIT_COLUMN_FILL_MAP_D>(t => orgIds.Contains(t.ORG_ID ?? Guid.Empty), new BaseFilter()).ToList();
            var srcEditColumnFillMapDetails = editColumnFillMapDetails.Where(t => t.ORG_ID == srcOrgId).ToList();
            foreach (var item in destOrgIds)
            {
                if (!entityRelations.TryGetValue(item, out tempRels))
                {
                    tempRels = new Dictionary<Guid, Guid>();
                    entityRelations.Add(item, tempRels);
                }
                GetConfigBySrc<T_PF_EDIT_COLUMN_FILL_MAP_D>(srcEditColumnFillMapDetails, item, tempRels, destAddEditColumnFillMapDs, destUpdateEditColumnFillMapDs, "EDIT_COLUMN_FILL_MAP_ID", startTime, null,
                    t =>
                    {
                        Guid destId;
                        if (tempRels.TryGetValue(t.EDIT_COLUMN_FILL_MAP_ID, out destId))
                        {
                            return editColumnFillMapDetails.FirstOrDefault(t1 => t1.ORG_ID == item && t1.SRC_FIELD_NAME == t.SRC_FIELD_NAME
                                && t1.DEST_FIELD_NAME == t.DEST_FIELD_NAME && t1.EDIT_COLUMN_FILL_MAP_ID == destId);
                        }
                        return null;
                    },
                    null,
                    t =>
                    {
                        var map = editColumnFillMaps.FirstOrDefault(t1 => t.ID == t.EDIT_COLUMN_FILL_MAP_ID);
                        if (map != null)
                        {
                            var editColumn = editColumns.FirstOrDefault(t1 => t1.ID == map.EDIT_COLUMN_ID);
                            if (editColumn != null)
                            {
                                var pageEdit = pageEdits.FirstOrDefault(t1 => t1.ID == editColumn.PAGE_EDIT_ID);
                                if (pageEdit != null)
                                {
                                    if (pageEdit.PAGE_FORM_ID != null && !updateFormIds.Contains(pageEdit.PAGE_FORM_ID.Value))
                                        updateFormIds.Add(pageEdit.PAGE_FORM_ID.Value);
                                }
                            }
                        }
                        if (!editColumnFillMapDetails.Any(t1 => t1.ID == t.ID))
                            editColumnFillMapDetails.Add(t);
                    });
            }
            #endregion

            #region 自定义页
            var pageCustoms = this.GetEntities<T_PF_PAGE_CUSTOM>(t => orgIds.Contains(t.ORG_ID ?? Guid.Empty), new BaseFilter()).ToList();
            var srcPageCustoms = pageCustoms.Where(t => t.ORG_ID == srcOrgId).ToList();
            foreach (var item in destOrgIds)
            {
                if (!entityRelations.TryGetValue(item, out tempRels))
                {
                    tempRels = new Dictionary<Guid, Guid>();
                    entityRelations.Add(item, tempRels);
                }
                GetConfigBySrc<T_PF_PAGE_CUSTOM>(srcPageCustoms, item, tempRels, destAddCustoms, destUpdateCustoms, "PAGE_FORM_ID", startTime, null,
                    t =>
                    {
                        Guid destPageFormId;
                        if (t.PAGE_FORM_ID != null && tempRels.TryGetValue(t.PAGE_FORM_ID.Value, out destPageFormId))
                        {
                            return pageCustoms.FirstOrDefault(t1 => t1.PAGE_FORM_ID == destPageFormId &&
                                    t1.ORG_ID == item && t1.COMPONENT_NAME == t.COMPONENT_NAME);
                        }
                        return null;
                    },
                    null,
                    t =>
                    {
                        if (t.PAGE_FORM_ID != null && !updateFormIds.Contains(t.PAGE_FORM_ID.Value))
                            updateFormIds.Add(t.PAGE_FORM_ID.Value);
                        if (!pageCustoms.Any(t1 => t1.ID == t.ID))
                            pageCustoms.Add(t);
                    });
            }
            #endregion

            #region 图表页 

            var pageCharts = this.GetEntities<T_PF_PAGE_CHART>(t => orgIds.Contains(t.ORG_ID ?? Guid.Empty), new BaseFilter()).ToList();
            var srcPageCharts = pageCharts.Where(t => t.ORG_ID == srcOrgId).ToList();
            foreach (var item in destOrgIds)
            {
                if (!entityRelations.TryGetValue(item, out tempRels))
                {
                    tempRels = new Dictionary<Guid, Guid>();
                    entityRelations.Add(item, tempRels);
                }
                GetConfigBySrc<T_PF_PAGE_CHART>(srcPageCharts, item, tempRels, destAddCharts, destUpdateCharts, "PAGE_FORM_ID", startTime, null,
                    t =>
                    {
                        Guid destPageFormId;
                        if (t.PAGE_FORM_ID != null && tempRels.TryGetValue(t.PAGE_FORM_ID.Value, out destPageFormId))
                        {
                            return pageCharts.FirstOrDefault(t1 => t1.PAGE_FORM_ID == destPageFormId &&
                                    t1.ORG_ID == item && t1.CODE == t.CODE);
                        }
                        return null;
                    },
                    null,
                    t =>
                    {
                        if (t.PAGE_FORM_ID != null && !updateFormIds.Contains(t.PAGE_FORM_ID.Value))
                            updateFormIds.Add(t.PAGE_FORM_ID.Value);
                        if (!pageCharts.Any(t1 => t1.ID == t.ID))
                            pageCharts.Add(t);
                    });
            }
            var pageChartParams = this.GetEntities<T_PF_PAGE_CHART_PARAM>(t => orgIds.Contains(t.ORG_ID ?? Guid.Empty), new BaseFilter()).ToList();
            var srcPageChartParams = pageChartParams.Where(t => t.ORG_ID == srcOrgId).ToList();
            foreach (var item in destOrgIds)
            {
                if (!entityRelations.TryGetValue(item, out tempRels))
                {
                    tempRels = new Dictionary<Guid, Guid>();
                    entityRelations.Add(item, tempRels);
                }
                GetConfigBySrc<T_PF_PAGE_CHART_PARAM>(srcPageChartParams, item, tempRels, destAddChartParams, destUpdateChartParams, "PAGE_CHART_ID", startTime, null,
                    t =>
                    {
                        Guid destId;
                        if (tempRels.TryGetValue(t.PAGE_CHART_ID, out destId))
                        {
                            return pageChartParams.FirstOrDefault(t1 => t1.PAGE_CHART_ID == destId &&
                                t1.FIELD_NAME == t.FIELD_NAME && t1.OPERATION == t.OPERATION && t1.VALUE == t.VALUE &&
                                t1.IS_CUSTOM == t.IS_CUSTOM && t1.ORG_ID == item);
                        }
                        return null;
                    },
                    null,
                    t =>
                    {
                        var pageChart = pageCharts.FirstOrDefault(t1 => t.ID == t.PAGE_CHART_ID);
                        if (pageChart != null)
                        {
                            if (pageChart.PAGE_FORM_ID != null && !updateFormIds.Contains(pageChart.PAGE_FORM_ID.Value))
                                updateFormIds.Add(pageChart.PAGE_FORM_ID.Value);
                        }
                        if (!pageChartParams.Any(t1 => t1.ID == t.ID))
                            pageChartParams.Add(t);
                    });
            }
            #endregion

            #region 列表页 
            var pageTables = this.GetEntities<T_PF_PAGE_TABLE>(t => orgIds.Contains(t.ORG_ID ?? Guid.Empty), new BaseFilter()).ToList();
            var srcPageTables = pageTables.Where(t => t.ORG_ID == srcOrgId).ToList();
            foreach (var item in destOrgIds)
            {
                if (!entityRelations.TryGetValue(item, out tempRels))
                {
                    tempRels = new Dictionary<Guid, Guid>();
                    entityRelations.Add(item, tempRels);
                }
                GetConfigBySrcOnTree<T_PF_PAGE_TABLE>(srcPageTables, null, null, item, tempRels, destAddTables, destUpdateTables, "PAGE_FORM_ID", startTime, (src, dest, rels) =>
                {
                    T_PF_PARAM_SCHEME destParamScheme = null;
                    if (src.PARAM_SCHEME_ID != null)
                    {
                        var srcScheme = paramSchemes.FirstOrDefault(t1 => t1.ID == src.PARAM_SCHEME_ID);
                        if (srcScheme != null)
                            destParamScheme = paramSchemes.FirstOrDefault(t1 => t1.ORG_ID == dest.ORG_ID && t1.CODE == srcScheme.CODE);
                    }
                    if (destParamScheme != null)
                        dest.PARAM_SCHEME_ID = destParamScheme.ID;
                    else
                        dest.PARAM_SCHEME_ID = null;
                },
                    (src, destParentId) =>
                    {
                        Guid destPageFormId;
                        if (src.PAGE_FORM_ID != null && tempRels.TryGetValue(src.PAGE_FORM_ID.Value, out destPageFormId))
                        {
                            return pageTables.FirstOrDefault(t1 => t1.PAGE_FORM_ID == destPageFormId &&
                                    t1.ORG_ID == item && t1.CODE == src.CODE && t1.PARENT_ID == destParentId);
                        }
                        return null;

                    },
                    null,
                    t =>
                    {
                        if (t.PAGE_FORM_ID != null && !updateFormIds.Contains(t.PAGE_FORM_ID.Value))
                            updateFormIds.Add(t.PAGE_FORM_ID.Value);
                        if (!pageTables.Any(t1 => t1.ID == t.ID))
                            pageTables.Add(t);
                    });
            }

            var tablePanels = this.GetEntities<T_PF_PAGE_TABLE_PANEL>(t => orgIds.Contains(t.ORG_ID ?? Guid.Empty), new BaseFilter()).ToList();
            var srcTablePanels = tablePanels.Where(t => t.ORG_ID == srcOrgId).ToList();
            foreach (var item in destOrgIds)
            {
                if (!entityRelations.TryGetValue(item, out tempRels))
                {
                    tempRels = new Dictionary<Guid, Guid>();
                    entityRelations.Add(item, tempRels);
                }
                GetConfigBySrc<T_PF_PAGE_TABLE_PANEL>(srcTablePanels, item, tempRels, destAddTablePanels, destUpdateTablePanels, "PAGE_TABLE_ID", startTime, null,
                    t =>
                    {
                        Guid destId;
                        if (tempRels.TryGetValue(t.PAGE_TABLE_ID, out destId))
                        {
                            return tablePanels.FirstOrDefault(t1 => t1.PAGE_TABLE_ID == destId &&
                                    t1.ORG_ID == item && t1.LABEL == t.LABEL);
                        }
                        return null;
                    },
                    null,
                    t =>
                    {
                        var pageEdit = pageTables.FirstOrDefault(t1 => t1.ID == t.PAGE_TABLE_ID);
                        if (pageEdit != null)
                        {
                            if (pageEdit.PAGE_FORM_ID != null && !updateFormIds.Contains(pageEdit.PAGE_FORM_ID.Value))
                                updateFormIds.Add(pageEdit.PAGE_FORM_ID.Value);
                        }
                        if (!tablePanels.Any(t1 => t1.ID == t.ID))
                            tablePanels.Add(t);
                    });
            }

            var columns = this.GetEntities<T_PF_COLUMN>(t => orgIds.Contains(t.ORG_ID ?? Guid.Empty), new BaseFilter()).ToList();
            var srcColumns = columns.Where(t => t.ORG_ID == srcOrgId).ToList();
            foreach (var item in destOrgIds)
            {
                if (!entityRelations.TryGetValue(item, out tempRels))
                {
                    tempRels = new Dictionary<Guid, Guid>();
                    entityRelations.Add(item, tempRels);
                }
                GetConfigBySrc<T_PF_COLUMN>(srcColumns, item, tempRels, destAddColums, destUpdateColums, "PAGE_TABLE_ID", startTime,
                    (src, dest, rel) =>
                    {
                        Guid temp;
                        if (src.PAGE_TABLE_PANEL_ID != null && rel.TryGetValue(src.PAGE_TABLE_PANEL_ID.Value, out temp))
                            dest.PAGE_TABLE_PANEL_ID = temp;
                    },
                    t =>
                    {
                        Guid destId;
                        if (t.PAGE_TABLE_ID != null && tempRels.TryGetValue(t.PAGE_TABLE_ID.Value, out destId))
                        {
                            return columns.FirstOrDefault(t1 => t1.ORG_ID == item && t1.FIELD_NAME == t.FIELD_NAME
                                && t1.PAGE_TABLE_ID == destId);
                        }
                        return null;
                    },
                    null,
                    t =>
                    {
                        var pageTable = pageTables.FirstOrDefault(t1 => t1.ID == t.PAGE_TABLE_ID);
                        if (pageTable != null)
                        {
                            if (pageTable.PAGE_FORM_ID != null && !updateFormIds.Contains(pageTable.PAGE_FORM_ID.Value))
                                updateFormIds.Add(pageTable.PAGE_FORM_ID.Value);
                        }
                        if (!columns.Any(t1 => t1.ID == t.ID))
                            columns.Add(t);

                    });
            }
            var columnFilters = this.GetEntities<T_PF_COLUMN_FILTER>(t => orgIds.Contains(t.ORG_ID ?? Guid.Empty), new BaseFilter()).ToList();
            var srcColumnFilters = columnFilters.Where(t => t.ORG_ID == srcOrgId).ToList();
            foreach (var item in destOrgIds)
            {
                if (!entityRelations.TryGetValue(item, out tempRels))
                {
                    tempRels = new Dictionary<Guid, Guid>();
                    entityRelations.Add(item, tempRels);
                }
                GetConfigBySrc<T_PF_COLUMN_FILTER>(srcColumnFilters, item, tempRels, destAddColumFilters, destUpdateColumFilters, "COLUMN_ID", startTime, null,
                    t =>
                    {
                        Guid destId;
                        if (tempRels.TryGetValue(t.COLUMN_ID, out destId))
                        {
                            return srcColumnFilters.FirstOrDefault(t1 => t1.ORG_ID == item && t1.FIELD_NAME == t.FIELD_NAME
                            && t1.OPERATION == t.OPERATION && t1.VALUE == t.VALUE && t1.IS_CUSTOM == t.IS_CUSTOM
                            && t1.COLUMN_ID == destId);
                        }
                        return null;
                    },
                    null,
                    t =>
                    {
                        var column = columns.FirstOrDefault(t1 => t1.ID == t.COLUMN_ID);
                        if (column != null)
                        {
                            var pageTable = pageTables.FirstOrDefault(t1 => t1.ID == column.PAGE_TABLE_ID);
                            if (pageTable != null)
                            {
                                if (pageTable.PAGE_FORM_ID != null && !updateFormIds.Contains(pageTable.PAGE_FORM_ID.Value))
                                    updateFormIds.Add(pageTable.PAGE_FORM_ID.Value);
                            }
                        }

                        if (!columnFilters.Any(t1 => t1.ID == t.ID))
                            columnFilters.Add(t);
                    });
            }
            var tableParams = this.GetEntities<T_PF_TABLE_PARAMS>(t => orgIds.Contains(t.ORG_ID ?? Guid.Empty), new BaseFilter()).ToList();
            var srcTableParams = tableParams.Where(t => t.ORG_ID == srcOrgId).ToList();
            foreach (var item in destOrgIds)
            {
                if (!entityRelations.TryGetValue(item, out tempRels))
                {
                    tempRels = new Dictionary<Guid, Guid>();
                    entityRelations.Add(item, tempRels);
                }
                GetConfigBySrc<T_PF_TABLE_PARAMS>(srcTableParams, item, tempRels, destAddTableParams, destUpdateTableParams, "PAGE_TABLE_ID", startTime, null,
                    t =>
                    {
                        Guid destId;
                        if (tempRels.TryGetValue(t.PAGE_TABLE_ID, out destId))
                        {
                            return tableParams.FirstOrDefault(t1 => t1.ORG_ID == item && t1.FIELD_NAME == t.FIELD_NAME
                                && t1.PAGE_TABLE_ID == destId);
                        }
                        return null;
                    },
                    null,
                    t =>
                    {
                        var pageTable = pageTables.FirstOrDefault(t1 => t1.ID == t.PAGE_TABLE_ID);
                        if (pageTable != null)
                        {
                            if (pageTable.PAGE_FORM_ID != null && !updateFormIds.Contains(pageTable.PAGE_FORM_ID.Value))
                                updateFormIds.Add(pageTable.PAGE_FORM_ID.Value);
                        }
                        if (!tableParams.Any(t1 => t1.ID == t.ID))
                            tableParams.Add(t);
                    });
            }


            #endregion
            #region 查询

            var formQuerys = this.GetEntities<T_PF_FORM_QUERY>(t => orgIds.Contains(t.ORG_ID ?? Guid.Empty), new BaseFilter()).ToList();
            var srcFormQuerys = formQuerys.Where(t => t.ORG_ID == srcOrgId).ToList();
            foreach (var item in destOrgIds)
            {
                if (!entityRelations.TryGetValue(item, out tempRels))
                {
                    tempRels = new Dictionary<Guid, Guid>();
                    entityRelations.Add(item, tempRels);
                }
                GetConfigBySrc<T_PF_FORM_QUERY>(srcFormQuerys, item, tempRels, destAddFormQuerys, destUpdateFormQuerys, "PAGE_FORM_ID", startTime, null,
                    t =>
                    {
                        Guid destPageFormId;
                        if (t.PAGE_FORM_ID != null && tempRels.TryGetValue(t.PAGE_FORM_ID.Value, out destPageFormId))
                        {
                            return formQuerys.FirstOrDefault(t1 => t1.PAGE_FORM_ID == destPageFormId &&
                                    t1.ORG_ID == item && t1.CODE == t.CODE);
                        }
                        return null;
                    },
                    null,
                    t =>
                    {
                        if (t.PAGE_FORM_ID != null && !updateFormIds.Contains(t.PAGE_FORM_ID.Value))
                            updateFormIds.Add(t.PAGE_FORM_ID.Value);
                        if (!formQuerys.Any(t1 => t1.ID == t.ID))
                            formQuerys.Add(t);
                    });
            }
            var querys = this.GetEntities<T_PF_QUERY>(t => orgIds.Contains(t.ORG_ID ?? Guid.Empty), new BaseFilter()).ToList();
            var srcQuerys = querys.Where(t => t.ORG_ID == srcOrgId).ToList();
            foreach (var item in destOrgIds)
            {
                if (!entityRelations.TryGetValue(item, out tempRels))
                {
                    tempRels = new Dictionary<Guid, Guid>();
                    entityRelations.Add(item, tempRels);
                }
                GetConfigBySrc<T_PF_QUERY>(srcQuerys, item, tempRels, destAddQuerys, destUpdateQuerys, "PAGE_FORM_ID", startTime, (src, dest, rels) =>
                    {
                        Guid formQueryId;
                        if (src.PAGE_FORM_QUERY_ID != null && rels.TryGetValue(src.PAGE_FORM_QUERY_ID.Value, out formQueryId))
                            dest.PAGE_FORM_QUERY_ID = formQueryId;
                    },
                    t =>
                    {
                        Guid destId;
                        if (t.PAGE_FORM_QUERY_ID != null && tempRels.TryGetValue(t.PAGE_FORM_QUERY_ID.Value, out destId))
                        {
                            return querys.FirstOrDefault(t1 => t1.ORG_ID == item && t1.QUERY_NAME == t.QUERY_NAME &&
                            t1.OP_DEFAULT == t.OP_DEFAULT && t1.DATA_VALUE == t.DATA_VALUE && t1.IS_CUSTOM == t.IS_CUSTOM
                            && t1.PAGE_FORM_QUERY_ID == destId);
                        }
                        return null;
                    },
                    null,
                    t =>
                    {
                        var formQury = formQuerys.FirstOrDefault(t1 => t1.ID == t.PAGE_FORM_QUERY_ID);
                        if (formQury != null)
                        {
                            if (formQury.PAGE_FORM_ID != null && !updateFormIds.Contains(formQury.PAGE_FORM_ID.Value))
                                updateFormIds.Add(formQury.PAGE_FORM_ID.Value);
                        }
                        if (!querys.Any(t1 => t1.ID == t.ID))
                            querys.Add(t);
                    });
            }
            #endregion
            #region 树页面


            var pageTrees = this.GetEntities<T_PF_PAGE_TREE>(t => orgIds.Contains(t.ORG_ID ?? Guid.Empty), new BaseFilter()).ToList();
            var srcPageTrees = pageTrees.Where(t => t.ORG_ID == srcOrgId).ToList();
            foreach (var item in destOrgIds)
            {
                if (!entityRelations.TryGetValue(item, out tempRels))
                {
                    tempRels = new Dictionary<Guid, Guid>();
                    entityRelations.Add(item, tempRels);
                }
                GetConfigBySrc<T_PF_PAGE_TREE>(srcPageTrees, item, tempRels, destAddTrees, destUpdateTrees, "PAGE_FORM_ID", startTime, null,
                    t =>
                    {
                        Guid destPageFormId;
                        if (t.PAGE_FORM_ID != null && tempRels.TryGetValue(t.PAGE_FORM_ID.Value, out destPageFormId))
                        {
                            return pageTrees.FirstOrDefault(t1 => t1.PAGE_FORM_ID == destPageFormId &&
                                    t1.ORG_ID == item && t1.CODE == t.CODE);
                        }
                        return null;
                    },
                    null,
                    t =>
                    {
                        if (t.PAGE_FORM_ID != null && !updateFormIds.Contains(t.PAGE_FORM_ID.Value))
                            updateFormIds.Add(t.PAGE_FORM_ID.Value);
                        if (!pageTrees.Any(t1 => t1.ID == t.ID))
                            pageTrees.Add(t);
                    });
            }
            var treeColumns = this.GetEntities<T_PF_TREE_COLUMN>(t => orgIds.Contains(t.ORG_ID ?? Guid.Empty), new BaseFilter()).ToList();
            var srcTreeColumns = treeColumns.Where(t => t.ORG_ID == srcOrgId).ToList();
            foreach (var item in destOrgIds)
            {
                if (!entityRelations.TryGetValue(item, out tempRels))
                {
                    tempRels = new Dictionary<Guid, Guid>();
                    entityRelations.Add(item, tempRels);
                }
                GetConfigBySrc<T_PF_TREE_COLUMN>(srcTreeColumns, item, tempRels, destAddTreeColumns, destUpdateTreeColumns, "PAGE_TREE_ID", startTime, null,
                    t =>
                    {
                        Guid destPageFormId;
                        if (tempRels.TryGetValue(t.PAGE_TREE_ID, out destPageFormId))
                        {
                            return treeColumns.FirstOrDefault(t1 => t1.PAGE_TREE_ID == destPageFormId &&
                                    t1.ORG_ID == item && t1.FIELD_NAME == t.FIELD_NAME);
                        }
                        return null;
                    },
                    null,
                    t =>
                    {
                        var pageTree = pageTrees.FirstOrDefault(t1 => t1.ID == t.PAGE_TREE_ID);
                        if (pageTree != null)
                        {
                            if (pageTree.PAGE_FORM_ID != null && !updateFormIds.Contains(pageTree.PAGE_FORM_ID.Value))
                                updateFormIds.Add(pageTree.PAGE_FORM_ID.Value);
                        }
                        if (!treeColumns.Any(t1 => t1.ID == t.ID))
                            treeColumns.Add(t);
                    });
            }
            #endregion

            #region 按钮
            var btns = this.GetEntities<T_PF_BTN>(t => orgIds.Contains(t.ORG_ID ?? Guid.Empty), new BaseFilter()).ToList();
            var srcBtns = btns.Where(t => t.ORG_ID == srcOrgId).ToList();
            foreach (var item in destOrgIds)
            {
                if (!entityRelations.TryGetValue(item, out tempRels))
                {
                    tempRels = new Dictionary<Guid, Guid>();
                    entityRelations.Add(item, tempRels);
                }
                GetConfigBySrc<T_PF_BTN>(srcBtns, item, tempRels, destAddBtns, destUpdateBtns, string.Empty, startTime,
                    (src, dest, rels) =>
                    {
                        Guid destId;
                        if (src.PAGE_CUSTOM_FORM_ID != null && rels.TryGetValue(src.PAGE_CUSTOM_FORM_ID.Value, out destId))
                        {
                            dest.PAGE_CUSTOM_FORM_ID = destId;
                            dest.PAGE_ID = destId;
                        }
                        if (src.PAGE_TABLE_ID != null && rels.TryGetValue(src.PAGE_TABLE_ID.Value, out destId))
                        {
                            dest.PAGE_TABLE_ID = destId;
                            dest.PAGE_ID = destId;
                        }

                        if (src.PAGE_EDIT_ID != null && rels.TryGetValue(src.PAGE_EDIT_ID.Value, out destId))
                        {
                            dest.PAGE_EDIT_ID = destId;
                            dest.PAGE_ID = destId;
                        }

                    },
                     t =>
                     {
                         Guid destId;
                         if (t.PAGE_CUSTOM_FORM_ID != null && tempRels.TryGetValue(t.PAGE_CUSTOM_FORM_ID.Value, out destId))
                             return btns.FirstOrDefault(t1 => t1.PAGE_CUSTOM_FORM_ID == destId && t1.ORG_ID == item &&
                                 t1.LABEL == t.LABEL && t1.BTN_TYPE == t.BTN_TYPE && t1.BTN_FUN_TYPE == t.BTN_FUN_TYPE);
                         if (t.PAGE_TABLE_ID != null && tempRels.TryGetValue(t.PAGE_TABLE_ID.Value, out destId))
                             return btns.FirstOrDefault(t1 => t1.PAGE_TABLE_ID == destId && t1.ORG_ID == item &&
                                 t1.LABEL == t.LABEL && t1.BTN_TYPE == t.BTN_TYPE && t1.BTN_FUN_TYPE == t.BTN_FUN_TYPE);
                         if (t.PAGE_EDIT_ID != null && tempRels.TryGetValue(t.PAGE_EDIT_ID.Value, out destId))
                             return btns.FirstOrDefault(t1 => t1.PAGE_EDIT_ID == destId && t1.ORG_ID == item &&
                                 t1.LABEL == t.LABEL && t1.BTN_TYPE == t.BTN_TYPE && t1.BTN_FUN_TYPE == t.BTN_FUN_TYPE);
                         return null;
                     },
                    null,
                    t =>
                    {
                        Guid? formId = null;
                        if (t.PAGE_CUSTOM_FORM_ID != null)
                        {
                            var pageCustom = pageCustoms.FirstOrDefault(t1 => t.ID == t.PAGE_CUSTOM_FORM_ID);
                            if (pageCustom != null)
                                formId = pageCustom.PAGE_FORM_ID;
                        }
                        if (t.PAGE_TABLE_ID != null)
                        {

                            var pageTable = pageTables.FirstOrDefault(t1 => t.ID == t.PAGE_TABLE_ID);
                            if (pageTable != null)
                                formId = pageTable.PAGE_FORM_ID;

                        }
                        if (t.PAGE_EDIT_ID != null)
                        {
                            var pageEdit = pageEdits.FirstOrDefault(t1 => t.ID == t.PAGE_EDIT_ID);
                            if (pageEdit != null)
                                formId = pageEdit.PAGE_FORM_ID;
                            else
                            {
                                var pageTree = pageTrees.FirstOrDefault(t1 => t.ID == t.PAGE_EDIT_ID);
                                if (pageTree != null)
                                    formId = pageTree.PAGE_FORM_ID;
                            }
                        }
                        if (formId != null && !updateFormIds.Contains(formId.Value))
                            updateFormIds.Add(formId.Value);
                        if (!btns.Any(t1 => t1.ID == t.ID))
                            btns.Add(t);
                    });
            }
            #endregion
            #endregion



            #region 菜单
            var menus = this.GetEntities<T_PF_MENU>(t => orgIds.Contains(t.ORG_ID ?? Guid.Empty), new BaseFilter()).ToList();
            var srcMenus = menus.Where(t => t.ORG_ID == srcOrgId).ToList();
            foreach (var item in destOrgIds)
            {
                if (!entityRelations.TryGetValue(item, out tempRels))
                {
                    tempRels = new Dictionary<Guid, Guid>();
                    entityRelations.Add(item, tempRels);
                }
                GetConfigBySrcOnTree<T_PF_MENU>(srcMenus, null, null, item, tempRels, destAddMenus, destUpdateMenus, "MENU_FORM_ID", startTime, null,
                    (src, destParentId) =>
                    {
                        return menus.FirstOrDefault(t1 =>
                            t1.ORG_ID == item && t1.NAME == src.NAME && t1.PARENT_ID == destParentId);
                    },
                    null,
                    t =>
                    {
                        if (!menus.Any(t1 => t1.ID == t.ID))
                            menus.Add(t);
                    });
            }
            var appMenus = this.GetEntities<T_PF_APPMENU>(t => orgIds.Contains(t.ORG_ID ?? Guid.Empty), new BaseFilter()).ToList();
            var srcAppMenus = appMenus.Where(t => t.ORG_ID == srcOrgId).ToList();
            foreach (var item in destOrgIds)
            {
                if (!entityRelations.TryGetValue(item, out tempRels))
                {
                    tempRels = new Dictionary<Guid, Guid>();
                    entityRelations.Add(item, tempRels);
                }
                GetConfigBySrcOnTree<T_PF_APPMENU>(srcAppMenus, null, null, item, tempRels, destAddAppMenus, destUpdateAppMenus, "MENU_FORM_ID", startTime, null,
                    (src, destParentId) =>
                    {
                        return appMenus.FirstOrDefault(t1 =>
                            t1.ORG_ID == item && t1.NAME == src.NAME && t1.PARENT_ID == destParentId);
                    },
                    null,
                    t =>
                    {
                        if (!appMenus.Any(t1 => t1.ID == t.ID))
                            appMenus.Add(t);
                    });
            }

            var clientMenus = this.GetEntities<T_PF_CLIENT_MENU>(t => orgIds.Contains(t.ORG_ID ?? Guid.Empty), new BaseFilter()).ToList();
            var srcClientMenus = clientMenus.Where(t => t.ORG_ID == srcOrgId).ToList();
            foreach (var item in destOrgIds)
            {
                if (!entityRelations.TryGetValue(item, out tempRels))
                {
                    tempRels = new Dictionary<Guid, Guid>();
                    entityRelations.Add(item, tempRels);
                }
                GetConfigBySrcOnTree<T_PF_CLIENT_MENU>(srcClientMenus, null, null, item, tempRels, destAddClientMenus, destUpdateClientMenus, "MENU_FORM_ID", startTime, null,
                    (src, destParentId) =>
                    {
                        return clientMenus.FirstOrDefault(t1 =>
                            t1.ORG_ID == item && t1.NAME == src.NAME && t1.PARENT_ID == destParentId);
                    },
                    null,
                    t =>
                    {
                        if (!clientMenus.Any(t1 => t1.ID == t.ID))
                            clientMenus.Add(t);
                    });
            }
            #endregion

            #region 其他
            var codeRules = this.GetEntities<T_PF_CODE_RULE>(t => orgIds.Contains(t.ORG_ID ?? Guid.Empty), new BaseFilter()).ToList();
            var srcCodeRules = codeRules.Where(t => t.ORG_ID == srcOrgId).ToList();
            foreach (var item in destOrgIds)
            {
                if (!entityRelations.TryGetValue(item, out tempRels))
                {
                    tempRels = new Dictionary<Guid, Guid>();
                    entityRelations.Add(item, tempRels);
                }
                GetConfigBySrc<T_PF_CODE_RULE>(srcCodeRules, item, tempRels, destAddCodeRules, destUpdateCodeRules, string.Empty, startTime, null,
                    t =>
                    {
                        return codeRules.FirstOrDefault(t1 => t1.CODE_TYPE == t.CODE_TYPE && t1.ORG_ID == item);
                    },
                    null,
                    t =>
                    {
                        if (!codeRules.Any(t1 => t1.ID == t.ID))
                            codeRules.Add(t);
                    });
            }
            if (destAddCodeRules.Any())
                destAddCodeRules.ForEach(t => t.CURRENT_DATA = null);
            #endregion


            #region 保存
            UnifiedCommit(() =>
            {

                this.BantchAddEntityNoCommit(destAddForms);
                this.BantchAddEntityNoCommit(destAddParamItems);
                this.BantchAddEntityNoCommit(destAddParamSchemes);
                this.BantchAddEntityNoCommit(destAddParamSchemeDetails);
                this.TreeBantchAddEntityNoCommit(destAddEdits);
                this.BantchAddEntityNoCommit(destAddEditPanels);
                this.BantchAddEntityNoCommit(destAddEditColumns);
                this.BantchAddEntityNoCommit(destAddEditColumnFilters);
                this.BantchAddEntityNoCommit(destAddEditColumnFillMaps);
                this.BantchAddEntityNoCommit(destAddEditColumnFillMapDs);
                this.BantchAddEntityNoCommit(destAddCustoms);
                this.BantchAddEntityNoCommit(destAddCharts);
                this.BantchAddEntityNoCommit(destAddChartParams);
                this.TreeBantchAddEntityNoCommit(destAddTables);
                this.BantchAddEntityNoCommit(destAddTablePanels);
                this.BantchAddEntityNoCommit(destAddColums);
                this.BantchAddEntityNoCommit(destAddColumFilters);
                this.BantchAddEntityNoCommit(destAddTableParams);
                this.BantchAddEntityNoCommit(destAddFormQuerys);
                this.BantchAddEntityNoCommit(destAddQuerys);
                this.BantchAddEntityNoCommit(destAddTrees);
                this.BantchAddEntityNoCommit(destAddTreeColumns);
                this.BantchAddEntityNoCommit(destAddBtns);
                this.TreeBantchAddEntityNoCommit(destAddMenus);
                this.TreeBantchAddEntityNoCommit(destAddAppMenus);
                this.TreeBantchAddEntityNoCommit(destAddClientMenus);
                this.BantchAddEntityNoCommit(destAddCodeRules);



                this.BantchUpdateEntityNoCommit(destUpdateForms);
                this.BantchUpdateEntityNoCommit(destUpdateParamItems);
                this.BantchUpdateEntityNoCommit(destUpdateParamSchemes);
                this.BantchUpdateEntityNoCommit(destUpdateParamSchemeDetails);
                this.TreeBantchUpdateEntityNoCommit(destUpdateEdits);
                this.BantchUpdateEntityNoCommit(destUpdateEditPanels);
                this.BantchUpdateEntityNoCommit(destUpdateTablePanels);
                this.BantchUpdateEntityNoCommit(destUpdateEditColumns);
                this.BantchUpdateEntityNoCommit(destUpdateEditColumnFilters);
                this.BantchUpdateEntityNoCommit(destUpdateEditColumnFillMaps);
                this.BantchUpdateEntityNoCommit(destUpdateEditColumnFillMapDs);
                this.BantchUpdateEntityNoCommit(destUpdateCustoms);
                this.BantchUpdateEntityNoCommit(destUpdateCharts);
                this.BantchUpdateEntityNoCommit(destUpdateChartParams);
                this.TreeBantchUpdateEntityNoCommit(destUpdateTables);
                this.BantchUpdateEntityNoCommit(destUpdateColums);
                this.BantchUpdateEntityNoCommit(destUpdateColumFilters);
                this.BantchUpdateEntityNoCommit(destUpdateTableParams);
                this.BantchUpdateEntityNoCommit(destUpdateFormQuerys);
                this.BantchUpdateEntityNoCommit(destUpdateQuerys);
                this.BantchUpdateEntityNoCommit(destUpdateTrees);
                this.BantchUpdateEntityNoCommit(destUpdateTreeColumns);
                this.BantchUpdateEntityNoCommit(destUpdateBtns);
                this.TreeBantchUpdateEntityNoCommit(destUpdateMenus);
                this.TreeBantchUpdateEntityNoCommit(destUpdateAppMenus);
                this.TreeBantchUpdateEntityNoCommit(destUpdateClientMenus);
                this.BantchUpdateEntityNoCommit(destUpdateCodeRules);


            });

            #region 系统参数修改表单配置
            Dictionary<Guid, List<string>> tempCookies = new Dictionary<Guid, List<string>>();
            var schemeIds = paramSchemeDetails.Where(t1 => paramSchemeDetailIds.Contains(t1.ID) || paramItemIds.Contains(t1.PARAM_ITEM_ID)).Select(t => t.PARAM_SCHEME_ID).ToList();

            var allParamSchemeIds = paramSchemes.Where(t => paramSchemeIds.Contains(t.ID) || schemeIds.Contains(t.ID)).Select(t => t.ID).ToList();
            if (allParamSchemeIds.Any())
            {
                var temppageTables = pageTables.Where(t => allParamSchemeIds.Contains(t.PARAM_SCHEME_ID ?? Guid.Empty)).ToList();
                temppageTables.ForEach(t =>
                {
                    if (t.PAGE_FORM_ID != null && !updateFormIds.Contains(t.PAGE_FORM_ID.Value))
                        updateFormIds.Add(t.PAGE_FORM_ID.Value);
                    List<string> list = null;
                    if (!tempCookies.TryGetValue(t.ORG_ID.Value, out list))
                    {
                        list = new List<string>();
                        tempCookies.Add(t.ORG_ID.Value, list);
                    }
                    list.Add(string.Format("form_config_tablebase_{0}_{1}", t.ID, t.ORG_ID.Value));
                    var tempForm = forms.FirstOrDefault(t1 => t1.ID == t.PAGE_FORM_ID);
                    if (tempForm != null)
                    {
                        list.Add(string.Format("form_config_table_{0}_{1}_{2}", tempForm.CODE, t.ORG_ID, (t.PARENT_ID == null ? "" : t.PARENT_ID.ToString())));
                        list.Add(string.Format("form_config_table_{0}_{1}_{2}_{3}", tempForm.CODE, t.ORG_ID, (t.PARENT_ID == null ? "" : t.PARENT_ID.ToString()),
                            t.ID));
                    }
                });
                var temppageEdits = this.GetEntities<T_PF_PAGE_EDIT>(t => allParamSchemeIds.Contains(t.PARAM_SCHEME_ID ?? Guid.Empty), new BaseFilter()).ToList();
                temppageEdits.ForEach(t =>
                {
                    if (t.PAGE_FORM_ID != null && !updateFormIds.Contains(t.PAGE_FORM_ID.Value))
                        updateFormIds.Add(t.PAGE_FORM_ID.Value);
                });
            }
            #endregion


            List<T_PF_PAGE_TABLE> versionPageTables = new List<T_PF_PAGE_TABLE>();
            List<T_PF_COLUMN> versionColumns = new List<T_PF_COLUMN>();
            List<T_PF_PAGE_TABLE_PANEL> versionPageTablePanels = new List<T_PF_PAGE_TABLE_PANEL>();

            if (destAddTables.Any()) versionPageTables.AddRange(destAddTables);
            if (destUpdateTables.Any()) versionPageTables.AddRange(destUpdateTables);
            if (destAddColums.Any()) versionColumns.AddRange(destAddColums);
            if (destUpdateColums.Any()) versionColumns.AddRange(destUpdateColums);
            if (destAddTablePanels.Any()) versionPageTablePanels.AddRange(destAddTablePanels);
            if (destUpdateTablePanels.Any()) versionPageTablePanels.AddRange(destUpdateTablePanels);

            AddFormConfigVersion(updateFormIds, versionPageTables, versionColumns,
                    versionPageTablePanels, tempCookies);

            #endregion
        }

        public void AddFormConfigVersion(List<Guid> formIds, List<T_PF_PAGE_TABLE> pageTables,
            List<T_PF_COLUMN> columns, List<T_PF_PAGE_TABLE_PANEL> pageTablePanels,
             Dictionary<Guid, List<string>> otherCookies)
        {

            //List<T_PF_FORM_CONFIG_VERSION> formConfigVersions = new List<T_PF_FORM_CONFIG_VERSION>();
            //List<T_PF_FORM_CONFIG_VERSION_D> formConfigVersionDetails = new List<T_PF_FORM_CONFIG_VERSION_D>();
            //this.DoAddFormConfigVersion(formIds, pageTables, columns, pageTablePanels, otherCookies,formConfigVersions,
            //    formConfigVersionDetails);
            //this.BantchAddEntity(formConfigVersions);
            //this.BantchAddEntity(formConfigVersionDetails);
        }
        private void DoAddFormConfigVersion(List<Guid> formIds, List<T_PF_PAGE_TABLE> pageTables,
            List<T_PF_COLUMN> columns, List<T_PF_PAGE_TABLE_PANEL> pageTablePanels,
             Dictionary<Guid, List<string>> otherCookies,
             List<T_PF_FORM_CONFIG_VERSION> formConfigVersions, List<T_PF_FORM_CONFIG_VERSION_D> formConfigVersionDetails)
        {
            //if (formIds == null || !formIds.Any()) return;
            //var versionForms = this.GetEntities<T_PF_FORM>(t => formIds.Contains(t.ID), new BaseFilter()).ToList();
            //var g= versionForms.GroupBy(t => t.ORG_ID.Value).ToList();
            //foreach (var orgG in g)
            //{
            //    var orgId = orgG.Key;
            //    List<string> cookies = new List<string>();
            //    var temps = orgG.ToList();
            //    temps.ForEach(t =>
            //    {
            //        cookies.Add(string.Format("form_config_form_{0}_{1}", t.CODE, orgId));
            //        cookies.Add(string.Format("form_config_custom_{0}_{1}", t.CODE, orgId));
            //        cookies.Add(string.Format("form_config_edit_{0}_{1}", t.CODE, orgId));
            //        cookies.Add(string.Format("form_config_tree_{0}_{1}", t.CODE, orgId));
            //        cookies.Add(string.Format("form_config_table_{0}_{1}", t.CODE, orgId));
            //        cookies.Add(string.Format("form_config_search_{0}_{1}", t.ID, orgId));
            //        cookies.Add(string.Format("form_config_search_{0}_{1}", t.CODE, orgId));
            //        cookies.Add(string.Format("form_config_chart_{0}_{1}", t.CODE, orgId));
            //        cookies.Add(string.Format("form_config_chart_{0}_{1}", t.ID, orgId));
            //    });
            //    if (pageTables != null && pageTables.Any())
            //    {
            //        var tempDestTables = pageTables.Where(t => t.ORG_ID == orgId).ToList();
            //        tempDestTables.ForEach(t =>
            //        {
            //            var tempForm = versionForms.FirstOrDefault(t1 => t1.ID == t.PAGE_FORM_ID);
            //            if (tempForm != null)
            //            {
            //                cookies.Add(string.Format("form_config_table_{0}_{1}_{2}", tempForm.CODE, orgId, (t.PARENT_ID == null ? "" : t.PARENT_ID.ToString())));
            //                cookies.Add(string.Format("form_config_table_{0}_{1}_{2}_{3}", tempForm.CODE, orgId, (t.PARENT_ID == null ? "" : t.PARENT_ID.ToString()),
            //                    t.ID));
            //            }
            //            cookies.Add(string.Format("form_config_tablebase_{0}_{1}", t.ID, orgId));
            //        });
            //    }

            //    var tempDestColumnTableIds = columns==null?new List<Guid>(): columns.Where(t => t.ORG_ID == orgId).Select(t => t.PAGE_TABLE_ID.Value).ToList();
            //    if(pageTablePanels!=null)
            //        tempDestColumnTableIds.AddRange(pageTablePanels.Where(t => t.ORG_ID == orgId).Select(t => t.PAGE_TABLE_ID).ToList());
            //    tempDestColumnTableIds.ForEach(t =>
            //    {
            //        cookies.Add(string.Format("form_config_tablebase_{0}_{1}", t, orgId));
            //    });
            //    List<string> list = null;
            //    if (otherCookies!=null&&otherCookies.TryGetValue(orgId, out list))
            //        cookies.AddRange(list);
            //    if (!cookies.Any()) continue;

            //    decimal version = decimal.Zero;
            //    BaseFilter filter = new BaseFilter();
            //    filter.OrgId = orgId;
            //    filter.Order = DbOrder.DESC;
            //    filter.Sort = "VERSION";
            //    filter.OrgType = FilterOrgTypeEnum.仅本组织;
            //    var lastMaxVersion = this.GetOrderEntities<T_PF_FORM_CONFIG_VERSION>(null, filter).FirstOrDefault();
            //    if (lastMaxVersion != null)
            //        version = lastMaxVersion.VERSION;
            //    version += 1;
            //    T_PF_FORM_CONFIG_VERSION formConfigVersion = new T_PF_FORM_CONFIG_VERSION();
            //    formConfigVersion.ORG_ID = orgId;
            //    formConfigVersion.VERSION = version;
            //    formConfigVersions.Add(formConfigVersion);
            //    foreach (var cookieds in cookies)
            //    {
            //        T_PF_FORM_CONFIG_VERSION_D formConfigVersionDetail = new T_PF_FORM_CONFIG_VERSION_D();
            //        formConfigVersionDetail.ORG_ID = orgId;
            //        formConfigVersionDetail.REMOVE_COOKIE_KEY = cookieds;
            //        formConfigVersionDetail.FORM_CONFIG_VERSION_ID = formConfigVersion.ID;
            //        formConfigVersionDetails.Add(formConfigVersionDetail);
            //    }
            //}

        }


        private void DoExcuteSqlAndGetResult(string sql, ExecuteSqlResult executeSqlResult,
            ref int tableIndex)
        {
            if (string.IsNullOrEmpty(sql)) return;
            sql = sql.Trim();
            if (string.IsNullOrEmpty(sql)) return;
            try
            {
                List<ExecuteSqlTable> tables = new List<ExecuteSqlTable>();
                if (sql.IndexOf("Update", StringComparison.OrdinalIgnoreCase) > -1 ||
                        sql.IndexOf("Insert", StringComparison.OrdinalIgnoreCase) > -1 ||
                        sql.IndexOf("Delete", StringComparison.OrdinalIgnoreCase) > -1)
                {
                    var ret = this.ExecuteNonQuery(System.Data.CommandType.Text, sql, null, null);
                    var executeSqlTable = new ExecuteSqlTable();
                    executeSqlTable.Label = "执行结果" + tableIndex;
                    executeSqlTable.Index = tableIndex;
                    tableIndex++;
                    ExecuteSqlColumn column = new ExecuteSqlColumn()
                    {
                        FieldName = "Result",
                        Label = "结果"
                    };
                    executeSqlTable.Columns.Add(column);
                    ExecuteSqlRowColumn executeSqlRowColumn = new ExecuteSqlRowColumn()
                    {
                        ColumnId = column.ID,
                        Value = ("影响" + ret + "行")
                    };
                    ExecuteSqlRow row = new ExecuteSqlRow();
                    row.Datas.Add(executeSqlRowColumn);
                    executeSqlTable.Rows.Add(row);

                    tables.Add(executeSqlTable);
                }
                else
                {
                    var tempTableIndex = tableIndex;
                    this.ExecuteReader(sql, null, (reader) =>
                     {
                         do
                         {
                             ExecuteSqlTable executeSqlTable = null;
                             while (reader.Read())
                             {
                                 if (executeSqlTable == null)
                                 {
                                     executeSqlTable = new ExecuteSqlTable();
                                     executeSqlTable.IsSelectTable = true;
                                     executeSqlTable.Label = "执行结果" + tempTableIndex;
                                     executeSqlTable.Index = tempTableIndex;
                                     tempTableIndex++;
                                     for (int i = 0; i < reader.FieldCount; i++)
                                     {
                                         ExecuteSqlColumn column = new ExecuteSqlColumn()
                                         {
                                             FieldName = reader.GetName(i),
                                             Label = reader.GetName(i),
                                             ColumnType = reader.GetFieldType(i),
                                         };
                                         executeSqlTable.Columns.Add(column);
                                     }
                                     tables.Add(executeSqlTable);
                                 }
                                 ExecuteSqlRow row = new ExecuteSqlRow();
                                 int j = 0;
                                 foreach (var item in executeSqlTable.Columns)
                                 {
                                     ExecuteSqlRowColumn executeSqlRowColumn = new ExecuteSqlRowColumn()
                                     {
                                         ColumnId = item.ID,
                                     };
                                     var obj = reader.GetValue(j);
                                     if (obj != null && obj != DBNull.Value)
                                     {
                                         if (obj is byte[])
                                         {
                                             var blob = obj as byte[];
                                             executeSqlRowColumn.Value = BitConverter.ToString(blob).Replace("-", "");
                                         }
                                         else
                                         {
                                             executeSqlRowColumn.Value = Convert.ToString(obj);
                                         }
                                     }
                                     row.Datas.Add(executeSqlRowColumn);
                                     j++;
                                 }
                                 executeSqlTable.Rows.Add(row);
                             }
                         }
                         while (reader.NextResult());

                     });

                    tableIndex = tempTableIndex;
                }
                if (tables.Any())
                    executeSqlResult.Tables.AddRange(tables);
            }
            catch (Exception ex)
            {
                var executeSqlTable = new ExecuteSqlTable();
                executeSqlTable.Label = "执行结果" + tableIndex;
                executeSqlTable.Index = tableIndex;
                tableIndex++;
                ExecuteSqlColumn column = new ExecuteSqlColumn()
                {
                    FieldName = "Result",
                    Label = "执行错误"
                };
                executeSqlTable.Columns.Add(column);
                ExecuteSqlRowColumn executeSqlRowColumn = new ExecuteSqlRowColumn()
                {
                    ColumnId = column.ID,
                    Value = ("执行语法错误：" + ex.Message)
                };
                ExecuteSqlRow row = new ExecuteSqlRow();
                row.Datas.Add(executeSqlRowColumn);
                executeSqlTable.Rows.Add(row);
                executeSqlResult.Tables.Add(executeSqlTable);
            }
        }


        public void GetEntitisSql<T>(DataBaseType dataBaseType, Guid orgId, List<string> result, string dstOrgId, int sqlType) where T : MesEntityBase, new()
        {
            var filiterThisDep = new BaseFilter(orgId, FilterOrgTypeEnum.仅本组织);
            filiterThisDep.IgnoreDataRule = true;
            var list = this.GetEntities<T>(null, filiterThisDep).ToList();
            if (list != null && list.Any())
            {
                foreach (var item in list)
                    this.DoGetSqlByEntity(dataBaseType, item, result, dstOrgId, sqlType);
            }
        }



        public PFModuleTypeEnum GetModelTypeByName(string tableName)
        {
            var newName = tableName.ToLower();
            var newNameAry = newName.Split(new char[] { '_' });
            string moduleName = string.Empty;
            if (newNameAry.Any() && newNameAry.Length >= 2)
                moduleName = newNameAry[1].ToUpper();
            if (string.IsNullOrEmpty(moduleName)) return PFModuleTypeEnum.平台管理;

            foreach (var item in Enum.GetValues(typeof(PFModuleTypeEnum)))
            {
                var attrs = item.GetType().GetField(item.ToString()).GetCustomAttributes(typeof(DescriptionAttribute), true);
                if (attrs != null && attrs.Length > 0)
                {
                    DescriptionAttribute descAttr = attrs[0] as DescriptionAttribute;
                    if (string.Compare(descAttr.Description, moduleName, true) == 0)
                        return (PFModuleTypeEnum)item;
                }
            }
            return PFModuleTypeEnum.平台管理;
        }


    }
}
