using Edu.Model.ViewModel.System;
using Edu.Repository.System;
using System.Collections.Generic;
using System.Linq;
using VT.FW.DB;

namespace Edu.Module.System
{
    /// <summary>
    /// 菜单处理类
    /// </summary>
    public class MenuModule
    {
        /// <summary>
        /// 系统菜单仓储层对象
        /// </summary>
        private readonly RB_MenuRepository menuRepository = new RB_MenuRepository();

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

        /// <summary>
        /// 获取菜单列表
        /// </summary>
        /// <param name="query"></param>
        /// <returns></returns>
        public List<RB_Menu_ViewModel> GetMenuListModule(RB_Menu_ViewModel query)
        {
            return menuRepository.GetMenuListRepository(query);
        }

        /// <summary>
        /// 获取角色权限菜单列表
        /// </summary>
        /// <param name="query"></param>
        /// <param name="roleId">角色编号</param>
        /// <returns></returns>
        public List<RB_Menu_ViewModel> GetRoleMenuListModule(RB_Menu_ViewModel query, int roleId = 0)
        {
            return menuRepository.GetMenuListRepository(query, isQueryPermission: true, roleId: roleId);
        }

        /// <summary>
        /// 获取菜单分页列表
        /// </summary>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <param name="rowsCount"></param>
        /// <param name="query"></param>
        /// <returns></returns>
        public List<RB_Menu_ViewModel> GetMenuPageListModule(int pageIndex, int pageSize, out long rowsCount, RB_Menu_ViewModel query)
        {
            return menuRepository.GetMenuPageListRepository(pageIndex, pageSize, out rowsCount, query);
        }

        /// <summary>
        /// 根据岗位编号获取岗位=>岗位角色列表=>角色列表=>角色菜单列表=>菜单列表
        /// </summary>
        /// <param name="postIds"></param>
        /// <param name="MenuType">菜单类型</param>
        /// <returns></returns>
        public List<RB_Menu_ViewModel> GetPostMenuListModule(string postIds,int MenuType, int teamType)
        {
            return menuRepository.GetPostMenuListRepository(postIds, MenuType, teamType);
        }

        /// <summary>
        /// 新增修改菜单
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public virtual bool SetMenuModule(RB_Menu_ViewModel model)
        {
            bool flag;
            if (model.MenuId > 0)
            {
                Dictionary<string, object> fileds = new Dictionary<string, object>()
                {
                    {nameof(RB_Menu_ViewModel.MenuName),model.MenuName.Trim() },
                    {nameof(RB_Menu_ViewModel.ParentId),model.ParentId },
                    {nameof(RB_Menu_ViewModel.MenuUrl),model.MenuUrl },
                    {nameof(RB_Menu_ViewModel.MenuIcon),model.MenuIcon },
                    {nameof(RB_Menu_ViewModel.MenuLevel),model.MenuLevel },
                    {nameof(RB_Menu_ViewModel.MenuType),model.MenuType },
                    {nameof(RB_Menu_ViewModel.SortNum),model.SortNum },
                    {nameof(RB_Menu_ViewModel.Status),model.Status },
                    {nameof(RB_Menu_ViewModel.IsUserCenter),model.IsUserCenter },
                    {nameof(RB_Menu_ViewModel.TeamType),model.TeamType },
                };
                flag = menuRepository.Update(fileds, new WhereHelper(nameof(RB_Menu_ViewModel.MenuId), model.MenuId));
            }
            else
            {
                var newId = menuRepository.Insert(model);
                model.MenuId = newId;
                flag = newId > 0;
            }
            return flag;
        }

        /// <summary>
        /// 根据菜单编号获取菜单实体
        /// </summary>
        /// <param name="MenuId"></param>
        /// <returns></returns>
        public RB_Menu_ViewModel GetMenuModule(object MenuId)
        {
            return menuRepository.GetEntity<RB_Menu_ViewModel>(MenuId);
        }

