using APT.BaseData.Domain.Entities;
using APT.BaseData.Domain.Enums;
using APT.Infrastructure.Core;
using APT.BaseData.Domain.Entities.FM;
using APT.BaseData.Domain.IServices.FM;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Linq;

using APT.Utility;
using APT.BaseData.Domain.ApiModel;
using System.IO;
using APT.BaseData.Domain.ApiModel.FM;
using NPOI.SS.Formula.Functions;
using APT.BaseData.Domain.ApiModel.Platform;
using System.Dynamic;
using APT.BaseData.Domain.Entities.PF;

namespace APT.FM.WebApi.Controllers.Api.FM
{
    /// <summary>
    /// 角色
    /// </summary>
    [Route("api/FM/Role")]
    public class RoleController : AuthorizeApiController<T_FM_ROLE>
    {
        #region

        /// <summary>
        /// 角色
        /// </summary>
        /// <param name="roleService"></param>
        public RoleController(IFMRoleService roleService)
        {
            this.RoleService = roleService;
        }

        #endregion

        #region properties
        IFMRoleService RoleService { get; set; }
        #endregion




        /// <summary>
        /// 查询
        /// </summary>
        /// <param name="filter"></param>
        /// <returns></returns>
        [HttpPost, Route("Entities")]
        public JsonActionResult<IEnumerable<T_FM_ROLE>> Entities([FromBody] KeywordFilter filter)
        {
            return WitEntities(null, filter);
        }
        /// <summary>
        /// 查询
        /// </summary>
        /// <param name="filter"></param>
        /// <returns></returns>
        /// 

        [HttpPost, Route("OrderEntities")]
        public JsonActionResult<IEnumerable<T_FM_ROLE>> OrderEntities([FromBody] KeywordFilter filter)
        {
            return WitOrderEntities(null, filter);
        }
        /// <summary>
        /// 查询
        /// </summary>
        /// <param name="pageFilter"></param>
        /// <returns></returns>
        [HttpPost, Route("Paged")]
        public PagedActionResult<T_FM_ROLE> Paged([FromBody] KeywordPageFilter pageFilter)
        {
            return WitPaged(null, pageFilter);
        }
        /// <summary>
        /// 查询
        /// </summary>
        /// <param name="pageFilter"></param>
        /// <returns></returns>
        [HttpPost, Route("OrderPaged")]
        public PagedActionResult<T_FM_ROLE> OrderPaged([FromBody] KeywordPageFilter pageFilter)
        {
            return WitOrderPaged(null, pageFilter);
        }


        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpGet, Route("Delete")]
        public JsonActionResult<bool> Delete(string id)
        {
            return WitRealDelete(id);
        }

        /// <summary>
        /// 更新
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        //[HttpPost, Route("Update")]
        //public JsonActionResult<bool> Update([FromBody] T_FM_ROLE role)
        //{
        //    return SafeExecute<bool>(() =>
        //    {
        //        var dbEntity = this.GetEntity<T_FM_ROLE>(role.ID.ToString(), new string[] { "Nav_MenuPermissions.Nav_PermissionBtns", "Nav_MenuPermissions.Nav_PermissionColumns", "Nav_Panels" });

        //        var menuPermissions = role.Nav_MenuPermissions;
        //        //var workstages = role.Nav_Workstages;
        //        var panels = role.Nav_Panels;
        //        role.Nav_MenuPermissions = null;
        //        //role.Nav_Workstages = null;
        //        bool isDeletePermission = dbEntity != null && (dbEntity.ORG_ID != role.ORG_ID || dbEntity.PLATFORM_TYPE != role.PLATFORM_TYPE);
        //        if (menuPermissions != null && menuPermissions.Any())
        //            menuPermissions.ForEach(t => t.ROLE_ID = role.ID);

        //        //if (workstages != null && workstages.Any())
        //        //	workstages.ForEach(t => t.BELONG_ROLE_ID = role.ID);
        //        if (panels != null && panels.Any())
        //            panels.ForEach(t => t.BELONG_ROLE_ID = role.ID);

        //        var deleteBtns = new List<Guid>();

        //        var deleteColumns = new List<Guid>();
        //        var deletePanels = new List<Guid>();
        //        var saveBtns = new List<T_FM_ROLE_MENU_PERM_BTN>();

        //        var saveColumns = new List<T_FM_ROLE_MENU_PERM_COL>();


        //        if (dbEntity != null && dbEntity.Nav_MenuPermissions != null && dbEntity.Nav_MenuPermissions.Any())
        //        {
        //            foreach (var item in dbEntity.Nav_MenuPermissions)
        //            {
        //                var tmp = menuPermissions == null ? null : menuPermissions.Where(i => i.ID == item.ID).FirstOrDefault();
        //                if (tmp != null)
        //                {
        //                    var tmpBtns = tmp.Nav_PermissionBtns;
        //                    var tmpColumns = tmp.Nav_PermissionColumns;
        //                    if (item.Nav_PermissionBtns != null && item.Nav_PermissionBtns.Any())
        //                    {
        //                        item.Nav_PermissionBtns.ForEach(t =>
        //                        {
        //                            if (tmpBtns == null || !tmpBtns.Any(i => i.ID == t.ID))
        //                                deleteBtns.Add(t.ID);
        //                        });
        //                    }

        //                    if (item.Nav_PermissionColumns != null && item.Nav_PermissionColumns.Any())
        //                    {
        //                        item.Nav_PermissionColumns.ForEach(t =>
        //                        {
        //                            if (tmpColumns == null || !tmpColumns.Any(i => i.ID == t.ID))
        //                                deleteColumns.Add(t.ID);
        //                        });
        //                    }
        //                }
        //            }
        //        }
        //        if (menuPermissions != null && menuPermissions.Any())
        //        {
        //            foreach (var item in menuPermissions)
        //            {
        //                var tmpBtns = item.Nav_PermissionBtns;
        //                var tmpColumns = item.Nav_PermissionColumns;
        //                item.Nav_PermissionBtns = null;
        //                item.Nav_PermissionColumns = null;
        //                if (tmpBtns != null && tmpBtns.Any())
        //                {
        //                    tmpBtns.ForEach(t => t.ROLE_MENU_PERMISSION_ID = item.ID);
        //                    saveBtns.AddRange(tmpBtns);
        //                }
        //                if (tmpColumns != null && tmpColumns.Any())
        //                {
        //                    tmpColumns.ForEach(t => t.ROLE_MENU_PERMISSION_ID = item.ID);
        //                    saveColumns.AddRange(tmpColumns);
        //                }
        //            }
        //        }

        //        if (dbEntity != null && dbEntity.Nav_Panels != null && dbEntity.Nav_Panels.Any())
        //        {
        //            dbEntity.Nav_Panels.ForEach(t =>
        //            {
        //                if (panels == null || panels.Count == 0 || !panels.Any(t1 => t1.ID == t.ID))
        //                    deletePanels.Add(t.ID);
        //            });
        //        }


        //        UnifiedCommit(() =>
        //        {
        //            this.UpdateEntityNoCommit(role);
        //            if (isDeletePermission)
        //            {
        //                this.DeleteEntityNoCommit<T_FM_ROLE_MENU_PERM_COL>(t => t.Nav_RoleMenuPermission.ROLE_ID == role.ID);
        //                this.DeleteEntityNoCommit<T_FM_ROLE_MENU_PERM_BTN>(t => t.Nav_RoleMenuPermission.ROLE_ID == role.ID);
        //                this.DeleteEntityNoCommit<T_FM_ROLE_MENU_PERM>(t => t.ROLE_ID == role.ID);
        //            }
        //            if (menuPermissions != null && menuPermissions.Any())
        //                this.BantchSaveEntityNoCommit(menuPermissions);
        //            //if (workstages != null && workstages.Any())
        //            //	this.BantchSaveEntityNoCommit(workstages);
        //            if (panels != null && panels.Any())
        //                this.BantchSaveEntityNoCommit(panels);
        //            if (saveBtns != null && saveBtns.Any())
        //                this.BantchSaveEntityNoCommit(saveBtns);
        //            if (saveColumns != null && saveColumns.Any())
        //                this.BantchSaveEntityNoCommit(saveColumns);

        //            if (deleteBtns != null && deleteBtns.Any())
        //                this.DeleteEntityNoCommit<T_FM_ROLE_MENU_PERM_BTN>(t => deleteBtns.Contains(t.ID));
        //            if (deleteColumns != null && deleteColumns.Any())
        //                this.DeleteEntityNoCommit<T_FM_ROLE_MENU_PERM_COL>(t => deleteColumns.Contains(t.ID));

        //            if (deletePanels != null && deletePanels.Any())
        //                this.DeleteEntityNoCommit<T_FM_PANEL_BELONG_ROLE>(t => deletePanels.Contains(t.ID));

