using Edu.AOP.CustomerAttribute;
using Edu.Common.Enum;
using Edu.Model.ViewModel.System;
using Edu.Repository.System;
using System;
using System.Collections.Generic;
using System.Linq;
using VT.FW.DB;

namespace Edu.Module.System
{
    /// <summary>
    /// 角色处理类
    /// </summary>
    public class RoleModule
    {
        /// <summary>
        /// 角色仓储层对象
        /// </summary>
        private readonly RB_RoleRepository roleRepository = new RB_RoleRepository();

        /// <summary>
        /// 角色菜单权限仓储层对象
        /// </summary>
        private readonly RB_Role_PermissionRepository rolePermissionRepository = new RB_Role_PermissionRepository();

        /// <summary>
        /// 角色功能权限表仓储层对象
        /// </summary>
        private readonly RB_Role_FunctionPermissionRepository roleFunctionPermissionRepository = new RB_Role_FunctionPermissionRepository();

        /// <summary>
        /// 菜单功能权限仓储层对象
        /// </summary>
        private readonly RB_Menu_FunctionRepository functionRepository = new RB_Menu_FunctionRepository();

        /// <summary>
        /// 系统菜单处理对象
        /// </summary>
        private readonly MenuModule menuModule = new MenuModule();

        /// <summary>
        /// 获取角色列表
        /// </summary>
        /// <param name="query"></param>
        /// <returns></returns>
        public List<RB_Role_ViewModel> GetRoleListModule(RB_Role_ViewModel query)
        {
            return roleRepository.GetRoleListRepository(query);
        }

        /// <summary>
        /// 获取角色分页列表
        /// </summary>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <param name="rowsCount"></param>
        /// <param name="query"></param>
        /// <returns></returns>
        public List<RB_Role_ViewModel> GetRolePageListModule(int pageIndex, int pageSize, out long rowsCount, RB_Role_ViewModel query)
        {
            return roleRepository.GetRolePageListRepository(pageIndex, pageSize, out rowsCount, query);
        }

        /// <summary>
        /// 新增修改角色
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        [TransactionCallHandler]
        public virtual bool SetRoleModule(RB_Role_ViewModel model)
        {
            bool flag;
            if (model.RoleId > 0)
            {
                Dictionary<string, object> fileds = new Dictionary<string, object>()
                {
                    {nameof(RB_Role_ViewModel.RoleName),model.RoleName.Trim() },
                    {nameof(RB_Role_ViewModel.RoleIntro),model.RoleIntro },
                    {nameof(RB_Role_ViewModel.Status),model.Status },
                };
                flag = roleRepository.Update(fileds, new WhereHelper(nameof(RB_Role_ViewModel.RoleId), model.RoleId));
            }
            else
            {
                var newId = roleRepository.Insert(model);
                model.RoleId = newId;
                flag = newId > 0;
            }
            rolePermissionRepository.DeleteOne(new WhereHelper(nameof(RB_Role_Permission_ViewModel.Role_Id), model.RoleId));
            //添加角色权限
            if (model.RolePermissionList != null && model.RolePermissionList.Count > 0)
            {
                foreach (var roleItem in model.RolePermissionList)
                {
                    roleItem.Role_Id = model.RoleId;
                    flag= rolePermissionRepository.Insert(roleItem) > 0;
                }
            }
            //添加角色功能权限
            roleFunctionPermissionRepository.DeleteOne(new WhereHelper(nameof(RB_Role_FunctionPermission_ViewModel.Role_Id), model.RoleId));
            if (model.RoleFunctionList != null && model.RoleFunctionList.Count > 0)
            {
                foreach (var roleItem in model.RoleFunctionList)
                {
                    roleItem.Role_Id = model.RoleId;
                    flag = roleFunctionPermissionRepository.Insert(roleItem) > 0;
                }
            }
            return flag;
        }

        /// <summary>
        /// 获取角色实体
        /// </summary>
        /// <param name="RoleId"></param>
        /// <returns></returns>
        public RB_Role_ViewModel GetRoleModule(object RoleId)
        {
            return roleRepository.GetEntity<RB_Role_ViewModel>(RoleId);
        }