        /// <summary>
        /// 更新菜单状态
        /// </summary>
        /// <param name="MenuId"></param>
        /// <param name="Status"></param>
        /// <returns></returns>
        public bool SetMenuStatusModule(int MenuId, int Status)
        {
            Dictionary<string, object> fileds = new Dictionary<string, object>()
            {
                {nameof(RB_Menu_ViewModel.Status),Status },
            };
            bool flag = menuRepository.Update(fileds, new WhereHelper(nameof(RB_Menu_ViewModel.MenuId), MenuId));
            return flag;
        }

        /// <summary>
        /// 获取岗位菜单树形列表
        /// </summary>
        /// <param name="query"></param>
        /// <param name="postIds"></param>
        /// <returns></returns>
        public List<MenuTree_ViewModel> GetPostMenuTreeModule(RB_Menu_ViewModel query, out List<RB_Menu_ViewModel> userCenterList, string postIds = "")
        {
            List<RB_Menu_ViewModel> list = new List<RB_Menu_ViewModel>();
            if (!string.IsNullOrEmpty(postIds))
            {
                query.TeamType = 1;
                list = GetPostMenuListModule(postIds,query.MenuType, query.TeamType);
            }
            userCenterList = menuRepository.GetMenuListRepository(new RB_Menu_ViewModel() { IsUserCenter = 1 });
            var newList= list.Where(qitem => qitem.MenuType == query.MenuType && qitem.IsUserCenter ==0).ToList();
            return GetMenuTreeModule(newList);
        }

        /// <summary>
        /// 获取树形菜单
        /// </summary>
        /// <param name="query"></param>
        /// <param name="roleId">角色编号</param>
        /// <param name="postIds">岗位编号</param>
        /// <returns></returns>
        public List<MenuTree_ViewModel> GetTreeMenuModule(RB_Menu_ViewModel query, int roleId = 0)
        {
            List<RB_Menu_ViewModel> list;
            if (roleId <= 0)
            {
                list = GetMenuListModule(query);
            }
            else
            {
                list = GetRoleMenuListModule(query, roleId: roleId);
            }
            return GetMenuTreeModule(list);
        }

        /// <summary>
        /// 获取菜单树形列表
        /// </summary>
        /// <param name="list"></param>
        /// <returns></returns>
        private List<MenuTree_ViewModel> GetMenuTreeModule(List<RB_Menu_ViewModel> list)
        {
            List<MenuTree_ViewModel> treeList = new List<MenuTree_ViewModel>();
            if (list != null && list.Count > 0)
            {
                //一级菜单
                var firstLevelList = list.Where(qitem => qitem.MenuLevel == 1).OrderBy(qitem=>qitem.SortNum).ToList();
                if (firstLevelList != null && firstLevelList.Count > 0)
                {
                    foreach (var fItem in firstLevelList)
                    {
                        MenuTree_ViewModel fModel = new MenuTree_ViewModel()
                        {
                            MenuId = fItem.MenuId,
                            MenuName = fItem.MenuName,
                            MenuUrl = fItem.MenuUrl,
                            MenuIcon = fItem.MenuIcon,
                            ParentId = 0,
                            MenuType = 1,
                            SubList = new List<MenuTree_ViewModel>(),
                        };
                        //二级菜单
                        var secondLevelList = list.Where(qitem => qitem.MenuLevel == 2 && qitem.ParentId == fItem.MenuId).ToList();
                        if (secondLevelList != null && secondLevelList.Count > 0)
                        {
                            foreach (var sItem in secondLevelList.OrderBy(qitem => qitem.SortNum))
                            {
                                MenuTree_ViewModel sModel = new MenuTree_ViewModel()
                                {
                                    MenuId = sItem.MenuId,
                                    MenuName = sItem.MenuName,
                                    MenuUrl = sItem.MenuUrl,
                                    MenuIcon = sItem.MenuIcon,
                                    ParentId = fItem.MenuId,
                                    MenuType = 1,
                                    SubList = new List<MenuTree_ViewModel>()
                                };
                                //三级菜单
                                var thirdLevelList = list.Where(qitem => qitem.MenuLevel == 3 && qitem.ParentId == sItem.MenuId).ToList();
                                if (thirdLevelList != null && thirdLevelList.Count > 0)
                                {
                                    foreach (var tItem in thirdLevelList.OrderBy(qitem => qitem.SortNum))
                                    {
                                        sModel.SubList.Add(new MenuTree_ViewModel()
                                        {
                                            MenuId = tItem.MenuId,
                                            ParentId = sItem.MenuId,
                                            MenuName = tItem.MenuName,
                                            MenuUrl = tItem.MenuUrl,
                                            MenuIcon = tItem.MenuIcon,
                                            MenuType = 1,
                                            SubList = new List<MenuTree_ViewModel>()
                                        });
                                    }
                                }
                                fModel.SubList.Add(sModel);
                            }
                        }
                        treeList.Add(fModel);
                    }
                }
            }
            return treeList;
        }