        //        });
        //        return true;
        //    });
        //}
        [HttpPost, Route("Update")]
        public JsonActionResult<bool> Update([FromBody] T_FM_ROLE role)
        {
            return SafeExecute<bool>(() =>
            {
                var orgId = APT.Infrastructure.Api.AppContext.CurrentSession.OrgId;
                var isExistrole = this.GetEntity<T_FM_ROLE>(role.ID.ToString());
                if (isExistrole != null && isExistrole.PLATFORM_TYPE != role.PLATFORM_TYPE)
                    throw new Exception("平台类型无法更改，若需要请重新创建平台类型角色！");
                var permissionList = this.GetEntities<T_FM_ROLE_MENU_PERM>(x => x.ROLE_ID == role.ID, new BaseFilter(role.ORG_ID)).ToList();
                var permissionBtns = this.GetEntities<T_FM_ROLE_MENU_PERM_BTN>(x => x.ROLE_ID == role.ID, new BaseFilter(role.ORG_ID)).ToList();
                var permissionCols = this.GetEntities<T_FM_ROLE_MENU_PERM_COL>(x => x.ROLE_ID == role.ID, new BaseFilter(role.ORG_ID)).ToList();
                var perms = new List<PermInfo>();
                var savePerms = new List<T_FM_ROLE_MENU_PERM>();
                var deletePerms = new List<Guid>();
                var columns = new List<ColInfo>();
                var deleteColumns = new List<Guid>();
                var saveColumns = new List<T_FM_ROLE_MENU_PERM_COL>();
                var btns = new List<BtnInfo>();
                var deleteBtns = new List<Guid>();
                //var menus = new List<Guid>();
                var saveMenus = new List<T_FM_ROLE_MENU_PERM>();
                var saveBtns = new List<T_FM_ROLE_MENU_PERM_BTN>();
                List<T_FM_ROLE_DEPARTMENT> roleDepartments = new List<T_FM_ROLE_DEPARTMENT>();
                Dictionary<string, string> dicRolePerm = new Dictionary<string, string>();
                foreach (var item in role.DepartmentCheckedKeys)
                {
                    foreach (var departmentCheckedKey in item.departmentCheckedKeys)
                    {
                        roleDepartments.Add(new T_FM_ROLE_DEPARTMENT
                        {
                            ROLE_ID = role.ID,
                            DEPARTMENT_ID = departmentCheckedKey,
                            ORG_ID = new Guid(item.OrgId)
                        });
                    }
                }
                if (role.CheckKeys.Count > 0)
                {
                    foreach (var permisionCodetem in role.CheckKeys.OrderBy(x => x.Length))
                    {
                        switch (permisionCodetem.Split('_').Count())
                        {
                            case 1://菜单权限
                                {
                                    perms.Add(new PermInfo() { MENUID = permisionCodetem });
                                    if (permissionList != null)
                                    {
                                        if (!permissionList.Any(x => x.MENU_ID == new Guid(permisionCodetem)))
                                        {
                                            saveMenus.Add(new T_FM_ROLE_MENU_PERM() { ORG_ID = role.ORG_ID, ROLE_ID = role.ID, MENU_ID = new Guid(permisionCodetem) });
                                        }
                                    }
                                    else
                                    {
                                        saveMenus.Add(new T_FM_ROLE_MENU_PERM() { ORG_ID = role.ORG_ID, ROLE_ID = role.ID, MENU_ID = new Guid(permisionCodetem) });
                                    }
                                    break;
                                }
                            case 2://表单权限
                                {
                                    perms.Add(new PermInfo() { MENUID = permisionCodetem.Split('_')[0], ID = permisionCodetem.Split('_')[1] });
                                    var guid = Guid.NewGuid();
                                    if (!dicRolePerm.ContainsKey(permisionCodetem))
                                    {
                                        dicRolePerm.Add(permisionCodetem, guid.ToString());
                                    }
                                    if (permissionList != null)
                                    {
                                        if (!permissionList.Any(x => x.MENU_ID == new Guid(permisionCodetem.Split('_')[0])
                                        && x.PERMISSION_FORM_ID == new Guid(permisionCodetem.Split('_')[1])))
                                        {
                                            savePerms.Add(new T_FM_ROLE_MENU_PERM()
                                            {
                                                ID = guid,
                                                ORG_ID = role.ORG_ID,
                                                ROLE_ID = role.ID,
                                                PERMISSION_FORM_ID = new Guid(permisionCodetem.Split('_')[1]),
                                                MENU_ID = new Guid(permisionCodetem.Split('_')[0]),
                                                IS_PERMISSION = true
                                            });
                                        }
                                        else
                                        {
                                            dicRolePerm[permisionCodetem] = permissionList.FirstOrDefault(x => x.MENU_ID == new Guid(permisionCodetem.Split('_')[0]) && x.PERMISSION_FORM_ID == new Guid(permisionCodetem.Split('_')[1])).ID.ToString();
                                        }
                                    }
                                    else
                                    {
                                        savePerms.Add(new T_FM_ROLE_MENU_PERM()
                                        {
                                            ID = guid,
                                            ORG_ID = role.ORG_ID,
                                            ROLE_ID = role.ID,
                                            PERMISSION_FORM_ID = new Guid(permisionCodetem.Split('_')[1]),
                                            MENU_ID = new Guid(permisionCodetem.Split('_')[0]),
                                            IS_PERMISSION = true
                                        });
                                    }
                                    break;
                                }
                            case 4://列、按钮权限
                                {
                                    if (permisionCodetem.Split('_')[3] == "BTN")//按钮
                                    {
                                        btns.Add(new BtnInfo() { MENUID = permisionCodetem.Split('_')[0], PERMID = permisionCodetem.Split('_')[1], ID = permisionCodetem.Split('_')[2] });
                                        if (permissionBtns.Count > 0)
                                        {
                                            if (!permissionBtns.Any(x => x.MENU_ID == new Guid(permisionCodetem.Split('_')[0]) && x.BTN_ID == new Guid(permisionCodetem.Split('_')[2])))
                                            {
                                                saveBtns.Add(new T_FM_ROLE_MENU_PERM_BTN()
                                                {
                                                    ROLE_MENU_PERMISSION_ID = new Guid(dicRolePerm[permisionCodetem.Split('_')[0] + "_" + permisionCodetem.Split('_')[1]]),
                                                    ORG_ID = role.ORG_ID,
                                                    ROLE_ID = role.ID,
                                                    MENU_ID = new Guid(permisionCodetem.Split('_')[0]),
                                                    BTN_ID = new Guid(permisionCodetem.Split('_')[2])
                                                });
                                            }
                                        }
                                        else
                                        {
                                            saveBtns.Add(new T_FM_ROLE_MENU_PERM_BTN()
                                            {
                                                ROLE_MENU_PERMISSION_ID = new Guid(dicRolePerm[permisionCodetem.Split('_')[0] + "_" + permisionCodetem.Split('_')[1]]),
                                                ORG_ID = role.ORG_ID,
                                                ROLE_ID = role.ID,
                                                MENU_ID = new Guid(permisionCodetem.Split('_')[0]),
                                                BTN_ID = new Guid(permisionCodetem.Split('_')[2])
                                            });
                                        }
                                    }
                                    else
                                    {
                                        columns.Add(new ColInfo() { MENUID = permisionCodetem.Split('_')[0], PERMID = permisionCodetem.Split('_')[1], ID = permisionCodetem.Split('_')[2] });
                                        if (permissionCols.Count > 0)
                                        {
                                            if (!permissionCols.Any(x => x.MENU_ID == new Guid(permisionCodetem.Split('_')[0]) && x.COLUMN_ID == new Guid(permisionCodetem.Split('_')[2])))
                                            {
                                                saveColumns.Add(new T_FM_ROLE_MENU_PERM_COL()
                                                {
                                                    ROLE_MENU_PERMISSION_ID = new Guid(dicRolePerm[permisionCodetem.Split('_')[0] + "_" + permisionCodetem.Split('_')[1]]),
                                                    ORG_ID = role.ORG_ID,
                                                    ROLE_ID = role.ID,
                                                    MENU_ID = new Guid(permisionCodetem.Split('_')[0]),
                                                    COLUMN_ID = new Guid(permisionCodetem.Split('_')[2])
                                                });
                                            }
                                        }
                                        else
                                        {
                                            saveColumns.Add(
                                                new T_FM_ROLE_MENU_PERM_COL()
                                                {
                                                    ROLE_MENU_PERMISSION_ID = new Guid(dicRolePerm[permisionCodetem.Split('_')[0] + "_" + permisionCodetem.Split('_')[1]]),
                                                    ORG_ID = role.ORG_ID,
                                                    ROLE_ID = role.ID,
                                                    MENU_ID = new Guid(permisionCodetem.Split('_')[0]),
                                                    COLUMN_ID = new Guid(permisionCodetem.Split('_')[2])
                                                });
                                        }
                                    }
                                    break;
                                }
                            default:
                                break;
                        }
                    }
                    foreach (var item in permissionList)
                    {
                        if (!perms.Any(x => new Guid(x.MENUID) == item.MENU_ID && x.ID == item.PERMISSION_FORM_ID?.ToString()))
                        {
                            deletePerms.Add(item.ID);//根据主键删除
                        }
                    }
                    foreach (var col in permissionCols)
                    {
                        if (!columns.Any(x => new Guid(x.MENUID) == col.MENU_ID && new Guid(x.ID) == col.COLUMN_ID))
                        {
                            deleteColumns.Add(col.ID);//根据主键删除
                        }
                    }
                    foreach (var btn in permissionBtns)
                    {
                        if (!btns.Any(x => new Guid(x.MENUID) == btn.MENU_ID && new Guid(x.ID) == btn.BTN_ID))
                        {
                            deleteBtns.Add(btn.ID);//根据主键删除
                        }
                    }
                    UnifiedCommit(() =>
                    {
                        this.UpdateEntityNoCommit(role);
                        //this.BantchDeleteEntityNoCommit<T_FM_ROLE_MENU_PERMISON>(deleteMenus);
                        this.BantchDeleteEntityNoCommit<T_FM_ROLE_MENU_PERM_COL>(deleteColumns);
                        this.BantchDeleteEntityNoCommit<T_FM_ROLE_MENU_PERM_BTN>(deleteBtns);
                        this.BantchDeleteEntityNoCommit<T_FM_ROLE_MENU_PERM>(deletePerms);

                        if (saveMenus != null && saveMenus.Any())
                            this.BantchSaveEntityNoCommit(saveMenus);
                        if (savePerms != null && savePerms.Any())
                            this.BantchAddEntityNoCommit(savePerms);
                        if (saveColumns != null && saveColumns.Any())
                            this.BantchAddEntityNoCommit(saveColumns);
                        if (saveBtns != null && saveBtns.Any())
                            this.BantchAddEntityNoCommit(saveBtns);

                        this.DeleteEntityNoCommit<T_FM_ROLE_DEPARTMENT>(x => x.ROLE_ID == role.ID);
                        if (roleDepartments.Any())
                            this.BantchAddEntityNoCommit<T_FM_ROLE_DEPARTMENT>(roleDepartments);
                        if (roleDepartments.Any())
                            this.BantchAddEntityNoCommit<T_FM_ROLE_DEPARTMENT>(roleDepartments);
                    });
                }
                else
                {
                    if (permissionList.Count > 0)
                    {
                        deletePerms.AddRange(permissionList.Select(x => x.ID).ToList());
                    }
                    if (permissionBtns.Count > 0)
                    {
                        deleteBtns.AddRange(permissionBtns.Select(x => x.ID).ToList());
                    }
                    if (permissionCols.Count > 0)
                    {
                        deleteColumns.AddRange(permissionCols.Select(x => x.ID).ToList());
                    }
                    UnifiedCommit(() =>
                    {
                        this.UpdateEntityNoCommit(role);
                        //this.BantchDeleteEntityNoCommit<T_FM_ROLE_MENU_PERMISON>(deleteMenus);
                        this.BantchDeleteEntityNoCommit<T_FM_ROLE_MENU_PERM_COL>(deleteColumns);
                        this.BantchDeleteEntityNoCommit<T_FM_ROLE_MENU_PERM_BTN>(deleteBtns);
                        this.BantchDeleteEntityNoCommit<T_FM_ROLE_MENU_PERM>(deletePerms);

                        this.DeleteEntityNoCommit<T_FM_ROLE_DEPARTMENT>(x => x.ROLE_ID == role.ID);
                        if (roleDepartments.Any())
                            this.BantchAddEntityNoCommit<T_FM_ROLE_DEPARTMENT>(roleDepartments);
                    });
                }
                return true;
            });
        }
        public class PermInfo
        {
            public string MENUID { get; set; }
            public string ID { get; set; }
        }
        public class ColInfo
        {
            public string MENUID { get; set; }

            public string PERMID { get; set; }
            public string ID { get; set; }
        }
        public class BtnInfo
        {
            public string MENUID { get; set; }
            public string PERMID { get; set; }
            public string ID { get; set; }
        }
        /// <summary>
        /// 批量删除
        /// </summary>
        /// <param name="ids"></param>
        /// <returns></returns>
        [HttpGet, Route("BatchDelete")]
        public JsonActionResult<bool> BatchDelete(string ids)
        {
            return WitRealBatchDelete(ids);
        }


        /// <summary>
        /// 获得单条实体数据
        /// </summary>
        /// <param name="filter"></param>
        /// <returns></returns>
        [HttpPost, Route("Get")]
        public JsonActionResult<T_FM_ROLE> Get([FromBody] KeywordFilter filter)
        {
            return WitEntity(null, filter);
        }


        /// <summary>
        /// 授权
        /// </summary>
        /// <param name="role"></param>
        /// <returns></returns>
        [HttpPost, Route("Permision")]
        public JsonActionResult<bool> Permision([FromBody] T_FM_ROLE role)
        {
            return SafeExecute<bool>(() =>
            {
                var dbRole = this.GetEntity<T_FM_ROLE>(role.ID.ToString(), new string[] { "Nav_MenuPermissions" });

                if (dbRole == null)
                    throw new Exception("角色不存在");

                var deleteList = new List<Guid>();

                if (role.Nav_MenuPermissions != null && role.Nav_MenuPermissions.Any())
                {
                    foreach (var item in dbRole.Nav_MenuPermissions)
                    {
                        if (!role.Nav_MenuPermissions.Any(t => t.ID == item.ID))
                            deleteList.Add(item.ID);
                    }
                    this.BantchSaveEntity<T_FM_ROLE_MENU_PERM>(role.Nav_MenuPermissions);
                }
                if (deleteList.Any())
                    this.DeleteEntity<T_FM_ROLE_MENU_PERM>(t => deleteList.Contains(dbRole.ID));

                return true;
            });
        }