        /// <summary>
        /// 预览角色权限
        /// </summary>
        /// <param name="RoleId">角色编号</param>
        /// <returns></returns>
        public object ViewRolePermissionModule(int RoleId)
        {
            var roleModel = GetRoleModule(RoleId);
            //菜单列表
            var menuList = menuModule.GetTreeMenuModule(new RB_Menu_ViewModel(), roleId: RoleId);
            //获取功能权限列表
            var actionList = functionRepository.GetMenuFunctionListRepository(new RB_Menu_Function_ViewModel() { }, isQueryNormal: true, roleId: RoleId);
            if (menuList != null && menuList.Count > 0)
            {
                foreach (var item in menuList)
                {
                    item.MenuType = 1;
                    if (item.SubList != null && item.SubList.Count > 0)
                    {
                        foreach (var subItem in item.SubList)
                        {
                            subItem.MenuType = 1;
                            if (subItem.SubList != null && subItem.SubList.Count > 0)
                            {
                                foreach (var childItem in subItem.SubList)
                                {
                                    childItem.MenuType = 1;
                                    childItem.SubList = new List<MenuTree_ViewModel>();
                                    var subActionList = actionList?.Where(qitem => qitem.MenuId == childItem.MenuId)?.ToList() ?? new List<RB_Menu_Function_ViewModel>();
                                    if (subActionList != null && subActionList.Count > 0)
                                    {
                                        foreach (var actionItem in actionList)
                                        {
                                            var actionModel = new MenuTree_ViewModel()
                                            {
                                                MenuName = actionItem?.FunctionName + "【F】",
                                                ParentId = childItem.MenuId,
                                                MenuIcon = "",
                                                MenuType = 2,
                                                MenuUrl = "",
                                                MenuId = actionItem.Id,
                                                SubList = new List<MenuTree_ViewModel>()
                                            };
                                            childItem.SubList.Add(actionModel);
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
            var obj = new
            {
                RoleName = roleModel?.RoleName ?? "",
                menuList
            };
            return obj;
        }

        /// <summary>
        /// 修改角色状态
        /// </summary>
        /// <param name="RoleId"></param>
        /// <param name="Status"></param>
        /// <returns></returns>
        public bool SetRoleStatusModule(object RoleId, int Status)
        {
            Dictionary<string, object> fileds = new Dictionary<string, object>()
            {
                {nameof(RB_Role_ViewModel.Status),Status},
            };
            bool flag = roleRepository.Update(fileds, new WhereHelper(nameof(RB_Role_ViewModel.RoleId), RoleId));
            return flag;
        }

        /// <summary>
        /// 获取角色权限信息
        /// </summary>
        /// <param name="roleId"></param>
        /// <returns></returns>
        public List<MenuTree_ViewModel> GetRolePermissionModule(int roleId)
        {
            //菜单列表
            var menuList = menuModule.GetTreeMenuModule(new RB_Menu_ViewModel());
            //获取功能权限列表
            var actionList = functionRepository.GetMenuFunctionListRepository(new RB_Menu_Function_ViewModel() { }, isQueryNormal: true);
            //角色菜单权限列表
            List<RB_Role_Permission_ViewModel> rolePermissionList = new List<RB_Role_Permission_ViewModel>();
            //角色功能权限列表
            List<RB_Role_FunctionPermission_ViewModel> roleFunctionList = new List<RB_Role_FunctionPermission_ViewModel>();
            if (roleId > 0)
            {
                rolePermissionList = GetRolePermissionListModule(new RB_Role_Permission_ViewModel() { Role_Id = roleId });
                roleFunctionList = GetRoleFunctionPermissionListModule(new RB_Role_FunctionPermission_ViewModel() { Role_Id = roleId });
            }

            if (menuList != null && menuList.Count > 0)
            {
                foreach (var item in menuList)
                {
                    item.MenuType = 1;
                    item.IsChecked = rolePermissionList?.Where(qitem => qitem.Menu_Id == item.MenuId)?.Count() > 0;
                    //item.FunctionSubList = actionList?.Where(qitem => qitem.MenuId == item.MenuId)?.ToList() ?? new List<RB_Menu_Function_ViewModel>();
                    if (item.SubList != null && item.SubList.Count > 0)
                    {
                        foreach (var subItem in item.SubList)
                        {
                            subItem.MenuType = 1;
                            subItem.IsChecked = rolePermissionList?.Where(qitem => qitem.Menu_Id == subItem.MenuId)?.Count() > 0;
                            if (subItem.SubList != null && subItem.SubList.Count > 0)
                            {
                                foreach (var childItem in subItem.SubList)
                                {
                                    childItem.MenuType = 1;
                                    childItem.IsChecked = rolePermissionList?.Where(qitem => qitem.Menu_Id == childItem.MenuId)?.Count() > 0;
                                    childItem.SubList = new List<MenuTree_ViewModel>();
                                    var subActionList = actionList?.Where(qitem => qitem.MenuId == childItem.MenuId)?.ToList() ?? new List<RB_Menu_Function_ViewModel>();
                                    if (subActionList != null && subActionList.Count > 0)
                                    {
                                        foreach (var actionItem in actionList)
                                        {
                                            var actionModel = new MenuTree_ViewModel()
                                            {
                                                MenuName = actionItem?.FunctionName+"【F】",
                                                ParentId = childItem.MenuId,
                                                MenuIcon = "",
                                                MenuType = 2,
                                                MenuUrl = "",
                                                MenuId = actionItem.Id,
                                                SubList = new List<MenuTree_ViewModel>()
                                            };
                                            actionModel.IsChecked = roleFunctionList?.Where(qitem => qitem.Action_Id == actionItem.Id)?.Count() > 0;
                                            childItem.SubList.Add(actionModel);
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
            return menuList;
        }

        /// <summary>
        /// 新增修改[角色菜单、角色功能]权限
        /// </summary>
        /// <param name="roleMenuList">菜单权限列表</param>
        /// <param name="roleFunctionList">功能权限列表</param>
        /// <returns></returns>
        [TransactionCallHandler]
        public bool SetRolePermissionModule(List<RB_Role_Permission_ViewModel> roleMenuList, List<RB_Role_FunctionPermission_ViewModel> roleFunctionList)
        {
            bool flag = false;
            //菜单权限
            if (roleMenuList != null && roleMenuList.Count > 0)
            {
                rolePermissionRepository.DeleteOne(new WhereHelper(nameof(RB_Role_Permission_ViewModel.Role_Id), roleMenuList[0].Role_Id));
                foreach (var item in roleMenuList)
                {
                    item.ID = 0;
                    flag = rolePermissionRepository.Insert(item) > 0;
                }
            }
            //功能权限
            if (roleFunctionList != null && roleFunctionList.Count > 0)
            {
                roleFunctionPermissionRepository.DeleteOne(new WhereHelper(nameof(RB_Role_FunctionPermission_ViewModel.Role_Id), roleFunctionList[0].Role_Id));
                foreach (var item in roleFunctionList)
                {
                    item.ID = 0;
                    flag = roleFunctionPermissionRepository.Insert(item) > 0;
                }
            }
            return flag;
        }

        /// <summary>
        /// 获取角色菜单权限列表
        /// </summary>
        /// <param name="query"></param>
        /// <returns></returns>
        public List<RB_Role_Permission_ViewModel> GetRolePermissionListModule(RB_Role_Permission_ViewModel query)
        {
            return rolePermissionRepository.GetRolePermissionListRepository(query);
        }

        /// <summary>
        /// 获取角色功能菜单列表
        /// </summary>
        /// <param name="query"></param>
        /// <returns></returns>
        public List<RB_Role_FunctionPermission_ViewModel> GetRoleFunctionPermissionListModule(RB_Role_FunctionPermission_ViewModel query)
        {
            return roleFunctionPermissionRepository.GetRoleFunctionPermissionListRepository(query);
        }
    }
}