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="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 },
                };
                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="roleId">角色编号</param>
        /// <returns></returns>
        public List<MenuTree_ViewModel> GetTreeMenuModule(RB_Menu_ViewModel query,int roleId=0)
        {
            List<MenuTree_ViewModel> treeList = new List<MenuTree_ViewModel>();
            List<RB_Menu_ViewModel> list = new List<RB_Menu_ViewModel>();
            if (roleId <= 0)
            {
                list = GetMenuListModule(query);
            }
            else
            {
                list = GetRoleMenuListModule(query, roleId: roleId);
            }
            
            if (list != null && list.Count > 0)
            {
                //一级菜单
                var firstLevelList = list.Where(qitem => qitem.MenuLevel == 1).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,
                            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)
                            {
                                MenuTree_ViewModel sModel = new MenuTree_ViewModel()
                                {
                                    MenuId = sItem.MenuId,
                                    MenuName = sItem.MenuName,
                                    MenuUrl = sItem.MenuUrl,
                                    MenuIcon = sItem.MenuIcon,
                                    ParentId = fItem.MenuId,
                                    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)
                                    {
                                        sModel.SubList.Add(new MenuTree_ViewModel()
                                        {
                                            MenuId = tItem.MenuId,
                                            ParentId = sItem.MenuId,
                                            MenuName = tItem.MenuName,
                                            MenuUrl = tItem.MenuUrl,
                                            MenuIcon = tItem.MenuIcon,
                                            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="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;
        }
    }
}