        /// <summary>
        /// 授权
        /// </summary>
        /// <param name="role"></param>
        /// <returns></returns>
        [HttpPost, Route("GetPermissionForms")]
        public JsonActionResult<RolePermissionFormResultModel> GetPermissionForms([FromBody] KeywordPageFilter pageFilter)
        {
            return SafeExecute<RolePermissionFormResultModel>(() =>
            {
                RolePermissionFormResultModel result = new RolePermissionFormResultModel();
                var pageList = this.GetOrderPageEntities<T_PF_FORM>(null, pageFilter);
                var list = pageList.Data.ToList();
                result.TotalCount = pageList.TotalCount;
                if (list == null || list.Count == 0) return result;

                string roleId = pageFilter.Keyword;//角色ID

                List<T_FM_ROLE_MENU_PERM> permissionList = null;
                if (!string.IsNullOrEmpty(roleId))
                    permissionList = this.GetEntitiesByRedis<T_FM_ROLE_MENU_PERM>(t => t.Nav_PermissionForm.ENABLE_STATUS == (int)FMEnableStatusEnum.启用,
                        new BaseFilter(pageFilter.OrgId), roleId, new string[] { "Nav_PermissionBtns", "Nav_PermissionColumns", "Nav_PermissionForm" }).ToList();

                //var formIds = list.Select(t => t.ID).ToList();

                List<T_PF_PAGE_TABLE> pageTableList = new List<T_PF_PAGE_TABLE>();
                List<T_PF_PAGE_EDIT> editTableList = new List<T_PF_PAGE_EDIT>();
                List<T_PF_PAGE_TABLE> treeTableList = new List<T_PF_PAGE_TABLE>();
                List<T_PF_PAGE_CHART> charList = new List<T_PF_PAGE_CHART>();
                List<T_PF_PAGE_CUSTOM> customList = new List<T_PF_PAGE_CUSTOM>();
                foreach (var l in list)
                {
                    switch (l.FORM_TYPE)
                    {
                        case (int)PFFormTypeEnum.列表表单:
                            {
                                var form = this.GetEntitiesByRedis<T_PF_PAGE_TABLE>(null, new BaseFilter(pageFilter.OrgId), l.ID.ToString()).ToList();
                                pageTableList.AddRange(form);
                                break;
                            }
                        case (int)PFFormTypeEnum.编辑表单:
                            {
                                var form = this.GetEntitiesByRedis<T_PF_PAGE_EDIT>(null, new BaseFilter(pageFilter.OrgId), l.ID.ToString()).ToList();
                                editTableList.AddRange(form);
                                break;
                            }
                        case (int)PFFormTypeEnum.树形编辑页:
                            {
                                var form = this.GetEntitiesByRedis<T_PF_PAGE_TABLE>(null, new BaseFilter(pageFilter.OrgId), l.ID.ToString()).ToList();
                                treeTableList.AddRange(form);
                                break;
                            }
                        case (int)PFFormTypeEnum.图表表单:
                            {
                                var form = this.GetEntitiesByRedis<T_PF_PAGE_CHART>(null, new BaseFilter(pageFilter.OrgId), l.ID.ToString()).ToList();
                                charList.AddRange(form);
                                break;
                            }
                        case (int)PFFormTypeEnum.自定义表单:
                            {
                                var form = this.GetEntitiesByRedis<T_PF_PAGE_CUSTOM>(null, new BaseFilter(pageFilter.OrgId), l.ID.ToString(), "Nav_PageForm").ToList();
                                customList.AddRange(form);
                                break;
                            }
                        default:
                            {
                                //var form = this.GetEntitiesByRedis<T_PF_PAGE_TABLE>(null, new BaseFilter(pageFilter.OrgId), l.ID.ToString()).ToList();
                                //pageTableList.AddRange(form);
                                break;
                            }
                    }

                }
                var pageTableIds = pageTableList.Select(t => t.ID).ToList();
                var editTableIds = editTableList.Select(t => t.ID).ToList();
                var treeTableIds = treeTableList.Select(t => t.ID).ToList();
                List<T_PF_COLUMN> columns = new List<T_PF_COLUMN>();
                foreach (var page in pageTableIds)
                {
                    var clm = this.GetEntitiesByRedis<T_PF_COLUMN>(t => t.IS_RULE, new BaseFilter(pageFilter.OrgId), page.ToString()).ToList();
                    columns.AddRange(clm);
                }
                List<T_PF_BTN> btns = new List<T_PF_BTN>();
                foreach (var pageId in pageTableIds)
                {
                    var pageBtns = this.GetEntitiesByRedis<T_PF_BTN>(t => t.IS_RULE && t.ENABLE_STATUS == (int)FMEnableStatusEnum.启用,
                        new BaseFilter(pageFilter.OrgId), pageId.ToString()).ToList();
                    btns.AddRange(pageBtns);
                }
                foreach (var pageId in editTableList)
                {
                    var pageBtns = this.GetEntitiesByRedis<T_PF_BTN>(t => t.IS_RULE && t.ENABLE_STATUS == (int)FMEnableStatusEnum.启用,
                        new BaseFilter(pageFilter.OrgId), pageId.ID.ToString()).ToList();
                    btns.AddRange(pageBtns);
                }
                foreach (var pageId in treeTableList)
                {
                    var pageBtns = this.GetEntitiesByRedis<T_PF_BTN>(t => t.IS_RULE && t.ENABLE_STATUS == (int)FMEnableStatusEnum.启用,
                        new BaseFilter(pageFilter.OrgId), pageId.ID.ToString()).ToList();
                    btns.AddRange(pageBtns);
                }
                foreach (var pageId in charList)
                {
                    var pageBtns = this.GetEntitiesByRedis<T_PF_BTN>(t => t.IS_RULE && t.ENABLE_STATUS == (int)FMEnableStatusEnum.启用,
                        new BaseFilter(pageFilter.OrgId), pageId.ID.ToString()).ToList();
                    btns.AddRange(pageBtns);
                }
                foreach (var pageId in customList)
                {
                    var pageBtns = this.GetEntitiesByRedis<T_PF_BTN>(t => t.IS_RULE && t.ENABLE_STATUS == (int)FMEnableStatusEnum.启用,
                        new BaseFilter(pageFilter.OrgId), pageId.ID.ToString()).ToList();
                    btns.AddRange(pageBtns);
                }

                //var btns = this.GetEntitiesByRedis<T_PF_BTN>(t => (pageTableIds.Contains(t.PAGE_TABLE_ID ?? Guid.Empty) || editTableIds.Contains(t.PAGE_EDIT_ID ?? Guid.Empty)
                //|| treeTableIds.Contains(t.PAGE_EDIT_ID ?? Guid.Empty) || formIds.Contains(t.PAGE_CUSTOM_FORM_ID ?? Guid.Empty))
                //&& t.IS_RULE && t.ENABLE_STATUS == (int)FMEnableStatusEnum.启用, new BaseFilter(pageFilter.OrgId)).ToList();

                Dictionary<Guid, Dictionary<T_PF_PAGE_TABLE, List<T_PF_COLUMN>>> columnDic = new Dictionary<Guid, Dictionary<T_PF_PAGE_TABLE, List<T_PF_COLUMN>>>();
                Dictionary<Guid, Dictionary<RolePermissionFormBtnTempModel, List<T_PF_BTN>>> btnDic = new Dictionary<Guid, Dictionary<RolePermissionFormBtnTempModel, List<T_PF_BTN>>>();

                if (pageTableList != null && pageTableList.Any())
                {
                    var tmpPageTableList = pageTableList.OrderBy(t => t.NUM).ThenBy(t => t.PARENT_ID).ToList();
                    foreach (var item in tmpPageTableList)
                    {
                        if (item.PAGE_FORM_ID == null) continue;
                        Dictionary<T_PF_PAGE_TABLE, List<T_PF_COLUMN>> tmp = null;
                        if (!columnDic.TryGetValue(item.PAGE_FORM_ID.Value, out tmp))
                        {
                            tmp = new Dictionary<T_PF_PAGE_TABLE, List<T_PF_COLUMN>>();
                            columnDic.Add(item.PAGE_FORM_ID.Value, tmp);
                        }
                        var colTmp = columns.Where(t => t.PAGE_TABLE_ID == item.ID).OrderBy(t => t.NUM).ToList();
                        if (colTmp != null && colTmp.Any())
                            tmp.Add(item, colTmp);

                        Dictionary<RolePermissionFormBtnTempModel, List<T_PF_BTN>> btnTmpDic = null;
                        if (!btnDic.TryGetValue(item.PAGE_FORM_ID.Value, out btnTmpDic))
                        {
                            btnTmpDic = new Dictionary<RolePermissionFormBtnTempModel, List<T_PF_BTN>>();
                            btnDic.Add(item.PAGE_FORM_ID.Value, btnTmpDic);
                        }
                        var btnTmp = btns.Where(t => t.PAGE_TABLE_ID == item.ID).OrderBy(t => t.BTN_FUN_TYPE).ThenBy(t => t.NUM).ToList();
                        if (btnTmp != null && btnTmp.Any())
                        {
                            RolePermissionFormBtnTempModel tempModel = new RolePermissionFormBtnTempModel();
                            tempModel.Title = item.LABEL_NAME;
                            btnTmpDic.Add(tempModel, btnTmp);
                        }

                    }
                }


                if (editTableList != null && editTableList.Any())
                {
                    foreach (var item in editTableList)
                    {
                        if (item.PAGE_FORM_ID == null) continue;
                        Dictionary<RolePermissionFormBtnTempModel, List<T_PF_BTN>> btnTmpDic = null;
                        if (!btnDic.TryGetValue(item.PAGE_FORM_ID.Value, out btnTmpDic))
                        {
                            btnTmpDic = new Dictionary<RolePermissionFormBtnTempModel, List<T_PF_BTN>>();
                            btnDic.Add(item.PAGE_FORM_ID.Value, btnTmpDic);
                        }
                        var btnTmp = btns.Where(t => t.PAGE_EDIT_ID == item.ID).OrderBy(t => t.BTN_FUN_TYPE).ThenBy(t => t.NUM).ToList();
                        if (btnTmp != null && btnTmp.Any())
                        {
                            RolePermissionFormBtnTempModel tempModel = new RolePermissionFormBtnTempModel();
                            tempModel.Title = item.LABEL_NAME;
                            btnTmpDic.Add(tempModel, btnTmp);
                        }
                    }
                }
                if (treeTableList != null && treeTableList.Any())
                {
                    foreach (var item in treeTableList)
                    {
                        if (item.PAGE_FORM_ID == null) continue;
                        Dictionary<RolePermissionFormBtnTempModel, List<T_PF_BTN>> btnTmpDic = null;
                        if (!btnDic.TryGetValue(item.PAGE_FORM_ID.Value, out btnTmpDic))
                        {
                            btnTmpDic = new Dictionary<RolePermissionFormBtnTempModel, List<T_PF_BTN>>();
                            btnDic.Add(item.PAGE_FORM_ID.Value, btnTmpDic);
                        }
                        var btnTmp = btns.Where(t => t.PAGE_EDIT_ID == item.ID).OrderBy(t => t.BTN_FUN_TYPE).ThenBy(t => t.NUM).ToList();
                        if (btnTmp != null && btnTmp.Any())
                        {
                            RolePermissionFormBtnTempModel tempModel = new RolePermissionFormBtnTempModel();
                            tempModel.Title = item.LABEL_NAME;
                            btnTmpDic.Add(tempModel, btnTmp);
                        }
                    }
                }
                if (charList != null && charList.Any())
                {
                    foreach (var item in charList)
                    {
                        if (item.PAGE_FORM_ID == null) continue;
                        Dictionary<RolePermissionFormBtnTempModel, List<T_PF_BTN>> btnTmpDic = null;
                        if (!btnDic.TryGetValue(item.PAGE_FORM_ID.Value, out btnTmpDic))
                        {
                            btnTmpDic = new Dictionary<RolePermissionFormBtnTempModel, List<T_PF_BTN>>();
                            btnDic.Add(item.PAGE_FORM_ID.Value, btnTmpDic);
                        }
                        var btnTmp = btns.Where(t => t.PAGE_EDIT_ID == item.ID).OrderBy(t => t.BTN_FUN_TYPE).ThenBy(t => t.NUM).ToList();
                        if (btnTmp != null && btnTmp.Any())
                        {
                            RolePermissionFormBtnTempModel tempModel = new RolePermissionFormBtnTempModel();
                            tempModel.Title = item.LABEL;
                            btnTmpDic.Add(tempModel, btnTmp);
                        }
                    }
                }



                var tmpList = list.Where(t => t.FORM_TYPE == (int)PFFormTypeEnum.自定义表单).ToList();
                foreach (var item in tmpList)
                {
                    Dictionary<RolePermissionFormBtnTempModel, List<T_PF_BTN>> btnTmpDic = null;
                    if (!btnDic.TryGetValue(item.ID, out btnTmpDic))
                    {
                        btnTmpDic = new Dictionary<RolePermissionFormBtnTempModel, List<T_PF_BTN>>();
                        btnDic.Add(item.ID, btnTmpDic);
                    }
                    var btnTmp = btns.Where(t => t.PAGE_CUSTOM_FORM_ID == item.ID).OrderBy(t => t.BTN_FUN_TYPE).ThenBy(t => t.NUM).ToList();
                    if (btnTmp != null && btnTmp.Any())
                    {
                        RolePermissionFormBtnTempModel tempModel = new RolePermissionFormBtnTempModel();
                        tempModel.Title = item.NAME;
                        btnTmpDic.Add(tempModel, btnTmp);
                    }
                }
                List<T_FM_ROLE_MENU_PERM> addRoleMenusList = new List<T_FM_ROLE_MENU_PERM>();
                foreach (var item in list)
                {

                    RolePermissionFormModel formModel = new RolePermissionFormModel();
                    formModel.Title = item.NAME;
                    formModel.Code = item.CODE;
                    T_FM_ROLE_MENU_PERM menuPermission = null;
                    if (permissionList != null && permissionList.Any())
                        menuPermission = permissionList.Where(t => t.PERMISSION_FORM_ID == item.ID).FirstOrDefault();
                    if (menuPermission == null)
                    {
                        menuPermission = new T_FM_ROLE_MENU_PERM();
                        menuPermission.ORG_ID = pageFilter.OrgId;
                        menuPermission.PERMISSION_FORM_ID = item.ID;
                        menuPermission.ROLE_ID = string.IsNullOrEmpty(roleId) ? Guid.NewGuid() : new Guid(roleId);
                    }
                    formModel.Permission = menuPermission;
                    formModel.ID = item.ID;
                    formModel.IsCheck = menuPermission == null ? false : menuPermission.IS_PERMISSION;
                    result.Forms.Add(formModel);
                    if (item.FORM_TYPE == (int)PFFormTypeEnum.列表表单)
                    {
                        if (columnDic != null && columnDic.Any())
                        {
                            Dictionary<T_PF_PAGE_TABLE, List<T_PF_COLUMN>> tmp = null;
                            if (columnDic.TryGetValue(item.ID, out tmp))
                            {
                                bool needAddTableLable = tmp.Count > 1;
                                foreach (var t in tmp)
                                {
                                    foreach (var c in t.Value)
                                    {
                                        RolePermissionFormColumnModel columnModel = new RolePermissionFormColumnModel();
                                        columnModel.Title = needAddTableLable ? t.Key.LABEL_NAME + "_" + c.LABEL : c.LABEL;
                                        T_FM_ROLE_MENU_PERM_COL permissionCol = null;
                                        if (menuPermission != null && menuPermission.Nav_PermissionColumns != null && menuPermission.Nav_PermissionColumns.Any())
                                        {
                                            permissionCol = menuPermission.Nav_PermissionColumns.Where(t1 => t1.COLUMN_ID == c.ID).FirstOrDefault();
                                        }
                                        columnModel.IsCheck = permissionCol != null;
                                        if (permissionCol == null)
                                        {
                                            permissionCol = new T_FM_ROLE_MENU_PERM_COL();
                                            permissionCol.ORG_ID = pageFilter.OrgId;
                                            permissionCol.COLUMN_ID = c.ID;
                                            permissionCol.ROLE_MENU_PERMISSION_ID = menuPermission.ID;
                                        }
                                        columnModel.Column = permissionCol;
                                        formModel.Nav_Columns.Add(columnModel);
                                    }
                                }
                            }
                        }
                    }
                    if (btnDic != null && btnDic.Any())
                    {
                        Dictionary<RolePermissionFormBtnTempModel, List<T_PF_BTN>> tmp = null;
                        if (btnDic.TryGetValue(item.ID, out tmp))
                        {
                            bool needAddTableLable = tmp.Count > 1;
                            foreach (var t in tmp)
                            {
                                foreach (var c in t.Value)
                                {
                                    RolePermissionFormBtnModel btnModel = new RolePermissionFormBtnModel();
                                    string title = string.Empty;
                                    if (needAddTableLable)
                                        title = t.Key.Title + "_";
                                    if (c.BTN_FUN_TYPE == (int)PFBtnFuncTypeEnum.行按钮)
                                        title = "行按钮_";
                                    else if (item.FORM_TYPE == (int)PFFormTypeEnum.列表表单)
                                        title = "按钮_";
                                    title += c.LABEL;
                                    btnModel.Title = title;
                                    T_FM_ROLE_MENU_PERM_BTN btnCol = null;
                                    if (menuPermission != null && menuPermission.Nav_PermissionBtns != null && menuPermission.Nav_PermissionBtns.Any())
                                    {
                                        btnCol = menuPermission.Nav_PermissionBtns.Where(t1 => t1.BTN_ID == c.ID).FirstOrDefault();
                                    }
                                    btnModel.IsCheck = btnCol != null;
                                    if (btnCol == null)
                                    {
                                        btnCol = new T_FM_ROLE_MENU_PERM_BTN();
                                        btnCol.ORG_ID = pageFilter.OrgId;
                                        btnCol.BTN_ID = c.ID;
                                        btnCol.ROLE_MENU_PERMISSION_ID = menuPermission.ID;
                                    }
                                    btnModel.Btn = btnCol;
                                    formModel.Nav_Btns.Add(btnModel);
                                }
                            }
                        }
                    }
                }

                return result;
            });
        }