        /// <summary>
        /// 获取菜单功能权限分页列表
        /// </summary>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <param name="rowsCount"></param>
        /// <param name="query"></param>
        /// <returns></returns>
        public List<RB_Menu_Function_ViewModel> GetMenuFunctionPageListModule(int pageIndex, int pageSize, out long rowsCount, RB_Menu_Function_ViewModel query)
        {
            return functionRepository.GetMenuFunctionPageListRepository(pageIndex, pageSize, out rowsCount, query);
        }

        /// <summary>
        /// 获取菜单功能权限列表
        /// </summary>
        /// <param name="query"></param>
        /// <returns></returns>
        public List<RB_Menu_Function_ViewModel> GetMenuFunctionListModule(RB_Menu_Function_ViewModel query)
        {
            return functionRepository.GetMenuFunctionListRepository(query);
        }

        /// <summary>
        /// 根据岗位编号获取岗位角色菜单功能权限
        /// </summary>
        /// <param name="postIds">岗位编号</param>
        /// <returns></returns>
        public List<RB_Menu_Function_ViewModel> GetPostMenuFunctionListModule(string postIds)
        {
            return functionRepository.GetPostMenuFunctionListRepository(postIds);
        }

        /// <summary>
        /// 判断用户是否有某项功能权限
        /// </summary>
        /// <param name="actionCode">权限编码</param>
        /// <param name="postIds">岗位编号【可以多个】</param>
        /// <returns></returns>
        public bool CheckUserFunctionModule(string actionCode, string postIds)
        {
            return functionRepository.CheckUserFunctionRepository(actionCode, postIds);
        }

        /// <summary>
        /// 新增修改菜单功能权限
        /// </summary>
        /// <param name="extModel"></param>
        /// <returns></returns>
        public bool SetMenuFunctionModule(RB_Menu_Function_ViewModel extModel)
        {
            bool flag;
            if (extModel.Id > 0)
            {
                Dictionary<string, object> fileds = new Dictionary<string, object>()
                {
                    { nameof(RB_Menu_Function_ViewModel.FunctionName),extModel.FunctionName},
                    { nameof(RB_Menu_Function_ViewModel.FunctionCode),extModel.FunctionCode},
                    { nameof(RB_Menu_Function_ViewModel.Remarks),extModel.Remarks},
                };
                flag = functionRepository.Update(fileds, new WhereHelper(nameof(RB_Menu_Function_ViewModel.Id), extModel.Id));
            }
            else
            {
                var newId = functionRepository.Insert(extModel);
                extModel.Id = newId;
                flag = newId > 0;
            }
            return flag;
        }

        /// <summary>
        /// 根据编号获取菜单功能权限实体
        /// </summary>
        /// <param name="Id"></param>
        /// <returns></returns>
        public RB_Menu_Function_ViewModel GetMenuFunctionModule(object Id)
        {
            return functionRepository.GetEntity<RB_Menu_Function_ViewModel>(Id);
        }

        /// <summary>
        /// 设置菜单功能权限状态
        /// </summary>
        /// <param name="Id">主键编号</param>
        /// <param name="Status">状态(1-禁用，0-启用)</param>
        /// <returns></returns>
        public bool SetMenuFunctionStatusModule(object Id, int Status)
        {
            Dictionary<string, object> fileds = new Dictionary<string, object>()
            {
                { nameof(RB_Menu_Function_ViewModel.Status),Status},
            };
            var flag = functionRepository.Update(fileds, new WhereHelper(nameof(RB_Menu_Function_ViewModel.Id), Id));
            return flag;
        }
    }
}