using APT.BaseData.Domain.ApiModel.FM;
using APT.BaseData.Domain.Entities;
using APT.BaseData.Domain.Entities.FM;
using APT.BaseData.Domain.Entities.OP;
using APT.BaseData.Domain.Entities.PF;
using APT.BaseData.Domain.Enums;
using APT.Infrastructure.Core;
using APT.Migrations;
using APT.Utility;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace APT.OP.WebApi.Controllers.Api
{
    [Route("api/OP/TenantVersion")]
    public class TenantVersionController : AuthorizeApiController<T_OP_VERSION>
    {

        [HttpPost, Route("Update")]
        public JsonActionResult<bool> Update([FromBody] T_OP_VERSION version)
        {
            return SafeExecute<bool>(() =>
            {
                var orgFilter = new BaseFilter();
                orgFilter.IgnoreOrgRule = true;
                #region OPT信息
                //var filter = new BaseFilter(version.TEMPLATE_ID);//OPT创建的菜单,表单等
                //filter.Level = -1;
                //filter.IsParentData = true;
                #region 系统参数模板
                //List<T_PF_PARAM_ITEM> optParamItems = this.GetEntities<T_PF_PARAM_ITEM>(null, filter).ToList();
                //List<T_PF_PARAM_SCHEME> optParamSchemes = this.GetEntities<T_PF_PARAM_SCHEME>(null, filter).ToList();
                //List<T_PF_PARAM_SCHEME_DETAIL> optParamSchemeDetails = this.GetEntities<T_PF_PARAM_SCHEME_DETAIL>(null, filter).ToList();
                #endregion
                #region  表单配置信息
                //var optForms = this.GetEntities<T_PF_FORM>(x => x.ORG_ID == filter.OrgId, filter);
                //var optFormRelations = this.GetEntities<T_PF_FORM_RELATION>(x => x.ORG_ID == filter.OrgId, filter);//表单关联
                //var optPageTable = this.GetEntities<T_PF_PAGE_TABLE>(x => x.ORG_ID == filter.OrgId, filter).ToList();
                //var optTablePanels = this.GetEntities<T_PF_PAGE_TABLE_PANEL>(x => x.ORG_ID == filter.OrgId, filter).ToList();
                //var optTableParams = this.GetEntities<T_PF_TABLE_PARAMS>(x => x.ORG_ID == filter.OrgId, filter).ToList();
                //var optPageEdit = this.GetEntities<T_PF_PAGE_EDIT>(x => x.ORG_ID == filter.OrgId, filter).ToList();
                //var optEditPanels = this.GetEntities<T_PF_PAGE_EDIT_PANEL>(x => x.ORG_ID == filter.OrgId, filter).ToList();
                //var optEditColumns = this.GetEntities<T_PF_EDIT_COLUMN>(x => x.ORG_ID == filter.OrgId, filter).ToList();
                //var optEditColumnFilters = this.GetEntities<T_PF_EDIT_COLUMN_FILTER>(x => x.ORG_ID == filter.OrgId, filter).ToList();
                //var optEditColumnFillMaps = this.GetEntities<T_PF_EDIT_COLUMN_FILL_MAP>(x => x.ORG_ID == filter.OrgId, filter).ToList();
                //var optEditColumnFillMapDs = this.GetEntities<T_PF_EDIT_COLUMN_FILL_MAP_D>(x => x.ORG_ID == filter.OrgId, filter).ToList();
                //var optPageTree = this.GetEntities<T_PF_PAGE_TREE>(x => x.ORG_ID == filter.OrgId, filter).ToList();
                //var optTreeColumns = this.GetEntities<T_PF_TREE_COLUMN>(x => x.ORG_ID == filter.OrgId, filter).ToList();
                //var optPageChart = this.GetEntities<T_PF_PAGE_CHART>(x => x.ORG_ID == filter.OrgId, filter).ToList();
                //var optChartParams = this.GetEntities<T_PF_PAGE_CHART_PARAM>(x => x.ORG_ID == filter.OrgId, filter).ToList();
                //var optPageCustom = this.GetEntities<T_PF_PAGE_CUSTOM>(x => x.ORG_ID == filter.OrgId, filter).ToList();
                #endregion
                //var optMenusTree = this.GetTreeOrderEntities<T_PF_MENU>(x => x.ENABLE_STATUS == (int)FMEnableStatusEnum.启用, filter);
                //var optMenus = this.GetEntities<T_PF_MENU>(x => x.ENABLE_STATUS == (int)FMEnableStatusEnum.启用, filter);               
                //var optColumFilters = this.GetEntities<T_PF_COLUMN_FILTER>(x => x.ORG_ID == filter.OrgId, filter).ToList();
                //var optQuerys = this.GetEntities<T_PF_QUERY>(x => x.ORG_ID == filter.OrgId, filter).ToList();
                //var optFormQuerys = this.GetEntities<T_PF_FORM_QUERY>(x => x.ORG_ID == filter.OrgId, filter).ToList();
                //var optCodeRules = this.GetEntities<T_PF_CODE_RULE>(x => x.ORG_ID == filter.OrgId, filter).ToList();
                //var optSyncTasks = this.GetEntities<T_FM_SYNC_TASK>(x => x.ORG_ID == filter.OrgId, filter).ToList();
                //var optVersions = this.GetEntities<T_PF_VERSION_MANAGE>(x => x.ORG_ID == filter.OrgId, filter).ToList();
                #endregion
                #region 来源模板数据
                IEnumerable<TreeNode<T_PF_MENU>> srcMenuTrees = null;
                List<T_PF_MENU> srcMenus = null;
                List<T_PF_MENU> srcEcpMenus = null;
                List<T_PF_FORM> srcForms = null;
                List<T_PF_FORM_RELATION> srcFormRelations = null;
                List<T_PF_PAGE_TABLE> srcPageTables = null;
                List<T_PF_PAGE_EDIT> srcPageEdits = null;
                List<T_PF_PAGE_TREE> srcPageTrees = null;
                List<T_PF_PAGE_CHART> srcPageCharts = null;
                List<T_PF_PAGE_CUSTOM> srcPageCustoms = null;
                List<T_PF_BTN> srcBtns = null;
                List<T_PF_COLUMN> srcColumns = null;
                List<T_PF_PAGE_TABLE_PANEL> srcPageTablePanels = null;
                List<T_PF_TABLE_PARAMS> srcTableParams = null;
                List<T_PF_PAGE_EDIT_PANEL> srcPageEditPanels = null;
                List<T_PF_EDIT_COLUMN> srcEditColumns = null;
                List<T_PF_EDIT_COLUMN_FILTER> srcEditColumnFilters = null;
                List<T_PF_EDIT_COLUMN_FILL_MAP> srcEditColumnFillMaps = null;
                List<T_PF_EDIT_COLUMN_FILL_MAP_D> srcEditColumnFillMapDs = null;
                List<T_PF_TREE_COLUMN> srcTreeColumns = null;
                List<T_PF_PAGE_CHART_PARAM> srcPageChartParams = null;
                List<T_PF_COLUMN_FILTER> srcColumnFilters = null;
                List<T_PF_QUERY> srcQuerys = null;
                List<T_PF_FORM_QUERY> srcFormQuerys = null;
                List<T_PF_CODE_RULE> srcCodeRules = null;
                List<T_FM_SYNC_TASK> srcSyncTasks = null;
                List<T_PF_PARAM_ITEM> srcParamItems = null;
                List<T_PF_PARAM_SCHEME> srcParamSchemes = null;
                List<T_PF_PARAM_SCHEME_DETAIL> srcParamSchemeDetails = null;
                List<T_PF_VERSION_MANAGE> srcVersionManages = null;
                var srcTemplateConnect = string.Empty;
                var tenantTemplate = this.GetEntities<T_OP_TENANT>(x => x.ID == version.TEMPLATE_ID, orgFilter).FirstOrDefault();
                var srcDB = this.GetEntities<T_OP_TENANT_DB_CONN>(x => x.ID == tenantTemplate.DB_CONN_ID, orgFilter).FirstOrDefault();
                if (srcDB != null && !string.IsNullOrEmpty(srcDB.DB_CONN))
                {
                    //var Conn = srcDB.DB_CONN;
                    srcTemplateConnect = srcDB.DB_CONN;
                    using (var context = new MigrationContext(srcTemplateConnect))
                    {
                        #region 来源数据             
                        var srcfilter = new BaseFilter();
                        srcfilter.IgnoreOrgRule = true;
                        srcfilter.Level = -1;
                        srcfilter.IsParentData = true;
                        List<T_PF_MENU> reslist = new List<T_PF_MENU>();
                        srcMenuTrees = context.GetTreeOrderEntities<T_PF_MENU>(x => x.ORG_ID == version.TEMPLATE_ID && x.ENABLE_STATUS == (int)FMEnableStatusEnum.启用, srcfilter, null, out reslist, false);
                        srcMenus = context.GetEntities<T_PF_MENU>(x => x.ORG_ID == version.TEMPLATE_ID && x.ENABLE_STATUS == (int)FMEnableStatusEnum.启用, null, false).ToList();//能管菜单
                        srcEcpMenus = context.GetEntities<T_PF_MENU>(x => x.ORG_ID == version.TEMPLATE_ID && x.ENABLE_STATUS == (int)FMEnableStatusEnum.启用 && x.PLATFORM_CATEGORY == 1, null, false).ToList();//企业平台菜单
                        srcForms = context.GetEntities<T_PF_FORM>(x => x.ORG_ID == version.TEMPLATE_ID, null, false).ToList();//租户表单
                        srcFormRelations = context.GetEntities<T_PF_FORM_RELATION>(x => x.ORG_ID == version.TEMPLATE_ID, null, false).ToList();//表单关联
                        srcPageTables = context.GetEntities<T_PF_PAGE_TABLE>(x => x.ORG_ID == version.TEMPLATE_ID, null, false).ToList();
                        srcPageEdits = context.GetEntities<T_PF_PAGE_EDIT>(x => x.ORG_ID == version.TEMPLATE_ID, null, false).ToList();
                        srcPageTrees = context.GetEntities<T_PF_PAGE_TREE>(x => x.ORG_ID == version.TEMPLATE_ID, null, false).ToList();
                        srcPageCharts = context.GetEntities<T_PF_PAGE_CHART>(x => x.ORG_ID == version.TEMPLATE_ID, null, false).ToList();
                        srcPageCustoms = context.GetEntities<T_PF_PAGE_CUSTOM>(x => x.ORG_ID == version.TEMPLATE_ID, null, false).ToList();
                        srcBtns = context.GetEntities<T_PF_BTN>(x => x.ORG_ID == version.TEMPLATE_ID, null, false).ToList();
                        srcColumns = context.GetEntities<T_PF_COLUMN>(x => x.ORG_ID == version.TEMPLATE_ID, null, false).ToList();
                        srcPageTablePanels = context.GetEntities<T_PF_PAGE_TABLE_PANEL>(x => x.ORG_ID == version.TEMPLATE_ID, null, false).ToList();
                        srcTableParams = context.GetEntities<T_PF_TABLE_PARAMS>(x => x.ORG_ID == version.TEMPLATE_ID, null, false).ToList(); ;
                        srcPageEditPanels = context.GetEntities<T_PF_PAGE_EDIT_PANEL>(x => x.ORG_ID == version.TEMPLATE_ID, null, false).ToList();
                        srcEditColumns = context.GetEntities<T_PF_EDIT_COLUMN>(x => x.ORG_ID == version.TEMPLATE_ID, null, false).ToList();
                        srcEditColumnFilters = context.GetEntities<T_PF_EDIT_COLUMN_FILTER>(x => x.ORG_ID == version.TEMPLATE_ID, null, false).ToList();
                        srcEditColumnFillMaps = context.GetEntities<T_PF_EDIT_COLUMN_FILL_MAP>(x => x.ORG_ID == version.TEMPLATE_ID, null, false).ToList();
                        srcEditColumnFillMapDs = context.GetEntities<T_PF_EDIT_COLUMN_FILL_MAP_D>(x => x.ORG_ID == version.TEMPLATE_ID, null, false).ToList();
                        srcTreeColumns = context.GetEntities<T_PF_TREE_COLUMN>(x => x.ORG_ID == version.TEMPLATE_ID, null, false).ToList();
                        srcPageChartParams = context.GetEntities<T_PF_PAGE_CHART_PARAM>(x => x.ORG_ID == version.TEMPLATE_ID, null, false).ToList();
                        srcColumnFilters = context.GetEntities<T_PF_COLUMN_FILTER>(x => x.ORG_ID == version.TEMPLATE_ID, null, false).ToList();
                        srcQuerys = context.GetEntities<T_PF_QUERY>(x => x.ORG_ID == version.TEMPLATE_ID, null, false).ToList();
                        srcFormQuerys = context.GetEntities<T_PF_FORM_QUERY>(x => x.ORG_ID == version.TEMPLATE_ID, null, false).ToList();
                        srcCodeRules = context.GetEntities<T_PF_CODE_RULE>(x => x.ORG_ID == version.TEMPLATE_ID, null, false).ToList();
                        srcSyncTasks = context.GetEntities<T_FM_SYNC_TASK>(x => x.ORG_ID == version.TEMPLATE_ID, null, false).ToList();
                        srcParamItems = context.GetEntities<T_PF_PARAM_ITEM>(x => x.ORG_ID == version.TEMPLATE_ID, null, false).ToList();
                        srcParamSchemes = context.GetEntities<T_PF_PARAM_SCHEME>(x => x.ORG_ID == version.TEMPLATE_ID, null, false).ToList();
                        srcParamSchemeDetails = context.GetEntities<T_PF_PARAM_SCHEME_DETAIL>(x => x.ORG_ID == version.TEMPLATE_ID, null, false).ToList();
                        srcVersionManages = context.GetEntities<T_PF_VERSION_MANAGE>(x => x.ORG_ID == version.TEMPLATE_ID, null, false).ToList();
                        #endregion
                    }
                }
                #endregion
                TenantInfo srcInfo = new TenantInfo();
                srcInfo.FormRelations = srcFormRelations;
                srcInfo.Forms = srcForms;
                srcInfo.PageTables = srcPageTables;
                srcInfo.PageEdits = srcPageEdits;
                srcInfo.PageTrees = srcPageTrees;
                srcInfo.PageCharts = srcPageCharts;
                srcInfo.PageCustoms = srcPageCustoms;
                srcInfo.Btns = srcBtns;
                srcInfo.Columns = srcColumns;
                srcInfo.TablePanels = srcPageTablePanels;
                srcInfo.TableParams = srcTableParams;
                srcInfo.EditPanels = srcPageEditPanels;
                srcInfo.EditColumns = srcEditColumns;
                srcInfo.EditColumnFilters = srcEditColumnFilters;
                srcInfo.EditColumnFillMaps = srcEditColumnFillMaps;
                srcInfo.EditColumnFillMapDs = srcEditColumnFillMapDs;
                srcInfo.TreeColumns = srcTreeColumns;
                srcInfo.ChartParams = srcPageChartParams;
                srcInfo.ColumnFilters = srcColumnFilters;
                srcInfo.Querys = srcQuerys;
                srcInfo.FormQuerys = srcFormQuerys;
                srcInfo.DicPageTable = new Dictionary<string, object>();
                srcInfo.DicPageTableRelation = new Dictionary<string, string>();
                srcInfo.DicForm = new Dictionary<string, string>();

                if (version.CheckMenuKeys.Count > 0 && version.CheckTenantKeys.Count > 0)
                {
                    var VersionMenu = this.GetEntities<T_OP_VERSION_MENU>(x => x.VERSION_ID == version.ID, orgFilter).ToList();
                    var versionTenants = this.GetEntities<T_OP_VERSION_TENANT>(x => x.VERSION_ID == version.ID, orgFilter).ToList();
                    if (!(VersionMenu.Any() && versionTenants.Any()))//升级
                    {
                        foreach (var versionTenant in version.CheckTenantKeys)//租户升级
                        {
                            #region 更新版本       
                            var saveVersionMenu = new List<T_OP_VERSION_MENU>();
                            var saveVersionTenant = new List<T_OP_VERSION_TENANT>();
                            var savePermPFMenu = new List<T_PF_MENU>();
                            var addPermPFMenu = new List<T_PF_MENU>();//暂时不处理
                            var savePermPFForm = new List<T_PF_FORM>();
                            var savePermPFFormRelation = new List<T_PF_FORM_RELATION>();
                            var savePermPageTable = new List<T_PF_PAGE_TABLE>();
                            var savePermPageEdit = new List<T_PF_PAGE_EDIT>();
                            var savePermPageTree = new List<T_PF_PAGE_TREE>();
                            var savePermPageChart = new List<T_PF_PAGE_CHART>();
                            var savePermPageCustom = new List<T_PF_PAGE_CUSTOM>();
                            var savePermBtn = new List<T_PF_BTN>();
                            var savePermCloumn = new List<T_PF_COLUMN>();
                            var saveTablePanels = new List<T_PF_PAGE_TABLE_PANEL>();
                            var saveTableParams = new List<T_PF_TABLE_PARAMS>();
                            var saveEditPanels = new List<T_PF_PAGE_EDIT_PANEL>();
                            var saveEditColumns = new List<T_PF_EDIT_COLUMN>();
                            var saveEditColumnFilters = new List<T_PF_EDIT_COLUMN_FILTER>();
                            var saveEditColumnFillMaps = new List<T_PF_EDIT_COLUMN_FILL_MAP>();
                            var saveEditColumnFillMapDs = new List<T_PF_EDIT_COLUMN_FILL_MAP_D>();
                            var saveTreeColumns = new List<T_PF_TREE_COLUMN>();
                            var saveChartParams = new List<T_PF_PAGE_CHART_PARAM>();
                            var saveColumFilters = new List<T_PF_COLUMN_FILTER>();
                            var saveQuerys = new List<T_PF_QUERY>();
                            var saveFormQuerys = new List<T_PF_FORM_QUERY>();
                            var saveCodeRules = new List<T_PF_CODE_RULE>();
                            var saveSyncTasks = new List<T_FM_SYNC_TASK>();
                            var saveVersions = new List<T_PF_VERSION_MANAGE>();
                            var saveParamItems = new List<T_PF_PARAM_ITEM>();
                            var saveParamSchemes = new List<T_PF_PARAM_SCHEME>();
                            var saveParamSchemeDetails = new List<T_PF_PARAM_SCHEME_DETAIL>();
                            var addSyncTasks = new List<T_FM_SYNC_TASK>();
                            var addCodeRules = new List<T_PF_CODE_RULE>();
                            var addParamItems = new List<T_PF_PARAM_ITEM>();
                            var addParamSchemes = new List<T_PF_PARAM_SCHEME>();
                            var addParamSchemeDetails = new List<T_PF_PARAM_SCHEME_DETAIL>();
                            #endregion
                            Dictionary<string, object> relationMenu = new Dictionary<string, object>();//id.pid
                            Dictionary<string, string> newRelationMenu = new Dictionary<string, string>();//id.newid
                            Dictionary<Guid, Guid> MenuFormRelation = new Dictionary<Guid, Guid>();//id.newid
                            Dictionary<Guid, Guid> newItems = new Dictionary<Guid, Guid>();//id.newid
                            Dictionary<Guid, Guid> newSchemes = new Dictionary<Guid, Guid>();//id.newid
                            Dictionary<Guid, Guid> newSchemeDetails = new Dictionary<Guid, Guid>();//id.newid
                            Dictionary<Guid, Guid> newTablePanels = new Dictionary<Guid, Guid>();//id.newid
                            Dictionary<Guid, Guid> newCodeRules = new Dictionary<Guid, Guid>();//id.newid
                            Dictionary<Guid, Guid> newEditPanels = new Dictionary<Guid, Guid>();
                            #region 授权数据
                            IEnumerable<TreeNode<T_PF_MENU>> tenantMenuTrees = null;
                            List<T_PF_MENU> tenantMenus = null;
                            List<T_PF_FORM> tenantForms = null;
                            List<T_PF_FORM_RELATION> tenantFormRelations = null;
                            List<T_PF_PAGE_TABLE> tenantPageTables = null;
                            List<T_PF_PAGE_EDIT> tenantPageEdits = null;
                            List<T_PF_PAGE_TREE> tenantPageTrees = null;
                            List<T_PF_PAGE_CHART> tenantPageCharts = null;
                            List<T_PF_PAGE_CUSTOM> tenantPageCustoms = null;
                            List<T_PF_BTN> tenantBtns = null;
                            List<T_PF_COLUMN> tenantColumns = null;
                            List<T_PF_PAGE_TABLE_PANEL> tenantPageTablePanels = null;
                            List<T_PF_TABLE_PARAMS> tenantTableParams = null;
                            List<T_PF_PAGE_EDIT_PANEL> tenantPageEditPanels = null;
                            List<T_PF_EDIT_COLUMN> tenantEditColumns = null;
                            List<T_PF_EDIT_COLUMN_FILTER> tenantEditColumnFilters = null;
                            List<T_PF_EDIT_COLUMN_FILL_MAP> tenantEditColumnFillMaps = null;
                            List<T_PF_EDIT_COLUMN_FILL_MAP_D> tenantEditColumnFillMapDs = null;
                            List<T_PF_TREE_COLUMN> tenantTreeColumns = null;
                            List<T_PF_PAGE_CHART_PARAM> tenantPageChartParams = null;
                            List<T_PF_COLUMN_FILTER> tenantColumnFilters = null;
                            List<T_PF_QUERY> tenantQuerys = null;
                            List<T_PF_FORM_QUERY> tenantFormQuerys = null;
                            List<T_PF_CODE_RULE> tenantCodeRules = null;
                            List<T_FM_SYNC_TASK> tenantSyncTasks = null;
                            List<T_PF_PARAM_ITEM> tenantParamItems = null;
                            List<T_PF_PARAM_SCHEME> tenantParamSchemes = null;
                            List<T_PF_PARAM_SCHEME_DETAIL> tenantParamSchemeDetails = null;
                            List<T_PF_VERSION_MANAGE> tenantVersionManages = null;
                            List<T_FM_USER> tenantUser = null;
                            List<T_FM_ROLE> tenantRole = null;
                            List<T_FM_USER_BELONG_ROLE> tenantUserBelongRole = null;
                            #endregion
                            var dicMenu = new Dictionary<Guid, object>();
                            List<Guid> existForm = new List<Guid>();
                            List<Guid> existTable = new List<Guid>();
                            List<Guid> existPermForm = new List<Guid>();
                            var tenantConnect = this.GetEntities<T_OP_TENANT>(x => x.ID == new Guid(versionTenant), orgFilter).FirstOrDefault();
                            var tenantDB = this.GetEntities<T_OP_TENANT_DB_CONN>(x => x.ID == tenantConnect.DB_CONN_ID, orgFilter).FirstOrDefault();
                            if (tenantDB != null && !string.IsNullOrEmpty(tenantDB.DB_CONN))
                            {
                                var Conn = tenantDB.DB_CONN;
                                using (var context = new MigrationContext(Conn))
                                {
                                    List<T_PF_MENU> reslist = new List<T_PF_MENU>();
                                    #region 授权数据                     
                                    tenantMenus = context.GetEntities<T_PF_MENU>(x => x.ORG_ID ==new Guid(versionTenant), null).ToList();//租户菜单
                                    tenantForms = context.GetEntities<T_PF_FORM>(x => x.ORG_ID ==new Guid(versionTenant), null).ToList();//租户表单
                                    tenantFormRelations = context.GetEntities<T_PF_FORM_RELATION>(x => x.ORG_ID ==new Guid(versionTenant), null).ToList();//表单关联
                                    tenantPageTables = context.GetEntities<T_PF_PAGE_TABLE>(x => x.ORG_ID ==new Guid(versionTenant), null).ToList();
                                    tenantPageEdits = context.GetEntities<T_PF_PAGE_EDIT>(x => x.ORG_ID ==new Guid(versionTenant), null).ToList();
                                    tenantPageTrees = context.GetEntities<T_PF_PAGE_TREE>(x => x.ORG_ID ==new Guid(versionTenant), null).ToList();
                                    tenantPageCharts = context.GetEntities<T_PF_PAGE_CHART>(x => x.ORG_ID ==new Guid(versionTenant), null).ToList();
                                    tenantPageCustoms = context.GetEntities<T_PF_PAGE_CUSTOM>(x => x.ORG_ID ==new Guid(versionTenant), null).ToList();
                                    tenantBtns = context.GetEntities<T_PF_BTN>(x => x.ORG_ID ==new Guid(versionTenant), null).ToList();
                                    tenantColumns = context.GetEntities<T_PF_COLUMN>(x => x.ORG_ID ==new Guid(versionTenant), null).ToList();
                                    tenantPageTablePanels = context.GetEntities<T_PF_PAGE_TABLE_PANEL>(x => x.ORG_ID ==new Guid(versionTenant), null).ToList();
                                    tenantTableParams = context.GetEntities<T_PF_TABLE_PARAMS>(x => x.ORG_ID ==new Guid(versionTenant), null).ToList(); ;
                                    tenantPageEditPanels = context.GetEntities<T_PF_PAGE_EDIT_PANEL>(x => x.ORG_ID ==new Guid(versionTenant), null).ToList();
                                    tenantEditColumns = context.GetEntities<T_PF_EDIT_COLUMN>(x => x.ORG_ID ==new Guid(versionTenant), null).ToList();
                                    tenantEditColumnFilters = context.GetEntities<T_PF_EDIT_COLUMN_FILTER>(x => x.ORG_ID ==new Guid(versionTenant), null).ToList();
                                    tenantEditColumnFillMaps = context.GetEntities<T_PF_EDIT_COLUMN_FILL_MAP>(x => x.ORG_ID ==new Guid(versionTenant), null).ToList();
                                    tenantEditColumnFillMapDs = context.GetEntities<T_PF_EDIT_COLUMN_FILL_MAP_D>(x => x.ORG_ID ==new Guid(versionTenant), null).ToList();
                                    tenantTreeColumns = context.GetEntities<T_PF_TREE_COLUMN>(x => x.ORG_ID ==new Guid(versionTenant), null).ToList();
                                    tenantPageChartParams = context.GetEntities<T_PF_PAGE_CHART_PARAM>(x => x.ORG_ID ==new Guid(versionTenant), null).ToList();
                                    tenantColumnFilters = context.GetEntities<T_PF_COLUMN_FILTER>(x => x.ORG_ID ==new Guid(versionTenant), null).ToList();
                                    tenantQuerys = context.GetEntities<T_PF_QUERY>(x => x.ORG_ID ==new Guid(versionTenant), null).ToList();
                                    tenantFormQuerys = context.GetEntities<T_PF_FORM_QUERY>(x => x.ORG_ID ==new Guid(versionTenant), null).ToList();
                                    tenantCodeRules = context.GetEntities<T_PF_CODE_RULE>(x => x.ORG_ID ==new Guid(versionTenant), null).ToList();
                                    tenantSyncTasks = context.GetEntities<T_FM_SYNC_TASK>(x => x.ORG_ID ==new Guid(versionTenant), null).ToList();
                                    tenantParamItems = context.GetEntities<T_PF_PARAM_ITEM>(x => x.ORG_ID ==new Guid(versionTenant), null).ToList();
                                    tenantParamSchemes = context.GetEntities<T_PF_PARAM_SCHEME>(x => x.ORG_ID ==new Guid(versionTenant), null).ToList();
                                    tenantParamSchemeDetails = context.GetEntities<T_PF_PARAM_SCHEME_DETAIL>(x => x.ORG_ID ==new Guid(versionTenant), null).ToList();
                                    tenantVersionManages = context.GetEntities<T_PF_VERSION_MANAGE>(x => x.ORG_ID ==new Guid(versionTenant), null).ToList();
                                    tenantUser = context.GetEntities<T_FM_USER>(x => x.ORG_ID ==new Guid(versionTenant), null).ToList();
                                    tenantRole = context.GetEntities<T_FM_ROLE>(x => x.ORG_ID ==new Guid(versionTenant), null).ToList();
                                    tenantUserBelongRole = context.GetEntities<T_FM_USER_BELONG_ROLE>(x => x.ORG_ID ==new Guid(versionTenant), null).ToList();
                                    #endregion
                                }
                                #region 租户已授权信息
                                TenantInfo tenantInfo = new TenantInfo();
                                tenantInfo.FormRelations = tenantFormRelations;
                                tenantInfo.Forms = tenantForms;
                                tenantInfo.PageTables = tenantPageTables;
                                tenantInfo.PageEdits = tenantPageEdits;
                                tenantInfo.PageTrees = tenantPageTrees;
                                tenantInfo.PageCharts = tenantPageCharts;
                                tenantInfo.PageCustoms = tenantPageCustoms;
                                tenantInfo.Btns = tenantBtns;
                                tenantInfo.Columns = tenantColumns;
                                tenantInfo.TablePanels = tenantPageTablePanels;
                                tenantInfo.TableParams = tenantTableParams;
                                tenantInfo.EditPanels = tenantPageEditPanels;
                                tenantInfo.EditColumns = tenantEditColumns;
                                tenantInfo.EditColumnFilters = tenantEditColumnFilters;
                                tenantInfo.EditColumnFillMaps = tenantEditColumnFillMaps;
                                tenantInfo.EditColumnFillMapDs = tenantEditColumnFillMapDs;
                                tenantInfo.TreeColumns = tenantTreeColumns;
                                tenantInfo.ChartParams = tenantPageChartParams;
                                tenantInfo.ColumnFilters = tenantColumnFilters;
                                tenantInfo.Querys = tenantQuerys;
                                tenantInfo.FormQuerys = tenantFormQuerys;
                                tenantInfo.DicPageTable = new Dictionary<string, object>();
                                tenantInfo.DicPageTableRelation = new Dictionary<string, string>();
                                tenantInfo.DicForm = new Dictionary<string, string>();
                                #endregion
                                #region addInfo(升级暂时用不到)
                                TenantInfo addEntity = new TenantInfo();
                                addEntity.FormRelations = new List<T_PF_FORM_RELATION>();
                                addEntity.Forms = new List<T_PF_FORM>();
                                addEntity.PageTables = new List<T_PF_PAGE_TABLE>();
                                addEntity.PageEdits = new List<T_PF_PAGE_EDIT>();
                                addEntity.PageTrees = new List<T_PF_PAGE_TREE>();
                                addEntity.PageCharts = new List<T_PF_PAGE_CHART>();
                                addEntity.PageCustoms = new List<T_PF_PAGE_CUSTOM>();
                                addEntity.Btns = new List<T_PF_BTN>();
                                addEntity.Columns = new List<T_PF_COLUMN>();
                                addEntity.TablePanels = new List<T_PF_PAGE_TABLE_PANEL>();
                                addEntity.TableParams = new List<T_PF_TABLE_PARAMS>();
                                addEntity.EditPanels = new List<T_PF_PAGE_EDIT_PANEL>();
                                addEntity.EditColumns = new List<T_PF_EDIT_COLUMN>();
                                addEntity.EditColumnFilters = new List<T_PF_EDIT_COLUMN_FILTER>();
                                addEntity.EditColumnFillMaps = new List<T_PF_EDIT_COLUMN_FILL_MAP>();
                                addEntity.EditColumnFillMapDs = new List<T_PF_EDIT_COLUMN_FILL_MAP_D>();
                                addEntity.TreeColumns = new List<T_PF_TREE_COLUMN>();
                                addEntity.ChartParams = new List<T_PF_PAGE_CHART_PARAM>();
                                addEntity.ColumnFilters = new List<T_PF_COLUMN_FILTER>();
                                addEntity.Querys = new List<T_PF_QUERY>();
                                addEntity.FormQuerys = new List<T_PF_FORM_QUERY>();
                                addEntity.DicPageTable = new Dictionary<string, object>();
                                addEntity.DicPageTableRelation = new Dictionary<string, string>();
                                addEntity.DicForm = new Dictionary<string, string>();
                                #endregion
                                #region 系统参数模板
                                // var tenantParamItems = this.GetEntities<T_PF_PARAM_ITEM>(x => x.ORG_ID == tenantFilter.OrgId, tenantFilter);
                                foreach (var item in srcParamItems)
                                {
                                    T_PF_PARAM_ITEM parmItem = item;
                                    if (tenantParamItems.Any(x => x.SRC_ID == item.ID))//修改
                                    {
                                        var tenantItem = tenantParamItems.FirstOrDefault(x => x.SRC_ID == item.ID);
                                        var oldItenID = parmItem.ID;
                                        parmItem.SRC_ID = parmItem.ID;
                                        parmItem.ID = tenantItem.ID;
                                        parmItem.ORG_ID = tenantItem.ORG_ID;
                                        newItems.Add(oldItenID, tenantItem.ID);
                                        saveParamItems.Add(parmItem);//添加到对应租户                                    
                                    }
                                    else//新增
                                    {
                                        var newItemID = Guid.NewGuid();
                                        var oldItenID = parmItem.ID;
                                        parmItem.SRC_ID = parmItem.ID;
                                        parmItem.ID = newItemID;
                                        parmItem.ORG_ID = new Guid(versionTenant);
                                        newItems.Add(oldItenID, newItemID);
                                        addParamItems.Add(parmItem);
                                    }

                                }
                                //var tenantParamSchemes = this.GetEntities<T_PF_PARAM_SCHEME>(x => x.ORG_ID == tenantFilter.OrgId, tenantFilter);
                                foreach (var item in srcParamSchemes)
                                {
                                    T_PF_PARAM_SCHEME paramScheme = item;
                                    if (tenantParamSchemes.Any(x => x.SRC_ID == item.ID))//修改
                                    {
                                        var tenantSchemes = tenantParamSchemes.FirstOrDefault(x => x.SRC_ID == item.ID);
                                        var oldSchemeID = paramScheme.ID;
                                        paramScheme.SRC_ID = paramScheme.ID;
                                        paramScheme.ID = tenantSchemes.ID;
                                        paramScheme.ORG_ID = tenantSchemes.ORG_ID;
                                        newSchemes.Add(oldSchemeID, tenantSchemes.ID);
                                        saveParamSchemes.Add(paramScheme);//添加到对应租户  
                                    }
                                    else//新增
                                    {
                                        var newSchemesID = Guid.NewGuid();
                                        var oldSchemeID = paramScheme.ID;
                                        paramScheme.SRC_ID = paramScheme.ID;
                                        paramScheme.ID = newSchemesID;
                                        paramScheme.ORG_ID = new Guid(versionTenant);
                                        newSchemes.Add(oldSchemeID, newSchemesID);
                                        addParamSchemes.Add(paramScheme);
                                    }

                                }
                                // var tenantParamSchemeDetails = this.GetEntities<T_PF_PARAM_SCHEME_DETAIL>(x => x.ORG_ID == tenantFilter.OrgId, tenantFilter);
                                foreach (var item in srcParamSchemeDetails)
                                {
                                    T_PF_PARAM_SCHEME_DETAIL paramSchemeDetail = item;
                                    if (tenantParamSchemeDetails.Any(x => x.SRC_ID == item.ID))//修改
                                    {
                                        var tenantSchemes = tenantParamSchemeDetails.FirstOrDefault(x => x.SRC_ID == item.ID);
                                        var oldSchemeDetailID = paramSchemeDetail.ID;
                                        var scheneId = newSchemes[item.PARAM_SCHEME_ID];
                                        var ItemId = newItems[item.PARAM_ITEM_ID];
                                        paramSchemeDetail.SRC_ID = paramSchemeDetail.ID;
                                        paramSchemeDetail.ID = tenantSchemes.ID;
                                        paramSchemeDetail.ORG_ID = tenantSchemes.ORG_ID;
                                        paramSchemeDetail.PARAM_SCHEME_ID = scheneId;
                                        paramSchemeDetail.PARAM_ITEM_ID = ItemId;
                                        newSchemeDetails.Add(oldSchemeDetailID, tenantSchemes.ID);
                                        saveParamSchemeDetails.Add(paramSchemeDetail);//添加到对应租户    
                                    }
                                    else//新增
                                    {
                                        var newSchemeDetailID = Guid.NewGuid();
                                        var oldSchemeDetailID = paramSchemeDetail.ID;
                                        var scheneId = newSchemes[item.PARAM_SCHEME_ID];
                                        var ItemId = newItems[item.PARAM_ITEM_ID];
                                        paramSchemeDetail.SRC_ID = paramSchemeDetail.ID;
                                        paramSchemeDetail.ID = newSchemeDetailID;
                                        paramSchemeDetail.ORG_ID = new Guid(versionTenant);
                                        paramSchemeDetail.PARAM_SCHEME_ID = scheneId;
                                        paramSchemeDetail.PARAM_ITEM_ID = ItemId;
                                        newSchemeDetails.Add(oldSchemeDetailID, newSchemeDetailID);
                                        addParamSchemeDetails.Add(paramSchemeDetail);
                                    }

                                }
                                #endregion
                                #region 编码规则表
                                // var tenantCodeRules = this.GetEntities<T_PF_CODE_RULE>(x => x.ORG_ID == tenantFilter.OrgId, tenantFilter);
                                foreach (var item in srcCodeRules)
                                {
                                    T_PF_CODE_RULE codeRule = item;
                                    if (tenantCodeRules.Any(x => x.SRC_ID == item.ID))
                                    {
                                        var tenantCodeRule = tenantCodeRules.FirstOrDefault(x => x.SRC_ID == item.ID);
                                        var oldCodeRuleID = item.ID;
                                        codeRule.SRC_ID = codeRule.ID;
                                        codeRule.ID = tenantCodeRule.ID;
                                        codeRule.ORG_ID = tenantCodeRule.ORG_ID;
                                        newCodeRules.Add(oldCodeRuleID, tenantCodeRule.ID);
                                        saveCodeRules.Add(codeRule);//添加到对应租户        
                                    }
                                    else
                                    {
                                        var newCodeRuleID = Guid.NewGuid();
                                        var oldCodeRuleID = item.ID;
                                        codeRule.SRC_ID = codeRule.ID;
                                        codeRule.ID = newCodeRuleID;
                                        codeRule.ORG_ID = new Guid(versionTenant);
                                        newCodeRules.Add(oldCodeRuleID, newCodeRuleID);
                                        addCodeRules.Add(codeRule);
                                    }

                                }
                                #endregion
                                #region T_FM_SYNC_TASK
                                // var tenantSyncTasks = this.GetEntities<T_FM_SYNC_TASK>(x => x.ORG_ID == tenantFilter.OrgId, tenantFilter);
                                foreach (var item in srcSyncTasks)
                                {
                                    T_FM_SYNC_TASK syncTask = item;
                                    if (tenantSyncTasks.Any(x => x.SRC_ID == item.ID))
                                    {
                                        var tenantSyncTask = tenantSyncTasks.FirstOrDefault(x => x.SRC_ID == item.ID);
                                        var oldCodeRuleID = item.ID;
                                        syncTask.SRC_ID = syncTask.ID;
                                        syncTask.ID = tenantSyncTask.ID;
                                        syncTask.ORG_ID = tenantSyncTask.ORG_ID;
                                        saveSyncTasks.Add(syncTask);//添加到对应租户     
                                    }
                                    else
                                    {
                                        var newSyncTaskID = Guid.NewGuid();
                                        var oldCodeRuleID = item.ID;
                                        syncTask.SRC_ID = syncTask.ID;
                                        syncTask.ID = newSyncTaskID;
                                        syncTask.ORG_ID = new Guid(versionTenant);
                                        addSyncTasks.Add(syncTask);
                                    }

                                }
                                #endregion
                                foreach (var CheckKey in version.CheckMenuKeys)
                                {
                                    var checkMenu = srcMenuTrees.FirstOrDefault(x => x.Node.ID == new Guid(CheckKey));
                                    if (checkMenu == null)
                                    {
                                        foreach (var item in srcMenuTrees)
                                        {
                                            checkMenu = selectNode(item, CheckKey);
                                            if (checkMenu != null)
                                                break;
                                        }
                                    }
                                    if (checkMenu != null)
                                    {
                                        saveVersionMenu.Add(new T_OP_VERSION_MENU()
                                        {
                                            VERSION_ID = version.ID,
                                            MENU_ID = new Guid(CheckKey),
                                            ORG_ID = version.ORG_ID
                                        });
                                        if (checkMenu.IsLeaf && checkMenu.Node.MENU_FORM_ID != null)//叶子节点
                                        {
                                            dicMenu.Add(new Guid(CheckKey), checkMenu.Node.MENU_FORM_ID);
                                            //T_PF_FORM t_PF_FORM = this.GetEntities<T_PF_FORM>(x => x.ORG_ID == filter.OrgId, filter).FirstOrDefault(x => x.ID == checkMenu.Node.MENU_FORM_ID);//OPT
                                            //T_PF_MENU t_PF_MENU = this.GetEntities<T_PF_MENU>(x => x.ORG_ID == filter.OrgId, filter).Where(x => x.ID == new Guid(CheckKey)).FirstOrDefault();
                                            T_PF_FORM t_PF_FORM = srcForms.FirstOrDefault(x => x.ID == checkMenu.Node.MENU_FORM_ID);
                                            T_PF_MENU t_PF_MENU = srcMenus.FirstOrDefault(x => x.ID == new Guid(CheckKey));
                                            if (tenantMenus.Any(x => x.SRC_ID == new Guid(CheckKey)))//存在
                                            {
                                                #region 生成表单
                                                // var tanentForm = this.GetEntities<T_PF_FORM>(x => x.ORG_ID == tenantFilter.OrgId, tenantFilter).FirstOrDefault(x => x.SRC_ID == checkMenu.Node.MENU_FORM_ID);
                                                var tanentForm = tenantForms.FirstOrDefault(x => x.SRC_ID == checkMenu.Node.MENU_FORM_ID);
                                                if (tanentForm != null)
                                                {
                                                    if (t_PF_FORM != null)//再次点击相同表单数据已更新
                                                    {
                                                        if (!existForm.Contains(t_PF_FORM.ID) && !savePermPFForm.Any(x => x.CODE == t_PF_FORM.CODE) && !addEntity.Forms.Any(x => x.CODE == t_PF_FORM.CODE))
                                                        {
                                                            AddTable(srcInfo, t_PF_FORM, savePermPFForm,
                                                        savePermPageTable, saveTablePanels, saveTableParams,
                                                        saveColumFilters, savePermCloumn,
                                                        savePermPageEdit, saveEditPanels, saveEditColumns,
                                                        saveEditColumnFilters, saveEditColumnFillMaps, saveEditColumnFillMapDs,
                                                        savePermPageTree, saveTreeColumns,
                                                        savePermPageChart, saveChartParams,
                                                        savePermPageCustom,
                                                        savePermBtn, saveFormQuerys, saveQuerys, savePermPFFormRelation,
                                                        newSchemes, newTablePanels, newEditPanels, new Guid(versionTenant), tanentForm.ID, existForm, existTable, tenantInfo, addEntity);
                                                            t_PF_FORM.SRC_ID = checkMenu.Node.MENU_FORM_ID;
                                                            t_PF_FORM.ID = tanentForm.ID;
                                                            t_PF_FORM.ORG_ID = new Guid(versionTenant);
                                                            t_PF_FORM.Nav_Org = null;
                                                            if (t_PF_FORM != null)
                                                            {
                                                                savePermPFForm.Add(t_PF_FORM);//添加表单到对应租户
                                                            }
                                                        }
                                                    }
                                                }
                                                #endregion
                                                #region 生成菜单      
                                                var tenantMenu = tenantMenus.FirstOrDefault(x => x.SRC_ID == new Guid(CheckKey));
                                                t_PF_MENU.SRC_ID = new Guid(CheckKey);
                                                t_PF_MENU.ID = tenantMenu.ID;
                                                t_PF_MENU.ORG_ID = new Guid(versionTenant);
                                                if (!tenantInfo.DicForm.ContainsKey(t_PF_MENU.MENU_FORM_ID.ToString()))
                                                {
                                                    tenantInfo.DicForm.Add(t_PF_MENU.MENU_FORM_ID.ToString(), tanentForm.ID.ToString());
                                                    t_PF_MENU.MENU_FORM_ID = tanentForm.ID;
                                                }
                                                else
                                                {
                                                    t_PF_MENU.MENU_FORM_ID = new Guid(tenantInfo.DicForm[t_PF_MENU.MENU_FORM_ID.ToString()]);
                                                }
                                                relationMenu.Add(CheckKey, checkMenu.Node.PARENT_ID);
                                                newRelationMenu.Add(CheckKey, tenantMenu.ID.ToString());
                                                savePermPFMenu.Add(t_PF_MENU);//添加菜单到对应租户   
                                                #endregion
                                            }
                                            else
                                            {
                                                #region 菜单授权
                                                var newMenuID = Guid.NewGuid();
                                                //savePermOPMenu.Add(new T_OP_ROLE_MENU() { TENANT_ID = tenant.ID, MENU_ID = new Guid(CheckKey), TENANT_MENU_ID = newMenuID, ORG_ID = tenant.ID });//菜单授权                                        
                                                #endregion
                                                #region 生成表单
                                                var newFormID = Guid.NewGuid();
                                                if (t_PF_FORM != null)
                                                {
                                                    if (!existForm.Contains(t_PF_FORM.ID) && !savePermPFForm.Any(x => x.CODE == t_PF_FORM.CODE) && !addEntity.Forms.Any(x => x.CODE == t_PF_FORM.CODE))
                                                    {
                                                        AddTable(srcInfo, t_PF_FORM, savePermPFForm,
                                                   savePermPageTable, saveTablePanels, saveTableParams,
                                                   saveColumFilters, savePermCloumn,
                                                   savePermPageEdit, saveEditPanels, saveEditColumns,
                                                   saveEditColumnFilters, saveEditColumnFillMaps, saveEditColumnFillMapDs,
                                                   savePermPageTree, saveTreeColumns,
                                                   savePermPageChart, saveChartParams,
                                                   savePermPageCustom,
                                                   savePermBtn, saveFormQuerys, saveQuerys, savePermPFFormRelation,
                                                   newSchemes, newTablePanels, newEditPanels, new Guid(versionTenant), newFormID, existForm, existTable, tenantInfo, addEntity);
                                                        t_PF_FORM.SRC_ID = checkMenu.Node.MENU_FORM_ID;
                                                        t_PF_FORM.ID = newFormID;
                                                        t_PF_FORM.ORG_ID = new Guid(versionTenant);
                                                        t_PF_FORM.Nav_Org = null;
                                                        if (t_PF_FORM != null)
                                                        {
                                                            addEntity.Forms.Add(t_PF_FORM);//添加表单到对应租户
                                                        }
                                                    }
                                                }
                                                #endregion
                                                #region 生成菜单        
                                                t_PF_MENU.SRC_ID = checkMenu.Node.ID;
                                                t_PF_MENU.ID = newMenuID;
                                                t_PF_MENU.ORG_ID = new Guid(versionTenant);
                                                if (!tenantInfo.DicForm.ContainsKey(t_PF_MENU.MENU_FORM_ID.ToString()))
                                                {
                                                    tenantInfo.DicForm.Add(t_PF_MENU.MENU_FORM_ID.ToString(), newFormID.ToString());
                                                    t_PF_MENU.MENU_FORM_ID = newFormID;
                                                }
                                                else
                                                {
                                                    t_PF_MENU.MENU_FORM_ID = new Guid(tenantInfo.DicForm[t_PF_MENU.MENU_FORM_ID.ToString()]);
                                                }
                                                relationMenu.Add(CheckKey, checkMenu.Node.PARENT_ID);
                                                newRelationMenu.Add(CheckKey, newMenuID.ToString());
                                                addPermPFMenu.Add(t_PF_MENU);//添加菜单到对应租户     
                                                #endregion
                                            }

                                        }
                                        else
                                        {
                                            dicMenu.Add(new Guid(CheckKey), null);
                                            //var tenantMenus = this.GetEntities<T_PF_MENU>(null, tenantFilter);
                                            T_PF_MENU t_PF_MENU = srcMenus.FirstOrDefault(x => x.ID == new Guid(CheckKey));
                                            if (tenantMenus.Any(x => x.SRC_ID == new Guid(CheckKey)))
                                            {
                                                var tenantMenu = tenantMenus.FirstOrDefault(x => x.SRC_ID == new Guid(CheckKey));
                                                t_PF_MENU.SRC_ID = checkMenu.Node.ID;
                                                t_PF_MENU.ID = tenantMenu.ID;
                                                t_PF_MENU.ORG_ID = tenantMenu.ORG_ID;
                                                relationMenu.Add(CheckKey, checkMenu.Node.PARENT_ID);
                                                newRelationMenu.Add(CheckKey, tenantMenu.ID.ToString());
                                                savePermPFMenu.Add(t_PF_MENU);//添加菜单到对应租户    
                                            }
                                            else
                                            {
                                                var newMenuID = Guid.NewGuid();
                                                //savePermOPMenu.Add(new T_OP_ROLE_MENU() { TENANT_ID = tenant.ID, MENU_ID = new Guid(CheckKey), TENANT_MENU_ID = newMenuID, ORG_ID = tenant.ID });//菜单授权                                        
                                                t_PF_MENU.SRC_ID = checkMenu.Node.ID;
                                                t_PF_MENU.ID = newMenuID;
                                                t_PF_MENU.ORG_ID = new Guid(versionTenant);
                                                relationMenu.Add(CheckKey, checkMenu.Node.PARENT_ID);
                                                newRelationMenu.Add(CheckKey, newMenuID.ToString());
                                                addPermPFMenu.Add(t_PF_MENU);
                                            }

                                        }
                                    }
                                }
                                #region 菜单节点关系
                                foreach (var item in relationMenu)
                                {
                                    if (item.Value != null)
                                    {
                                        var newMenuID = newRelationMenu[item.Key];//需要更新父ID
                                        if (savePermPFMenu.Count > 0 && savePermPFMenu.Any(x => x.ID == new Guid(newMenuID)))
                                        {
                                            savePermPFMenu.Where(x => x.ID == new Guid(newMenuID)).FirstOrDefault().PARENT_ID = new Guid(newRelationMenu[item.Value.ToString()]);
                                        }                        
                                    }
                                }
                                #endregion
                                #region PageTable关系关联
                                foreach (var item in savePermPageTable)
                                {
                                    if (item.PARENT_ID != null)
                                    {
                                        //item.PARENT_ID = new Guid(tenantInfo.DicPageTableRelation[tenantInfo.DicPageTable[item.SRC_ID.ToString()]]);
                                        var oldParentID = tenantInfo.DicPageTable[item.SRC_ID.ToString()];
                                        item.PARENT_ID = new Guid(tenantInfo.DicPageTableRelation[oldParentID.ToString()]);
                                    }
                                }
                                #endregion
                                #region pageEdit关系关联
                                foreach (var item in savePermPageEdit)
                                {
                                    if (item.PARENT_ID != null)
                                    {
                                        //item.PARENT_ID = new Guid(tenantInfo.DicPageTableRelation[tenantInfo.DicPageTable[item.SRC_ID.ToString()]]);
                                        var oldParentID = tenantInfo.DicPageTable[item.SRC_ID.ToString()];
                                        item.PARENT_ID = new Guid(tenantInfo.DicPageTableRelation[oldParentID.ToString()]);
                                    }
                                }
                                #endregion
                                saveVersionTenant.Add(new T_OP_VERSION_TENANT()
                                {
                                    VERSION_ID = version.ID,
                                    TENANT_ID = new Guid(versionTenant),
                                    ORG_ID = version.ORG_ID
                                });
                                saveVersions.Add(new T_PF_VERSION_MANAGE()
                                {
                                    ORG_ID = new Guid(versionTenant),
                                    VERSION_NO = version.VERSION_NO,
                                    VERSION_ZH = version.VERSION_ZH,
                                    CONTENT = version.CONTENT
                                });

                                #region 子企业升级
                                IEnumerable<TreeNode<T_FM_ORGANIZATION>> tenantOrgs = null;
                                List<T_PF_MENU> menus = null;
                                using (var context = new MigrationContext(Conn))
                                {
                                    var filterOrg = new BaseFilter();//OPT创建的菜单,表单等
                                    filterOrg.IgnoreOrgRule = true;
                                    filterOrg.Level = -1;
                                    filterOrg.IsParentData = true;
                                    List<T_FM_ORGANIZATION> reslist = new List<T_FM_ORGANIZATION>();
                                    tenantOrgs = context.GetTreeOrderEntities<T_FM_ORGANIZATION>(x => x.ENABLE_STATUS == (int)FMEnableStatusEnum.启用, filterOrg, null, out reslist);
                                    var tenantOrg = tenantOrgs.Where(x => x.Node.ID == new Guid(versionTenant)).FirstOrDefault();
                                    menus = context.GetEntities<T_PF_MENU>(x => x.ORG_ID == tenantOrg.Node.ID && version.CheckMenuKeys.Contains(x.SRC_ID.ToString()), null).ToList();//租户菜单
                                }
                                Dictionary<Guid, Guid> dicOptMenu = new Dictionary<Guid, Guid>();
                                foreach (var menu in menus)
                                {
                                    dicOptMenu.Add(menu.ID, (Guid)menu.SRC_ID);
                                }
                                List<Guid> CheckMenuKeys = new List<Guid>();
                                foreach (var CheckMenuKey in menus)
                                {
                                    CheckMenuKeys.Add((Guid)CheckMenuKey.ID);
                                }
                                List<Guid> dicTenant = new List<Guid>();
                                foreach (var CheckMenuKey in CheckMenuKeys)
                                {
                                    IEnumerable<T_PF_MENU> enterpriseMenus = null;
                                    T_PF_MENU srcMenu = null;
                                    using (var context = new MigrationContext(Conn))
                                    {
                                        enterpriseMenus = context.GetEntities<T_PF_MENU>(x => x.SRC_ID == CheckMenuKey, null);
                                    }
                                    using (var srccontext = new MigrationContext(srcTemplateConnect))//模板库
                                    {
                                        srcMenu = srccontext.GetEntities<T_PF_MENU>(x => x.ID == dicOptMenu[CheckMenuKey], null).FirstOrDefault();//OPT菜单
                                    }
                                    var tenantSaveMenus = savePermPFMenu.FirstOrDefault(x => x.ID == CheckMenuKey);
                                    foreach (var enterpriseMenu in enterpriseMenus)
                                    {
                                        if (!dicTenant.Contains((Guid)enterpriseMenu.ORG_ID))
                                        {
                                            dicTenant.Add((Guid)enterpriseMenu.ORG_ID);
                                        }
                                        enterpriseMenu.NAME = srcMenu.NAME;
                                        enterpriseMenu.MENU_FORM_PARAMS = srcMenu.MENU_FORM_PARAMS;
                                        enterpriseMenu.NAME_ACRONYM = srcMenu.NAME_ACRONYM;
                                        enterpriseMenu.MENU_FORM_ID = tenantSaveMenus.MENU_FORM_ID;
                                        enterpriseMenu.ICON = srcMenu.ICON;
                                        savePermPFMenu.Add(enterpriseMenu);//添加菜单到对应租户  
                                    }
                                }
                                // updateChildren(tenantOrg, savePermPFMenu, CheckMenuKeys, dicOptMenu);
                                if (dicTenant.Any())
                                {
                                    foreach (var item in dicTenant)
                                    {
                                        saveVersions.Add(new T_PF_VERSION_MANAGE()
                                        {
                                            ORG_ID = item,
                                            VERSION_NO = version.VERSION_NO,
                                            VERSION_ZH = version.VERSION_ZH,
                                            CONTENT = version.CONTENT
                                        });
                                    }                                 
                                }
                                #endregion
                                this.UpdateEntity(version);
                                if (saveVersionMenu != null && saveVersionMenu.Any())
                                    this.BantchSaveEntity(saveVersionMenu);
                                if (saveVersionTenant != null && saveVersionTenant.Any())
                                    this.BantchSaveEntity(saveVersionTenant);
                                using (var context = new MigrationContext(Conn))
                                {                               
                                    if (saveCodeRules != null && saveCodeRules.Any())
                                        context.UpdateEntities(saveCodeRules);
                                    if (saveSyncTasks != null && saveSyncTasks.Any())
                                        context.UpdateEntities(saveSyncTasks);
                                    if (savePermPFForm != null && savePermPFForm.Any())
                                        context.UpdateEntities(savePermPFForm);
                                    if (saveParamItems != null && saveParamItems.Any())
                                        context.UpdateEntities(saveParamItems);
                                    if (saveParamSchemes != null && saveParamSchemes.Any())
                                        context.UpdateEntities(saveParamSchemes);
                                    if (saveParamSchemeDetails != null && saveParamSchemeDetails.Any())
                                        context.UpdateEntities(saveParamSchemeDetails);
                                    if (savePermPageEdit != null && savePermPageEdit.Any())
                                        context.UpdateEntities(savePermPageEdit);
                                    if (saveEditPanels != null && saveEditPanels.Any())
                                        context.UpdateEntities(saveEditPanels);
                                    if (saveEditColumns != null && saveEditColumns.Any())
                                        context.UpdateEntities(saveEditColumns);
                                    if (saveEditColumnFilters != null && saveEditColumnFilters.Any())
                                        context.UpdateEntities(saveEditColumnFilters);
                                    if (saveEditColumnFillMaps != null && saveEditColumnFillMaps.Any())
                                        context.UpdateEntities(saveEditColumnFillMaps);
                                    if (saveEditColumnFillMapDs != null && saveEditColumnFillMapDs.Any())
                                        context.UpdateEntities(saveEditColumnFillMapDs);
                                    if (savePermPageCustom != null && savePermPageCustom.Any())
                                        context.UpdateEntities(savePermPageCustom);
                                    if (savePermPageChart != null && savePermPageChart.Any())
                                        context.UpdateEntities(savePermPageChart);
                                    if (saveChartParams != null && saveChartParams.Any())
                                        context.UpdateEntities(saveChartParams);
                                    if (savePermPageTable != null && savePermPageTable.Any())
                                        context.UpdateEntities(savePermPageTable);
                                    if (saveTablePanels != null && saveTablePanels.Any())
                                        context.UpdateEntities(saveTablePanels);
                                    if (savePermCloumn != null && savePermCloumn.Any())
                                        context.UpdateEntities(savePermCloumn);
                                    if (saveColumFilters != null && saveColumFilters.Any())
                                        context.UpdateEntities(saveColumFilters);
                                    if (saveTableParams != null && saveTableParams.Any())
                                        context.UpdateEntities(saveTableParams);
                                    if (saveFormQuerys != null && saveFormQuerys.Any())
                                        context.UpdateEntities(saveFormQuerys);
                                    if (saveQuerys != null && saveQuerys.Any())
                                        context.UpdateEntities(saveQuerys);
                                    if (savePermPageTree != null && savePermPageTree.Any())
                                        context.UpdateEntities(savePermPageTree);
                                    if (saveTreeColumns != null && saveTreeColumns.Any())
                                        context.UpdateEntities(saveTreeColumns);
                                    if (savePermBtn != null && savePermBtn.Any())
                                        context.UpdateEntities(savePermBtn);
                                    if (savePermPFMenu != null && savePermPFMenu.Any())
                                        context.UpdateEntities(savePermPFMenu);
                                    if (savePermPFFormRelation != null && savePermPFFormRelation.Any())
                                        context.UpdateEntities(savePermPFFormRelation);
                                    if (saveVersions != null && saveVersions.Any())
                                        context.AddEntities(saveVersions);
                                    context.SaveChanges();
                                }
                            }
                        }

                    }
                }
                //UnifiedCommit(() =>
                //{
                //    this.UpdateEntityNoCommit(version);

                //    if (saveCodeRules != null && saveCodeRules.Any())
                //        this.BantchSaveEntityNoCommit(saveCodeRules);
                //    if (saveSyncTasks != null && saveSyncTasks.Any())
                //        this.BantchSaveEntityNoCommit(saveSyncTasks);
                //    if (savePermPFForm != null && savePermPFForm.Any())
                //        this.BantchSaveEntityNoCommit(savePermPFForm);
                //    if (saveParamItems != null && saveParamItems.Any())
                //        this.BantchSaveEntityNoCommit(saveParamItems);
                //    if (saveParamSchemes != null && saveParamSchemes.Any())
                //        this.BantchSaveEntityNoCommit(saveParamSchemes);
                //    if (saveParamSchemeDetails != null && saveParamSchemeDetails.Any())
                //        this.BantchSaveEntityNoCommit(saveParamSchemeDetails);
                //    if (savePermPageEdit != null && savePermPageEdit.Any())
                //        this.BantchSaveEntityNoCommit(savePermPageEdit);
                //    if (saveEditPanels != null && saveEditPanels.Any())
                //        this.BantchSaveEntityNoCommit(saveEditPanels);
                //    if (saveEditColumns != null && saveEditColumns.Any())
                //        this.BantchSaveEntityNoCommit(saveEditColumns);
                //    if (saveEditColumnFilters != null && saveEditColumnFilters.Any())
                //        this.BantchSaveEntityNoCommit(saveEditColumnFilters);
                //    if (saveEditColumnFillMaps != null && saveEditColumnFillMaps.Any())
                //        this.BantchSaveEntityNoCommit(saveEditColumnFillMaps);
                //    if (saveEditColumnFillMapDs != null && saveEditColumnFillMapDs.Any())
                //        this.BantchSaveEntityNoCommit(saveEditColumnFillMapDs);
                //    if (savePermPageCustom != null && savePermPageCustom.Any())
                //        this.BantchSaveEntityNoCommit(savePermPageCustom);
                //    if (savePermPageChart != null && savePermPageChart.Any())
                //        this.BantchSaveEntityNoCommit(savePermPageChart);
                //    if (saveChartParams != null && saveChartParams.Any())
                //        this.BantchSaveEntityNoCommit(saveChartParams);
                //    if (savePermPageTable != null && savePermPageTable.Any())
                //        this.BantchSaveEntityNoCommit(savePermPageTable);
                //    if (saveTablePanels != null && saveTablePanels.Any())
                //        this.BantchSaveEntityNoCommit(saveTablePanels);
                //    if (savePermCloumn != null && savePermCloumn.Any())
                //        this.BantchSaveEntityNoCommit(savePermCloumn);
                //    if (saveColumFilters != null && saveColumFilters.Any())
                //        this.BantchSaveEntityNoCommit(saveColumFilters);
                //    if (saveTableParams != null && saveTableParams.Any())
                //        this.BantchSaveEntityNoCommit(saveTableParams);
                //    if (saveFormQuerys != null && saveFormQuerys.Any())
                //        this.BantchSaveEntityNoCommit(saveFormQuerys);
                //    if (saveQuerys != null && saveQuerys.Any())
                //        this.BantchSaveEntityNoCommit(saveQuerys);
                //    if (savePermPageTree != null && savePermPageTree.Any())
                //        this.BantchSaveEntityNoCommit(savePermPageTree);
                //    if (saveTreeColumns != null && saveTreeColumns.Any())
                //        this.BantchSaveEntityNoCommit(saveTreeColumns);
                //    if (savePermBtn != null && savePermBtn.Any())
                //        this.BantchSaveEntityNoCommit(savePermBtn);
                //    if (savePermPFMenu != null && savePermPFMenu.Any())
                //        this.BantchSaveEntityNoCommit(savePermPFMenu);
                //    if (savePermPFFormRelation != null && savePermPFFormRelation.Any())
                //        this.BantchSaveEntityNoCommit(savePermPFFormRelation);
                //    if (saveVersions != null && saveVersions.Any())
                //        this.BantchSaveEntityNoCommit(saveVersions);

                //    if (saveVersionMenu != null && saveVersionMenu.Any())
                //        this.BantchSaveEntityNoCommit(saveVersionMenu);
                //    if (saveVersionTenant != null && saveVersionTenant.Any())
                //        this.BantchSaveEntityNoCommit(saveVersionTenant);
                //});
                return true;
            });
        }

        public void updateChildren(TreeNode<T_FM_ORGANIZATION> orgs, List<T_PF_MENU> saveMenus, List<Guid> CheckMenuKeys, Dictionary<Guid, Guid> dicOptMenu)
        {
            if (orgs.Children.Count > 0)
            {
                foreach (var org in orgs.Children)
                {
                    List<Guid> TenantCheckMenuKeys = new List<Guid>();//对应父节点选中菜单  
                    var menus = this.GetEntities<T_PF_MENU>(x => x.ORG_ID == org.Node.ID && CheckMenuKeys.Contains((Guid)x.SRC_ID), new BaseFilter()).ToList();//子企业菜单
                    // TenantCheckMenuKeys.AddRange(this.GetEntities<T_PF_MENU>(x => x.ORG_ID == org.Node.ID && CheckMenuKeys.Contains((Guid)x.SRC_ID), new BaseFilter()).Select(x => x.ID));//子企业对应选中的菜单
                    var TenantCheckMenus = this.GetEntities<T_PF_MENU>(x => x.ORG_ID == org.Node.ID && CheckMenuKeys.Contains((Guid)x.SRC_ID), new BaseFilter());
                    foreach (var TenantCheckMenu in TenantCheckMenus)
                    {
                        TenantCheckMenuKeys.Add((Guid)TenantCheckMenu.SRC_ID);
                    }
                    foreach (var menu in menus)
                    {
                        dicOptMenu.Add(menu.ID, dicOptMenu[(Guid)menu.SRC_ID]);
                    }
                    if (TenantCheckMenuKeys != null && TenantCheckMenuKeys.Any())
                    {
                        updateMenu(org, saveMenus, TenantCheckMenuKeys, dicOptMenu);
                    }
                    updateChildren(org, saveMenus, TenantCheckMenuKeys, dicOptMenu);
                }
            }
        }
        public void updateMenu(TreeNode<T_FM_ORGANIZATION> orgs, List<T_PF_MENU> saveMenus, List<Guid> CheckMenuKeys, Dictionary<Guid, Guid> dicOptMenu)
        {
            var enterpriseMenus = this.GetEntities<T_PF_MENU>(x => x.ORG_ID == orgs.Node.ID, new BaseFilter());
            var enterpriseMenuIds = enterpriseMenus.Select(x => x.ID);
            foreach (var CheckKey in CheckMenuKeys)
            {
                if (enterpriseMenuIds.Contains(CheckKey))//子企业有对应菜单权限才更新
                {
                    var enterprisetenantMenu = enterpriseMenus.FirstOrDefault(x => x.ID == CheckKey);
                    T_PF_MENU t_PF_MENU = this.GetEntities<T_PF_MENU>(x => x.ID == dicOptMenu[CheckKey], new BaseFilter()).FirstOrDefault();//OPT菜单
                    enterprisetenantMenu.NAME = t_PF_MENU.NAME;
                    enterprisetenantMenu.MENU_FORM_PARAMS = t_PF_MENU.MENU_FORM_PARAMS;
                    enterprisetenantMenu.NAME_ACRONYM = t_PF_MENU.NAME_ACRONYM;
                    enterprisetenantMenu.MENU_FORM_ID = t_PF_MENU.MENU_FORM_ID;
                    enterprisetenantMenu.ICON = t_PF_MENU.ICON;
                    saveMenus.Add(enterprisetenantMenu);//添加菜单到对应租户         
                }
            }

        }
        /// <summary>
        /// 树形授权
        /// </summary>
        /// <param name="role"></param>
        /// <returns></returns>
        [HttpPost, Route("GetTreePermissionForms")]
        public JsonActionResult<RolePermissionResul> GetTreePermissionForms([FromBody] KeywordPageFilter pageFilter)
        {
            return SafeExecute<RolePermissionResul>(() =>
            {
                List<RolePermissionTreeModel> result = new List<RolePermissionTreeModel>();//最终返回树结果
                IEnumerable<TreeNode<T_PF_MENU>> menuTree = null;
                //var filter = new BaseFilter(new Guid(pageFilter.Parameter6));//OPT OrgId
                //filter.Level = -1;
                //filter.IsParentData = true;
                var orgFilter = new BaseFilter();
                orgFilter.IgnoreOrgRule = true;
                string VersionId = pageFilter.Keyword;//租户ID
                List<T_OP_VERSION_MENU> permissionMenu = null;
                List<T_OP_VERSION_TENANT> versionTenant = null;
                var tenant = this.GetEntities<T_OP_TENANT>(x => x.ID == new Guid(pageFilter.Parameter6), orgFilter).FirstOrDefault();
                var targetDB = this.GetEntities<T_OP_TENANT_DB_CONN>(x => x.ID == tenant.DB_CONN_ID, orgFilter).FirstOrDefault();
                if (targetDB != null && !string.IsNullOrEmpty(targetDB.DB_CONN))
                {
                    var Conn = targetDB.DB_CONN;
                    using (var context = new MigrationContext(Conn))
                    {
                        var filter = new BaseFilter();
                        filter.IgnoreOrgRule = true;
                        filter.Level = -1;
                        filter.IsParentData = true;
                        List<T_PF_MENU> reslist = new List<T_PF_MENU>();
                        menuTree = context.GetTreeOrderEntities<T_PF_MENU>(x => x.ENABLE_STATUS == (int)FMEnableStatusEnum.启用 && x.PLATFORM_CATEGORY == 0 && x.ORG_ID == new Guid(pageFilter.Parameter6), filter, null, out reslist);
                    }

                }
                // var menuTree = this.GetTreeOrderEntities<T_PF_MENU>(x => x.ENABLE_STATUS == (int)FMEnableStatusEnum.启用 && x.ORG_ID == filter.OrgId && x.PLATFORM_CATEGORY == 0, filter);
                if (!string.IsNullOrEmpty(VersionId))
                {
                    permissionMenu = this.GetEntities<T_OP_VERSION_MENU>(x => x.VERSION_ID == new Guid(VersionId), orgFilter).ToList();
                    versionTenant = this.GetEntities<T_OP_VERSION_TENANT>(x => x.VERSION_ID == new Guid(VersionId), orgFilter).ToList();
                }
                List<FormInfo> formInfos = new List<FormInfo>();
                List<string> checks = new List<string>();
                List<string> checkLastKey = new List<string>();
                List<string> checkTenants = new List<string>();
                foreach (var menu in menuTree)
                {
                    AddChildren(menu, permissionMenu, formInfos);
                }
                result = RolePermissionTreeModel.GetRolePermissionTree(formInfos);
                result = result[0].children;
                foreach (var item in result)
                {
                    SelectCheck(item, checks, checkLastKey);
                }
                if (versionTenant != null && versionTenant.Any())
                {
                    foreach (var item in versionTenant)
                    {
                        checkTenants.Add(item.TENANT_ID.ToString());
                    }
                }
                var rlt = new RolePermissionResul() { checks = checks, checkLastKey = checkLastKey, checkTenants = checkTenants, rolePermissionTreeModels = result };
                return rlt;
            });
        }
        public void AddChildren(TreeNode<T_PF_MENU> treeNode, IEnumerable<T_OP_VERSION_MENU> permissionMenu, List<FormInfo> formInfos)
        {
            var permission = false;
            if (!treeNode.Node.IS_LEAF)
            {
                permission = permissionMenu == null ? false : permissionMenu.Any(x => x.MENU_ID == treeNode.Node.ID);
                formInfos.Add(new FormInfo()
                {
                    Code = treeNode.Node.ID.ToString(),
                    Title = treeNode.Node.NAME,
                    ParentCode = treeNode.Node.PARENT_ID.ToString(),
                    IsCheck = permission
                });
                foreach (var item in treeNode.Children)
                {
                    AddChildren(item, permissionMenu, formInfos);
                }
            }
            else
            {
                permission = permissionMenu == null ? false : permissionMenu.Any(x => x.MENU_ID == treeNode.Node.ID);
                formInfos.Add(new FormInfo()
                {
                    Code = treeNode.Node.ID.ToString(),
                    Title = treeNode.Node.NAME,
                    ParentCode = treeNode.Node.PARENT_ID.ToString(),
                    IsCheck = permission
                });
            }
        }
        public void SelectCheck(RolePermissionTreeModel RolePermissions, List<string> checks, List<string> checkLastKey)
        {
            if (RolePermissions.IsCheck)
            {
                checks.Add(RolePermissions.key);
            }
            if (RolePermissions.children.Count == 0 && RolePermissions.IsCheck)
            {
                checkLastKey.Add(RolePermissions.key);
            }
            if (RolePermissions.children.Count > 0)
            {
                foreach (var item in RolePermissions.children)
                {
                    SelectCheck(item, checks, checkLastKey);
                }
            }

        }

        public void AddTable(TenantInfo srcInfo, T_PF_FORM form, List<T_PF_FORM> savePermPFForm,
            List<T_PF_PAGE_TABLE> savePermPageTable, List<T_PF_PAGE_TABLE_PANEL> saveTablePanels, List<T_PF_TABLE_PARAMS> saveTableParams,
           List<T_PF_COLUMN_FILTER> saveColumFilters, List<T_PF_COLUMN> savePermCloumn,
           List<T_PF_PAGE_EDIT> savePermPageEdit, List<T_PF_PAGE_EDIT_PANEL> saveEditPanels, List<T_PF_EDIT_COLUMN> saveEditColumns,
           List<T_PF_EDIT_COLUMN_FILTER> saveEditColumnFilters, List<T_PF_EDIT_COLUMN_FILL_MAP> saveEditColumnFillMaps,
           List<T_PF_EDIT_COLUMN_FILL_MAP_D> saveEditColumnFillMapDs,
           List<T_PF_PAGE_TREE> savePermPageTree, List<T_PF_TREE_COLUMN> saveTreeColumns,
           List<T_PF_PAGE_CHART> savePermPageChart, List<T_PF_PAGE_CHART_PARAM> saveChartParams,
           List<T_PF_PAGE_CUSTOM> savePermPageCustom,
           List<T_PF_BTN> savePermBtn, List<T_PF_FORM_QUERY> saveFormQuerys, List<T_PF_QUERY> saveQuerys, List<T_PF_FORM_RELATION> savePermPFFormRelation,
           Dictionary<Guid, Guid> newSchemes, Dictionary<Guid, Guid> newTablePanels, Dictionary<Guid, Guid> newEditPanels, Guid tenantID, Guid pageFormID,
           List<Guid> existForm, List<Guid> existTable, TenantInfo tenantInfo, TenantInfo addEntity)
        {
            existForm.Add(form.ID);//去重
            tenantInfo.DicForm.Add(form.ID.ToString(), pageFormID.ToString());
            switch (form.FORM_TYPE)
            {
                case (int)PFFormTypeEnum.列表表单:
                case (int)PFFormTypeEnum.组合表单:
                    {
                        #region 配置关联表单T_PF_FORM_RELATION
                        var formRelations = srcInfo.FormRelations.Where(x => x.PAGE_FORM_ID == form.ID);
                        if (formRelations.Any())
                        {
                            foreach (var formRelation in formRelations)
                            {
                                T_PF_FORM_RELATION relation = APT.Infrastructure.Core.CloneExtends.DeepCloneObject(formRelation);
                                if (tenantInfo.FormRelations.Any(x => x.SRC_ID == formRelation.ID))
                                {
                                    var tenantRelation = tenantInfo.FormRelations.FirstOrDefault(x => x.SRC_ID == formRelation.ID);
                                    var relationForm = srcInfo.Forms.Where(x => x.CODE == formRelation.CODE).FirstOrDefault();
                                    if (!existForm.Contains(relationForm.ID) && !savePermPFForm.Any(x => x.CODE == relationForm.CODE) && !addEntity.Forms.Any(x => x.CODE == relationForm.CODE))//去重
                                    {
                                        if (relationForm != null)
                                        {
                                            if (tenantInfo.Forms.Any(x => x.SRC_ID == relationForm.ID))
                                            {
                                                var tenantForm = tenantInfo.Forms.FirstOrDefault(x => x.SRC_ID == relationForm.ID);//取旧数据ID不变
                                                T_PF_FORM newForm = APT.Infrastructure.Core.CloneExtends.DeepCloneObject(relationForm);//OPT创建的表单                                                   
                                                AddTable(srcInfo, relationForm, savePermPFForm,
                                                          savePermPageTable, saveTablePanels, saveTableParams,
                                                          saveColumFilters, savePermCloumn,
                                                          savePermPageEdit, saveEditPanels, saveEditColumns,
                                                          saveEditColumnFilters, saveEditColumnFillMaps,
                                                          saveEditColumnFillMapDs,
                                                          savePermPageTree, saveTreeColumns,
                                                          savePermPageChart, saveChartParams,
                                                          savePermPageCustom,
                                                          savePermBtn, saveFormQuerys, saveQuerys, savePermPFFormRelation,
                                                          newSchemes, newTablePanels, newEditPanels, tenantID, tenantForm.ID, existForm, existTable, tenantInfo, addEntity);
                                                newForm.SRC_ID = relationForm.ID;
                                                newForm.ID = tenantForm.ID;
                                                newForm.ORG_ID = tenantID;
                                                newForm.Nav_Org = null;
                                                savePermPFForm.Add(newForm);//添加表单到对应租户
                                            }
                                            else
                                            {
                                                var newFormID = Guid.NewGuid();
                                                T_PF_FORM newForm = APT.Infrastructure.Core.CloneExtends.DeepCloneObject(relationForm);//OPT创建的表单                                                   
                                                AddTable(srcInfo, relationForm, savePermPFForm,
                                                          savePermPageTable, saveTablePanels, saveTableParams,
                                                          saveColumFilters, savePermCloumn,
                                                          savePermPageEdit, saveEditPanels, saveEditColumns,
                                                          saveEditColumnFilters, saveEditColumnFillMaps,
                                                          saveEditColumnFillMapDs,
                                                          savePermPageTree, saveTreeColumns,
                                                          savePermPageChart, saveChartParams,
                                                          savePermPageCustom,
                                                          savePermBtn, saveFormQuerys, saveQuerys, savePermPFFormRelation,
                                                          newSchemes, newTablePanels, newEditPanels, tenantID, newFormID, existForm, existTable, tenantInfo, addEntity);
                                                newForm.SRC_ID = relationForm.ID;
                                                newForm.ID = newFormID;
                                                newForm.ORG_ID = tenantID;
                                                newForm.Nav_Org = null;
                                                addEntity.Forms.Add(newForm);
                                                //savePermPFForm.Add(newForm);//添加表单到对应租户
                                            }

                                        }
                                    }
                                    relation.SRC_ID = formRelation.ID;
                                    relation.ID = tenantRelation.ID;
                                    relation.ORG_ID = tenantID;
                                    relation.PAGE_FORM_ID = pageFormID;
                                    savePermPFFormRelation.Add(relation);
                                }
                                else
                                {
                                    var relationForm = srcInfo.Forms.Where(x => x.CODE == formRelation.CODE).FirstOrDefault();
                                    if (!existForm.Contains(relationForm.ID) && !savePermPFForm.Any(x => x.CODE == relationForm.CODE) && !addEntity.Forms.Any(x => x.CODE == relationForm.CODE))//去重
                                    {
                                        if (relationForm != null)
                                        {
                                            if (tenantInfo.Forms.Any(x => x.SRC_ID == relationForm.ID))
                                            {
                                                var tenantForm = tenantInfo.Forms.FirstOrDefault(x => x.SRC_ID == relationForm.ID);
                                                T_PF_FORM newForm = APT.Infrastructure.Core.CloneExtends.DeepCloneObject(relationForm);//OPT创建的表单                                                   
                                                AddTable(srcInfo, relationForm, savePermPFForm,
                                                           savePermPageTable, saveTablePanels, saveTableParams,
                                                           saveColumFilters, savePermCloumn,
                                                          savePermPageEdit, saveEditPanels, saveEditColumns,
                                                          saveEditColumnFilters, saveEditColumnFillMaps,
                                                          saveEditColumnFillMapDs,
                                                          savePermPageTree, saveTreeColumns,
                                                          savePermPageChart, saveChartParams,
                                                          savePermPageCustom,
                                                          savePermBtn, saveFormQuerys, saveQuerys, savePermPFFormRelation,
                                                          newSchemes, newTablePanels, newEditPanels, tenantID, tenantForm.ID, existForm, existTable, tenantInfo, addEntity);
                                                newForm.SRC_ID = relationForm.ID;
                                                newForm.ID = tenantForm.ID;
                                                newForm.ORG_ID = tenantID;
                                                newForm.Nav_Org = null;
                                                savePermPFForm.Add(newForm);//添加表单到对应租户
                                            }
                                            else
                                            {
                                                var newFormID = Guid.NewGuid();
                                                T_PF_FORM newForm = APT.Infrastructure.Core.CloneExtends.DeepCloneObject(relationForm);//OPT创建的表单                                                   
                                                AddTable(srcInfo, relationForm, savePermPFForm,
                                                          savePermPageTable, saveTablePanels, saveTableParams,
                                                          saveColumFilters, savePermCloumn,
                                                          savePermPageEdit, saveEditPanels, saveEditColumns,
                                                          saveEditColumnFilters, saveEditColumnFillMaps,
                                                          saveEditColumnFillMapDs,
                                                          savePermPageTree, saveTreeColumns,
                                                          savePermPageChart, saveChartParams,
                                                          savePermPageCustom,
                                                          savePermBtn, saveFormQuerys, saveQuerys, savePermPFFormRelation,
                                                          newSchemes, newTablePanels, newEditPanels, tenantID, newFormID, existForm, existTable, tenantInfo, addEntity);
                                                newForm.SRC_ID = relationForm.ID;
                                                newForm.ID = newFormID;
                                                newForm.ORG_ID = tenantID;
                                                newForm.Nav_Org = null;
                                                addEntity.Forms.Add(newForm);
                                            }

                                        }
                                    }
                                    var newRelationID = new Guid();
                                    relation.SRC_ID = formRelation.ID;
                                    relation.ID = newRelationID;
                                    relation.ORG_ID = tenantID;
                                    relation.PAGE_FORM_ID = pageFormID;
                                    addEntity.FormRelations.Add(relation);
                                }

                            }
                        }
                        #endregion
                        #region 查询
                        AddQuerys(form, tenantID, pageFormID, srcInfo.FormQuerys, saveFormQuerys, srcInfo.Querys, saveQuerys, tenantInfo, addEntity);
                        #endregion
                        var resTable = srcInfo.PageTables.Where(x => x.PAGE_FORM_ID == form.ID).ToList();//查找相关表单      
                        if (resTable.Count > 0)
                        {
                            if (form.FORM_TYPE == (int)PFFormTypeEnum.列表表单)
                            {
                                foreach (var item in resTable)
                                {
                                    var oldpageTable = item.ID;
                                    if (!existTable.Contains(oldpageTable))
                                    {
                                        existTable.Add(oldpageTable);
                                        var newTableID = AddPageTable(item, pageFormID, tenantID, savePermPageTable, newSchemes, saveTablePanels, saveTableParams,
                                        newTablePanels, saveEditPanels, newEditPanels, saveChartParams, tenantInfo, srcInfo, addEntity);
                                        #region 添加列
                                        //var allClm = this.GetEntities<T_PF_COLUMN>(t => t.PAGE_TABLE_ID == oldpageTable, filter).ToList();//OPT所有列
                                        var allClm = srcInfo.Columns.Where(t => t.PAGE_TABLE_ID == oldpageTable).ToList();
                                        foreach (var col in allClm)
                                        {
                                            var oldColID = col.ID;
                                            var newColumnID = Guid.NewGuid();
                                            if (tenantInfo.Columns.Any(x => x.SRC_ID == col.ID))
                                            {
                                                var tenantColumn = tenantInfo.Columns.FirstOrDefault(x => x.SRC_ID == col.ID);
                                                T_PF_COLUMN column = APT.Infrastructure.Core.CloneExtends.DeepCloneObject(col);//OPT创建的表单   
                                                column.SRC_ID = col.ID;
                                                column.ID = tenantColumn.ID;
                                                column.ORG_ID = tenantID;
                                                column.PAGE_TABLE_ID = newTableID;
                                                if (column.PAGE_TABLE_PANEL_ID != null)
                                                {
                                                    column.PAGE_TABLE_PANEL_ID = newTablePanels[(Guid)column.PAGE_TABLE_PANEL_ID];
                                                }
                                                savePermCloumn.Add(column);//添加列基础到对应租户
                                                newColumnID = tenantColumn.ID;//更新
                                            }
                                            else
                                            {
                                                T_PF_COLUMN column = APT.Infrastructure.Core.CloneExtends.DeepCloneObject(col);//OPT创建的表单         
                                                column.SRC_ID = col.ID;
                                                column.ID = newColumnID;
                                                column.ORG_ID = tenantID;
                                                column.PAGE_TABLE_ID = newTableID;
                                                if (column.PAGE_TABLE_PANEL_ID != null)
                                                {
                                                    column.PAGE_TABLE_PANEL_ID = newTablePanels[(Guid)column.PAGE_TABLE_PANEL_ID];
                                                }
                                                //savePermCloumn.Add(column);//添加列基础到对应租户
                                                addEntity.Columns.Add(column);
                                            }
                                            #region T_PF_COLUMN_FILTER
                                            var colFilters = srcInfo.ColumnFilters.Where(x => x.COLUMN_ID == oldColID);
                                            foreach (var colFilter in colFilters)
                                            {
                                                if (tenantInfo.ColumnFilters.Any(x => x.SRC_ID == colFilter.ID))
                                                {
                                                    var tenantColumnFilters = tenantInfo.ColumnFilters.FirstOrDefault(x => x.SRC_ID == colFilter.ID);
                                                    var cf = APT.Infrastructure.Core.CloneExtends.DeepCloneObject(colFilter);
                                                    cf.SRC_ID = colFilter.ID;
                                                    cf.ID = tenantColumnFilters.ID;
                                                    cf.COLUMN_ID = newColumnID;
                                                    cf.ORG_ID = tenantID;
                                                    saveColumFilters.Add(cf);
                                                }
                                                else
                                                {
                                                    var colID = Guid.NewGuid();
                                                    var cf = APT.Infrastructure.Core.CloneExtends.DeepCloneObject(colFilter);
                                                    cf.SRC_ID = colFilter.ID;
                                                    cf.ID = colID;
                                                    cf.COLUMN_ID = newColumnID;
                                                    cf.ORG_ID = tenantID;
                                                    addEntity.ColumnFilters.Add(cf);
                                                }

                                            }
                                            #endregion
                                            if (!string.IsNullOrEmpty(col.SHOW_FORM_CODE))//有配置关联表单
                                            {
                                                var colForm = srcInfo.Forms.Where(x => x.CODE == col.SHOW_FORM_CODE).FirstOrDefault();
                                                if (!existForm.Contains(form.ID) && !savePermPFForm.Any(x => x.CODE == colForm.CODE) && !addEntity.Forms.Any(x => x.CODE == colForm.CODE))//去重
                                                {
                                                    if (colForm != null)
                                                    {
                                                        if (tenantInfo.Forms.Any(x => x.SRC_ID == colForm.ID))
                                                        {
                                                            var tenantForm = tenantInfo.Forms.FirstOrDefault(x => x.SRC_ID == colForm.ID);
                                                            T_PF_FORM newForm = APT.Infrastructure.Core.CloneExtends.DeepCloneObject(colForm);//OPT创建的表单                                                   
                                                            AddTable(srcInfo, colForm, savePermPFForm,
                                                           savePermPageTable, saveTablePanels, saveTableParams,
                                                           saveColumFilters, savePermCloumn,
                                                           savePermPageEdit, saveEditPanels, saveEditColumns,
                                                           saveEditColumnFilters, saveEditColumnFillMaps,
                                                           saveEditColumnFillMapDs,
                                                           savePermPageTree, saveTreeColumns,
                                                           savePermPageChart, saveChartParams,
                                                           savePermPageCustom,
                                                           savePermBtn, saveFormQuerys, saveQuerys, savePermPFFormRelation,
                                                           newSchemes, newTablePanels, newEditPanels, tenantID, tenantForm.ID, existForm, existTable, tenantInfo, addEntity);
                                                            newForm.SRC_ID = colForm.ID;
                                                            newForm.ID = tenantForm.ID;
                                                            newForm.ORG_ID = tenantID;
                                                            newForm.Nav_Org = null;
                                                            savePermPFForm.Add(newForm);//添加表单到对应租户
                                                        }
                                                        else
                                                        {
                                                            var newFormID = Guid.NewGuid();
                                                            T_PF_FORM newForm = APT.Infrastructure.Core.CloneExtends.DeepCloneObject(colForm);//OPT创建的表单                                                   
                                                            AddTable(srcInfo, colForm, savePermPFForm,
                                                          savePermPageTable, saveTablePanels, saveTableParams,
                                                          saveColumFilters, savePermCloumn,
                                                          savePermPageEdit, saveEditPanels, saveEditColumns,
                                                          saveEditColumnFilters, saveEditColumnFillMaps,
                                                          saveEditColumnFillMapDs,
                                                          savePermPageTree, saveTreeColumns,
                                                          savePermPageChart, saveChartParams,
                                                          savePermPageCustom,
                                                          savePermBtn, saveFormQuerys, saveQuerys, savePermPFFormRelation,
                                                          newSchemes, newTablePanels, newEditPanels, tenantID, newFormID, existForm, existTable, tenantInfo, addEntity);
                                                            newForm.SRC_ID = colForm.ID;
                                                            newForm.ID = newFormID;
                                                            newForm.ORG_ID = tenantID;
                                                            newForm.Nav_Org = null;
                                                            addEntity.Forms.Add(newForm);
                                                        }
                                                    }
                                                }
                                            }
                                        }
                                        #endregion
                                        #region 添加按钮
                                        //var allBtn = this.GetEntities<T_PF_BTN>(t => t.PAGE_ID == oldpageTable, filter).ToList();//OPT所有列
                                        var allBtn = srcInfo.Btns.Where(t => t.PAGE_ID == oldpageTable).ToList();
                                        AddTableBtn(srcInfo, savePermPFForm,
                                                      savePermPageTable, saveTablePanels, saveTableParams,
                                                      saveColumFilters, savePermCloumn,
                                                      savePermPageEdit, saveEditPanels, saveEditColumns,
                                                      saveEditColumnFilters, saveEditColumnFillMaps,
                                                      saveEditColumnFillMapDs,
                                                      savePermPageTree, saveTreeColumns,
                                                      savePermPageChart, saveChartParams,
                                                      savePermPageCustom,
                                                      savePermBtn, saveFormQuerys, saveQuerys, savePermPFFormRelation,
                                                      newSchemes, newTablePanels, newEditPanels, tenantID, existForm, existTable, allBtn, newTableID, tenantInfo, addEntity);
                                        #endregion
                                    }
                                }
                            }
                            else//组合表单
                            {
                                foreach (var item in resTable)
                                {
                                    var newRelationFormID = Guid.NewGuid();
                                    var reformId = srcInfo.Forms.Where(x => x.CODE == item.CODE).FirstOrDefault();
                                    //var reformId = this.GetEntities<T_PF_FORM>(null, filter).FirstOrDefault(x => x.CODE == item.CODE);
                                    if (reformId != null && !existForm.Contains(reformId.ID) && !savePermPFForm.Any(x => x.CODE == reformId.CODE) && !addEntity.Forms.Any(x => x.CODE == reformId.CODE))
                                    {
                                        if (tenantInfo.Forms.Any(x => x.SRC_ID == reformId.ID))//存在
                                        {
                                            var tenantForm = tenantInfo.Forms.FirstOrDefault(x => x.SRC_ID == reformId.ID);
                                            T_PF_FORM newForm = APT.Infrastructure.Core.CloneExtends.DeepCloneObject(reformId);//OPT创建的表单                                                   
                                            AddTable(srcInfo, newForm, savePermPFForm,
                                                      savePermPageTable, saveTablePanels, saveTableParams,
                                                      saveColumFilters, savePermCloumn,
                                                      savePermPageEdit, saveEditPanels, saveEditColumns,
                                                      saveEditColumnFilters, saveEditColumnFillMaps,
                                                      saveEditColumnFillMapDs,
                                                      savePermPageTree, saveTreeColumns,
                                                      savePermPageChart, saveChartParams,
                                                      savePermPageCustom,
                                                      savePermBtn, saveFormQuerys, saveQuerys, savePermPFFormRelation,
                                                      newSchemes, newTablePanels, newEditPanels, tenantID, tenantForm.ID, existForm, existTable, tenantInfo, addEntity);
                                            newForm.SRC_ID = reformId.ID;
                                            newForm.ID = tenantForm.ID;
                                            newForm.ORG_ID = tenantID;
                                            newForm.Nav_Org = null;
                                            savePermPFForm.Add(newForm);//添加表单到对应租户
                                            newRelationFormID = tenantForm.ID;
                                        }
                                        else
                                        {
                                            //var newFormID = Guid.NewGuid();
                                            T_PF_FORM newForm = APT.Infrastructure.Core.CloneExtends.DeepCloneObject(reformId);//OPT创建的表单                                                   
                                            AddTable(srcInfo, newForm, savePermPFForm,
                                                      savePermPageTable, saveTablePanels, saveTableParams,
                                                      saveColumFilters, savePermCloumn,
                                                      savePermPageEdit, saveEditPanels, saveEditColumns,
                                                      saveEditColumnFilters, saveEditColumnFillMaps,
                                                      saveEditColumnFillMapDs,
                                                      savePermPageTree, saveTreeColumns,
                                                      savePermPageChart, saveChartParams,
                                                      savePermPageCustom,
                                                      savePermBtn, saveFormQuerys, saveQuerys, savePermPFFormRelation,
                                                      newSchemes, newTablePanels, newEditPanels, tenantID, newRelationFormID, existForm, existTable, tenantInfo, addEntity);
                                            newForm.SRC_ID = reformId.ID;
                                            newForm.ID = newRelationFormID;
                                            newForm.ORG_ID = tenantID;
                                            newForm.Nav_Org = null;
                                            addEntity.Forms.Add(newForm);
                                        }
                                    }
                                    #region 组合关联pageTable
                                    var oldrelationPageID = item.ID;
                                    var pageTables = srcInfo.PageTables.Where(x => x.PAGE_FORM_ID == reformId.ID).ToList();//真实表单
                                    if (!existTable.Contains(oldrelationPageID))
                                    {
                                        existTable.Add(oldrelationPageID);
                                        var newRelationTableID = AddPageTable(item, pageFormID, tenantID, savePermPageTable, newSchemes, saveTablePanels, saveTableParams,
                                        newTablePanels, saveEditPanels, newEditPanels, saveChartParams, tenantInfo, srcInfo, addEntity);
                                    }
                                    #endregion
                                    #region  添加T_PF_PAGE_TABLE        
                                    //foreach (var pageTable in pageTables)
                                    //{
                                    //    var oldpageTable = pageTable.ID;
                                    //    if (!existTable.Contains(oldpageTable))
                                    //    {
                                    //        existTable.Add(oldpageTable);
                                    //        var newTableID = AddPageTable(pageTable, newRelationFormID, tenantID, savePermPageTable, newSchemes,  saveTablePanels, saveTableParams,
                                    //        newTablePanels,  saveEditPanels, newEditPanels, saveChartParams, tenantInfo);

                                    //        #region 添加列
                                    //        var allClm = this.GetEntities<T_PF_COLUMN>(t => t.PAGE_TABLE_ID == oldpageTable, filter).ToList();//OPT所有列
                                    //        foreach (var col in allClm)
                                    //        {
                                    //            var oldColID = col.ID;
                                    //            var newColumnID = Guid.NewGuid();
                                    //            if (tenantInfo.Columns.Any(x => x.SRC_ID == col.ID))
                                    //            {
                                    //                var tenantColumn = tenantInfo.Columns.FirstOrDefault(x => x.SRC_ID == col.ID);
                                    //                T_PF_COLUMN column = col;//OPT创建的表单   
                                    //                column.SRC_ID = col.ID;
                                    //                column.ID = tenantColumn.ID;
                                    //                column.ORG_ID = tenantID;
                                    //                column.PAGE_TABLE_ID = newTableID;
                                    //                if (column.PAGE_TABLE_PANEL_ID != null)
                                    //                {
                                    //                    column.PAGE_TABLE_PANEL_ID = newTablePanels[(Guid)column.PAGE_TABLE_PANEL_ID];
                                    //                }
                                    //                savePermCloumn.Add(column);//添加列基础到对应租户
                                    //                newColumnID = tenantColumn.ID;//更新
                                    //            }
                                    //            else
                                    //            {
                                    //                T_PF_COLUMN column = col;//OPT创建的表单         
                                    //                column.SRC_ID = col.ID;
                                    //                column.ID = newColumnID;
                                    //                column.ORG_ID = tenantID;
                                    //                column.PAGE_TABLE_ID = newTableID;
                                    //                if (column.PAGE_TABLE_PANEL_ID != null)
                                    //                {
                                    //                    column.PAGE_TABLE_PANEL_ID = newTablePanels[(Guid)column.PAGE_TABLE_PANEL_ID];
                                    //                }
                                    //                savePermCloumn.Add(column);//添加列基础到对应租户
                                    //            }
                                    //            #region T_PF_COLUMN_FILTER
                                    //            var colFilters = srcInfo.ColumnFilters.Where(x => x.COLUMN_ID == oldColID);
                                    //            foreach (var colFilter in colFilters)
                                    //            {
                                    //                if (tenantInfo.ColumnFilters.Any(x => x.SRC_ID == colFilter.ID))
                                    //                {
                                    //                    var tenantColumnFilters = tenantInfo.ColumnFilters.FirstOrDefault(x => x.SRC_ID == colFilter.ID);
                                    //                    var cf = colFilter;
                                    //                    cf.SRC_ID = colFilter.ID;
                                    //                    cf.ID = tenantColumnFilters.ID;
                                    //                    cf.COLUMN_ID = newColumnID;
                                    //                    saveColumFilters.Add(cf);
                                    //                }
                                    //                else
                                    //                {
                                    //                    var colID = Guid.NewGuid();
                                    //                    var cf = colFilter;
                                    //                    cf.SRC_ID = colFilter.ID;
                                    //                    cf.ID = colID;
                                    //                    cf.COLUMN_ID = newColumnID;
                                    //                    saveColumFilters.Add(cf);
                                    //                }

                                    //            }
                                    //            #endregion
                                    //            if (!string.IsNullOrEmpty(col.SHOW_FORM_CODE))//有配置关联表单
                                    //            {
                                    //                var colForm = srcInfo.Forms.Where(x => x.CODE == col.SHOW_FORM_CODE).FirstOrDefault();
                                    //                if (!existForm.Contains(form.ID))//去重
                                    //                {
                                    //                    if (colForm != null)
                                    //                    {
                                    //                        if (tenantInfo.Forms.Any(x => x.SRC_ID == colForm.ID))
                                    //                        {
                                    //                            var tenantForm = tenantInfo.Forms.FirstOrDefault(x => x.SRC_ID == colForm.ID);
                                    //                            T_PF_FORM newForm = colForm;//OPT创建的表单                                                   
                                    //                            AddTable(srcInfo, colForm, savePermPFForm,
                                    //                           savePermPageTable,  saveTablePanels, saveTableParams,
                                    //                           saveColumFilters, savePermCloumn,
                                    //                           savePermPageEdit,  saveEditPanels, saveEditColumns,
                                    //                           saveEditColumnFilters,  saveEditColumnFillMaps,
                                    //                           saveEditColumnFillMapDs,
                                    //                           savePermPageTree, saveTreeColumns,
                                    //                           savePermPageChart, saveChartParams,
                                    //                           savePermPageCustom,
                                    //                           savePermBtn,  saveFormQuerys,  saveQuerys, savePermPFFormRelation,
                                    //                           newSchemes, newTablePanels, newEditPanels, tenantID, tenantForm.ID, existForm, existTable, tenantInfo);
                                    //                            newForm.SRC_ID = colForm.ID;
                                    //                            newForm.ID = tenantForm.ID;
                                    //                            newForm.ORG_ID = tenantID;
                                    //                            newForm.Nav_Org = null;
                                    //                            savePermPFForm.Add(newForm);//添加表单到对应租户
                                    //                        }
                                    //                        else
                                    //                        {
                                    //                            var newFormID = Guid.NewGuid();
                                    //                            T_PF_FORM newForm = colForm;//OPT创建的表单                                                   
                                    //                            AddTable(srcInfo, colForm, savePermPFForm,
                                    //                          savePermPageTable,  saveTablePanels, saveTableParams,
                                    //                          saveColumFilters, savePermCloumn,
                                    //                          savePermPageEdit,  saveEditPanels, saveEditColumns,
                                    //                          saveEditColumnFilters,  saveEditColumnFillMaps,
                                    //                          saveEditColumnFillMapDs,
                                    //                          savePermPageTree, saveTreeColumns,
                                    //                          savePermPageChart, saveChartParams,
                                    //                          savePermPageCustom,
                                    //                          savePermBtn,  saveFormQuerys,  saveQuerys, savePermPFFormRelation,
                                    //                          newSchemes, newTablePanels, newEditPanels, tenantID, newFormID, existForm, existTable, tenantInfo);
                                    //                            newForm.SRC_ID = colForm.ID;
                                    //                            newForm.ID = newFormID;
                                    //                            newForm.ORG_ID = tenantID;
                                    //                            newForm.Nav_Org = null;
                                    //                            savePermPFForm.Add(newForm);//添加表单到对应租户
                                    //                        }
                                    //                    }
                                    //                }
                                    //            }
                                    //        }
                                    //        #endregion
                                    //        #region 添加按钮
                                    //        var allBtn = this.GetEntities<T_PF_BTN>(t => t.PAGE_ID == oldpageTable, filter).ToList();//OPT所有列
                                    //        AddTableBtn(srcInfo, savePermPFForm,
                                    //                      savePermPageTable,  saveTablePanels, saveTableParams,
                                    //                      saveColumFilters, savePermCloumn,
                                    //                      savePermPageEdit,  saveEditPanels, saveEditColumns,
                                    //                      saveEditColumnFilters,  saveEditColumnFillMaps,
                                    //                      saveEditColumnFillMapDs,
                                    //                      savePermPageTree, saveTreeColumns,
                                    //                      savePermPageChart, saveChartParams,
                                    //                      savePermPageCustom,
                                    //                      savePermBtn,  saveFormQuerys,  saveQuerys, savePermPFFormRelation,
                                    //                      newSchemes, newTablePanels, newEditPanels, tenantID, existForm, existTable, allBtn, newTableID, tenantInfo);
                                    //        #endregion
                                    //    }
                                    //}
                                    #endregion
                                }
                            }
                        }
                        break;
                    }
                case (int)PFFormTypeEnum.编辑表单:
                    {
                        #region 配置关联表单T_PF_FORM_RELATION
                        var formRelations = srcInfo.FormRelations.Where(x => x.PAGE_FORM_ID == form.ID);
                        if (formRelations.Any())
                        {
                            foreach (var formRelation in formRelations)
                            {
                                T_PF_FORM_RELATION relation = APT.Infrastructure.Core.CloneExtends.DeepCloneObject(formRelation);
                                if (tenantInfo.FormRelations.Any(x => x.SRC_ID == formRelation.ID))
                                {
                                    var tenantRelation = tenantInfo.FormRelations.FirstOrDefault(x => x.SRC_ID == formRelation.ID);
                                    var relationForm = srcInfo.Forms.Where(x => x.CODE == formRelation.CODE).FirstOrDefault();
                                    if (!existForm.Contains(relationForm.ID) && !savePermPFForm.Any(x => x.CODE == relationForm.CODE) && !addEntity.Forms.Any(x => x.CODE == relationForm.CODE))//去重
                                    {
                                        if (relationForm != null)
                                        {
                                            if (tenantInfo.Forms.Any(x => x.SRC_ID == relationForm.ID))
                                            {
                                                var tenantForm = tenantInfo.Forms.FirstOrDefault(x => x.SRC_ID == relationForm.ID);//取旧数据ID不变
                                                T_PF_FORM newForm = APT.Infrastructure.Core.CloneExtends.DeepCloneObject(relationForm);//OPT创建的表单                                                   
                                                AddTable(srcInfo, relationForm, savePermPFForm,
                                                          savePermPageTable, saveTablePanels, saveTableParams,
                                                          saveColumFilters, savePermCloumn,
                                                          savePermPageEdit, saveEditPanels, saveEditColumns,
                                                          saveEditColumnFilters, saveEditColumnFillMaps,
                                                          saveEditColumnFillMapDs,
                                                          savePermPageTree, saveTreeColumns,
                                                          savePermPageChart, saveChartParams,
                                                          savePermPageCustom,
                                                          savePermBtn, saveFormQuerys, saveQuerys, savePermPFFormRelation,
                                                          newSchemes, newTablePanels, newEditPanels, tenantID, tenantForm.ID, existForm, existTable, tenantInfo, addEntity);
                                                newForm.SRC_ID = relationForm.ID;
                                                newForm.ID = tenantForm.ID;
                                                newForm.ORG_ID = tenantID;
                                                newForm.Nav_Org = null;
                                                savePermPFForm.Add(newForm);//添加表单到对应租户
                                            }
                                            else
                                            {
                                                var newFormID = Guid.NewGuid();
                                                T_PF_FORM newForm = APT.Infrastructure.Core.CloneExtends.DeepCloneObject(relationForm);//OPT创建的表单                                                   
                                                AddTable(srcInfo, relationForm, savePermPFForm,
                                                          savePermPageTable, saveTablePanels, saveTableParams,
                                                          saveColumFilters, savePermCloumn,
                                                          savePermPageEdit, saveEditPanels, saveEditColumns,
                                                          saveEditColumnFilters, saveEditColumnFillMaps,
                                                          saveEditColumnFillMapDs,
                                                          savePermPageTree, saveTreeColumns,
                                                          savePermPageChart, saveChartParams,
                                                          savePermPageCustom,
                                                          savePermBtn, saveFormQuerys, saveQuerys, savePermPFFormRelation,
                                                          newSchemes, newTablePanels, newEditPanels, tenantID, newFormID, existForm, existTable, tenantInfo, addEntity);
                                                newForm.SRC_ID = relationForm.ID;
                                                newForm.ID = newFormID;
                                                newForm.ORG_ID = tenantID;
                                                newForm.Nav_Org = null;
                                                addEntity.Forms.Add(newForm);
                                                //savePermPFForm.Add(newForm);//添加表单到对应租户
                                            }

                                        }
                                    }
                                    relation.SRC_ID = formRelation.ID;
                                    relation.ID = tenantRelation.ID;
                                    relation.ORG_ID = tenantID;
                                    relation.PAGE_FORM_ID = pageFormID;
                                    savePermPFFormRelation.Add(relation);
                                }
                                else
                                {
                                    var relationForm = srcInfo.Forms.Where(x => x.CODE == formRelation.CODE).FirstOrDefault();
                                    if (!existForm.Contains(relationForm.ID) && !savePermPFForm.Any(x => x.CODE == relationForm.CODE) && !addEntity.Forms.Any(x => x.CODE == relationForm.CODE))//去重
                                    {
                                        if (relationForm != null)
                                        {
                                            if (tenantInfo.Forms.Any(x => x.SRC_ID == relationForm.ID))
                                            {
                                                var tenantForm = tenantInfo.Forms.FirstOrDefault(x => x.SRC_ID == relationForm.ID);
                                                T_PF_FORM newForm = APT.Infrastructure.Core.CloneExtends.DeepCloneObject(relationForm);//OPT创建的表单                                                   
                                                AddTable(srcInfo, relationForm, savePermPFForm,
                                                           savePermPageTable, saveTablePanels, saveTableParams,
                                                           saveColumFilters, savePermCloumn,
                                                          savePermPageEdit, saveEditPanels, saveEditColumns,
                                                          saveEditColumnFilters, saveEditColumnFillMaps,
                                                          saveEditColumnFillMapDs,
                                                          savePermPageTree, saveTreeColumns,
                                                          savePermPageChart, saveChartParams,
                                                          savePermPageCustom,
                                                          savePermBtn, saveFormQuerys, saveQuerys, savePermPFFormRelation,
                                                          newSchemes, newTablePanels, newEditPanels, tenantID, tenantForm.ID, existForm, existTable, tenantInfo, addEntity);
                                                newForm.SRC_ID = relationForm.ID;
                                                newForm.ID = tenantForm.ID;
                                                newForm.ORG_ID = tenantID;
                                                newForm.Nav_Org = null;
                                                savePermPFForm.Add(newForm);//添加表单到对应租户
                                            }
                                            else
                                            {
                                                var newFormID = Guid.NewGuid();
                                                T_PF_FORM newForm = APT.Infrastructure.Core.CloneExtends.DeepCloneObject(relationForm);//OPT创建的表单                                                   
                                                AddTable(srcInfo, relationForm, savePermPFForm,
                                                          savePermPageTable, saveTablePanels, saveTableParams,
                                                          saveColumFilters, savePermCloumn,
                                                          savePermPageEdit, saveEditPanels, saveEditColumns,
                                                          saveEditColumnFilters, saveEditColumnFillMaps,
                                                          saveEditColumnFillMapDs,
                                                          savePermPageTree, saveTreeColumns,
                                                          savePermPageChart, saveChartParams,
                                                          savePermPageCustom,
                                                          savePermBtn, saveFormQuerys, saveQuerys, savePermPFFormRelation,
                                                          newSchemes, newTablePanels, newEditPanels, tenantID, newFormID, existForm, existTable, tenantInfo, addEntity);
                                                newForm.SRC_ID = relationForm.ID;
                                                newForm.ID = newFormID;
                                                newForm.ORG_ID = tenantID;
                                                newForm.Nav_Org = null;
                                                addEntity.Forms.Add(newForm);
                                            }

                                        }
                                    }
                                    var newRelationID = new Guid();
                                    relation.SRC_ID = formRelation.ID;
                                    relation.ID = newRelationID;
                                    relation.ORG_ID = tenantID;
                                    relation.PAGE_FORM_ID = pageFormID;
                                    addEntity.FormRelations.Add(relation);
                                }

                            }
                        }
                        #endregion
                        #region 查询
                        AddQuerys(form, tenantID, pageFormID, srcInfo.FormQuerys, saveFormQuerys, srcInfo.Querys, saveQuerys, tenantInfo, addEntity);
                        #endregion
                        var resEdit = srcInfo.PageEdits.Where(x => x.PAGE_FORM_ID == form.ID).ToList();//查找相关表单      
                        if (resEdit.Count > 0)
                        {
                            foreach (var item in resEdit)
                            {
                                var oldpageEdit = item.ID;
                                if (!existTable.Contains(oldpageEdit))
                                {
                                    existTable.Add(oldpageEdit);
                                    var newTableID = AddPageTable(item, pageFormID, tenantID, savePermPageEdit, newSchemes, saveTablePanels, saveTableParams,
                                    newTablePanels, saveEditPanels, newEditPanels, saveChartParams, tenantInfo, srcInfo, addEntity);
                                    #region 添加编辑列
                                    //var allClm = this.GetEntities<T_PF_EDIT_COLUMN>(t => t.PAGE_EDIT_ID == oldpageEdit, filter).ToList();//OPT所有列
                                    var allClm = srcInfo.EditColumns.Where(t => t.PAGE_EDIT_ID == oldpageEdit).ToList();
                                    foreach (var col in allClm)
                                    {
                                        var newColumnID = Guid.NewGuid();
                                        var oldColID = col.ID;
                                        if (tenantInfo.EditColumns.Any(x => x.SRC_ID == col.ID))
                                        {
                                            var tenantEditColumn = tenantInfo.EditColumns.FirstOrDefault(x => x.SRC_ID == col.ID);
                                            T_PF_EDIT_COLUMN column = APT.Infrastructure.Core.CloneExtends.DeepCloneObject(col);//OPT创建的表单      
                                            column.SRC_ID = col.ID;
                                            column.ID = tenantEditColumn.ID;
                                            column.ORG_ID = tenantID;
                                            column.PAGE_EDIT_ID = newTableID;
                                            if (column.PAGE_EDIT_PANEL_ID != null)
                                            {
                                                column.PAGE_EDIT_PANEL_ID = newEditPanels[(Guid)column.PAGE_EDIT_PANEL_ID];
                                            }
                                            saveEditColumns.Add(column);//添加列基础到对应租户
                                            newColumnID = tenantEditColumn.ID;//更新
                                        }
                                        else
                                        {
                                            T_PF_EDIT_COLUMN column = APT.Infrastructure.Core.CloneExtends.DeepCloneObject(col);//OPT创建的表单         
                                            column.SRC_ID = col.ID;
                                            column.ID = newColumnID;
                                            column.ORG_ID = tenantID;
                                            column.PAGE_EDIT_ID = newTableID;
                                            if (column.PAGE_EDIT_PANEL_ID != null)
                                            {
                                                column.PAGE_EDIT_PANEL_ID = newEditPanels[(Guid)column.PAGE_EDIT_PANEL_ID];
                                            }
                                            addEntity.EditColumns.Add(column);//添加列基础到对应租户
                                        }
                                        #region T_PF_EDIT_COLUMN_FILTER
                                        var colFilters = srcInfo.EditColumnFilters.Where(x => x.EDIT_COLUMN_ID == oldColID);
                                        foreach (var colFilter in colFilters)
                                        {
                                            if (tenantInfo.EditColumnFilters.Any(x => x.SRC_ID == colFilter.ID))
                                            {
                                                var tenantEditColumnFilter = tenantInfo.EditColumnFilters.FirstOrDefault(x => x.SRC_ID == colFilter.ID);
                                                var cf = APT.Infrastructure.Core.CloneExtends.DeepCloneObject(colFilter);
                                                cf.SRC_ID = colFilter.ID;
                                                cf.ID = tenantEditColumnFilter.ID;
                                                cf.ORG_ID = tenantID;
                                                cf.PAGE_EDIT_ID = newTableID;
                                                cf.EDIT_COLUMN_ID = newColumnID;
                                                saveEditColumnFilters.Add(cf);
                                            }
                                            else
                                            {
                                                var colID = Guid.NewGuid();
                                                var cf = APT.Infrastructure.Core.CloneExtends.DeepCloneObject(colFilter);
                                                cf.SRC_ID = colFilter.ID;
                                                cf.ID = colID;
                                                cf.ORG_ID = tenantID;
                                                cf.PAGE_EDIT_ID = newTableID;
                                                cf.EDIT_COLUMN_ID = newColumnID;
                                                addEntity.EditColumnFilters.Add(cf);
                                            }

                                        }
                                        #endregion
                                        #region T_PF_EDIT_COLUMN_FILL_MAP
                                        var colFillMaps = srcInfo.EditColumnFillMaps.Where(x => x.EDIT_COLUMN_ID == oldColID);
                                        foreach (var colFillMap in colFillMaps)
                                        {
                                            var colMapID = Guid.NewGuid();
                                            if (tenantInfo.EditColumnFillMaps.Any(x => x.SRC_ID == colFillMap.ID))
                                            {
                                                var tenantEditColumnFillMap = tenantInfo.EditColumnFillMaps.FirstOrDefault(x => x.SRC_ID == colFillMap.ID);
                                                var cm = APT.Infrastructure.Core.CloneExtends.DeepCloneObject(colFillMap);
                                                cm.SRC_ID = colFillMap.ID;
                                                cm.ID = tenantEditColumnFillMap.ID;
                                                cm.ORG_ID = tenantID;
                                                cm.PAGE_EDIT_ID = newTableID;
                                                cm.EDIT_COLUMN_ID = newColumnID;
                                                saveEditColumnFillMaps.Add(cm);
                                                colMapID = tenantEditColumnFillMap.ID;
                                            }
                                            else
                                            {
                                                var cm = APT.Infrastructure.Core.CloneExtends.DeepCloneObject(colFillMap);
                                                cm.SRC_ID = colFillMap.ID;
                                                cm.ID = colMapID;
                                                cm.ORG_ID = tenantID;
                                                cm.PAGE_EDIT_ID = newTableID;
                                                cm.EDIT_COLUMN_ID = newColumnID;
                                                addEntity.EditColumnFillMaps.Add(cm);
                                            }
                                            #region T_PF_EDIT_COLUMN_FILL_MAP
                                            var colFillMapDs = srcInfo.EditColumnFillMapDs.Where(x => x.EDIT_COLUMN_FILL_MAP_ID == colMapID);
                                            foreach (var colFillMapD in colFillMapDs)
                                            {
                                                if (tenantInfo.EditColumnFillMapDs.Any(x => x.SRC_ID == colFillMapD.ID))
                                                {
                                                    var tenantEditColumnFillMapD = tenantInfo.EditColumnFillMapDs.FirstOrDefault(x => x.SRC_ID == colFillMapD.ID);
                                                    var cmd = APT.Infrastructure.Core.CloneExtends.DeepCloneObject(colFillMapD);
                                                    cmd.SRC_ID = colFillMapD.ID;
                                                    cmd.ID = tenantEditColumnFillMapD.ID;
                                                    cmd.ORG_ID = tenantID;
                                                    cmd.EDIT_COLUMN_FILL_MAP_ID = colMapID;
                                                    saveEditColumnFillMapDs.Add(cmd);
                                                }
                                                else
                                                {
                                                    var colMapDID = Guid.NewGuid();
                                                    var cmd = APT.Infrastructure.Core.CloneExtends.DeepCloneObject(colFillMapD);
                                                    cmd.SRC_ID = colFillMapD.ID;
                                                    cmd.ID = colMapDID;
                                                    cmd.ORG_ID = tenantID;
                                                    cmd.EDIT_COLUMN_FILL_MAP_ID = colMapID;
                                                    addEntity.EditColumnFillMapDs.Add(cmd);
                                                }
                                            }
                                            #endregion
                                        }
                                        #endregion

                                        if (!string.IsNullOrEmpty(col.SHOW_FORM_CODE))//有配置关联表单
                                        {
                                            var colForm = srcInfo.Forms.Where(x => x.CODE == col.SHOW_FORM_CODE).FirstOrDefault();
                                            if (!existForm.Contains(form.ID) && !savePermPFForm.Any(x => x.CODE == colForm.CODE) && !addEntity.Forms.Any(x => x.CODE == colForm.CODE))//去重
                                            {
                                                if (colForm != null)
                                                {
                                                    if (tenantInfo.Forms.Any(x => x.SRC_ID == colForm.ID))
                                                    {
                                                        var tenantColForm = tenantInfo.Forms.FirstOrDefault(x => x.SRC_ID == colForm.ID);
                                                        T_PF_FORM newForm = APT.Infrastructure.Core.CloneExtends.DeepCloneObject(colForm);//OPT创建的表单                                                   
                                                        AddTable(srcInfo, colForm, savePermPFForm,
                                                      savePermPageTable, saveTablePanels, saveTableParams,
                                                      saveColumFilters, savePermCloumn,
                                                      savePermPageEdit, saveEditPanels, saveEditColumns,
                                                      saveEditColumnFilters, saveEditColumnFillMaps,
                                                      saveEditColumnFillMapDs,
                                                      savePermPageTree, saveTreeColumns,
                                                      savePermPageChart, saveChartParams,
                                                      savePermPageCustom,
                                                      savePermBtn, saveFormQuerys, saveQuerys, savePermPFFormRelation,
                                                      newSchemes, newTablePanels, newEditPanels, tenantID, tenantColForm.ID, existForm, existTable, tenantInfo, addEntity);
                                                        newForm.SRC_ID = colForm.ID;
                                                        newForm.ID = tenantColForm.ID;
                                                        newForm.ORG_ID = tenantID;
                                                        newForm.Nav_Org = null;
                                                        savePermPFForm.Add(newForm);//添加表单到对应租户
                                                    }
                                                    else
                                                    {
                                                        var newFormID = Guid.NewGuid();
                                                        T_PF_FORM newForm = APT.Infrastructure.Core.CloneExtends.DeepCloneObject(colForm);//OPT创建的表单                                                   
                                                        AddTable(srcInfo, colForm, savePermPFForm,
                                                      savePermPageTable, saveTablePanels, saveTableParams,
                                                      saveColumFilters, savePermCloumn,
                                                      savePermPageEdit, saveEditPanels, saveEditColumns,
                                                      saveEditColumnFilters, saveEditColumnFillMaps,
                                                      saveEditColumnFillMapDs,
                                                      savePermPageTree, saveTreeColumns,
                                                      savePermPageChart, saveChartParams,
                                                      savePermPageCustom,
                                                      savePermBtn, saveFormQuerys, saveQuerys, savePermPFFormRelation,
                                                      newSchemes, newTablePanels, newEditPanels, tenantID, newFormID, existForm, existTable, tenantInfo, addEntity);
                                                        newForm.SRC_ID = colForm.ID;
                                                        newForm.ID = newFormID;
                                                        newForm.ORG_ID = tenantID;
                                                        newForm.Nav_Org = null;
                                                        addEntity.Forms.Add(newForm);
                                                    }

                                                }
                                            }
                                        }
                                    }
                                    #endregion
                                    #region 添加按钮
                                    // var allBtn = this.GetEntities<T_PF_BTN>(t => t.PAGE_ID == oldpageEdit, filter).ToList();//OPT所有列
                                    var allBtn = srcInfo.Btns.Where(x => x.PAGE_ID == oldpageEdit).ToList();
                                    AddTableBtn(srcInfo, savePermPFForm,
                                                  savePermPageTable, saveTablePanels, saveTableParams,
                                                  saveColumFilters, savePermCloumn,
                                                  savePermPageEdit, saveEditPanels, saveEditColumns,
                                                  saveEditColumnFilters, saveEditColumnFillMaps,
                                                  saveEditColumnFillMapDs,
                                                  savePermPageTree, saveTreeColumns,
                                                  savePermPageChart, saveChartParams,
                                                  savePermPageCustom,
                                                  savePermBtn, saveFormQuerys, saveQuerys, savePermPFFormRelation,
                                                  newSchemes, newTablePanels, newEditPanels, tenantID, existForm, existTable, allBtn, newTableID, tenantInfo, addEntity);
                                    #endregion
                                }
                            }
                        }
                        break;
                    }
                case (int)PFFormTypeEnum.树形编辑页:
                case (int)PFFormTypeEnum.横向树编辑页:
                    {
                        #region 配置关联表单T_PF_FORM_RELATION
                        var formRelations = srcInfo.FormRelations.Where(x => x.PAGE_FORM_ID == form.ID);
                        if (formRelations.Any())
                        {
                            foreach (var formRelation in formRelations)
                            {
                                T_PF_FORM_RELATION relation = APT.Infrastructure.Core.CloneExtends.DeepCloneObject(formRelation);
                                if (tenantInfo.FormRelations.Any(x => x.SRC_ID == formRelation.ID))
                                {
                                    var tenantRelation = tenantInfo.FormRelations.FirstOrDefault(x => x.SRC_ID == formRelation.ID);
                                    var relationForm = srcInfo.Forms.Where(x => x.CODE == formRelation.CODE).FirstOrDefault();
                                    if (!existForm.Contains(relationForm.ID) && !savePermPFForm.Any(x => x.CODE == relationForm.CODE) && !addEntity.Forms.Any(x => x.CODE == relationForm.CODE))//去重
                                    {
                                        if (relationForm != null)
                                        {
                                            if (tenantInfo.Forms.Any(x => x.SRC_ID == relationForm.ID))
                                            {
                                                var tenantForm = tenantInfo.Forms.FirstOrDefault(x => x.SRC_ID == relationForm.ID);//取旧数据ID不变
                                                T_PF_FORM newForm = APT.Infrastructure.Core.CloneExtends.DeepCloneObject(relationForm);//OPT创建的表单                                                   
                                                AddTable(srcInfo, relationForm, savePermPFForm,
                                                          savePermPageTable, saveTablePanels, saveTableParams,
                                                          saveColumFilters, savePermCloumn,
                                                          savePermPageEdit, saveEditPanels, saveEditColumns,
                                                          saveEditColumnFilters, saveEditColumnFillMaps,
                                                          saveEditColumnFillMapDs,
                                                          savePermPageTree, saveTreeColumns,
                                                          savePermPageChart, saveChartParams,
                                                          savePermPageCustom,
                                                          savePermBtn, saveFormQuerys, saveQuerys, savePermPFFormRelation,
                                                          newSchemes, newTablePanels, newEditPanels, tenantID, tenantForm.ID, existForm, existTable, tenantInfo, addEntity);
                                                newForm.SRC_ID = relationForm.ID;
                                                newForm.ID = tenantForm.ID;
                                                newForm.ORG_ID = tenantID;
                                                newForm.Nav_Org = null;
                                                savePermPFForm.Add(newForm);//添加表单到对应租户
                                            }
                                            else
                                            {
                                                var newFormID = Guid.NewGuid();
                                                T_PF_FORM newForm = APT.Infrastructure.Core.CloneExtends.DeepCloneObject(relationForm);//OPT创建的表单                                                   
                                                AddTable(srcInfo, relationForm, savePermPFForm,
                                                          savePermPageTable, saveTablePanels, saveTableParams,
                                                          saveColumFilters, savePermCloumn,
                                                          savePermPageEdit, saveEditPanels, saveEditColumns,
                                                          saveEditColumnFilters, saveEditColumnFillMaps,
                                                          saveEditColumnFillMapDs,
                                                          savePermPageTree, saveTreeColumns,
                                                          savePermPageChart, saveChartParams,
                                                          savePermPageCustom,
                                                          savePermBtn, saveFormQuerys, saveQuerys, savePermPFFormRelation,
                                                          newSchemes, newTablePanels, newEditPanels, tenantID, newFormID, existForm, existTable, tenantInfo, addEntity);
                                                newForm.SRC_ID = relationForm.ID;
                                                newForm.ID = newFormID;
                                                newForm.ORG_ID = tenantID;
                                                newForm.Nav_Org = null;
                                                addEntity.Forms.Add(newForm);
                                                //savePermPFForm.Add(newForm);//添加表单到对应租户
                                            }

                                        }
                                    }
                                    relation.SRC_ID = formRelation.ID;
                                    relation.ID = tenantRelation.ID;
                                    relation.ORG_ID = tenantID;
                                    relation.PAGE_FORM_ID = pageFormID;
                                    savePermPFFormRelation.Add(relation);
                                }
                                else
                                {
                                    var relationForm = srcInfo.Forms.Where(x => x.CODE == formRelation.CODE).FirstOrDefault();
                                    if (!existForm.Contains(relationForm.ID) && !savePermPFForm.Any(x => x.CODE == relationForm.CODE) && !addEntity.Forms.Any(x => x.CODE == relationForm.CODE))//去重
                                    {
                                        if (relationForm != null)
                                        {
                                            if (tenantInfo.Forms.Any(x => x.SRC_ID == relationForm.ID))
                                            {
                                                var tenantForm = tenantInfo.Forms.FirstOrDefault(x => x.SRC_ID == relationForm.ID);
                                                T_PF_FORM newForm = APT.Infrastructure.Core.CloneExtends.DeepCloneObject(relationForm);//OPT创建的表单                                                   
                                                AddTable(srcInfo, relationForm, savePermPFForm,
                                                           savePermPageTable, saveTablePanels, saveTableParams,
                                                           saveColumFilters, savePermCloumn,
                                                          savePermPageEdit, saveEditPanels, saveEditColumns,
                                                          saveEditColumnFilters, saveEditColumnFillMaps,
                                                          saveEditColumnFillMapDs,
                                                          savePermPageTree, saveTreeColumns,
                                                          savePermPageChart, saveChartParams,
                                                          savePermPageCustom,
                                                          savePermBtn, saveFormQuerys, saveQuerys, savePermPFFormRelation,
                                                          newSchemes, newTablePanels, newEditPanels, tenantID, tenantForm.ID, existForm, existTable, tenantInfo, addEntity);
                                                newForm.SRC_ID = relationForm.ID;
                                                newForm.ID = tenantForm.ID;
                                                newForm.ORG_ID = tenantID;
                                                newForm.Nav_Org = null;
                                                savePermPFForm.Add(newForm);//添加表单到对应租户
                                            }
                                            else
                                            {
                                                var newFormID = Guid.NewGuid();
                                                T_PF_FORM newForm = APT.Infrastructure.Core.CloneExtends.DeepCloneObject(relationForm);//OPT创建的表单                                                   
                                                AddTable(srcInfo, relationForm, savePermPFForm,
                                                          savePermPageTable, saveTablePanels, saveTableParams,
                                                          saveColumFilters, savePermCloumn,
                                                          savePermPageEdit, saveEditPanels, saveEditColumns,
                                                          saveEditColumnFilters, saveEditColumnFillMaps,
                                                          saveEditColumnFillMapDs,
                                                          savePermPageTree, saveTreeColumns,
                                                          savePermPageChart, saveChartParams,
                                                          savePermPageCustom,
                                                          savePermBtn, saveFormQuerys, saveQuerys, savePermPFFormRelation,
                                                          newSchemes, newTablePanels, newEditPanels, tenantID, newFormID, existForm, existTable, tenantInfo, addEntity);
                                                newForm.SRC_ID = relationForm.ID;
                                                newForm.ID = newFormID;
                                                newForm.ORG_ID = tenantID;
                                                newForm.Nav_Org = null;
                                                addEntity.Forms.Add(newForm);
                                            }

                                        }
                                    }
                                    var newRelationID = new Guid();
                                    relation.SRC_ID = formRelation.ID;
                                    relation.ID = newRelationID;
                                    relation.ORG_ID = tenantID;
                                    relation.PAGE_FORM_ID = pageFormID;
                                    addEntity.FormRelations.Add(relation);
                                }

                            }
                        }
                        #endregion
                        #region 查询
                        AddQuerys(form, tenantID, pageFormID, srcInfo.FormQuerys, saveFormQuerys, srcInfo.Querys, saveQuerys, tenantInfo, addEntity);
                        #endregion
                        #region T_PF_PAGE_TREE
                        var resTree = srcInfo.PageTrees.Where(x => x.PAGE_FORM_ID == form.ID).ToList();//查找相关表单      
                        if (resTree.Count > 0)
                        {
                            foreach (var item in resTree)
                            {
                                #region MyRegion 添加T_PF_PAGE_TABLE
                                var oldPageID = item.ID;
                                if (!existTable.Contains(oldPageID))
                                {
                                    existTable.Add(oldPageID);
                                    var newTableID = AddPageTable(item, pageFormID, tenantID, savePermPageTree, newSchemes, saveTablePanels, saveTableParams,
                                    newTablePanels, saveEditPanels, newEditPanels, saveChartParams, tenantInfo, srcInfo, addEntity);
                                    #endregion
                                    #region T_PF_TREE_COLUMN
                                    //var allClm = this.GetEntities<T_PF_TREE_COLUMN>(t => t.PAGE_TREE_ID == oldPageID, filter).ToList();//OPT所有列
                                    var allClm = srcInfo.TreeColumns.Where(t => t.PAGE_TREE_ID == oldPageID).ToList();
                                    foreach (var col in allClm)
                                    {
                                        var newColumnID = Guid.NewGuid();
                                        T_PF_TREE_COLUMN column = APT.Infrastructure.Core.CloneExtends.DeepCloneObject(col);//OPT创建的表单
                                        var oldColID = col.ID;
                                        if (tenantInfo.TreeColumns.Any(x => x.SRC_ID == col.ID))
                                        {
                                            var tenantTreeColumn = tenantInfo.TreeColumns.FirstOrDefault(x => x.SRC_ID == col.ID);
                                            column.SRC_ID = col.ID;
                                            column.ID = tenantTreeColumn.ID;
                                            column.ORG_ID = tenantID;
                                            column.PAGE_TREE_ID = newTableID;
                                            saveTreeColumns.Add(column);//添加列基础到对应租户
                                        }
                                        else
                                        {
                                            column.SRC_ID = col.ID;
                                            column.ID = newColumnID;
                                            column.ORG_ID = tenantID;
                                            column.PAGE_TREE_ID = newTableID;
                                            addEntity.TreeColumns.Add(column);
                                        }

                                    }
                                    #endregion
                                    #region 添加按钮
                                    // var allBtn = this.GetEntities<T_PF_BTN>(t => t.PAGE_ID == oldPageID, filter).ToList();//OPT所有列
                                    var allBtn = srcInfo.Btns.Where(x => x.PAGE_ID == oldPageID).ToList();
                                    AddTableBtn(srcInfo, savePermPFForm,
                                                      savePermPageTable, saveTablePanels, saveTableParams,
                                                      saveColumFilters, savePermCloumn,
                                                      savePermPageEdit, saveEditPanels, saveEditColumns,
                                                      saveEditColumnFilters, saveEditColumnFillMaps,
                                                      saveEditColumnFillMapDs,
                                                      savePermPageTree, saveTreeColumns,
                                                      savePermPageChart, saveChartParams,
                                                      savePermPageCustom,
                                                      savePermBtn, saveFormQuerys, saveQuerys, savePermPFFormRelation,
                                                      newSchemes, newTablePanels, newEditPanels, tenantID, existForm, existTable, allBtn, newTableID, tenantInfo, addEntity);
                                    #endregion
                                }
                            }
                        }
                        #endregion
                        #region 编辑配置srcInfo.PageEdits
                        var resEdit = srcInfo.PageEdits.Where(x => x.PAGE_FORM_ID == form.ID).ToList();//查找相关表单      
                        if (resEdit.Count > 0)
                        {
                            foreach (var item in resEdit)
                            {
                                var oldpageEdit = item.ID;
                                if (!existTable.Contains(oldpageEdit))
                                {
                                    existTable.Add(oldpageEdit);
                                    var newTableID = AddPageTable(item, pageFormID, tenantID, savePermPageEdit, newSchemes, saveTablePanels, saveTableParams,
                                    newTablePanels, saveEditPanels, newEditPanels, saveChartParams, tenantInfo, srcInfo, addEntity);
                                    #region 添加编辑列
                                    //var allClm = this.GetEntities<T_PF_EDIT_COLUMN>(t => t.PAGE_EDIT_ID == oldpageEdit, filter).ToList();//OPT所有列
                                    var allClm = srcInfo.EditColumns.Where(t => t.PAGE_EDIT_ID == oldpageEdit).ToList();
                                    foreach (var col in allClm)
                                    {
                                        var newColumnID = Guid.NewGuid();
                                        var oldColID = col.ID;
                                        if (tenantInfo.EditColumns.Any(x => x.SRC_ID == col.ID))
                                        {
                                            var tenantEditColumn = tenantInfo.EditColumns.FirstOrDefault(x => x.SRC_ID == col.ID);
                                            T_PF_EDIT_COLUMN column = APT.Infrastructure.Core.CloneExtends.DeepCloneObject(col);//OPT创建的表单      
                                            column.SRC_ID = col.ID;
                                            column.ID = tenantEditColumn.ID;
                                            column.ORG_ID = tenantID;
                                            column.PAGE_EDIT_ID = newTableID;
                                            if (column.PAGE_EDIT_PANEL_ID != null)
                                            {
                                                column.PAGE_EDIT_PANEL_ID = newEditPanels[(Guid)column.PAGE_EDIT_PANEL_ID];
                                            }
                                            saveEditColumns.Add(column);//添加列基础到对应租户
                                            newColumnID = tenantEditColumn.ID;//更新
                                        }
                                        else
                                        {
                                            T_PF_EDIT_COLUMN column = APT.Infrastructure.Core.CloneExtends.DeepCloneObject(col);//OPT创建的表单         
                                            column.SRC_ID = col.ID;
                                            column.ID = newColumnID;
                                            column.ORG_ID = tenantID;
                                            column.PAGE_EDIT_ID = newTableID;
                                            if (column.PAGE_EDIT_PANEL_ID != null)
                                            {
                                                column.PAGE_EDIT_PANEL_ID = newEditPanels[(Guid)column.PAGE_EDIT_PANEL_ID];
                                            }
                                            addEntity.EditColumns.Add(column);//添加列基础到对应租户
                                        }
                                        #region T_PF_EDIT_COLUMN_FILTER
                                        var colFilters = srcInfo.EditColumnFilters.Where(x => x.EDIT_COLUMN_ID == oldColID);
                                        foreach (var colFilter in colFilters)
                                        {
                                            if (tenantInfo.EditColumnFilters.Any(x => x.SRC_ID == colFilter.ID))
                                            {
                                                var tenantEditColumnFilter = tenantInfo.EditColumnFilters.FirstOrDefault(x => x.SRC_ID == colFilter.ID);
                                                var cf = APT.Infrastructure.Core.CloneExtends.DeepCloneObject(colFilter);
                                                cf.SRC_ID = colFilter.ID;
                                                cf.ID = tenantEditColumnFilter.ID;
                                                cf.ORG_ID = tenantID;
                                                cf.PAGE_EDIT_ID = newTableID;
                                                cf.EDIT_COLUMN_ID = newColumnID;
                                                saveEditColumnFilters.Add(cf);
                                            }
                                            else
                                            {
                                                var colID = Guid.NewGuid();
                                                var cf = APT.Infrastructure.Core.CloneExtends.DeepCloneObject(colFilter);
                                                cf.SRC_ID = colFilter.ID;
                                                cf.ID = colID;
                                                cf.ORG_ID = tenantID;
                                                cf.PAGE_EDIT_ID = newTableID;
                                                cf.EDIT_COLUMN_ID = newColumnID;
                                                addEntity.EditColumnFilters.Add(cf);
                                            }

                                        }
                                        #endregion
                                        #region T_PF_EDIT_COLUMN_FILL_MAP
                                        var colFillMaps = srcInfo.EditColumnFillMaps.Where(x => x.EDIT_COLUMN_ID == oldColID);
                                        foreach (var colFillMap in colFillMaps)
                                        {
                                            var colMapID = Guid.NewGuid();
                                            if (tenantInfo.EditColumnFillMaps.Any(x => x.SRC_ID == colFillMap.ID))
                                            {
                                                var tenantEditColumnFillMap = tenantInfo.EditColumnFillMaps.FirstOrDefault(x => x.SRC_ID == colFillMap.ID);
                                                var cm = APT.Infrastructure.Core.CloneExtends.DeepCloneObject(colFillMap);
                                                cm.SRC_ID = colFillMap.ID;
                                                cm.ID = tenantEditColumnFillMap.ID;
                                                cm.ORG_ID = tenantID;
                                                cm.PAGE_EDIT_ID = newTableID;
                                                cm.EDIT_COLUMN_ID = newColumnID;
                                                saveEditColumnFillMaps.Add(cm);
                                                colMapID = tenantEditColumnFillMap.ID;
                                            }
                                            else
                                            {
                                                var cm = APT.Infrastructure.Core.CloneExtends.DeepCloneObject(colFillMap);
                                                cm.SRC_ID = colFillMap.ID;
                                                cm.ID = colMapID;
                                                cm.ORG_ID = tenantID;
                                                cm.PAGE_EDIT_ID = newTableID;
                                                cm.EDIT_COLUMN_ID = newColumnID;
                                                addEntity.EditColumnFillMaps.Add(cm);
                                            }
                                            #region T_PF_EDIT_COLUMN_FILL_MAP
                                            var colFillMapDs = srcInfo.EditColumnFillMapDs.Where(x => x.EDIT_COLUMN_FILL_MAP_ID == colMapID);
                                            foreach (var colFillMapD in colFillMapDs)
                                            {
                                                if (tenantInfo.EditColumnFillMapDs.Any(x => x.SRC_ID == colFillMapD.ID))
                                                {
                                                    var tenantEditColumnFillMapD = tenantInfo.EditColumnFillMapDs.FirstOrDefault(x => x.SRC_ID == colFillMapD.ID);
                                                    var cmd = APT.Infrastructure.Core.CloneExtends.DeepCloneObject(colFillMapD);
                                                    cmd.SRC_ID = colFillMapD.ID;
                                                    cmd.ID = tenantEditColumnFillMapD.ID;
                                                    cmd.ORG_ID = tenantID;
                                                    cmd.EDIT_COLUMN_FILL_MAP_ID = colMapID;
                                                    saveEditColumnFillMapDs.Add(cmd);
                                                }
                                                else
                                                {
                                                    var colMapDID = Guid.NewGuid();
                                                    var cmd = APT.Infrastructure.Core.CloneExtends.DeepCloneObject(colFillMapD);
                                                    cmd.SRC_ID = colFillMapD.ID;
                                                    cmd.ID = colMapDID;
                                                    cmd.ORG_ID = tenantID;
                                                    cmd.EDIT_COLUMN_FILL_MAP_ID = colMapID;
                                                    addEntity.EditColumnFillMapDs.Add(cmd);
                                                }
                                            }
                                            #endregion
                                        }
                                        #endregion

                                        if (!string.IsNullOrEmpty(col.SHOW_FORM_CODE))//有配置关联表单
                                        {
                                            var colForm = srcInfo.Forms.Where(x => x.CODE == col.SHOW_FORM_CODE).FirstOrDefault();
                                            if (!existForm.Contains(form.ID) && !savePermPFForm.Any(x => x.CODE == colForm.CODE) && !addEntity.Forms.Any(x => x.CODE == colForm.CODE))//去重
                                            {
                                                if (colForm != null)
                                                {
                                                    if (tenantInfo.Forms.Any(x => x.SRC_ID == colForm.ID))
                                                    {
                                                        var tenantColForm = tenantInfo.Forms.FirstOrDefault(x => x.SRC_ID == colForm.ID);
                                                        T_PF_FORM newForm = APT.Infrastructure.Core.CloneExtends.DeepCloneObject(colForm);//OPT创建的表单                                                   
                                                        AddTable(srcInfo, colForm, savePermPFForm,
                                                      savePermPageTable, saveTablePanels, saveTableParams,
                                                      saveColumFilters, savePermCloumn,
                                                      savePermPageEdit, saveEditPanels, saveEditColumns,
                                                      saveEditColumnFilters, saveEditColumnFillMaps,
                                                      saveEditColumnFillMapDs,
                                                      savePermPageTree, saveTreeColumns,
                                                      savePermPageChart, saveChartParams,
                                                      savePermPageCustom,
                                                      savePermBtn, saveFormQuerys, saveQuerys, savePermPFFormRelation,
                                                      newSchemes, newTablePanels, newEditPanels, tenantID, tenantColForm.ID, existForm, existTable, tenantInfo, addEntity);
                                                        newForm.SRC_ID = colForm.ID;
                                                        newForm.ID = tenantColForm.ID;
                                                        newForm.ORG_ID = tenantID;
                                                        newForm.Nav_Org = null;
                                                        savePermPFForm.Add(newForm);//添加表单到对应租户
                                                    }
                                                    else
                                                    {
                                                        var newFormID = Guid.NewGuid();
                                                        T_PF_FORM newForm = APT.Infrastructure.Core.CloneExtends.DeepCloneObject(colForm);//OPT创建的表单                                                   
                                                        AddTable(srcInfo, colForm, savePermPFForm,
                                                      savePermPageTable, saveTablePanels, saveTableParams,
                                                      saveColumFilters, savePermCloumn,
                                                      savePermPageEdit, saveEditPanels, saveEditColumns,
                                                      saveEditColumnFilters, saveEditColumnFillMaps,
                                                      saveEditColumnFillMapDs,
                                                      savePermPageTree, saveTreeColumns,
                                                      savePermPageChart, saveChartParams,
                                                      savePermPageCustom,
                                                      savePermBtn, saveFormQuerys, saveQuerys, savePermPFFormRelation,
                                                      newSchemes, newTablePanels, newEditPanels, tenantID, newFormID, existForm, existTable, tenantInfo, addEntity);
                                                        newForm.SRC_ID = colForm.ID;
                                                        newForm.ID = newFormID;
                                                        newForm.ORG_ID = tenantID;
                                                        newForm.Nav_Org = null;
                                                        addEntity.Forms.Add(newForm);
                                                    }

                                                }
                                            }
                                        }
                                    }
                                    #endregion
                                    #region 添加按钮
                                    // var allBtn = this.GetEntities<T_PF_BTN>(t => t.PAGE_ID == oldpageEdit, filter).ToList();//OPT所有列
                                    var allBtn = srcInfo.Btns.Where(x => x.PAGE_ID == oldpageEdit).ToList();
                                    AddTableBtn(srcInfo, savePermPFForm,
                                                  savePermPageTable, saveTablePanels, saveTableParams,
                                                  saveColumFilters, savePermCloumn,
                                                  savePermPageEdit, saveEditPanels, saveEditColumns,
                                                  saveEditColumnFilters, saveEditColumnFillMaps,
                                                  saveEditColumnFillMapDs,
                                                  savePermPageTree, saveTreeColumns,
                                                  savePermPageChart, saveChartParams,
                                                  savePermPageCustom,
                                                  savePermBtn, saveFormQuerys, saveQuerys, savePermPFFormRelation,
                                                  newSchemes, newTablePanels, newEditPanels, tenantID, existForm, existTable, allBtn, newTableID, tenantInfo, addEntity);
                                    #endregion
                                }
                            }
                        }
                        #endregion
                        break;
                    }
                case (int)PFFormTypeEnum.图表表单:
                    {
                        #region 配置关联表单T_PF_FORM_RELATION
                        var formRelations = srcInfo.FormRelations.Where(x => x.PAGE_FORM_ID == form.ID);
                        if (formRelations.Any())
                        {
                            foreach (var formRelation in formRelations)
                            {
                                T_PF_FORM_RELATION relation = APT.Infrastructure.Core.CloneExtends.DeepCloneObject(formRelation);
                                if (tenantInfo.FormRelations.Any(x => x.SRC_ID == formRelation.ID))
                                {
                                    var tenantRelation = tenantInfo.FormRelations.FirstOrDefault(x => x.SRC_ID == formRelation.ID);
                                    var relationForm = srcInfo.Forms.Where(x => x.CODE == formRelation.CODE).FirstOrDefault();
                                    if (!existForm.Contains(relationForm.ID) && !savePermPFForm.Any(x => x.CODE == relationForm.CODE) && !addEntity.Forms.Any(x => x.CODE == relationForm.CODE))//去重
                                    {
                                        if (relationForm != null)
                                        {
                                            if (tenantInfo.Forms.Any(x => x.SRC_ID == relationForm.ID))
                                            {
                                                var tenantForm = tenantInfo.Forms.FirstOrDefault(x => x.SRC_ID == relationForm.ID);//取旧数据ID不变
                                                T_PF_FORM newForm = APT.Infrastructure.Core.CloneExtends.DeepCloneObject(relationForm);//OPT创建的表单                                                   
                                                AddTable(srcInfo, relationForm, savePermPFForm,
                                                          savePermPageTable, saveTablePanels, saveTableParams,
                                                          saveColumFilters, savePermCloumn,
                                                          savePermPageEdit, saveEditPanels, saveEditColumns,
                                                          saveEditColumnFilters, saveEditColumnFillMaps,
                                                          saveEditColumnFillMapDs,
                                                          savePermPageTree, saveTreeColumns,
                                                          savePermPageChart, saveChartParams,
                                                          savePermPageCustom,
                                                          savePermBtn, saveFormQuerys, saveQuerys, savePermPFFormRelation,
                                                          newSchemes, newTablePanels, newEditPanels, tenantID, tenantForm.ID, existForm, existTable, tenantInfo, addEntity);
                                                newForm.SRC_ID = relationForm.ID;
                                                newForm.ID = tenantForm.ID;
                                                newForm.ORG_ID = tenantID;
                                                newForm.Nav_Org = null;
                                                savePermPFForm.Add(newForm);//添加表单到对应租户
                                            }
                                            else
                                            {
                                                var newFormID = Guid.NewGuid();
                                                T_PF_FORM newForm = APT.Infrastructure.Core.CloneExtends.DeepCloneObject(relationForm);//OPT创建的表单                                                   
                                                AddTable(srcInfo, relationForm, savePermPFForm,
                                                          savePermPageTable, saveTablePanels, saveTableParams,
                                                          saveColumFilters, savePermCloumn,
                                                          savePermPageEdit, saveEditPanels, saveEditColumns,
                                                          saveEditColumnFilters, saveEditColumnFillMaps,
                                                          saveEditColumnFillMapDs,
                                                          savePermPageTree, saveTreeColumns,
                                                          savePermPageChart, saveChartParams,
                                                          savePermPageCustom,
                                                          savePermBtn, saveFormQuerys, saveQuerys, savePermPFFormRelation,
                                                          newSchemes, newTablePanels, newEditPanels, tenantID, newFormID, existForm, existTable, tenantInfo, addEntity);
                                                newForm.SRC_ID = relationForm.ID;
                                                newForm.ID = newFormID;
                                                newForm.ORG_ID = tenantID;
                                                newForm.Nav_Org = null;
                                                addEntity.Forms.Add(newForm);
                                                //savePermPFForm.Add(newForm);//添加表单到对应租户
                                            }

                                        }
                                    }
                                    relation.SRC_ID = formRelation.ID;
                                    relation.ID = tenantRelation.ID;
                                    relation.ORG_ID = tenantID;
                                    relation.PAGE_FORM_ID = pageFormID;
                                    savePermPFFormRelation.Add(relation);
                                }
                                else
                                {
                                    var relationForm = srcInfo.Forms.Where(x => x.CODE == formRelation.CODE).FirstOrDefault();
                                    if (!existForm.Contains(relationForm.ID) && !savePermPFForm.Any(x => x.CODE == relationForm.CODE) && !addEntity.Forms.Any(x => x.CODE == relationForm.CODE))//去重
                                    {
                                        if (relationForm != null)
                                        {
                                            if (tenantInfo.Forms.Any(x => x.SRC_ID == relationForm.ID))
                                            {
                                                var tenantForm = tenantInfo.Forms.FirstOrDefault(x => x.SRC_ID == relationForm.ID);
                                                T_PF_FORM newForm = APT.Infrastructure.Core.CloneExtends.DeepCloneObject(relationForm);//OPT创建的表单                                                   
                                                AddTable(srcInfo, relationForm, savePermPFForm,
                                                           savePermPageTable, saveTablePanels, saveTableParams,
                                                           saveColumFilters, savePermCloumn,
                                                          savePermPageEdit, saveEditPanels, saveEditColumns,
                                                          saveEditColumnFilters, saveEditColumnFillMaps,
                                                          saveEditColumnFillMapDs,
                                                          savePermPageTree, saveTreeColumns,
                                                          savePermPageChart, saveChartParams,
                                                          savePermPageCustom,
                                                          savePermBtn, saveFormQuerys, saveQuerys, savePermPFFormRelation,
                                                          newSchemes, newTablePanels, newEditPanels, tenantID, tenantForm.ID, existForm, existTable, tenantInfo, addEntity);
                                                newForm.SRC_ID = relationForm.ID;
                                                newForm.ID = tenantForm.ID;
                                                newForm.ORG_ID = tenantID;
                                                newForm.Nav_Org = null;
                                                savePermPFForm.Add(newForm);//添加表单到对应租户
                                            }
                                            else
                                            {
                                                var newFormID = Guid.NewGuid();
                                                T_PF_FORM newForm = APT.Infrastructure.Core.CloneExtends.DeepCloneObject(relationForm);//OPT创建的表单                                                   
                                                AddTable(srcInfo, relationForm, savePermPFForm,
                                                          savePermPageTable, saveTablePanels, saveTableParams,
                                                          saveColumFilters, savePermCloumn,
                                                          savePermPageEdit, saveEditPanels, saveEditColumns,
                                                          saveEditColumnFilters, saveEditColumnFillMaps,
                                                          saveEditColumnFillMapDs,
                                                          savePermPageTree, saveTreeColumns,
                                                          savePermPageChart, saveChartParams,
                                                          savePermPageCustom,
                                                          savePermBtn, saveFormQuerys, saveQuerys, savePermPFFormRelation,
                                                          newSchemes, newTablePanels, newEditPanels, tenantID, newFormID, existForm, existTable, tenantInfo, addEntity);
                                                newForm.SRC_ID = relationForm.ID;
                                                newForm.ID = newFormID;
                                                newForm.ORG_ID = tenantID;
                                                newForm.Nav_Org = null;
                                                addEntity.Forms.Add(newForm);
                                            }

                                        }
                                    }
                                    var newRelationID = new Guid();
                                    relation.SRC_ID = formRelation.ID;
                                    relation.ID = newRelationID;
                                    relation.ORG_ID = tenantID;
                                    relation.PAGE_FORM_ID = pageFormID;
                                    addEntity.FormRelations.Add(relation);
                                }

                            }
                        }
                        #endregion
                        #region 查询
                        AddQuerys(form, tenantID, pageFormID, srcInfo.FormQuerys, saveFormQuerys, srcInfo.Querys, saveQuerys, tenantInfo, addEntity);
                        #endregion
                        var resChart = srcInfo.PageCharts.Where(x => x.PAGE_FORM_ID == form.ID).ToList();//查找相关表单      
                        if (resChart.Count > 0)
                        {
                            foreach (var item in resChart)
                            {
                                #region MyRegion 添加T_PF_PAGE_TABLE
                                var oldPageID = item.ID;
                                if (!existTable.Contains(oldPageID))
                                {
                                    existTable.Add(oldPageID);
                                    var newTableID = AddPageTable(item, pageFormID, tenantID, savePermPageChart, newSchemes, saveTablePanels,
                                    saveTableParams, newTablePanels, saveEditPanels, newEditPanels, saveChartParams, tenantInfo, srcInfo, addEntity);
                                    #endregion
                                    #region 添加按钮
                                    //var allBtn = this.GetEntities<T_PF_BTN>(t => t.PAGE_ID == oldPageID, filter).ToList();//OPT所有列
                                    var allBtn = srcInfo.Btns.Where(x => x.PAGE_ID == oldPageID).ToList();
                                    AddTableBtn(srcInfo, savePermPFForm,
                                                      savePermPageTable, saveTablePanels, saveTableParams,
                                                      saveColumFilters, savePermCloumn,
                                                      savePermPageEdit, saveEditPanels, saveEditColumns,
                                                      saveEditColumnFilters, saveEditColumnFillMaps,
                                                      saveEditColumnFillMapDs,
                                                      savePermPageTree, saveTreeColumns,
                                                      savePermPageChart, saveChartParams,
                                                      savePermPageCustom,
                                                      savePermBtn, saveFormQuerys, saveQuerys, savePermPFFormRelation,
                                                      newSchemes, newTablePanels, newEditPanels, tenantID, existForm, existTable, allBtn, newTableID, tenantInfo, addEntity);
                                    #endregion
                                }
                            }
                        }
                        break;
                    }
                case (int)PFFormTypeEnum.自定义表单:
                    {
                        #region 配置关联表单T_PF_FORM_RELATION
                        var formRelations = srcInfo.FormRelations.Where(x => x.PAGE_FORM_ID == form.ID);
                        if (formRelations.Any())
                        {
                            foreach (var formRelation in formRelations)
                            {
                                T_PF_FORM_RELATION relation = APT.Infrastructure.Core.CloneExtends.DeepCloneObject(formRelation);
                                if (tenantInfo.FormRelations.Any(x => x.SRC_ID == formRelation.ID))
                                {
                                    var tenantRelation = tenantInfo.FormRelations.FirstOrDefault(x => x.SRC_ID == formRelation.ID);
                                    var relationForm = srcInfo.Forms.Where(x => x.CODE == formRelation.CODE).FirstOrDefault();
                                    if (!existForm.Contains(relationForm.ID) && !savePermPFForm.Any(x => x.CODE == relationForm.CODE) && !addEntity.Forms.Any(x => x.CODE == relationForm.CODE))//去重
                                    {
                                        if (relationForm != null)
                                        {
                                            if (tenantInfo.Forms.Any(x => x.SRC_ID == relationForm.ID))
                                            {
                                                var tenantForm = tenantInfo.Forms.FirstOrDefault(x => x.SRC_ID == relationForm.ID);//取旧数据ID不变
                                                T_PF_FORM newForm = APT.Infrastructure.Core.CloneExtends.DeepCloneObject(relationForm);//OPT创建的表单                                                   
                                                AddTable(srcInfo, relationForm, savePermPFForm,
                                                          savePermPageTable, saveTablePanels, saveTableParams,
                                                          saveColumFilters, savePermCloumn,
                                                          savePermPageEdit, saveEditPanels, saveEditColumns,
                                                          saveEditColumnFilters, saveEditColumnFillMaps,
                                                          saveEditColumnFillMapDs,
                                                          savePermPageTree, saveTreeColumns,
                                                          savePermPageChart, saveChartParams,
                                                          savePermPageCustom,
                                                          savePermBtn, saveFormQuerys, saveQuerys, savePermPFFormRelation,
                                                          newSchemes, newTablePanels, newEditPanels, tenantID, tenantForm.ID, existForm, existTable, tenantInfo, addEntity);
                                                newForm.SRC_ID = relationForm.ID;
                                                newForm.ID = tenantForm.ID;
                                                newForm.ORG_ID = tenantID;
                                                newForm.Nav_Org = null;
                                                savePermPFForm.Add(newForm);//添加表单到对应租户
                                            }
                                            else
                                            {
                                                var newFormID = Guid.NewGuid();
                                                T_PF_FORM newForm = APT.Infrastructure.Core.CloneExtends.DeepCloneObject(relationForm);//OPT创建的表单                                                   
                                                AddTable(srcInfo, relationForm, savePermPFForm,
                                                          savePermPageTable, saveTablePanels, saveTableParams,
                                                          saveColumFilters, savePermCloumn,
                                                          savePermPageEdit, saveEditPanels, saveEditColumns,
                                                          saveEditColumnFilters, saveEditColumnFillMaps,
                                                          saveEditColumnFillMapDs,
                                                          savePermPageTree, saveTreeColumns,
                                                          savePermPageChart, saveChartParams,
                                                          savePermPageCustom,
                                                          savePermBtn, saveFormQuerys, saveQuerys, savePermPFFormRelation,
                                                          newSchemes, newTablePanels, newEditPanels, tenantID, newFormID, existForm, existTable, tenantInfo, addEntity);
                                                newForm.SRC_ID = relationForm.ID;
                                                newForm.ID = newFormID;
                                                newForm.ORG_ID = tenantID;
                                                newForm.Nav_Org = null;
                                                addEntity.Forms.Add(newForm);
                                                //savePermPFForm.Add(newForm);//添加表单到对应租户
                                            }

                                        }
                                    }
                                    relation.SRC_ID = formRelation.ID;
                                    relation.ID = tenantRelation.ID;
                                    relation.ORG_ID = tenantID;
                                    relation.PAGE_FORM_ID = pageFormID;
                                    savePermPFFormRelation.Add(relation);
                                }
                                else
                                {
                                    var relationForm = srcInfo.Forms.Where(x => x.CODE == formRelation.CODE).FirstOrDefault();
                                    if (!existForm.Contains(relationForm.ID) && !savePermPFForm.Any(x => x.CODE == relationForm.CODE) && !addEntity.Forms.Any(x => x.CODE == relationForm.CODE))//去重
                                    {
                                        if (relationForm != null)
                                        {
                                            if (tenantInfo.Forms.Any(x => x.SRC_ID == relationForm.ID))
                                            {
                                                var tenantForm = tenantInfo.Forms.FirstOrDefault(x => x.SRC_ID == relationForm.ID);
                                                T_PF_FORM newForm = APT.Infrastructure.Core.CloneExtends.DeepCloneObject(relationForm);//OPT创建的表单                                                   
                                                AddTable(srcInfo, relationForm, savePermPFForm,
                                                           savePermPageTable, saveTablePanels, saveTableParams,
                                                           saveColumFilters, savePermCloumn,
                                                          savePermPageEdit, saveEditPanels, saveEditColumns,
                                                          saveEditColumnFilters, saveEditColumnFillMaps,
                                                          saveEditColumnFillMapDs,
                                                          savePermPageTree, saveTreeColumns,
                                                          savePermPageChart, saveChartParams,
                                                          savePermPageCustom,
                                                          savePermBtn, saveFormQuerys, saveQuerys, savePermPFFormRelation,
                                                          newSchemes, newTablePanels, newEditPanels, tenantID, tenantForm.ID, existForm, existTable, tenantInfo, addEntity);
                                                newForm.SRC_ID = relationForm.ID;
                                                newForm.ID = tenantForm.ID;
                                                newForm.ORG_ID = tenantID;
                                                newForm.Nav_Org = null;
                                                savePermPFForm.Add(newForm);//添加表单到对应租户
                                            }
                                            else
                                            {
                                                var newFormID = Guid.NewGuid();
                                                T_PF_FORM newForm = APT.Infrastructure.Core.CloneExtends.DeepCloneObject(relationForm);//OPT创建的表单                                                   
                                                AddTable(srcInfo, relationForm, savePermPFForm,
                                                          savePermPageTable, saveTablePanels, saveTableParams,
                                                          saveColumFilters, savePermCloumn,
                                                          savePermPageEdit, saveEditPanels, saveEditColumns,
                                                          saveEditColumnFilters, saveEditColumnFillMaps,
                                                          saveEditColumnFillMapDs,
                                                          savePermPageTree, saveTreeColumns,
                                                          savePermPageChart, saveChartParams,
                                                          savePermPageCustom,
                                                          savePermBtn, saveFormQuerys, saveQuerys, savePermPFFormRelation,
                                                          newSchemes, newTablePanels, newEditPanels, tenantID, newFormID, existForm, existTable, tenantInfo, addEntity);
                                                newForm.SRC_ID = relationForm.ID;
                                                newForm.ID = newFormID;
                                                newForm.ORG_ID = tenantID;
                                                newForm.Nav_Org = null;
                                                addEntity.Forms.Add(newForm);
                                            }

                                        }
                                    }
                                    var newRelationID = new Guid();
                                    relation.SRC_ID = formRelation.ID;
                                    relation.ID = newRelationID;
                                    relation.ORG_ID = tenantID;
                                    relation.PAGE_FORM_ID = pageFormID;
                                    addEntity.FormRelations.Add(relation);
                                }

                            }
                        }
                        #endregion
                        #region 查询
                        AddQuerys(form, tenantID, pageFormID, srcInfo.FormQuerys, saveFormQuerys, srcInfo.Querys, saveQuerys, tenantInfo, addEntity);
                        #endregion
                        #region srcInfo.PageCustoms
                        var res = srcInfo.PageCustoms.Where(x => x.PAGE_FORM_ID == form.ID).ToList();//查找相关表单      
                        if (res.Count > 0)
                        {
                            foreach (var item in res)
                            {
                                #region MyRegion 添加T_PF_PAGE_TABLE
                                var oldFormId = form.ID;
                                var tenantPageFormID = pageFormID.ToString();
                                var oldPageID = item.ID;
                                if (!existTable.Contains(oldPageID))
                                {
                                    existTable.Add(oldPageID);
                                    var newTableID = AddPageTable(item, pageFormID, tenantID, savePermPageCustom, newSchemes, saveTablePanels,
                                    saveTableParams, newTablePanels, saveEditPanels, newEditPanels, saveChartParams, tenantInfo, srcInfo, addEntity);
                                    #endregion
                                    #region 添加按钮
                                    //var allBtn = this.GetEntities<T_PF_BTN>(t => t.PAGE_ID == oldFormId, filter).ToList();//OPT所有列
                                    var allBtn = srcInfo.Btns.Where(x => x.PAGE_ID == oldFormId).ToList();
                                    AddTableBtn(srcInfo, savePermPFForm,
                                                      savePermPageTable, saveTablePanels, saveTableParams,
                                                      saveColumFilters, savePermCloumn,
                                                      savePermPageEdit, saveEditPanels, saveEditColumns,
                                                      saveEditColumnFilters, saveEditColumnFillMaps,
                                                      saveEditColumnFillMapDs,
                                                      savePermPageTree, saveTreeColumns,
                                                      savePermPageChart, saveChartParams,
                                                      savePermPageCustom,
                                                      savePermBtn, saveFormQuerys, saveQuerys, savePermPFFormRelation,
                                                      newSchemes, newTablePanels, newEditPanels, tenantID, existForm, existTable, allBtn, newTableID, tenantInfo, addEntity, tenantPageFormID);
                                    #endregion
                                }
                            }
                        }
                        #endregion
                        #region srcInfo.PageTables
                        var resTable = srcInfo.PageTables.Where(x => x.PAGE_FORM_ID == form.ID).ToList();//查找相关表单      
                        if (resTable.Count > 0)
                        {
                            foreach (var item in resTable)
                            {
                                var oldpageTable = item.ID;
                                if (!existTable.Contains(oldpageTable))
                                {
                                    existTable.Add(oldpageTable);
                                    var newTableID = AddPageTable(item, pageFormID, tenantID, savePermPageTable, newSchemes, saveTablePanels, saveTableParams,
                                    newTablePanels, saveEditPanels, newEditPanels, saveChartParams, tenantInfo, srcInfo, addEntity);
                                    #region 添加列
                                    //var allClm = this.GetEntities<T_PF_COLUMN>(t => t.PAGE_TABLE_ID == oldpageTable, filter).ToList();//OPT所有列
                                    var allClm = srcInfo.Columns.Where(t => t.PAGE_TABLE_ID == oldpageTable).ToList();
                                    foreach (var col in allClm)
                                    {
                                        var oldColID = col.ID;
                                        var newColumnID = Guid.NewGuid();
                                        if (tenantInfo.Columns.Any(x => x.SRC_ID == col.ID))
                                        {
                                            var tenantColumn = tenantInfo.Columns.FirstOrDefault(x => x.SRC_ID == col.ID);
                                            T_PF_COLUMN column = APT.Infrastructure.Core.CloneExtends.DeepCloneObject(col);//OPT创建的表单   
                                            column.SRC_ID = col.ID;
                                            column.ID = tenantColumn.ID;
                                            column.ORG_ID = tenantID;
                                            column.PAGE_TABLE_ID = newTableID;
                                            if (column.PAGE_TABLE_PANEL_ID != null)
                                            {
                                                column.PAGE_TABLE_PANEL_ID = newTablePanels[(Guid)column.PAGE_TABLE_PANEL_ID];
                                            }
                                            savePermCloumn.Add(column);//添加列基础到对应租户
                                            newColumnID = tenantColumn.ID;//更新
                                        }
                                        else
                                        {
                                            T_PF_COLUMN column = APT.Infrastructure.Core.CloneExtends.DeepCloneObject(col);//OPT创建的表单         
                                            column.SRC_ID = col.ID;
                                            column.ID = newColumnID;
                                            column.ORG_ID = tenantID;
                                            column.PAGE_TABLE_ID = newTableID;
                                            if (column.PAGE_TABLE_PANEL_ID != null)
                                            {
                                                column.PAGE_TABLE_PANEL_ID = newTablePanels[(Guid)column.PAGE_TABLE_PANEL_ID];
                                            }
                                            addEntity.Columns.Add(column);//添加列基础到对应租户
                                        }
                                        #region T_PF_COLUMN_FILTER
                                        var colFilters = srcInfo.ColumnFilters.Where(x => x.COLUMN_ID == oldColID);
                                        foreach (var colFilter in colFilters)
                                        {
                                            if (tenantInfo.ColumnFilters.Any(x => x.SRC_ID == colFilter.ID))
                                            {
                                                var tenantColumnFilters = tenantInfo.ColumnFilters.FirstOrDefault(x => x.SRC_ID == colFilter.ID);
                                                var cf = APT.Infrastructure.Core.CloneExtends.DeepCloneObject(colFilter);
                                                cf.SRC_ID = colFilter.ID;
                                                cf.ID = tenantColumnFilters.ID;
                                                cf.ORG_ID = tenantID;
                                                cf.COLUMN_ID = newColumnID;
                                                saveColumFilters.Add(cf);
                                            }
                                            else
                                            {
                                                var colID = Guid.NewGuid();
                                                var cf = APT.Infrastructure.Core.CloneExtends.DeepCloneObject(colFilter);
                                                cf.SRC_ID = colFilter.ID;
                                                cf.ID = colID;
                                                cf.ORG_ID = tenantID;
                                                cf.COLUMN_ID = newColumnID;
                                                addEntity.ColumnFilters.Add(cf);
                                            }

                                        }
                                        #endregion
                                        if (!string.IsNullOrEmpty(col.SHOW_FORM_CODE))//有配置关联表单
                                        {
                                            var colForm = srcInfo.Forms.Where(x => x.CODE == col.SHOW_FORM_CODE).FirstOrDefault();
                                            if (!existForm.Contains(form.ID) && !savePermPFForm.Any(x => x.CODE == colForm.CODE) && !addEntity.Forms.Any(x => x.CODE == colForm.CODE))//去重
                                            {
                                                if (colForm != null)
                                                {
                                                    if (tenantInfo.Forms.Any(x => x.SRC_ID == colForm.ID))
                                                    {
                                                        var tenantForm = tenantInfo.Forms.FirstOrDefault(x => x.SRC_ID == colForm.ID);
                                                        T_PF_FORM newForm = APT.Infrastructure.Core.CloneExtends.DeepCloneObject(colForm);//OPT创建的表单                                                   
                                                        AddTable(srcInfo, colForm, savePermPFForm,
                                                       savePermPageTable, saveTablePanels, saveTableParams,
                                                       saveColumFilters, savePermCloumn,
                                                       savePermPageEdit, saveEditPanels, saveEditColumns,
                                                       saveEditColumnFilters, saveEditColumnFillMaps,
                                                       saveEditColumnFillMapDs,
                                                       savePermPageTree, saveTreeColumns,
                                                       savePermPageChart, saveChartParams,
                                                       savePermPageCustom,
                                                       savePermBtn, saveFormQuerys, saveQuerys, savePermPFFormRelation,
                                                       newSchemes, newTablePanels, newEditPanels, tenantID, tenantForm.ID, existForm, existTable, tenantInfo, addEntity);
                                                        newForm.SRC_ID = colForm.ID;
                                                        newForm.ID = tenantForm.ID;
                                                        newForm.ORG_ID = tenantID;
                                                        newForm.Nav_Org = null;
                                                        savePermPFForm.Add(newForm);//添加表单到对应租户
                                                    }
                                                    else
                                                    {
                                                        var newFormID = Guid.NewGuid();
                                                        T_PF_FORM newForm = APT.Infrastructure.Core.CloneExtends.DeepCloneObject(colForm);//OPT创建的表单                                                   
                                                        AddTable(srcInfo, colForm, savePermPFForm,
                                                      savePermPageTable, saveTablePanels, saveTableParams,
                                                      saveColumFilters, savePermCloumn,
                                                      savePermPageEdit, saveEditPanels, saveEditColumns,
                                                      saveEditColumnFilters, saveEditColumnFillMaps,
                                                      saveEditColumnFillMapDs,
                                                      savePermPageTree, saveTreeColumns,
                                                      savePermPageChart, saveChartParams,
                                                      savePermPageCustom,
                                                      savePermBtn, saveFormQuerys, saveQuerys, savePermPFFormRelation,
                                                      newSchemes, newTablePanels, newEditPanels, tenantID, newFormID, existForm, existTable, tenantInfo, addEntity);
                                                        newForm.SRC_ID = colForm.ID;
                                                        newForm.ID = newFormID;
                                                        newForm.ORG_ID = tenantID;
                                                        newForm.Nav_Org = null;
                                                        addEntity.Forms.Add(newForm);
                                                    }
                                                }
                                            }
                                        }
                                    }
                                    #endregion
                                    #region 添加按钮
                                    //var allBtn = this.GetEntities<T_PF_BTN>(t => t.PAGE_ID == oldpageTable, filter).ToList();//OPT所有列
                                    var allBtn = srcInfo.Btns.Where(x => x.PAGE_ID == oldpageTable).ToList();
                                    AddTableBtn(srcInfo, savePermPFForm,
                                                  savePermPageTable, saveTablePanels, saveTableParams,
                                                  saveColumFilters, savePermCloumn,
                                                  savePermPageEdit, saveEditPanels, saveEditColumns,
                                                  saveEditColumnFilters, saveEditColumnFillMaps,
                                                  saveEditColumnFillMapDs,
                                                  savePermPageTree, saveTreeColumns,
                                                  savePermPageChart, saveChartParams,
                                                  savePermPageCustom,
                                                  savePermBtn, saveFormQuerys, saveQuerys, savePermPFFormRelation,
                                                  newSchemes, newTablePanels, newEditPanels, tenantID, existForm, existTable, allBtn, newTableID, tenantInfo, addEntity);
                                    #endregion
                                }
                            }
                        }
                        #endregion
                        #region srcInfo.PageEdits
                        var resEdit = srcInfo.PageEdits.Where(x => x.PAGE_FORM_ID == form.ID).ToList();//查找相关表单      
                        if (resEdit.Count > 0)
                        {
                            foreach (var item in resEdit)
                            {
                                var oldpageEdit = item.ID;
                                if (!existTable.Contains(oldpageEdit))
                                {
                                    existTable.Add(oldpageEdit);
                                    var newTableID = AddPageTable(item, pageFormID, tenantID, savePermPageEdit, newSchemes, saveTablePanels, saveTableParams,
                                    newTablePanels, saveEditPanels, newEditPanels, saveChartParams, tenantInfo, srcInfo, addEntity);
                                    #region 添加编辑列
                                    //var allClm = this.GetEntities<T_PF_EDIT_COLUMN>(t => t.PAGE_EDIT_ID == oldpageEdit, filter).ToList();//OPT所有列
                                    var allClm = srcInfo.EditColumns.Where(t => t.PAGE_EDIT_ID == oldpageEdit).ToList();
                                    foreach (var col in allClm)
                                    {
                                        var newColumnID = Guid.NewGuid();
                                        var oldColID = col.ID;
                                        if (tenantInfo.EditColumns.Any(x => x.SRC_ID == col.ID))
                                        {
                                            var tenantEditColumn = tenantInfo.EditColumns.FirstOrDefault(x => x.SRC_ID == col.ID);
                                            T_PF_EDIT_COLUMN column = APT.Infrastructure.Core.CloneExtends.DeepCloneObject(col);//OPT创建的表单      
                                            column.SRC_ID = col.ID;
                                            column.ID = tenantEditColumn.ID;
                                            column.ORG_ID = tenantID;
                                            column.PAGE_EDIT_ID = newTableID;
                                            if (column.PAGE_EDIT_PANEL_ID != null)
                                            {
                                                column.PAGE_EDIT_PANEL_ID = newEditPanels[(Guid)column.PAGE_EDIT_PANEL_ID];
                                            }
                                            saveEditColumns.Add(column);//添加列基础到对应租户
                                            newColumnID = tenantEditColumn.ID;//更新
                                        }
                                        else
                                        {
                                            T_PF_EDIT_COLUMN column = APT.Infrastructure.Core.CloneExtends.DeepCloneObject(col);//OPT创建的表单         
                                            column.SRC_ID = col.ID;
                                            column.ID = newColumnID;
                                            column.ORG_ID = tenantID;
                                            column.PAGE_EDIT_ID = newTableID;
                                            if (column.PAGE_EDIT_PANEL_ID != null)
                                            {
                                                column.PAGE_EDIT_PANEL_ID = newEditPanels[(Guid)column.PAGE_EDIT_PANEL_ID];
                                            }
                                            addEntity.EditColumns.Add(column);//添加列基础到对应租户
                                        }
                                        #region T_PF_EDIT_COLUMN_FILTER
                                        var colFilters = srcInfo.EditColumnFilters.Where(x => x.EDIT_COLUMN_ID == oldColID);
                                        foreach (var colFilter in colFilters)
                                        {
                                            if (tenantInfo.EditColumnFilters.Any(x => x.SRC_ID == colFilter.ID))
                                            {
                                                var tenantEditColumnFilter = tenantInfo.EditColumnFilters.FirstOrDefault(x => x.SRC_ID == colFilter.ID);
                                                var cf = APT.Infrastructure.Core.CloneExtends.DeepCloneObject(colFilter);
                                                cf.SRC_ID = colFilter.ID;
                                                cf.ID = tenantEditColumnFilter.ID;
                                                cf.ORG_ID = tenantID;
                                                cf.PAGE_EDIT_ID = newTableID;
                                                cf.EDIT_COLUMN_ID = newColumnID;
                                                saveEditColumnFilters.Add(cf);
                                            }
                                            else
                                            {
                                                var colID = Guid.NewGuid();
                                                var cf = APT.Infrastructure.Core.CloneExtends.DeepCloneObject(colFilter);
                                                cf.SRC_ID = colFilter.ID;
                                                cf.ID = colID;
                                                cf.ORG_ID = tenantID;
                                                cf.PAGE_EDIT_ID = newTableID;
                                                cf.EDIT_COLUMN_ID = newColumnID;
                                                addEntity.EditColumnFilters.Add(cf);
                                            }

                                        }
                                        #endregion
                                        #region T_PF_EDIT_COLUMN_FILL_MAP
                                        var colFillMaps = srcInfo.EditColumnFillMaps.Where(x => x.EDIT_COLUMN_ID == oldColID);
                                        foreach (var colFillMap in colFillMaps)
                                        {
                                            var colMapID = Guid.NewGuid();
                                            if (tenantInfo.EditColumnFillMaps.Any(x => x.SRC_ID == colFillMap.ID))
                                            {
                                                var tenantEditColumnFillMap = tenantInfo.EditColumnFillMaps.FirstOrDefault(x => x.SRC_ID == colFillMap.ID);
                                                var cm = APT.Infrastructure.Core.CloneExtends.DeepCloneObject(colFillMap);
                                                cm.SRC_ID = colFillMap.ID;
                                                cm.ID = tenantEditColumnFillMap.ID;
                                                cm.ORG_ID = tenantID;
                                                cm.PAGE_EDIT_ID = newTableID;
                                                cm.EDIT_COLUMN_ID = newColumnID;
                                                saveEditColumnFillMaps.Add(cm);
                                                colMapID = tenantEditColumnFillMap.ID;
                                            }
                                            else
                                            {
                                                var cm = APT.Infrastructure.Core.CloneExtends.DeepCloneObject(colFillMap);
                                                cm.SRC_ID = colFillMap.ID;
                                                cm.ID = colMapID;
                                                cm.ORG_ID = tenantID;
                                                cm.PAGE_EDIT_ID = newTableID;
                                                cm.EDIT_COLUMN_ID = newColumnID;
                                                addEntity.EditColumnFillMaps.Add(cm);
                                            }
                                            #region T_PF_EDIT_COLUMN_FILL_MAP
                                            var colFillMapDs = srcInfo.EditColumnFillMapDs.Where(x => x.EDIT_COLUMN_FILL_MAP_ID == colMapID);
                                            foreach (var colFillMapD in colFillMapDs)
                                            {
                                                if (tenantInfo.EditColumnFillMapDs.Any(x => x.SRC_ID == colFillMapD.ID))
                                                {
                                                    var tenantEditColumnFillMapD = tenantInfo.EditColumnFillMapDs.FirstOrDefault(x => x.SRC_ID == colFillMapD.ID);
                                                    var cmd = APT.Infrastructure.Core.CloneExtends.DeepCloneObject(colFillMapD);
                                                    cmd.SRC_ID = colFillMapD.ID;
                                                    cmd.ID = tenantEditColumnFillMapD.ID;
                                                    cmd.ORG_ID = tenantID;
                                                    cmd.EDIT_COLUMN_FILL_MAP_ID = colMapID;
                                                    saveEditColumnFillMapDs.Add(cmd);
                                                }
                                                else
                                                {
                                                    var colMapDID = Guid.NewGuid();
                                                    var cmd = APT.Infrastructure.Core.CloneExtends.DeepCloneObject(colFillMapD);
                                                    cmd.SRC_ID = colFillMapD.ID;
                                                    cmd.ID = colMapDID;
                                                    cmd.ORG_ID = tenantID;
                                                    cmd.EDIT_COLUMN_FILL_MAP_ID = colMapID;
                                                    addEntity.EditColumnFillMapDs.Add(cmd);
                                                }
                                            }
                                            #endregion
                                        }
                                        #endregion

                                        if (!string.IsNullOrEmpty(col.SHOW_FORM_CODE))//有配置关联表单
                                        {
                                            var colForm = srcInfo.Forms.Where(x => x.CODE == col.SHOW_FORM_CODE).FirstOrDefault();
                                            if (!existForm.Contains(form.ID) && !savePermPFForm.Any(x => x.CODE == colForm.CODE) && !addEntity.Forms.Any(x => x.CODE == colForm.CODE))//去重
                                            {
                                                if (colForm != null)
                                                {
                                                    if (tenantInfo.Forms.Any(x => x.SRC_ID == colForm.ID))
                                                    {
                                                        var tenantColForm = tenantInfo.Forms.FirstOrDefault(x => x.SRC_ID == colForm.ID);
                                                        T_PF_FORM newForm = APT.Infrastructure.Core.CloneExtends.DeepCloneObject(colForm);//OPT创建的表单                                                   
                                                        AddTable(srcInfo, colForm, savePermPFForm,
                                                      savePermPageTable, saveTablePanels, saveTableParams,
                                                      saveColumFilters, savePermCloumn,
                                                      savePermPageEdit, saveEditPanels, saveEditColumns,
                                                      saveEditColumnFilters, saveEditColumnFillMaps,
                                                      saveEditColumnFillMapDs,
                                                      savePermPageTree, saveTreeColumns,
                                                      savePermPageChart, saveChartParams,
                                                      savePermPageCustom,
                                                      savePermBtn, saveFormQuerys, saveQuerys, savePermPFFormRelation,
                                                      newSchemes, newTablePanels, newEditPanels, tenantID, tenantColForm.ID, existForm, existTable, tenantInfo, addEntity);
                                                        newForm.SRC_ID = colForm.ID;
                                                        newForm.ID = tenantColForm.ID;
                                                        newForm.ORG_ID = tenantID;
                                                        newForm.Nav_Org = null;
                                                        savePermPFForm.Add(newForm);//添加表单到对应租户
                                                    }
                                                    else
                                                    {
                                                        var newFormID = Guid.NewGuid();
                                                        T_PF_FORM newForm = APT.Infrastructure.Core.CloneExtends.DeepCloneObject(colForm);//OPT创建的表单                                                   
                                                        AddTable(srcInfo, colForm, savePermPFForm,
                                                      savePermPageTable, saveTablePanels, saveTableParams,
                                                      saveColumFilters, savePermCloumn,
                                                      savePermPageEdit, saveEditPanels, saveEditColumns,
                                                      saveEditColumnFilters, saveEditColumnFillMaps,
                                                      saveEditColumnFillMapDs,
                                                      savePermPageTree, saveTreeColumns,
                                                      savePermPageChart, saveChartParams,
                                                      savePermPageCustom,
                                                      savePermBtn, saveFormQuerys, saveQuerys, savePermPFFormRelation,
                                                      newSchemes, newTablePanels, newEditPanels, tenantID, newFormID, existForm, existTable, tenantInfo, addEntity);
                                                        newForm.SRC_ID = colForm.ID;
                                                        newForm.ID = newFormID;
                                                        newForm.ORG_ID = tenantID;
                                                        newForm.Nav_Org = null;
                                                        addEntity.Forms.Add(newForm);
                                                    }

                                                }
                                            }
                                        }
                                    }
                                    #endregion
                                    #region 添加按钮
                                    //var allBtn = this.GetEntities<T_PF_BTN>(t => t.PAGE_ID == oldpageEdit, filter).ToList();//OPT所有列
                                    var allBtn = srcInfo.Btns.Where(x => x.PAGE_ID == oldpageEdit).ToList();
                                    AddTableBtn(srcInfo, savePermPFForm,
                                                  savePermPageTable, saveTablePanels, saveTableParams,
                                                  saveColumFilters, savePermCloumn,
                                                  savePermPageEdit, saveEditPanels, saveEditColumns,
                                                  saveEditColumnFilters, saveEditColumnFillMaps,
                                                  saveEditColumnFillMapDs,
                                                  savePermPageTree, saveTreeColumns,
                                                  savePermPageChart, saveChartParams,
                                                  savePermPageCustom,
                                                  savePermBtn, saveFormQuerys, saveQuerys, savePermPFFormRelation,
                                                  newSchemes, newTablePanels, newEditPanels, tenantID, existForm, existTable, allBtn, newTableID, tenantInfo, addEntity);
                                    #endregion
                                }
                            }
                        }
                        #endregion
                        #region srcInfo.PageTrees
                        var resTree = srcInfo.PageTrees.Where(x => x.PAGE_FORM_ID == form.ID).ToList();//查找相关表单      
                        if (resTree.Count > 0)
                        {
                            foreach (var item in resTree)
                            {
                                #region MyRegion 添加T_PF_PAGE_TABLE
                                var oldPageID = item.ID;
                                if (!existTable.Contains(oldPageID))
                                {
                                    existTable.Add(oldPageID);
                                    var newTableID = AddPageTable(item, pageFormID, tenantID, savePermPageTree, newSchemes, saveTablePanels, saveTableParams,
                                    newTablePanels, saveEditPanels, newEditPanels, saveChartParams, tenantInfo, srcInfo, addEntity);
                                    #endregion
                                    #region T_PF_TREE_COLUMN
                                    //var allClm = this.GetEntities<T_PF_TREE_COLUMN>(t => t.PAGE_TREE_ID == oldPageID, filter).ToList();//OPT所有列
                                    var allClm = srcInfo.TreeColumns.Where(t => t.PAGE_TREE_ID == oldPageID).ToList();
                                    foreach (var col in allClm)
                                    {
                                        var newColumnID = Guid.NewGuid();
                                        T_PF_TREE_COLUMN column = APT.Infrastructure.Core.CloneExtends.DeepCloneObject(col);//OPT创建的表单
                                        var oldColID = col.ID;
                                        if (tenantInfo.TreeColumns.Any(x => x.SRC_ID == col.ID))
                                        {
                                            var tenantTreeColumn = tenantInfo.TreeColumns.FirstOrDefault(x => x.SRC_ID == col.ID);
                                            column.SRC_ID = col.ID;
                                            column.ID = tenantTreeColumn.ID;
                                            column.ORG_ID = tenantID;
                                            column.PAGE_TREE_ID = newTableID;
                                            saveTreeColumns.Add(column);//添加列基础到对应租户
                                        }
                                        else
                                        {
                                            column.SRC_ID = col.ID;
                                            column.ID = newColumnID;
                                            column.ORG_ID = tenantID;
                                            column.PAGE_TREE_ID = newTableID;
                                            addEntity.TreeColumns.Add(column);
                                        }

                                    }
                                    #endregion
                                    #region 添加按钮
                                    var allBtn = srcInfo.Btns.Where(x => x.PAGE_ID == oldPageID).ToList();
                                    AddTableBtn(srcInfo, savePermPFForm,
                                                      savePermPageTable, saveTablePanels, saveTableParams,
                                                      saveColumFilters, savePermCloumn,
                                                      savePermPageEdit, saveEditPanels, saveEditColumns,
                                                      saveEditColumnFilters, saveEditColumnFillMaps,
                                                      saveEditColumnFillMapDs,
                                                      savePermPageTree, saveTreeColumns,
                                                      savePermPageChart, saveChartParams,
                                                      savePermPageCustom,
                                                      savePermBtn, saveFormQuerys, saveQuerys, savePermPFFormRelation,
                                                      newSchemes, newTablePanels, newEditPanels, tenantID, existForm, existTable, allBtn, newTableID, tenantInfo, addEntity);
                                    #endregion
                                }
                            }
                        }
                        #endregion
                        #region srcInfo.PageCharts
                        var resChart = srcInfo.PageCharts.Where(x => x.PAGE_FORM_ID == form.ID).ToList();//查找相关表单      
                        if (resChart.Count > 0)
                        {
                            foreach (var item in resChart)
                            {
                                #region MyRegion 添加T_PF_PAGE_TABLE
                                var oldPageID = item.ID;
                                if (!existTable.Contains(oldPageID))
                                {
                                    existTable.Add(oldPageID);
                                    var newTableID = AddPageTable(item, pageFormID, tenantID, savePermPageChart, newSchemes, saveTablePanels,
                                    saveTableParams, newTablePanels, saveEditPanels, newEditPanels, saveChartParams, tenantInfo, srcInfo, addEntity);
                                    #endregion
                                    #region 添加按钮
                                    //var allBtn = this.GetEntities<T_PF_BTN>(t => t.PAGE_ID == oldPageID, filter).ToList();//OPT所有列
                                    var allBtn = srcInfo.Btns.Where(x => x.PAGE_ID == oldPageID).ToList();
                                    AddTableBtn(srcInfo, savePermPFForm,
                                                      savePermPageTable, saveTablePanels, saveTableParams,
                                                      saveColumFilters, savePermCloumn,
                                                      savePermPageEdit, saveEditPanels, saveEditColumns,
                                                      saveEditColumnFilters, saveEditColumnFillMaps,
                                                      saveEditColumnFillMapDs,
                                                      savePermPageTree, saveTreeColumns,
                                                      savePermPageChart, saveChartParams,
                                                      savePermPageCustom,
                                                      savePermBtn, saveFormQuerys, saveQuerys, savePermPFFormRelation,
                                                      newSchemes, newTablePanels, newEditPanels, tenantID, existForm, existTable, allBtn, newTableID, tenantInfo, addEntity);
                                    #endregion
                                }
                            }
                        }
                        #endregion
                        break;
                    }
                default:
                    {
                        break;
                    }

            }

        }
        public class TenantInfo
        {
            public List<T_PF_FORM_RELATION> FormRelations { get; set; }
            public List<T_PF_FORM> Forms { get; set; }
            public List<T_PF_PAGE_TABLE> PageTables { get; set; }
            public List<T_PF_PAGE_EDIT> PageEdits { get; set; }
            public List<T_PF_PAGE_TREE> PageTrees { get; set; }
            public List<T_PF_PAGE_CHART> PageCharts { get; set; }
            public List<T_PF_PAGE_CUSTOM> PageCustoms { get; set; }
            public List<T_PF_BTN> Btns { get; set; }
            public List<T_PF_COLUMN> Columns { get; set; }
            public List<T_PF_PAGE_TABLE_PANEL> TablePanels { get; set; }
            public List<T_PF_TABLE_PARAMS> TableParams { get; set; }
            public List<T_PF_PAGE_EDIT_PANEL> EditPanels { get; set; }
            public List<T_PF_EDIT_COLUMN> EditColumns { get; set; }
            public List<T_PF_EDIT_COLUMN_FILTER> EditColumnFilters { get; set; }
            public List<T_PF_EDIT_COLUMN_FILL_MAP> EditColumnFillMaps { get; set; }
            public List<T_PF_EDIT_COLUMN_FILL_MAP_D> EditColumnFillMapDs { get; set; }
            public List<T_PF_TREE_COLUMN> TreeColumns { get; set; }
            public List<T_PF_PAGE_CHART_PARAM> ChartParams { get; set; }
            public List<T_PF_COLUMN_FILTER> ColumnFilters { get; set; }
            public List<T_PF_QUERY> Querys { get; set; }
            public List<T_PF_FORM_QUERY> FormQuerys { get; set; }
            public Dictionary<string, object> DicPageTable { get; set; }
            public Dictionary<string, string> DicPageTableRelation { get; set; }
            public Dictionary<string, string> DicForm { get; set; }
        }
        public TreeNode<T_PF_MENU> selectNode(TreeNode<T_PF_MENU> treeNode, string checkKey)
        {
            if (treeNode.Node.ID == new Guid(checkKey))
            {
                return treeNode;
            }
            else
            {
                TreeNode<T_PF_MENU> checkNode = null;
                foreach (var item in treeNode.Children)
                {
                    checkNode = selectNode(item, checkKey);
                    if (checkNode != null)
                    {
                        return checkNode;
                    }
                }
                return checkNode;
            }
        }
        public Guid AddPageTable<T>(T tableType, Guid pageFormID, Guid tenantID, List<T> savePermPageTable, Dictionary<Guid, Guid> newSchemes,
           List<T_PF_PAGE_TABLE_PANEL> saveTablePanels,
           List<T_PF_TABLE_PARAMS> saveTableParams, Dictionary<Guid, Guid> newTablePanels, List<T_PF_PAGE_EDIT_PANEL> saveEditPanels,
           Dictionary<Guid, Guid> newEditPanels, List<T_PF_PAGE_CHART_PARAM> saveChartParams, TenantInfo tenantInfo, TenantInfo srcInfo, TenantInfo addEntity) where T : class
        {
            var newTableID = Guid.NewGuid();
            if (tableType is T_PF_PAGE_TABLE)
            {
                T_PF_PAGE_TABLE pageTable = APT.Infrastructure.Core.CloneExtends.DeepCloneObject(tableType as T_PF_PAGE_TABLE);//OPT创建的表单
                var schemesId = pageTable.PARAM_SCHEME_ID;
                var oldPageId = pageTable.ID;
                (tableType as T_PF_PAGE_TABLE).PAGE_FORM_ID = pageFormID;
                if (pageTable.PARENT_ID != null)
                {
                    tenantInfo.DicPageTable.Add(oldPageId.ToString(), pageTable.PARENT_ID);
                }
                if (tenantInfo.PageTables.Any(x => x.SRC_ID == pageTable.ID))
                {
                    var tenantPageTable = tenantInfo.PageTables.FirstOrDefault(x => x.SRC_ID == pageTable.ID);
                    (tableType as T_PF_PAGE_TABLE).SRC_ID = pageTable.ID;
                    (tableType as T_PF_PAGE_TABLE).ID = tenantPageTable.ID;
                    (tableType as T_PF_PAGE_TABLE).ORG_ID = tenantID;
                    if (pageTable.PARAM_SCHEME_ID != null)
                    {
                        (tableType as T_PF_PAGE_TABLE).PARAM_SCHEME_ID = newSchemes[(Guid)schemesId];//参数更新                     
                    }                          
                    savePermPageTable.Add(tableType);//添加表基础到对应租户   
                    newTableID = tenantPageTable.ID;//存在更新成原来的
                }
                else
                {
                    (tableType as T_PF_PAGE_TABLE).SRC_ID = pageTable.ID;
                    (tableType as T_PF_PAGE_TABLE).ID = newTableID;
                    (tableType as T_PF_PAGE_TABLE).ORG_ID = tenantID;
                    if (pageTable.PARAM_SCHEME_ID != null)
                    {
                        (tableType as T_PF_PAGE_TABLE).PARAM_SCHEME_ID = newSchemes[(Guid)schemesId];//参数更新
                    }
                    //savePermPageTable.Add(tableType);//添加表基础到对应租户   
                    addEntity.PageTables.Add((tableType as T_PF_PAGE_TABLE));
                }
                #region T_PF_PAGE_TABLE_PANEL 页面列表区域
                var tablePanels = srcInfo.TablePanels.Where(x => x.PAGE_TABLE_ID == oldPageId);
                foreach (var tablePanel in tablePanels)
                {
                    if (tenantInfo.TablePanels.Any(x => x.SRC_ID == tablePanel.ID))
                    {
                        var tenantTablePanel = tenantInfo.TablePanels.FirstOrDefault(x => x.SRC_ID == tablePanel.ID);
                        var tp = APT.Infrastructure.Core.CloneExtends.DeepCloneObject(tablePanel);
                        var oldID = tablePanel.ID;
                        tp.SRC_ID = tablePanel.ID;
                        tp.ORG_ID = tenantID;
                        tp.ID = tenantTablePanel.ID;
                        tp.PAGE_TABLE_ID = newTableID;
                        saveTablePanels.Add(tp);
                        newTablePanels.Add(oldID, tenantTablePanel.ID);
                    }
                    else
                    {
                        var tpID = Guid.NewGuid();
                        var tp = APT.Infrastructure.Core.CloneExtends.DeepCloneObject(tablePanel);
                        var oldID = tablePanel.ID;
                        tp.SRC_ID = tablePanel.ID;
                        tp.ID = tpID;
                        tp.ORG_ID = tenantID;
                        tp.PAGE_TABLE_ID = newTableID;
                        addEntity.TablePanels.Add(tp);
                        newTablePanels.Add(oldID, tpID);
                    }
                }
                #endregion
                #region T_PF_TABLE_PARAMS  
                var tableParams = srcInfo.TableParams.Where(x => x.PAGE_TABLE_ID == schemesId);
                foreach (var tableParam in tableParams)
                {
                    if (tenantInfo.TableParams.Any(x => x.SRC_ID == tableParam.ID))
                    {
                        var tenantTableParam = tenantInfo.TableParams.FirstOrDefault(x => x.SRC_ID == tableParam.ID);
                        var tp = APT.Infrastructure.Core.CloneExtends.DeepCloneObject(tableParam);
                        tp.SRC_ID = tableParam.ID;
                        tp.ID = tenantTableParam.ID;
                        tp.ORG_ID = tenantID;
                        tp.PAGE_TABLE_ID = newTableID;
                        saveTableParams.Add(tp);
                    }
                    else
                    {
                        var tpID = Guid.NewGuid();
                        var tp = APT.Infrastructure.Core.CloneExtends.DeepCloneObject(tableParam);
                        tp.SRC_ID = tableParam.ID;
                        tp.ID = tpID;
                        tp.ORG_ID = tenantID;
                        tp.PAGE_TABLE_ID = newTableID;
                        addEntity.TableParams.Add(tp);
                    }
                }
                #endregion
                tenantInfo.DicPageTableRelation.Add(oldPageId.ToString(), newTableID.ToString());
            }
            if (tableType is T_PF_PAGE_EDIT)
            {
                T_PF_PAGE_EDIT pageEdit = APT.Infrastructure.Core.CloneExtends.DeepCloneObject(tableType as T_PF_PAGE_EDIT);//OPT创建的表单
                var schemesId = pageEdit.PARAM_SCHEME_ID;
                var oldPageId = pageEdit.ID;
                if (pageEdit.PARENT_ID != null)
                {
                    tenantInfo.DicPageTable.Add(oldPageId.ToString(), pageEdit.PARENT_ID);
                }
                (tableType as T_PF_PAGE_EDIT).PAGE_FORM_ID = pageFormID;
                if (tenantInfo.PageEdits.Any(x => x.SRC_ID == pageEdit.ID))
                {
                    var tenantEditTable = tenantInfo.PageEdits.FirstOrDefault(x => x.SRC_ID == pageEdit.ID);
                    (tableType as T_PF_PAGE_EDIT).SRC_ID = pageEdit.ID;
                    (tableType as T_PF_PAGE_EDIT).ID = tenantEditTable.ID;
                    (tableType as T_PF_PAGE_EDIT).ORG_ID = tenantID;
                    (tableType as T_PF_PAGE_EDIT).Nav_PageForm = null;
                    if (pageEdit.PARAM_SCHEME_ID != null)
                    {
                        (tableType as T_PF_PAGE_EDIT).PARAM_SCHEME_ID = newSchemes[(Guid)schemesId];//参数更新                       
                    }                 
                    savePermPageTable.Add(tableType);//添加表基础到对应租户 
                    newTableID = tenantEditTable.ID;//存在更新成原来的
                }
                else
                {
                    (tableType as T_PF_PAGE_EDIT).SRC_ID = pageEdit.ID;
                    (tableType as T_PF_PAGE_EDIT).ID = newTableID;
                    (tableType as T_PF_PAGE_EDIT).ORG_ID = tenantID;
                    (tableType as T_PF_PAGE_EDIT).Nav_PageForm = null;
                    if (pageEdit.PARAM_SCHEME_ID != null)
                    {
                        (tableType as T_PF_PAGE_EDIT).PARAM_SCHEME_ID = newSchemes[(Guid)schemesId];//参数更新
                    }
                    //savePermPageTable.Add(tableType);//添加表基础到对应租户 
                    addEntity.PageEdits.Add((tableType as T_PF_PAGE_EDIT));
                }

                #region T_PF_PAGE_EDIT_PANEL
                var editPanels = srcInfo.EditPanels.Where(x => x.PAGE_EDIT_ID == oldPageId);
                foreach (var editPanel in editPanels)
                {
                    if (tenantInfo.EditPanels.Any(x => x.SRC_ID == editPanel.ID))
                    {
                        var tenantEditPanel = tenantInfo.EditPanels.FirstOrDefault(x => x.SRC_ID == editPanel.ID);
                        var ep = APT.Infrastructure.Core.CloneExtends.DeepCloneObject(editPanel);
                        var oldID = editPanel.ID;
                        ep.SRC_ID = editPanel.ID;
                        ep.ID = tenantEditPanel.ID;
                        ep.ORG_ID = tenantID;
                        ep.PAGE_EDIT_ID = newTableID;
                        saveEditPanels.Add(ep);
                        newEditPanels.Add(oldID, tenantEditPanel.ID);
                    }
                    else
                    {
                        var epID = Guid.NewGuid();
                        var ep = APT.Infrastructure.Core.CloneExtends.DeepCloneObject(editPanel);
                        var oldID = editPanel.ID;
                        ep.SRC_ID = editPanel.ID;
                        ep.ID = epID;
                        ep.ORG_ID = tenantID;
                        ep.PAGE_EDIT_ID = newTableID;
                        addEntity.EditPanels.Add(ep);
                        newEditPanels.Add(oldID, epID);
                    }
                }
                #endregion
                tenantInfo.DicPageTableRelation.Add(oldPageId.ToString(), newTableID.ToString());
            }
            if (tableType is T_PF_PAGE_TREE)
            {
                T_PF_PAGE_TREE pageTree = APT.Infrastructure.Core.CloneExtends.DeepCloneObject(tableType as T_PF_PAGE_TREE);//OPT创建的表单
                (tableType as T_PF_PAGE_TREE).PAGE_FORM_ID = pageFormID;
                if (tenantInfo.PageTrees.Any(x => x.SRC_ID == pageTree.ID))
                {
                    var tenantPageTree = tenantInfo.PageTrees.FirstOrDefault(x => x.SRC_ID == pageTree.ID);
                    (tableType as T_PF_PAGE_TREE).SRC_ID = pageTree.ID;
                    (tableType as T_PF_PAGE_TREE).ID = tenantPageTree.ID;
                    (tableType as T_PF_PAGE_TREE).ORG_ID = tenantID;            
                    savePermPageTable.Add(tableType);//添加表基础到对应租户  
                    newTableID = tenantPageTree.ID;//存在更新成原来的
                }
                else
                {
                    (tableType as T_PF_PAGE_TREE).SRC_ID = pageTree.ID;
                    (tableType as T_PF_PAGE_TREE).ID = newTableID;
                    (tableType as T_PF_PAGE_TREE).ORG_ID = tenantID;
                    //savePermPageTable.Add(tableType);//添加表基础到对应租户  
                    addEntity.PageTrees.Add((tableType as T_PF_PAGE_TREE));
                }

            }
            if (tableType is T_PF_PAGE_CHART)
            {
                T_PF_PAGE_CHART pageChart = APT.Infrastructure.Core.CloneExtends.DeepCloneObject(tableType as T_PF_PAGE_CHART);//OPT创建的表单
                var oldChartId = pageChart.ID;
                (tableType as T_PF_PAGE_CHART).PAGE_FORM_ID = pageFormID;
                if (tenantInfo.PageCharts.Any(x => x.SRC_ID == pageChart.ID))
                {
                    var tenantPageChart = tenantInfo.PageCharts.FirstOrDefault(x => x.SRC_ID == pageChart.ID);
                    (tableType as T_PF_PAGE_CHART).SRC_ID = pageChart.ID;
                    (tableType as T_PF_PAGE_CHART).ID = tenantPageChart.ID;
                    (tableType as T_PF_PAGE_CHART).ORG_ID = tenantID;
                    savePermPageTable.Add(tableType);//添加表基础到对应租户   
                    newTableID = tenantPageChart.ID;//存在更新成原来的
                }
                else
                {
                    (tableType as T_PF_PAGE_CHART).SRC_ID = pageChart.ID;
                    (tableType as T_PF_PAGE_CHART).ID = newTableID;
                    (tableType as T_PF_PAGE_CHART).ORG_ID = tenantID;
                    //savePermPageTable.Add(tableType);//添加表基础到对应租户   
                    addEntity.PageCharts.Add((tableType as T_PF_PAGE_CHART));
                }

                #region T_PF_PAGE_CHART_PARAM
                var chartParams = srcInfo.ChartParams.Where(x => x.PAGE_CHART_ID == oldChartId);
                foreach (var chartParam in chartParams)
                {
                    if (tenantInfo.ChartParams.Any(x => x.SRC_ID == chartParam.ID))
                    {
                        var tenantChartParam = tenantInfo.ChartParams.FirstOrDefault(x => x.SRC_ID == chartParam.ID);
                        var cp = APT.Infrastructure.Core.CloneExtends.DeepCloneObject(chartParam);
                        cp.SRC_ID = chartParam.ID;
                        cp.ID = tenantChartParam.ID;
                        cp.ORG_ID = tenantID;
                        cp.PAGE_CHART_ID = newTableID;
                        saveChartParams.Add(cp);
                    }
                    else
                    {
                        var cpID = Guid.NewGuid();
                        var cp = APT.Infrastructure.Core.CloneExtends.DeepCloneObject(chartParam);
                        cp.SRC_ID = chartParam.ID;
                        cp.ID = cpID;
                        cp.ORG_ID = tenantID;
                        cp.PAGE_CHART_ID = newTableID;
                        addEntity.ChartParams.Add(cp);
                    }
                }
                #endregion
            }
            if (tableType is T_PF_PAGE_CUSTOM)
            {
                T_PF_PAGE_CUSTOM pageCustom = APT.Infrastructure.Core.CloneExtends.DeepCloneObject(tableType as T_PF_PAGE_CUSTOM);//OPT创建的表单
                (tableType as T_PF_PAGE_CUSTOM).PAGE_FORM_ID = pageFormID;
                if (tenantInfo.PageCustoms.Any(x => x.SRC_ID == pageCustom.ID))
                {
                    var tenantPageCustom = tenantInfo.PageCustoms.FirstOrDefault(x => x.SRC_ID == pageCustom.ID);
                    (tableType as T_PF_PAGE_CUSTOM).SRC_ID = pageCustom.ID;
                    (tableType as T_PF_PAGE_CUSTOM).ID = tenantPageCustom.ID;
                    (tableType as T_PF_PAGE_CUSTOM).ORG_ID = tenantID;
                    savePermPageTable.Add(tableType);//添加表基础到对应租户  
                    newTableID = tenantPageCustom.ID;//存在更新成原来的
                }
                else
                {
                    (tableType as T_PF_PAGE_CUSTOM).SRC_ID = pageCustom.ID;
                    (tableType as T_PF_PAGE_CUSTOM).ID = newTableID;
                    (tableType as T_PF_PAGE_CUSTOM).ORG_ID = tenantID;
                    //savePermPageTable.Add(tableType);//添加表基础到对应租户   
                    addEntity.PageCustoms.Add((tableType as T_PF_PAGE_CUSTOM));
                }
            }
            return newTableID;
        }
        public void AddQuerys(T_PF_FORM form, Guid tenantID, Guid pageFormID, IEnumerable<T_PF_FORM_QUERY> optFormQuerys, List<T_PF_FORM_QUERY> saveFormQuerys, IEnumerable<T_PF_QUERY> optQuerys, List<T_PF_QUERY> saveQuerys,
            TenantInfo tenantInfo, TenantInfo addEntity)
        {
            var formQuerys = optFormQuerys.Where(x => x.PAGE_FORM_ID == form.ID);
            foreach (var formQuery in formQuerys)
            {
                if (tenantInfo.FormQuerys.Any(x => x.SRC_ID == formQuery.ID))
                {
                    var tenantFormQuery = tenantInfo.FormQuerys.FirstOrDefault((x => x.SRC_ID == formQuery.ID));
                    T_PF_FORM_QUERY formquery = APT.Infrastructure.Core.CloneExtends.DeepCloneObject(formQuery);//OPT创建的表单
                    var oldformQueryID = formQuery.ID;
                    formquery.SRC_ID = formQuery.ID;
                    formquery.ID = tenantFormQuery.ID;
                    formquery.ORG_ID = tenantID;
                    formquery.PAGE_FORM_ID = pageFormID;
                    saveFormQuerys.Add(formquery);//添加列基础到对应租户
                    var querys = optQuerys.Where(x => x.PAGE_FORM_QUERY_ID == oldformQueryID);
                    foreach (var query in querys)
                    {
                        if (tenantInfo.Querys.Any(x => x.SRC_ID == query.ID))
                        {
                            var tenantQuery = tenantInfo.Querys.FirstOrDefault(x => x.SRC_ID == query.ID);
                            T_PF_QUERY queryInfo = APT.Infrastructure.Core.CloneExtends.DeepCloneObject(query);//OPT创建的表单     
                            queryInfo.SRC_ID = query.ID;
                            queryInfo.ID = tenantQuery.ID;
                            queryInfo.ORG_ID = tenantID;
                            queryInfo.PAGE_FORM_ID = pageFormID;
                            queryInfo.PAGE_FORM_QUERY_ID = tenantFormQuery.ID;
                            saveQuerys.Add(queryInfo);//添加列基础到对应租户
                        }
                        else
                        {
                            var newQueryID = Guid.NewGuid();
                            T_PF_QUERY queryInfo = APT.Infrastructure.Core.CloneExtends.DeepCloneObject(query);//OPT创建的表单      
                            queryInfo.SRC_ID = query.ID;
                            queryInfo.ID = newQueryID;
                            queryInfo.ORG_ID = tenantID;
                            queryInfo.PAGE_FORM_ID = pageFormID;
                            queryInfo.PAGE_FORM_QUERY_ID = tenantFormQuery.ID;
                            addEntity.Querys.Add(queryInfo);//添加列基础到对应租户
                        }
                    }
                }
                else
                {
                    var newFormQueryID = Guid.NewGuid();
                    T_PF_FORM_QUERY formquery = APT.Infrastructure.Core.CloneExtends.DeepCloneObject(formQuery);//OPT创建的表单
                    var oldformQueryID = formQuery.ID;
                    formquery.SRC_ID = formQuery.ID;
                    formquery.ID = newFormQueryID;
                    formquery.ORG_ID = tenantID;
                    formquery.PAGE_FORM_ID = pageFormID;
                    addEntity.FormQuerys.Add(formquery);//添加列基础到对应租户
                    var querys = optQuerys.Where(x => x.PAGE_FORM_QUERY_ID == oldformQueryID);
                    foreach (var query in querys)
                    {
                        var newQueryID = Guid.NewGuid();
                        T_PF_QUERY queryInfo = APT.Infrastructure.Core.CloneExtends.DeepCloneObject(query);//OPT创建的表单       
                        queryInfo.SRC_ID = query.ID;
                        queryInfo.ID = newQueryID;
                        queryInfo.ORG_ID = tenantID;
                        queryInfo.PAGE_FORM_ID = pageFormID;
                        queryInfo.PAGE_FORM_QUERY_ID = newFormQueryID;
                        addEntity.Querys.Add(queryInfo);
                    }
                }

            }
        }
        public void AddTableBtn(TenantInfo srcInfo, List<T_PF_FORM> savePermPFForm,
           List<T_PF_PAGE_TABLE> savePermPageTable, List<T_PF_PAGE_TABLE_PANEL> saveTablePanels, List<T_PF_TABLE_PARAMS> saveTableParams,
             List<T_PF_COLUMN_FILTER> saveColumFilters, List<T_PF_COLUMN> savePermCloumn,
             List<T_PF_PAGE_EDIT> savePermPageEdit, List<T_PF_PAGE_EDIT_PANEL> saveEditPanels, List<T_PF_EDIT_COLUMN> saveEditColumns,
             List<T_PF_EDIT_COLUMN_FILTER> saveEditColumnFilters, List<T_PF_EDIT_COLUMN_FILL_MAP> saveEditColumnFillMaps,
             List<T_PF_EDIT_COLUMN_FILL_MAP_D> saveEditColumnFillMapDs,
             List<T_PF_PAGE_TREE> savePermPageTree, List<T_PF_TREE_COLUMN> saveTreeColumns,
             List<T_PF_PAGE_CHART> savePermPageChart, List<T_PF_PAGE_CHART_PARAM> saveChartParams,
             List<T_PF_PAGE_CUSTOM> savePermPageCustom,
             List<T_PF_BTN> savePermBtn, List<T_PF_FORM_QUERY> saveFormQuerys, List<T_PF_QUERY> saveQuerys, List<T_PF_FORM_RELATION> savePermPFFormRelation,
             Dictionary<Guid, Guid> newSchemes, Dictionary<Guid, Guid> newTablePanels, Dictionary<Guid, Guid> newEditPanels, Guid tenantID, List<Guid> existForm, List<Guid> existTable, List<T_PF_BTN> allBtn, Guid newTableID, TenantInfo tenantInfo, TenantInfo addEntity, string formId = "")
        {
            foreach (var btn in allBtn)
            {
                var newBtnID = Guid.NewGuid();
                T_PF_BTN button = APT.Infrastructure.Core.CloneExtends.DeepCloneObject(btn);//OPT创建的表单
                if (tenantInfo.Btns.Any(x => x.SRC_ID == btn.ID))
                {
                    var tenantBtn = tenantInfo.Btns.FirstOrDefault(x => x.SRC_ID == btn.ID);
                    button.SRC_ID = btn.ID;
                    button.ID = tenantBtn.ID;
                    button.ORG_ID = tenantID;
                    if (formId != "")
                    {
                        button.PAGE_ID = new Guid(formId);
                    }
                    else
                    {
                        button.PAGE_ID = newTableID;
                    }
                    if (button.PAGE_TABLE_ID != null)
                    {
                        button.PAGE_TABLE_ID = newTableID;
                    }
                    if (button.PAGE_EDIT_ID != null)
                    {
                        button.PAGE_EDIT_ID = newTableID;
                    }
                    if (button.PAGE_CUSTOM_FORM_ID != null)
                    {
                        // button.PAGE_CUSTOM_FORM_ID = newTableID;
                        button.PAGE_CUSTOM_FORM_ID = new Guid(formId);
                    }
                    savePermBtn.Add(button);//添加列基础到对应租户
                    newBtnID = tenantBtn.ID;
                }
                else
                {
                    button.SRC_ID = btn.ID;
                    button.ID = newBtnID;
                    button.ORG_ID = tenantID;
                    if (formId != "")
                    {
                        button.PAGE_ID = new Guid(formId);
                    }
                    else
                    {
                        button.PAGE_ID = newTableID;
                    }
                    if (button.PAGE_TABLE_ID != null)
                    {
                        button.PAGE_TABLE_ID = newTableID;
                    }
                    if (button.PAGE_EDIT_ID != null)
                    {
                        button.PAGE_EDIT_ID = newTableID;
                    }
                    if (button.PAGE_CUSTOM_FORM_ID != null)
                    {
                        // button.PAGE_CUSTOM_FORM_ID = newTableID;
                        button.PAGE_CUSTOM_FORM_ID = new Guid(formId);
                    }
                    addEntity.Btns.Add(button);//添加列基础到对应租户
                }
                if (!string.IsNullOrEmpty(btn.FORM_CODE))//有配置关联表单
                {
                    //var btnForm = t_PF_FORMs.Where(x => x.CODE == btn.FORM_CODE).FirstOrDefault();
                    // var btnForm = this.GetEntities<T_PF_FORM>(null, filter).FirstOrDefault(x => x.CODE == btn.FORM_CODE);
                    var btnForm = srcInfo.Forms.FirstOrDefault(x => x.CODE == btn.FORM_CODE);
                    if (btnForm != null)
                    {
                        if (tenantInfo.Forms.Any(x => x.SRC_ID == btnForm.ID))
                        {
                            var tenantBtnForm = tenantInfo.Forms.FirstOrDefault(x => x.SRC_ID == btnForm.ID);
                            T_PF_FORM newForm = APT.Infrastructure.Core.CloneExtends.DeepCloneObject(btnForm);//OPT创建的表单            
                            if (!existForm.Contains(newForm.ID) && !savePermPFForm.Any(x => x.CODE == newForm.CODE) && !addEntity.Forms.Any(x => x.CODE == newForm.CODE))
                            {
                                AddTable(srcInfo, btnForm, savePermPFForm,
                                                      savePermPageTable, saveTablePanels, saveTableParams,
                                                      saveColumFilters, savePermCloumn,
                                                      savePermPageEdit, saveEditPanels, saveEditColumns,
                                                       saveEditColumnFilters, saveEditColumnFillMaps,
                                                       saveEditColumnFillMapDs,
                                                       savePermPageTree, saveTreeColumns,
                                                      savePermPageChart, saveChartParams,
                                                      savePermPageCustom,
                                                      savePermBtn, saveFormQuerys, saveQuerys, savePermPFFormRelation,
                                                      newSchemes, newTablePanels, newEditPanels, tenantID, tenantBtnForm.ID, existForm, existTable, tenantInfo, addEntity);
                                newForm.SRC_ID = btnForm.ID;
                                newForm.ID = tenantBtnForm.ID;
                                newForm.ORG_ID = tenantID;
                                newForm.Nav_Org = null;
                                savePermPFForm.Add(newForm);//添加表单到对应租户
                            }
                        }
                        else
                        {
                            var newFormID = Guid.NewGuid();
                            T_PF_FORM newForm = APT.Infrastructure.Core.CloneExtends.DeepCloneObject(btnForm);//OPT创建的表单            
                            if (!existForm.Contains(newForm.ID) && !savePermPFForm.Any(x => x.CODE == newForm.CODE) && !addEntity.Forms.Any(x => x.CODE == newForm.CODE))
                            {
                                AddTable(srcInfo, btnForm, savePermPFForm,
                                                       savePermPageTable, saveTablePanels, saveTableParams,
                                                      saveColumFilters, savePermCloumn,
                                                     savePermPageEdit, saveEditPanels, saveEditColumns,
                                                      saveEditColumnFilters, saveEditColumnFillMaps,
                                                     saveEditColumnFillMapDs,
                                                     savePermPageTree, saveTreeColumns,
                                                      savePermPageChart, saveChartParams,
                                                     savePermPageCustom,
                                                      savePermBtn, saveFormQuerys, saveQuerys, savePermPFFormRelation,
                                                      newSchemes, newTablePanels, newEditPanels, tenantID, newFormID, existForm, existTable, tenantInfo, addEntity);
                                newForm.SRC_ID = btnForm.ID;
                                newForm.ID = newFormID;
                                newForm.ORG_ID = tenantID;
                                newForm.Nav_Org = null;
                                addEntity.Forms.Add(newForm);//添加表单到对应租户
                            }
                        }

                    }

                }
            }
        }
    }
}