        /// <summary>
        /// 树形授权
        /// </summary>
        /// <param name="role"></param>
        /// <returns></returns>
        [HttpPost, Route("GetTreePermissionForms")]
        public JsonActionResult<RolePermissionResul> GetTreePermissionForms([FromBody] KeywordPageFilter pageFilter)
        {
            return SafeExecute<RolePermissionResul>(() =>
            {
                var filter = new BaseFilter(pageFilter.OrgId);
                var rlt = new RolePermissionResul();
                var rule = pageFilter.FilterGroup.Rules.FirstOrDefault(x => x.Field == "PLATFORM_TYPE");
                if (Convert.ToInt32(rule.Value) == (int)PFPlatTypeEnum.后台)
                {
                    #region 获取菜单树
                    //filter.SelectField = new string[] { "PARENT_ID", "NUM", "ID", "NAME", "MENU_FORM_ID" };
                    filter.Sort = "NUM";
                    filter.Order = DbOrder.ASC;
                    filter.Level = -1;
                    filter.IsParentData = true;
                    var menuTree = this.GetTreeOrderEntities<T_PF_MENU>(x => x.ENABLE_STATUS == (int)FMEnableStatusEnum.启用 && x.PLATFORM_CATEGORY == 0, filter);
                    #endregion
                    var list = this.GetEntities<T_PF_FORM>(null, filter).ToList();//所有表单数据
                    var allFormRelations = this.GetEntities<T_PF_FORM_RELATION>(null, filter).ToList();//所有关联表单数据
                    List<T_PF_PAGE_TABLE> pageTableList = new List<T_PF_PAGE_TABLE>();
                    List<T_PF_PAGE_EDIT> editTableList = new List<T_PF_PAGE_EDIT>();
                    List<T_PF_PAGE_TREE> treeTableList = new List<T_PF_PAGE_TREE>();
                    List<T_PF_PAGE_CHART> charList = new List<T_PF_PAGE_CHART>();
                    List<T_PF_PAGE_CUSTOM> customList = new List<T_PF_PAGE_CUSTOM>();
                    var allBtns = this.GetEntities<T_PF_BTN>(null, filter).ToList();
                    var allCols = this.GetEntities<T_PF_COLUMN>(t => t.IS_RULE, filter).ToList();
                    var allEditCols = this.GetEntities<T_PF_EDIT_COLUMN>(t => t.IS_RULE, filter).ToList();

                    //var filter = new BaseFilter(pageFilter.OrgId);
                    // filter.SelectField = new string[] { "PAGE_FORM_ID", "LABEL_NAME", "NUM", "ID" };
                    var resPagTable = this.GetEntities<T_PF_PAGE_TABLE>(null, filter).ToList();//T_PF_PAGE_TABLE 所有数据             
                    var resPagEdit = this.GetEntities<T_PF_PAGE_EDIT>(null, filter).ToList();//T_PF_PAGE_TABLE 所有数据
                    var resPagTree = this.GetEntities<T_PF_PAGE_TREE>(null, filter).ToList();//T_PF_PAGE_TABLE 所有数据
                                                                                             //filter.SelectField = new string[] { "PAGE_FORM_ID", "LABEL", "NUM", "ID" };
                    var resPagChart = this.GetEntities<T_PF_PAGE_CHART>(null, filter).ToList();//T_PF_PAGE_TABLE 所有数据
                                                                                               // filter.SelectField = new string[] { "PAGE_FORM_ID", "COMPONENT_NAME", "ID" };
                    var resPagCustom = this.GetEntities<T_PF_PAGE_CUSTOM>(null, filter).ToList();//T_PF_PAGE_TABLE 所有数据
                    List<RolePermissionTreeModel> result = new List<RolePermissionTreeModel>();//最终返回树结果

                    List<FormInfo> formInfos = new List<FormInfo>();
                    Dictionary<string, FormInfo> dicforms = new Dictionary<string, FormInfo>();
                    Dictionary<string, PageTables> dicPages = new Dictionary<string, PageTables>();//表单所属菜单
                    #region 获取权限
                    string roleId = pageFilter.Keyword;//角色ID
                    List<T_FM_ROLE_MENU_PERM> permissionList = null;
                    List<T_FM_ROLE_MENU_PERM_BTN> permissionBtns = null;
                    List<T_FM_ROLE_MENU_PERM_COL> permissionCols = null;
                    // List<T_FM_ROLE_MENU_PERMISON> menuPermissions = null;
                    //  List<T_FM_ROLE_MENU_MENU> permissionMenu = null;

                    if (!string.IsNullOrEmpty(roleId))
                    {
                        //permissionList = this.GetEntitiesByRedis<T_FM_ROLE_MENU_PERM>(t => t.Nav_PermissionForm.ENABLE_STATUS == (int)FMEnableStatusEnum.启用,
                        //    new BaseFilter(pageFilter.OrgId), roleId, new string[] { "Nav_PermissionBtns", "Nav_PermissionColumns", "Nav_PermissionForm" }).ToList();

                        permissionList = this.GetEntities<T_FM_ROLE_MENU_PERM>(x => x.ROLE_ID == new Guid(roleId), new BaseFilter(pageFilter.OrgId)).ToList();
                        permissionBtns = this.GetEntities<T_FM_ROLE_MENU_PERM_BTN>(x => x.ROLE_ID == new Guid(roleId), new BaseFilter(pageFilter.OrgId)).ToList();
                        permissionCols = this.GetEntities<T_FM_ROLE_MENU_PERM_COL>(x => x.ROLE_ID == new Guid(roleId), new BaseFilter(pageFilter.OrgId)).ToList();
                        //menuPermissions = this.GetEntities<T_FM_ROLE_MENU_PERMISON>(x => x.ROLE_ID == new Guid(roleId), new BaseFilter(pageFilter.OrgId)).ToList();
                        // permissionMenu = this.GetEntities<T_FM_ROLE_MENU_MENU>(null, filterMenu).ToList();
                    }

                    #endregion
                    foreach (var menu in menuTree)
                    {
                        //formInfos.Add(new FormInfo() { Code = menu.Node.ID.ToString(), Title = menu.Node.NAME, ParentCode = "Root" });//最外层菜单 
                        AddChildren(menu, permissionList, permissionBtns, permissionCols, list, resPagTable, resPagEdit, resPagTree, resPagChart,
                                      resPagCustom, pageFilter.OrgId.ToString(), formInfos, dicforms, allFormRelations, allBtns, allCols, allEditCols);
                    }
                    List<string> checks = new List<string>();
                    List<string> checkLastKey = new List<string>();
                    foreach (var dicform in dicforms)
                    {
                        formInfos.Add(dicform.Value);
                    }
                    result = RolePermissionTreeModel.GetRolePermissionTree(formInfos);
                    result = result[0].children;
                    foreach (var item in result)
                    {
                        SelectCheck(item, checks, checkLastKey);
                    }
                    rlt = new RolePermissionResul() { checks = checks, checkLastKey = checkLastKey, rolePermissionTreeModels = result };
                }
                else //APP菜单
                {
                    filter.Sort = "NUM";
                    filter.Order = DbOrder.ASC;
                    filter.Level = -1;
                    filter.IsParentData = true;
                    var menuTree = this.GetTreeOrderEntities<T_PF_APPMENU>(x => x.ENABLE_STATUS == (int)FMEnableStatusEnum.启用, filter);
                    var list = this.GetEntities<T_PF_FORM>(x => x.PLATFORM_TYPE == (int)PFPlatTypeEnum.APP, filter).ToList();//所有表单数据
                    List<RolePermissionTreeModel> result = new List<RolePermissionTreeModel>();//最终返回树结果
                    List<FormInfo> formInfos = new List<FormInfo>();
                    Dictionary<string, FormInfo> dicforms = new Dictionary<string, FormInfo>();
                    string roleId = pageFilter.Keyword;//角色ID
                    List<T_FM_ROLE_MENU_PERM> permissionList = null;
                    if (!string.IsNullOrEmpty(roleId))
                    {
                        permissionList = this.GetEntities<T_FM_ROLE_MENU_PERM>(x => x.ROLE_ID == new Guid(roleId),
                            new BaseFilter(pageFilter.OrgId)).ToList();
                    }
                    foreach (var menu in menuTree)
                    {
                        AddChildren(menu, permissionList, dicforms, list);
                    }
                    List<string> checks = new List<string>();
                    List<string> checkLastKey = new List<string>();
                    foreach (var dicform in dicforms)
                    {
                        formInfos.Add(dicform.Value);
                    }
                    result = RolePermissionTreeModel.GetRolePermissionTree(formInfos);
                    result = result[0].children;
                    foreach (var item in result)
                    {
                        SelectCheck(item, checks, checkLastKey);
                    }
                    rlt = new RolePermissionResul() { checks = checks, checkLastKey = checkLastKey, rolePermissionTreeModels = result };
                }

                return rlt;
            });
        }

        /// <summary>
        /// 树形授权
        /// </summary>
        /// <param name="role"></param>
        /// <returns></returns>
        [HttpPost, Route("GetTreeDepartment")]
        public JsonActionResult<dynamic> GetTreeDepartment([FromBody] KeywordPageFilter filter)
        {
            return SafeExecute<dynamic>(() =>
            {
                //dynamic result = new ExpandoObject();
                List<OrgTreeData> result = new List<OrgTreeData>();
                OrgTreeData orgTreeData = new OrgTreeData();
                var list = this.GetEntities<T_FM_DEPARTMENT>(null, filter);
                var parents = list.Where(i => i.PARENT_ID == null).ToList();
                var treeData = new List<TreeDataModel>();
                foreach (var item in parents)
                {
                    var model = new TreeDataModel { key = item.ID.ToString(), title = item.NAME };
                    model.children = GetDepartmentTreeData(list, item);
                    treeData.Add(model);
                }
                //result.departmentTreeData = treeData;
                orgTreeData.treeDataModels.AddRange(treeData);

                var currentDept = this.GetEntities<T_FM_DEPARTMENT>(x => x.ORG_ID == filter.OrgId, filter).Select(x => x.ID);
                var roles = this.GetEntities<T_FM_ROLE_DEPARTMENT>(x => x.ROLE_ID.ToString() == filter.Keyword && currentDept.Contains(x.DEPARTMENT_ID), filter);
                orgTreeData.departmentCheckedKeys.AddRange(roles.Select(x => x.DEPARTMENT_ID));
                var orgInfo = this.GetEntity<T_FM_ORGANIZATION>(x => x.ID == filter.OrgId, filter);
                orgTreeData.OrgId = filter.OrgId.ToString();
                orgTreeData.title = orgInfo.NAME;
                result.Add(orgTreeData);
                if (filter.OrgRule.Any())
                {
                    filter.IgnoreOrgRule = false;
                    var orgFilter = new BaseFilter();
                    foreach (var item in filter.OrgRule)
                    {
                        orgFilter.OrgId = item;
                        OrgTreeData resultRule = new OrgTreeData();
                        var listOrgRules = this.GetEntities<T_FM_DEPARTMENT>(null, orgFilter);
                        var parentOrgRules = listOrgRules.Where(i => i.PARENT_ID == null).ToList();
                        var treeDataOrgRule = new List<TreeDataModel>();
                        foreach (var parentOrgRule in parentOrgRules)
                        {
                            var model = new TreeDataModel { key = parentOrgRule.ID.ToString(), title = parentOrgRule.NAME };
                            model.children = GetDepartmentTreeData(listOrgRules, parentOrgRule);
                            treeDataOrgRule.Add(model);
                        }
                        resultRule.treeDataModels.AddRange(treeDataOrgRule);
                        var orgRuleDept = this.GetEntities<T_FM_DEPARTMENT>(null, orgFilter).Select(x => x.ID);
                        var roleOrgRules = this.GetEntities<T_FM_ROLE_DEPARTMENT>(x => x.ROLE_ID.ToString() == filter.Keyword && orgRuleDept.Contains(x.DEPARTMENT_ID), orgFilter);
                        resultRule.departmentCheckedKeys.AddRange(roleOrgRules.Select(x => x.DEPARTMENT_ID));
                        var orgDataRuleInfo = this.GetEntity<T_FM_ORGANIZATION>(item);
                        resultRule.OrgId = item.ToString();
                        resultRule.title = orgDataRuleInfo.NAME;
                        result.Add(resultRule);
                    }
                }
                return result;
            });
        }
        public class OrgTreeData
        {
            public OrgTreeData()
            {
                this.treeDataModels = new List<TreeDataModel>();
                this.departmentCheckedKeys = new List<Guid>();
            }
            public List<TreeDataModel> treeDataModels { get; set; }
            public List<Guid> departmentCheckedKeys { get; set; }
            public string OrgId { get; set; }
            public string title { get; set; }

        }

        private List<TreeDataModel> GetDepartmentTreeData(IEnumerable<T_FM_DEPARTMENT> list, T_FM_DEPARTMENT node)
        {
            var treeData = new List<TreeDataModel>();
            var nodes = list.Where(x => x.PARENT_ID == node.ID);
            foreach (var item in nodes)
            {
                var model = new TreeDataModel { key = item.ID.ToString(), title = item.NAME };
                var children = GetDepartmentTreeData(list, item);
                if (children.Any())
                    model.children = children;
                treeData.Add(model);
            }
            return treeData;
        }


        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 AddChildren(TreeNode<T_PF_APPMENU> treeNode, List<T_FM_ROLE_MENU_PERM> t_FM_ROLE_MENU_PERMs,
            Dictionary<string, FormInfo> dicforms, List<T_PF_FORM> t_PF_FORMs)
        {
            var permission = false;
            if (!dicforms.ContainsKey(treeNode.Node.ID.ToString() + "_" + treeNode.Node.PARENT_ID.ToString()))
            {
                if (t_FM_ROLE_MENU_PERMs != null && t_FM_ROLE_MENU_PERMs.Count > 0)
                    permission = t_FM_ROLE_MENU_PERMs.Any(x => x.MENU_ID == treeNode.Node.ID);
                else
                    permission = false;
                dicforms.Add(treeNode.Node.ID.ToString() + "_" + treeNode.Node.PARENT_ID.ToString(), new FormInfo()
                {
                    Code = treeNode.Node.ID.ToString(),
                    Title = treeNode.Node.NAME,
                    ParentCode = treeNode.Node.PARENT_ID.ToString(),
                    IsCheck = permission
                });
                var form = t_PF_FORMs.SingleOrDefault(x => x.ID == treeNode.Node.MENU_FORM_ID);//菜单下的表单              
                if (form != null)
                {
                    if (!dicforms.ContainsKey(treeNode.Node.ID.ToString() + "_" + form.ID.ToString()))
                    {
                        if (t_FM_ROLE_MENU_PERMs != null)
                            permission = t_FM_ROLE_MENU_PERMs.Any(x => x.PERMISSION_FORM_ID == form.ID && x.MENU_ID == treeNode.Node.ID);
                        else
                            permission = false;
                        dicforms.Add(treeNode.Node.ID + "_" + form.ID, new FormInfo()
                        {
                            Code = treeNode.Node.ID + "_" + form.ID.ToString(),
                            Title = form.NAME + "(" + form.CODE + ")",
                            ParentCode = treeNode.Node.ID.ToString(),
                            IsCheck = permission
                        });
                    }
                }
            }
            foreach (var item in treeNode.Children)
            {
                AddChildren(item, t_FM_ROLE_MENU_PERMs, dicforms, t_PF_FORMs);
            }
        }
        public void AddChildren(TreeNode<T_PF_MENU> treeNode, List<T_FM_ROLE_MENU_PERM> t_FM_ROLE_MENU_PERMs, List<T_FM_ROLE_MENU_PERM_BTN> permissionBtns, List<T_FM_ROLE_MENU_PERM_COL> permissionCols,
        List<T_PF_FORM> t_PF_FORMs, List<T_PF_PAGE_TABLE> pageTableList,
            List<T_PF_PAGE_EDIT> editTableList, List<T_PF_PAGE_TREE> treeTableList,
            List<T_PF_PAGE_CHART> charList, List<T_PF_PAGE_CUSTOM> customList, string OrgId, List<FormInfo> formInfos, Dictionary<string, FormInfo> dicforms, List<T_PF_FORM_RELATION> allFormRelations, List<T_PF_BTN> allBtns,
            List<T_PF_COLUMN> allCols, List<T_PF_EDIT_COLUMN> allEditCols)
        {
            var permission = false;

            if (!dicforms.ContainsKey(treeNode.Node.ID.ToString() + "_" + treeNode.Node.PARENT_ID.ToString()))
            {
                if (t_FM_ROLE_MENU_PERMs != null && t_FM_ROLE_MENU_PERMs.Count > 0)
                {
                    permission = t_FM_ROLE_MENU_PERMs.Any(x => x.MENU_ID == treeNode.Node.ID);
                }
                else
                {
                    permission = false;
                }
                dicforms.Add(treeNode.Node.ID.ToString() + "_" + treeNode.Node.PARENT_ID.ToString(), new FormInfo()
                {
                    Code = treeNode.Node.ID.ToString(),
                    Title = treeNode.Node.NAME,
                    ParentCode = treeNode.Node.PARENT_ID.ToString(),
                    IsCheck = permission
                });
                if (treeNode.Node.MENU_FORM_ID != null)
                {
                    var form = t_PF_FORMs.SingleOrDefault(x => x.ID == treeNode.Node.MENU_FORM_ID);//菜单下的表单
                    List<Guid> existForm = new List<Guid>();
                    if (form != null)
                    {
                        AddRelated(t_FM_ROLE_MENU_PERMs, permissionBtns, permissionCols, t_PF_FORMs, pageTableList, editTableList, treeTableList,
                            charList, customList, form, OrgId, formInfos, treeNode.Node.ID.ToString(), dicforms, existForm, allFormRelations, allBtns, allCols, allEditCols);
                    }
                }
            }
            foreach (var item in treeNode.Children)
            {
                AddChildren(item, t_FM_ROLE_MENU_PERMs, permissionBtns, permissionCols, t_PF_FORMs, pageTableList, editTableList, treeTableList, charList, customList, OrgId, formInfos, dicforms, allFormRelations, allBtns, allCols, allEditCols);
            }
            //}
            //else  //有配置表单没有下一级
            //{
            //    if (t_FM_ROLE_MENU_PERMs != null && t_FM_ROLE_MENU_PERMs.Count > 0)
            //    {
            //        permission = t_FM_ROLE_MENU_PERMs.Any(x => x.MENU_ID == treeNode.Node.ID);
            //    }
            //    else
            //    {
            //        permission = false;
            //    }
            //    if (!dicforms.ContainsKey(treeNode.Node.ID.ToString() + "_" + treeNode.Node.PARENT_ID.ToString()))
            //    {
            //        dicforms.Add(treeNode.Node.ID.ToString() + "_" + treeNode.Node.PARENT_ID.ToString(), new FormInfo()
            //        {
            //            Code = treeNode.Node.ID.ToString(),
            //            Title = treeNode.Node.NAME,
            //            ParentCode = treeNode.Node.PARENT_ID.ToString(),
            //            IsCheck = permission
            //        });
            //        //formInfos.Add(new FormInfo() { Code = treeNode.Node.ID.ToString(), Title = treeNode.Node.NAME, ParentCode = treeNode.Node.PARENT_ID.ToString() });
            //    }
            //    var form = t_PF_FORMs.SingleOrDefault(x => x.ID == treeNode.Node.MENU_FORM_ID);//菜单下的表单
            //    List<Guid> existForm = new List<Guid>();
            //    if (form != null)
            //    {
            //        AddRelated(t_FM_ROLE_MENU_PERMs, permissionBtns, permissionCols, t_PF_FORMs, pageTableList, editTableList, treeTableList,
            //            charList, customList, form, OrgId, formInfos, treeNode.Node.ID.ToString(), dicforms, existForm, allFormRelations, allBtns, allCols, allEditCols);
            //    }
            //}
        }

        public class PageTables
        {
            public List<T_PF_PAGE_TABLE> pageTableList { get; set; } = new List<T_PF_PAGE_TABLE>();
            public List<T_PF_PAGE_EDIT> editTableList { get; set; } = new List<T_PF_PAGE_EDIT>();
            public List<T_PF_PAGE_TREE> treeTableList { get; set; } = new List<T_PF_PAGE_TREE>();
            public List<T_PF_PAGE_CHART> charList { get; set; } = new List<T_PF_PAGE_CHART>();
            public List<T_PF_PAGE_CUSTOM> customList { get; set; } = new List<T_PF_PAGE_CUSTOM>();

        }
        /// <summary>
        /// 递归查找关联数据
        /// relateID关联表单全部挂着这个表单下
        /// </summary>
        public void AddRelated(List<T_FM_ROLE_MENU_PERM> t_FM_ROLE_MENU_PERMs, List<T_FM_ROLE_MENU_PERM_BTN> permissionBtns, List<T_FM_ROLE_MENU_PERM_COL> permissionCols,
         List<T_PF_FORM> t_PF_FORMs, List<T_PF_PAGE_TABLE> pageTableList, List<T_PF_PAGE_EDIT> editTableList, List<T_PF_PAGE_TREE> treeTableList,
            List<T_PF_PAGE_CHART> charList, List<T_PF_PAGE_CUSTOM> customList, T_PF_FORM form, string OrgId, List<FormInfo> formInfos, string relateID,
            Dictionary<string, FormInfo> dicforms, List<Guid> existForm, List<T_PF_FORM_RELATION> allFormRelations, List<T_PF_BTN> allBtns,
            List<T_PF_COLUMN> allCols, List<T_PF_EDIT_COLUMN> allEditCols)
        {
            if (form.CODE == "FM103")
            {
                var ssss = 1;
            }

            bool permission = false;
            switch (form.FORM_TYPE)
            {
                case (int)PFFormTypeEnum.列表表单:
                case (int)PFFormTypeEnum.组合表单:
                    {
                        #region 配置相关联表单T_PF_FORM_RELATION
                        var formRelations = allFormRelations.Where(x => x.PAGE_FORM_ID == form.ID);
                        if (formRelations.Any())
                        {
                            foreach (var formRelation in formRelations)
                            {
                                var relationForm = t_PF_FORMs.Where(x => x.CODE == formRelation.CODE).FirstOrDefault();
                                if (relationForm != null && !existForm.Contains(relationForm.ID))
                                {
                                    AddRelated(t_FM_ROLE_MENU_PERMs, permissionBtns, permissionCols, t_PF_FORMs, pageTableList,
                                   editTableList, treeTableList, charList, customList, relationForm, OrgId, formInfos, relateID, dicforms, existForm, allFormRelations, allBtns, allCols, allEditCols);
                                }
                            }
                        }
                        #endregion
                        var resTable = pageTableList.Where(x => x.PAGE_FORM_ID == form.ID).ToList();//查找相关表单       
                        if (resTable.Count > 0)
                        {
                            if (!dicforms.ContainsKey(relateID + "_" + form.ID))
                            {
                                if (t_FM_ROLE_MENU_PERMs != null)
                                {
                                    permission = t_FM_ROLE_MENU_PERMs.Any(x => x.PERMISSION_FORM_ID == form.ID && x.MENU_ID == new Guid(relateID));
                                }
                                else
                                {
                                    permission = false;
                                }
                                dicforms.Add(relateID + "_" + form.ID, new FormInfo()
                                {
                                    Code = relateID + "_" + form.ID.ToString(),
                                    Title = form.NAME + "(" + form.CODE + ")",
                                    ParentCode = relateID,
                                    IsCheck = permission
                                });
                                existForm.Add(form.ID);

                                if (form.FORM_TYPE == (int)PFFormTypeEnum.列表表单)
                                {
                                    foreach (var item in resTable)
                                    {
                                        #region 表单授权列
                                        //var clm = this.GetEntitiesByRedis<T_PF_COLUMN>(t => t.IS_RULE, new BaseFilter(new Guid(OrgId)), item.ID.ToString());
                                        var clm = allCols.Where(x => x.PAGE_TABLE_ID == item.ID);
                                        foreach (var col in clm)
                                        {
                                            if (!string.IsNullOrEmpty(col.SHOW_FORM_CODE))//有配置关联表单
                                            {
                                                var colForm = t_PF_FORMs.Where(x => x.CODE == col.SHOW_FORM_CODE).FirstOrDefault();
                                                if (colForm != null && !existForm.Contains(colForm.ID))
                                                {
                                                    AddRelated(t_FM_ROLE_MENU_PERMs, permissionBtns, permissionCols, t_PF_FORMs, pageTableList,
                                                   editTableList, treeTableList, charList, customList, colForm, OrgId, formInfos, relateID, dicforms, existForm, allFormRelations, allBtns, allCols, allEditCols);
                                                }
                                            }
                                            if (permissionCols != null && permissionCols.Count > 0)
                                            {
                                                permission = permissionCols.Any(x => x.COLUMN_ID == col.ID && x.MENU_ID == new Guid(relateID));
                                            }
                                            else
                                            {
                                                permission = false;
                                            }
                                            if (!dicforms.ContainsKey(relateID + "_" + form.ID.ToString() + "_" + col.ID.ToString() + "_COL"))
                                            {
                                                dicforms.Add(relateID + "_" + form.ID.ToString() + "_" + col.ID.ToString() + "_COL", new FormInfo()
                                                {
                                                    Code = relateID + "_" + form.ID.ToString() + "_" + col.ID.ToString() + "_COL",
                                                    Title = col.LABEL,
                                                    ParentCode = relateID + "_" + form.ID.ToString(),
                                                    IsCheck = permission
                                                });
                                            }
                                            //formInfos.Add(new FormInfo() { Code = col.ID.ToString(), Title = col.LABEL, ParentCode = reformId.ID.ToString(), IsCheck = permission });//挂在对应表单下
                                        }
                                        #endregion
                                        #region 按钮授权
                                        //var btns = this.GetEntitiesByRedis<T_PF_BTN>(null, new BaseFilter(new Guid(OrgId)), item.ID.ToString()).ToList();
                                        // var btns = this.GetEntities<T_PF_BTN>(t => t.PAGE_ID == item.ID, new BaseFilter(new Guid(OrgId)));
                                        var btns = allBtns.Where(x => x.PAGE_ID == item.ID);
                                        BtnPermissions(t_FM_ROLE_MENU_PERMs, permissionBtns, permissionCols, t_PF_FORMs,
                                                        pageTableList, editTableList, treeTableList, charList, customList, form, OrgId, formInfos, relateID, dicforms, existForm, btns, allFormRelations, allBtns, allCols, allEditCols);
                                        #endregion
                                    }
                                }
                                else//组合表单
                                {
                                    foreach (var item in resTable)
                                    {
                                        var reformId = t_PF_FORMs.Where(x => x.CODE == item.CODE).FirstOrDefault();
                                        if (reformId != null)
                                        {
                                            if (t_FM_ROLE_MENU_PERMs != null)
                                            {
                                                permission = t_FM_ROLE_MENU_PERMs.Any(x => x.PERMISSION_FORM_ID == reformId.ID && x.MENU_ID == new Guid(relateID));
                                            }
                                            else
                                            {
                                                permission = false;
                                            }
                                            if (!dicforms.ContainsKey(relateID + "_" + reformId.ID.ToString()))
                                            {
                                                //dicforms.Add(relateID + "_" + reformId.ID.ToString(), new FormInfo()
                                                //{
                                                //    Code = relateID + "_" + reformId.ID.ToString(),
                                                //    Title = reformId.NAME + "(" + reformId.CODE + ")",
                                                //    ParentCode = relateID,
                                                //    IsCheck = permission
                                                //});
                                                if (form.ID != reformId.ID && !existForm.Contains(reformId.ID))
                                                {
                                                    AddRelated(t_FM_ROLE_MENU_PERMs, permissionBtns, permissionCols, t_PF_FORMs, pageTableList,
                                                        editTableList, treeTableList, charList, customList, reformId, OrgId, formInfos, relateID, dicforms, existForm, allFormRelations, allBtns, allCols, allEditCols);
                                                }
                                                //existForm.Add(reformId.ID);
                                            }

                                            //var tablePages = pageTableList.Where(x => x.PAGE_FORM_ID == reformId.ID).ToList();//组合表单
                                            //foreach (var tablepage in tablePages)
                                            //{
                                            //    #region 表单授权列
                                            //    //var column = this.GetEntitiesByRedis<T_PF_COLUMN>(t => t.IS_RULE, new BaseFilter(new Guid(OrgId)), tablepage.ID.ToString());
                                            //    var column = allCols.Where(x => x.PAGE_TABLE_ID == tablepage.ID);
                                            //    foreach (var col in column)
                                            //    {
                                            //        if (!string.IsNullOrEmpty(col.SHOW_FORM_CODE))//有配置关联表单
                                            //        {
                                            //            var colForm = t_PF_FORMs.Where(x => x.CODE == col.SHOW_FORM_CODE).FirstOrDefault();
                                            //            if (colForm != null && !existForm.Contains(colForm.ID))
                                            //            {
                                            //                AddRelated(t_FM_ROLE_MENU_PERMs, permissionBtns, permissionCols, t_PF_FORMs, pageTableList,
                                            //               editTableList, treeTableList, charList, customList, colForm, OrgId, formInfos, relateID, dicforms, existForm, allFormRelations, allBtns, allCols, allEditCols);
                                            //            }
                                            //        }
                                            //        if (permissionCols != null && permissionCols.Count > 0)
                                            //        {
                                            //            permission = permissionCols.Any(x => x.COLUMN_ID == col.ID && x.MENU_ID == new Guid(relateID));
                                            //        }
                                            //        else
                                            //        {
                                            //            permission = false;
                                            //        }
                                            //        if (!dicforms.ContainsKey(relateID + "_" + form.ID.ToString() + "_" + col.ID.ToString() + "_COL"))
                                            //        {
                                            //            dicforms.Add(relateID + "_" + form.ID.ToString() + "_" + col.ID.ToString() + "_COL", new FormInfo()
                                            //            {
                                            //                Code = relateID + "_" + form.ID.ToString() + "_" + col.ID.ToString() + "_COL",
                                            //                Title = col.LABEL,
                                            //                ParentCode = relateID + "_" + form.ID.ToString(),
                                            //                IsCheck = permission
                                            //            });
                                            //        }
                                            //    }
                                            //    #endregion
                                            //    #region 按钮授权
                                            //    //var btns = this.GetEntitiesByRedis<T_PF_BTN>(null, new BaseFilter(new Guid(OrgId)), item.ID.ToString()).ToList();
                                            //    // var buttons = this.GetEntities<T_PF_BTN>(t => t.PAGE_ID == tablepage.ID, new BaseFilter(new Guid(OrgId)));
                                            //    var buttons = allBtns.Where(x => x.PAGE_ID == tablepage.ID);
                                            //    BtnPermissions(t_FM_ROLE_MENU_PERMs, permissionBtns, permissionCols, t_PF_FORMs,
                                            //                    pageTableList, editTableList, treeTableList, charList, customList, reformId, OrgId, formInfos, relateID, dicforms, existForm, buttons, allFormRelations, allBtns, allCols, allEditCols);
                                            //    #endregion
                                            //}

                                        }

                                    }
                                }
                            }
                        }
                        break;
                    }
                case (int)PFFormTypeEnum.编辑表单:
                    {
                        #region 配置相关联表单T_PF_FORM_RELATION

                        var formRelations = allFormRelations.Where(x => x.PAGE_FORM_ID == form.ID);
                        if (formRelations.Any())
                        {
                            foreach (var formRelation in formRelations)
                            {
                                var relationForm = t_PF_FORMs.Where(x => x.CODE == formRelation.CODE).FirstOrDefault();
                                if (relationForm != null && !existForm.Contains(relationForm.ID))
                                {
                                    AddRelated(t_FM_ROLE_MENU_PERMs, permissionBtns, permissionCols, t_PF_FORMs, pageTableList,
                                   editTableList, treeTableList, charList, customList, relationForm, OrgId, formInfos, relateID, dicforms, existForm, allFormRelations, allBtns, allCols, allEditCols);
                                }
                            }
                        }
                        #endregion
                        #region editTableList
                        var resEdit = editTableList.Where(x => x.PAGE_FORM_ID == form.ID).ToList();//查找相关表单   
                        if (resEdit.Count > 0)
                        {
                            if (!dicforms.ContainsKey(relateID + "_" + form.ID))
                            {
                                if (t_FM_ROLE_MENU_PERMs != null)
                                {
                                    permission = t_FM_ROLE_MENU_PERMs.Any(x => x.PERMISSION_FORM_ID == form.ID && x.MENU_ID == new Guid(relateID));
                                }
                                else
                                {
                                    permission = false;
                                }
                                dicforms.Add(relateID + "_" + form.ID, new FormInfo()
                                {
                                    Code = relateID + "_" + form.ID.ToString(),
                                    Title = form.NAME + "(" + form.CODE + ")",
                                    ParentCode = relateID,
                                    IsCheck = permission
                                });
                                existForm.Add(form.ID);
                                foreach (var item in resEdit)
                                {
                                    #region 编辑列授权
                                    // var clm = this.GetEntitiesByRedis<T_PF_EDIT_COLUMN>(t => t.IS_RULE, new BaseFilter(new Guid(OrgId)), item.ID.ToString());
                                    var clm = allEditCols.Where(x => x.PAGE_EDIT_ID == item.ID);
                                    foreach (var col in clm)
                                    {
                                        if (!string.IsNullOrEmpty(col.SHOW_FORM_CODE))//有配置关联表单
                                        {
                                            var colForm = t_PF_FORMs.Where(x => x.CODE == col.SHOW_FORM_CODE).FirstOrDefault();
                                            if (colForm != null && !existForm.Contains(colForm.ID))
                                            {
                                                AddRelated(t_FM_ROLE_MENU_PERMs, permissionBtns, permissionCols, t_PF_FORMs, pageTableList,
                                               editTableList, treeTableList, charList, customList, colForm, OrgId, formInfos, relateID, dicforms, existForm, allFormRelations, allBtns, allCols, allEditCols);
                                            }
                                        }
                                        if (permissionCols != null && permissionCols.Count > 0)
                                        {
                                            permission = permissionCols.Any(x => x.COLUMN_ID == col.ID && x.MENU_ID == new Guid(relateID));
                                        }
                                        else
                                        {
                                            permission = false;
                                        }
                                        if (!dicforms.ContainsKey(relateID + "_" + form.ID.ToString() + "_" + col.ID.ToString() + "_COL"))
                                        {
                                            dicforms.Add(relateID + "_" + form.ID.ToString() + "_" + col.ID.ToString() + "_COL", new FormInfo()
                                            {
                                                Code = relateID + "_" + form.ID.ToString() + "_" + col.ID.ToString() + "_COL",
                                                Title = col.LABEL,
                                                ParentCode = relateID + "_" + form.ID.ToString(),
                                                IsCheck = permission
                                            });
                                        }
                                        //formInfos.Add(new FormInfo() { Code = col.ID.ToString(), Title = col.LABEL, ParentCode = reformId.ID.ToString(), IsCheck = permission });//挂在对应表单下
                                    }
                                    #endregion
                                    #region 按钮授权
                                    //var btns = this.GetEntitiesByRedis<T_PF_BTN>(null, new BaseFilter(new Guid(OrgId)), item.ID.ToString()).ToList();
                                    //var btns = this.GetEntities<T_PF_BTN>(t => t.PAGE_ID == item.ID, new BaseFilter(new Guid(OrgId)));
                                    var btns = allBtns.Where(x => x.PAGE_ID == item.ID);
                                    BtnPermissions(t_FM_ROLE_MENU_PERMs, permissionBtns, permissionCols, t_PF_FORMs,
                                                   pageTableList, editTableList, treeTableList, charList, customList, form, OrgId, formInfos, relateID, dicforms, existForm, btns, allFormRelations, allBtns, allCols, allEditCols);
                                    #endregion
                                }
                            }
                        }
                        #endregion
                        break;
                    }
                case (int)PFFormTypeEnum.树形编辑页:
                case (int)PFFormTypeEnum.横向树编辑页:
                    {

                        #region 配置相关联表单T_PF_FORM_RELATION
                        var formRelations = allFormRelations.Where(x => x.PAGE_FORM_ID == form.ID);
                        if (formRelations.Any())
                        {
                            foreach (var formRelation in formRelations)
                            {
                                var relationForm = t_PF_FORMs.Where(x => x.CODE == formRelation.CODE).FirstOrDefault();
                                if (relationForm != null && !existForm.Contains(relationForm.ID))
                                {
                                    AddRelated(t_FM_ROLE_MENU_PERMs, permissionBtns, permissionCols, t_PF_FORMs, pageTableList,
                                   editTableList, treeTableList, charList, customList, relationForm, OrgId, formInfos, relateID, dicforms, existForm, allFormRelations, allBtns, allCols, allEditCols);
                                }
                            }
                        }
                        #endregion
                        #region treeTableList
                        var resTree = treeTableList.Where(x => x.PAGE_FORM_ID == form.ID).ToList();//查找相关表单   
                        if (resTree.Count > 0)
                        {
                            if (!dicforms.ContainsKey(relateID + "_" + form.ID))
                            {
                                if (t_FM_ROLE_MENU_PERMs != null)
                                {
                                    permission = t_FM_ROLE_MENU_PERMs.Any(x => x.PERMISSION_FORM_ID == form.ID && x.MENU_ID == new Guid(relateID));
                                }
                                else
                                {
                                    permission = false;
                                }
                                dicforms.Add(relateID + "_" + form.ID, new FormInfo()
                                {
                                    Code = relateID + "_" + form.ID.ToString(),
                                    Title = form.NAME + "(" + form.CODE + ")",
                                    ParentCode = relateID,
                                    IsCheck = permission
                                });
                                existForm.Add(form.ID);
                                foreach (var item in resTree)
                                {
                                    #region 按钮授权
                                    // var btns = this.GetEntitiesByRedis<T_PF_BTN>(t => t.IS_RULE, new BaseFilter(new Guid(OrgId)), item.ID.ToString()).ToList();
                                    //var btns = this.GetEntities<T_PF_BTN>(t => t.PAGE_ID == item.ID, new BaseFilter(new Guid(OrgId)));
                                    var btns = allBtns.Where(x => x.PAGE_ID == item.ID);
                                    BtnPermissions(t_FM_ROLE_MENU_PERMs, permissionBtns, permissionCols, t_PF_FORMs,
                                                       pageTableList, editTableList, treeTableList, charList, customList, form, OrgId, formInfos, relateID, dicforms, existForm, btns, allFormRelations, allBtns, allCols, allEditCols);
                                    #endregion
                                }
                            }
                        }
                        #endregion
                        #region editTableList
                        var resEdit = editTableList.Where(x => x.PAGE_FORM_ID == form.ID).ToList();//查找相关表单   
                        if (resEdit.Count > 0)
                        {
                            if (!dicforms.ContainsKey(relateID + "_" + form.ID))
                            {
                                if (t_FM_ROLE_MENU_PERMs != null)
                                {
                                    permission = t_FM_ROLE_MENU_PERMs.Any(x => x.PERMISSION_FORM_ID == form.ID && x.MENU_ID == new Guid(relateID));
                                }
                                else
                                {
                                    permission = false;
                                }
                                dicforms.Add(relateID + "_" + form.ID, new FormInfo()
                                {
                                    Code = relateID + "_" + form.ID.ToString(),
                                    Title = form.NAME + "(" + form.CODE + ")",
                                    ParentCode = relateID,
                                    IsCheck = permission
                                });
                                existForm.Add(form.ID);
                            }
                            foreach (var item in resEdit)
                            {
                                #region 编辑列授权
                                //var clm = this.GetEntitiesByRedis<T_PF_EDIT_COLUMN>(t => t.IS_RULE, new BaseFilter(new Guid(OrgId)), item.ID.ToString());
                                var clm = allEditCols.Where(x => x.PAGE_EDIT_ID == item.ID);
                                foreach (var col in clm)
                                {
                                    if (!string.IsNullOrEmpty(col.SHOW_FORM_CODE))//有配置关联表单
                                    {
                                        var colForm = t_PF_FORMs.Where(x => x.CODE == col.SHOW_FORM_CODE).FirstOrDefault();
                                        if (colForm != null && !existForm.Contains(colForm.ID))
                                        {
                                            AddRelated(t_FM_ROLE_MENU_PERMs, permissionBtns, permissionCols, t_PF_FORMs, pageTableList,
                                           editTableList, treeTableList, charList, customList, colForm, OrgId, formInfos, relateID, dicforms, existForm, allFormRelations, allBtns, allCols, allEditCols);
                                        }
                                    }
                                    if (permissionCols != null && permissionCols.Count > 0)
                                    {
                                        permission = permissionCols.Any(x => x.COLUMN_ID == col.ID && x.MENU_ID == new Guid(relateID));
                                    }
                                    else
                                    {
                                        permission = false;
                                    }
                                    if (!dicforms.ContainsKey(relateID + "_" + form.ID.ToString() + "_" + col.ID.ToString() + "_COL"))
                                    {
                                        dicforms.Add(relateID + "_" + form.ID.ToString() + "_" + col.ID.ToString() + "_COL", new FormInfo()
                                        {
                                            Code = relateID + "_" + form.ID.ToString() + "_" + col.ID.ToString() + "_COL",
                                            Title = col.LABEL,
                                            ParentCode = relateID + "_" + form.ID.ToString(),
                                            IsCheck = permission
                                        });
                                    }
                                    //formInfos.Add(new FormInfo() { Code = col.ID.ToString(), Title = col.LABEL, ParentCode = reformId.ID.ToString(), IsCheck = permission });//挂在对应表单下
                                }
                                #endregion
                                #region 按钮授权
                                //var btns = this.GetEntitiesByRedis<T_PF_BTN>(null, new BaseFilter(new Guid(OrgId)), item.ID.ToString()).ToList();
                                //var btns = this.GetEntities<T_PF_BTN>(t => t.PAGE_ID == item.ID, new BaseFilter(new Guid(OrgId)));
                                var btns = allBtns.Where(x => x.PAGE_ID == item.ID);
                                BtnPermissions(t_FM_ROLE_MENU_PERMs, permissionBtns, permissionCols, t_PF_FORMs,
                                               pageTableList, editTableList, treeTableList, charList, customList, form, OrgId, formInfos, relateID, dicforms, existForm, btns, allFormRelations, allBtns, allCols, allEditCols);
                                #endregion
                            }

                        }
                        #endregion
                        break;
                    }
                case (int)PFFormTypeEnum.图表表单:
                    {
                        #region 配置相关联表单T_PF_FORM_RELATION
                        var formRelations = allFormRelations.Where(x => x.PAGE_FORM_ID == form.ID);
                        if (formRelations.Any())
                        {
                            foreach (var formRelation in formRelations)
                            {
                                var relationForm = t_PF_FORMs.Where(x => x.CODE == formRelation.CODE).FirstOrDefault();
                                if (relationForm != null && !existForm.Contains(relationForm.ID))
                                {
                                    AddRelated(t_FM_ROLE_MENU_PERMs, permissionBtns, permissionCols, t_PF_FORMs, pageTableList,
                                   editTableList, treeTableList, charList, customList, relationForm, OrgId, formInfos, relateID, dicforms, existForm, allFormRelations, allBtns, allCols, allEditCols);
                                }
                            }
                        }
                        #endregion
                        var resChart = charList.Where(x => x.PAGE_FORM_ID == form.ID).ToList();//查找相关表单   
                        if (resChart.Count > 0)
                        {
                            if (!dicforms.ContainsKey(relateID + "_" + form.ID))
                            {
                                if (t_FM_ROLE_MENU_PERMs != null)
                                {
                                    permission = t_FM_ROLE_MENU_PERMs.Any(x => x.PERMISSION_FORM_ID == form.ID && x.MENU_ID == new Guid(relateID));
                                }
                                else
                                {
                                    permission = false;
                                }
                                dicforms.Add(form.ID.ToString() + "_" + relateID, new FormInfo()
                                {
                                    Code = relateID + "_" + form.ID.ToString(),
                                    Title = form.NAME + "(" + form.CODE + ")",
                                    ParentCode = relateID,
                                    IsCheck = permission
                                });
                                existForm.Add(form.ID);
                                foreach (var item in resChart)
                                {
                                    #region 按钮授权
                                    //var btns = this.GetEntitiesByRedis<T_PF_BTN>(null, new BaseFilter(new Guid(OrgId)), resChart[0].ID.ToString()).ToList();
                                    //var btns = this.GetEntities<T_PF_BTN>(t => t.PAGE_ID == item.ID, new BaseFilter(new Guid(OrgId)));
                                    var btns = allBtns.Where(x => x.PAGE_ID == item.ID);
                                    BtnPermissions(t_FM_ROLE_MENU_PERMs, permissionBtns, permissionCols, t_PF_FORMs,
                                                       pageTableList, editTableList, treeTableList, charList, customList, form, OrgId, formInfos, relateID, dicforms, existForm, btns, allFormRelations, allBtns, allCols, allEditCols);
                                    #endregion
                                }
                            }

                        }
                        break;
                    }
                case (int)PFFormTypeEnum.自定义表单:
                    {
                        #region 配置相关联表单T_PF_FORM_RELATION
                        var formRelations = allFormRelations.Where(x => x.PAGE_FORM_ID == form.ID);
                        if (formRelations.Any())
                        {
                            foreach (var formRelation in formRelations)
                            {
                                var relationForm = t_PF_FORMs.Where(x => x.CODE == formRelation.CODE).FirstOrDefault();
                                if (relationForm != null && !existForm.Contains(relationForm.ID))
                                {
                                    AddRelated(t_FM_ROLE_MENU_PERMs, permissionBtns, permissionCols, t_PF_FORMs, pageTableList,
                                   editTableList, treeTableList, charList, customList, relationForm, OrgId, formInfos, relateID, dicforms, existForm, allFormRelations, allBtns, allCols, allEditCols);
                                }
                            }
                        }
                        #endregion
                        #region customList
                        var res = customList.Where(x => x.PAGE_FORM_ID == form.ID).ToList();//查找相关表单   
                        if (res.Count > 0)
                        {
                            if (!dicforms.ContainsKey(relateID + "_" + form.ID))
                            {
                                if (t_FM_ROLE_MENU_PERMs != null)
                                {
                                    permission = t_FM_ROLE_MENU_PERMs.Any(x => x.PERMISSION_FORM_ID == form.ID && x.MENU_ID == new Guid(relateID));
                                }
                                else
                                {
                                    permission = false;
                                }
                                dicforms.Add(relateID + "_" + form.ID, new FormInfo()
                                {
                                    Code = relateID + "_" + form.ID.ToString(),
                                    Title = form.NAME + "(" + form.CODE + ")",
                                    ParentCode = relateID,
                                    IsCheck = permission
                                });
                                existForm.Add(form.ID);
                                #region 按钮授权
                                //var btns = this.GetEntitiesByRedis<T_PF_BTN>(null, new BaseFilter(new Guid(OrgId)), res[0].ID.ToString()).ToList();
                                //var btns = this.GetEntities<T_PF_BTN>(t => t.PAGE_ID == form.ID, new BaseFilter(new Guid(OrgId)));
                                var btns = allBtns.Where(x => x.PAGE_ID == form.ID);
                                BtnPermissions(t_FM_ROLE_MENU_PERMs, permissionBtns, permissionCols, t_PF_FORMs,
                                                   pageTableList, editTableList, treeTableList, charList, customList, form, OrgId, formInfos, relateID, dicforms, existForm, btns, allFormRelations, allBtns, allCols, allEditCols);
                                #endregion

                                //foreach (var item in res)
                                //{
                                //    #region 按钮授权
                                //    //var btns = this.GetEntitiesByRedis<T_PF_BTN>(null, new BaseFilter(new Guid(OrgId)), res[0].ID.ToString()).ToList();
                                //    var btns = this.GetEntities<T_PF_BTN>(t => t.PAGE_ID == item.ID, new BaseFilter(new Guid(OrgId)));
                                //    BtnPermissions(t_FM_ROLE_MENU_PERMs, permissionBtns, permissionCols, t_PF_FORMs,
                                //                       pageTableList, editTableList, treeTableList, charList, customList, form, OrgId, formInfos, relateID, dicforms, existForm, btns, allFormRelations);
                                //    #endregion
                                //}
                            }
                        }
                        #endregion
                        #region pageTableList
                        var resTable = pageTableList.Where(x => x.PAGE_FORM_ID == form.ID).ToList();//查找相关表单       
                        if (resTable.Count > 0)
                        {
                            if (!dicforms.ContainsKey(relateID + "_" + form.ID))
                            {
                                if (t_FM_ROLE_MENU_PERMs != null)
                                {
                                    permission = t_FM_ROLE_MENU_PERMs.Any(x => x.PERMISSION_FORM_ID == form.ID && x.MENU_ID == new Guid(relateID));
                                }
                                else
                                {
                                    permission = false;
                                }
                                dicforms.Add(relateID + "_" + form.ID, new FormInfo()
                                {
                                    Code = relateID + "_" + form.ID.ToString(),
                                    Title = form.NAME + "(" + form.CODE + ")",
                                    ParentCode = relateID,
                                    IsCheck = permission
                                });
                                existForm.Add(form.ID);

                            }
                            foreach (var item in resTable)
                            {
                                #region 表单授权列
                                //  var clm = this.GetEntitiesByRedis<T_PF_COLUMN>(t => t.IS_RULE, new BaseFilter(new Guid(OrgId)), item.ID.ToString());
                                var clm = allCols.Where(x => x.PAGE_TABLE_ID == item.ID);
                                foreach (var col in clm)
                                {
                                    if (!string.IsNullOrEmpty(col.SHOW_FORM_CODE))//有配置关联表单
                                    {
                                        var colForm = t_PF_FORMs.Where(x => x.CODE == col.SHOW_FORM_CODE).FirstOrDefault();
                                        if (colForm != null && !existForm.Contains(colForm.ID))
                                        {
                                            AddRelated(t_FM_ROLE_MENU_PERMs, permissionBtns, permissionCols, t_PF_FORMs, pageTableList,
                                           editTableList, treeTableList, charList, customList, colForm, OrgId, formInfos, relateID, dicforms, existForm, allFormRelations, allBtns, allCols, allEditCols);
                                        }
                                    }
                                    if (permissionCols != null && permissionCols.Count > 0)
                                    {
                                        permission = permissionCols.Any(x => x.COLUMN_ID == col.ID && x.MENU_ID == new Guid(relateID));
                                    }
                                    else
                                    {
                                        permission = false;
                                    }
                                    if (!dicforms.ContainsKey(relateID + "_" + form.ID.ToString() + "_" + col.ID.ToString() + "_COL"))
                                    {
                                        dicforms.Add(relateID + "_" + form.ID.ToString() + "_" + col.ID.ToString() + "_COL", new FormInfo()
                                        {
                                            Code = relateID + "_" + form.ID.ToString() + "_" + col.ID.ToString() + "_COL",
                                            Title = col.LABEL,
                                            ParentCode = relateID + "_" + form.ID.ToString(),
                                            IsCheck = permission
                                        });
                                    }
                                    //formInfos.Add(new FormInfo() { Code = col.ID.ToString(), Title = col.LABEL, ParentCode = reformId.ID.ToString(), IsCheck = permission });//挂在对应表单下
                                }
                                #endregion
                                #region 按钮授权
                                //var btns = this.GetEntitiesByRedis<T_PF_BTN>(null, new BaseFilter(new Guid(OrgId)), item.ID.ToString()).ToList();
                                //var btns = this.GetEntities<T_PF_BTN>(t => t.PAGE_ID == item.ID, new BaseFilter(new Guid(OrgId)));
                                var btns = allBtns.Where(x => x.PAGE_ID == item.ID);
                                BtnPermissions(t_FM_ROLE_MENU_PERMs, permissionBtns, permissionCols, t_PF_FORMs,
                                                pageTableList, editTableList, treeTableList, charList, customList, form, OrgId, formInfos, relateID, dicforms, existForm, btns, allFormRelations, allBtns, allCols, allEditCols);
                                #endregion
                            }
                        }
                        #endregion
                        #region editTableList
                        var resEdit = editTableList.Where(x => x.PAGE_FORM_ID == form.ID).ToList();//查找相关表单   
                        if (resEdit.Count > 0)
                        {
                            if (!dicforms.ContainsKey(relateID + "_" + form.ID))
                            {
                                if (t_FM_ROLE_MENU_PERMs != null)
                                {
                                    permission = t_FM_ROLE_MENU_PERMs.Any(x => x.PERMISSION_FORM_ID == form.ID && x.MENU_ID == new Guid(relateID));
                                }
                                else
                                {
                                    permission = false;
                                }
                                dicforms.Add(relateID + "_" + form.ID, new FormInfo()
                                {
                                    Code = relateID + "_" + form.ID.ToString(),
                                    Title = form.NAME + "(" + form.CODE + ")",
                                    ParentCode = relateID,
                                    IsCheck = permission
                                });
                                existForm.Add(form.ID);
                            }

                            foreach (var item in resEdit)
                            {
                                #region 编辑列授权
                                //var clm = this.GetEntitiesByRedis<T_PF_EDIT_COLUMN>(t => t.IS_RULE, new BaseFilter(new Guid(OrgId)), item.ID.ToString());
                                var clm = allEditCols.Where(x => x.PAGE_EDIT_ID == item.ID);
                                foreach (var col in clm)
                                {
                                    if (!string.IsNullOrEmpty(col.SHOW_FORM_CODE))//有配置关联表单
                                    {
                                        var colForm = t_PF_FORMs.Where(x => x.CODE == col.SHOW_FORM_CODE).FirstOrDefault();
                                        if (colForm != null && !existForm.Contains(colForm.ID))
                                        {
                                            AddRelated(t_FM_ROLE_MENU_PERMs, permissionBtns, permissionCols, t_PF_FORMs, pageTableList,
                                           editTableList, treeTableList, charList, customList, colForm, OrgId, formInfos, relateID, dicforms, existForm, allFormRelations, allBtns, allCols, allEditCols);
                                        }
                                    }
                                    if (permissionCols != null && permissionCols.Count > 0)
                                    {
                                        permission = permissionCols.Any(x => x.COLUMN_ID == col.ID && x.MENU_ID == new Guid(relateID));
                                    }
                                    else
                                    {
                                        permission = false;
                                    }
                                    if (!dicforms.ContainsKey(relateID + "_" + form.ID.ToString() + "_" + col.ID.ToString() + "_COL"))
                                    {
                                        dicforms.Add(relateID + "_" + form.ID.ToString() + "_" + col.ID.ToString() + "_COL", new FormInfo()
                                        {
                                            Code = relateID + "_" + form.ID.ToString() + "_" + col.ID.ToString() + "_COL",
                                            Title = col.LABEL,
                                            ParentCode = relateID + "_" + form.ID.ToString(),
                                            IsCheck = permission
                                        });
                                    }
                                    //formInfos.Add(new FormInfo() { Code = col.ID.ToString(), Title = col.LABEL, ParentCode = reformId.ID.ToString(), IsCheck = permission });//挂在对应表单下
                                }
                                #endregion
                                #region 按钮授权
                                //var btns = this.GetEntitiesByRedis<T_PF_BTN>(null, new BaseFilter(new Guid(OrgId)), item.ID.ToString()).ToList();
                                //var btns = this.GetEntities<T_PF_BTN>(t => t.PAGE_ID == item.ID, new BaseFilter(new Guid(OrgId)));
                                var btns = allBtns.Where(x => x.PAGE_ID == item.ID);
                                BtnPermissions(t_FM_ROLE_MENU_PERMs, permissionBtns, permissionCols, t_PF_FORMs,
                                               pageTableList, editTableList, treeTableList, charList, customList, form, OrgId, formInfos, relateID, dicforms, existForm, btns, allFormRelations, allBtns, allCols, allEditCols);
                                #endregion
                            }
                        }
                        #endregion
                        #region treeTableList
                        var resTree = treeTableList.Where(x => x.PAGE_FORM_ID == form.ID).ToList();//查找相关表单   
                        if (resTree.Count > 0)
                        {
                            if (!dicforms.ContainsKey(relateID + "_" + form.ID))
                            {
                                if (t_FM_ROLE_MENU_PERMs != null)
                                {
                                    permission = t_FM_ROLE_MENU_PERMs.Any(x => x.PERMISSION_FORM_ID == form.ID && x.MENU_ID == new Guid(relateID));
                                }
                                else
                                {
                                    permission = false;
                                }
                                dicforms.Add(relateID + "_" + form.ID, new FormInfo()
                                {
                                    Code = relateID + "_" + form.ID.ToString(),
                                    Title = form.NAME + "(" + form.CODE + ")",
                                    ParentCode = relateID,
                                    IsCheck = permission
                                });
                                existForm.Add(form.ID);

                            }
                            foreach (var item in resTree)
                            {
                                #region 按钮授权
                                // var btns = this.GetEntitiesByRedis<T_PF_BTN>(t => t.IS_RULE, new BaseFilter(new Guid(OrgId)), item.ID.ToString()).ToList();
                                //var btns = this.GetEntities<T_PF_BTN>(t => t.PAGE_ID == item.ID, new BaseFilter(new Guid(OrgId)));
                                var btns = allBtns.Where(x => x.PAGE_ID == item.ID);
                                BtnPermissions(t_FM_ROLE_MENU_PERMs, permissionBtns, permissionCols, t_PF_FORMs,
                                                   pageTableList, editTableList, treeTableList, charList, customList, form, OrgId, formInfos, relateID, dicforms, existForm, btns, allFormRelations, allBtns, allCols, allEditCols);
                                #endregion
                            }
                        }
                        #endregion
                        #region charList
                        var resChart = charList.Where(x => x.PAGE_FORM_ID == form.ID).ToList();//查找相关表单   
                        if (resChart.Count > 0)
                        {
                            if (!dicforms.ContainsKey(relateID + "_" + form.ID))
                            {
                                if (t_FM_ROLE_MENU_PERMs != null)
                                {
                                    permission = t_FM_ROLE_MENU_PERMs.Any(x => x.PERMISSION_FORM_ID == form.ID && x.MENU_ID == new Guid(relateID));
                                }
                                else
                                {
                                    permission = false;
                                }
                                dicforms.Add(form.ID.ToString() + "_" + relateID, new FormInfo()
                                {
                                    Code = relateID + "_" + form.ID.ToString(),
                                    Title = form.NAME + "(" + form.CODE + ")",
                                    ParentCode = relateID,
                                    IsCheck = permission
                                });
                                existForm.Add(form.ID);

                            }
                            foreach (var item in resChart)
                            {
                                #region 按钮授权
                                //var btns = this.GetEntitiesByRedis<T_PF_BTN>(null, new BaseFilter(new Guid(OrgId)), resChart[0].ID.ToString()).ToList();
                                //var btns = this.GetEntities<T_PF_BTN>(t => t.PAGE_ID == item.ID, new BaseFilter(new Guid(OrgId)));
                                var btns = allBtns.Where(x => x.PAGE_ID == item.ID);
                                BtnPermissions(t_FM_ROLE_MENU_PERMs, permissionBtns, permissionCols, t_PF_FORMs,
                                                   pageTableList, editTableList, treeTableList, charList, customList, form, OrgId, formInfos, relateID, dicforms, existForm, btns, allFormRelations, allBtns, allCols, allEditCols);
                                #endregion
                            }
                        }
                        #endregion
                        break;
                    }
                default:
                    {
                        break;
                    }
            }

        }
        /// <summary>
        /// 按钮权限
        /// </summary>
        /// <param name="t_FM_ROLE_MENU_PERMs"></param>
        /// <param name="permissionBtns"></param>
        /// <param name="permissionCols"></param>
        /// <param name="menuPermissions"></param>
        /// <param name="t_PF_FORMs"></param>
        /// <param name="pageTableList"></param>
        /// <param name="editTableList"></param>
        /// <param name="treeTableList"></param>
        /// <param name="charList"></param>
        /// <param name="customList"></param>
        /// <param name="form"></param>
        /// <param name="OrgId"></param>
        /// <param name="formInfos"></param>
        /// <param name="relateID"></param>
        /// <param name="dicforms"></param>
        /// <param name="existForm"></param>
        /// <param name="btns"></param>
        public void BtnPermissions(List<T_FM_ROLE_MENU_PERM> t_FM_ROLE_MENU_PERMs, List<T_FM_ROLE_MENU_PERM_BTN> permissionBtns, List<T_FM_ROLE_MENU_PERM_COL> permissionCols,
        List<T_PF_FORM> t_PF_FORMs, List<T_PF_PAGE_TABLE> pageTableList, List<T_PF_PAGE_EDIT> editTableList, List<T_PF_PAGE_TREE> treeTableList,
            List<T_PF_PAGE_CHART> charList, List<T_PF_PAGE_CUSTOM> customList, T_PF_FORM reformId, string OrgId, List<FormInfo> formInfos, string relateID, Dictionary<string, FormInfo> dicforms,
            List<Guid> existForm, IEnumerable<T_PF_BTN> btns, List<T_PF_FORM_RELATION> allFormRelations, List<T_PF_BTN> allBtns,
            List<T_PF_COLUMN> allCols, List<T_PF_EDIT_COLUMN> allEditCols)
        {
            bool permission = false;
            foreach (var btn in btns)
            {
                if (btn.ID.ToString() == "43c7343f-8d31-4688-9a87-602d0d27509e")
                {
                    var s = 1;
                }
                if (!string.IsNullOrEmpty(btn.FORM_CODE))//有配置关联跳转表单
                {
                    var btnForm = t_PF_FORMs.Where(x => x.CODE == btn.FORM_CODE).FirstOrDefault();
                    if (btnForm != null && !existForm.Contains(btnForm.ID))
                    {
                        AddRelated(t_FM_ROLE_MENU_PERMs, permissionBtns, permissionCols, t_PF_FORMs, pageTableList,
                            editTableList, treeTableList, charList, customList, btnForm, OrgId, formInfos, relateID, dicforms, existForm, allFormRelations, allBtns, allCols, allEditCols);
                    }
                }
                if (permissionBtns != null && permissionBtns.Count > 0)
                {
                    permission = permissionBtns.Any(x => x.BTN_ID == btn.ID && x.MENU_ID == new Guid(relateID));
                }
                else
                {
                    permission = false;
                }
                if (!dicforms.ContainsKey(relateID + "_" + reformId.ID.ToString() + "_" + btn.ID.ToString() + "_BTN"))
                {
                    dicforms.Add(relateID + "_" + reformId.ID.ToString() + "_" + btn.ID.ToString() + "_BTN", new FormInfo()
                    {
                        Code = relateID + "_" + reformId.ID.ToString() + "_" + btn.ID.ToString() + "_BTN",
                        Title = btn.BTN_FUN_TYPE == (int)PFBtnFuncTypeEnum.行按钮 ? "行按钮_" + btn.LABEL : "按钮_" + btn.LABEL,
                        ParentCode = relateID + "_" + reformId.ID.ToString(),
                        IsCheck = permission
                    });
                }
            }
        }
    }
}
