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

namespace Edu.Module.Web
{
    /// <summary>
    /// 网站菜单管理处理类
    /// </summary>
    public class WebMenuModule
    {
        /// <summary>
        /// 网站菜单管理仓储层对象
        /// </summary>
        private readonly RB_Web_MenuRepository web_MenuRepository = new RB_Web_MenuRepository();

        /// <summary>
        /// 获取网站菜单分页列表
        /// </summary>
        /// <param name="pageIndex">页码</param>
        /// <param name="pageSize">每页显示调试</param>
        /// <param name="rowsCount">总条数</param>
        /// <param name="query">查询条件</param>
        /// <returns></returns>
        public List<RB_Web_Menu_ViewModel> GetWebMenuPageModule(int pageIndex, int pageSize, out long rowsCount, RB_Web_Menu_ViewModel query)
        {
            return web_MenuRepository.GetWebMenuPageRepository(pageIndex, pageSize, out rowsCount, query);
        }

        /// <summary>
        /// 获取网站菜单列表
        /// </summary>
        /// <param name="query">查询条件</param>
        /// <returns></returns>
        public List<RB_Web_Menu_ViewModel> GetWebMenuListModule(RB_Web_Menu_ViewModel query)
        {
            return web_MenuRepository.GetWebMenuListRepository(query);
        }

        /// <summary>
        /// 获取树形结构菜单
        /// </summary>
        /// <param name="query"></param>
        /// <returns></returns>
        public object GetMenuTreeModule(RB_Web_Menu_ViewModel query)
        {
            List<object> result = new List<object>();
            var list = GetWebMenuListModule(query);
            if (list != null && list.Count > 0)
            {
                var firstList = list.Where(qitem => qitem.MenuLevel == 1).OrderBy(qitem => qitem.SortNum);
                if (firstList != null && firstList.Count() > 0)
                {
                    foreach (var rootItem in firstList)
                    {
                        var subList = list.Where(qitem => qitem.MenuLevel == 2 && qitem.ParentId == rootItem.Id).OrderBy(qitem => qitem.SortNum);
                        List<object> secondList = new List<object>();
                        foreach (var subItem in subList)
                        {
                            var childList = list.Where(qitem => qitem.MenuLevel == 3 && qitem.ParentId == subItem.Id).OrderBy(qitem => qitem.SortNum);
                            secondList.Add(new
                            {
                                subItem.Name,
                                subItem.MenuUrl,
                                subItem.Icon,
                                subItem.ClassName,
                                ChildList = childList?.Select(qitem => new { qitem.Name, qitem.MenuUrl, qitem.Icon, qitem.ClassName })
                            });
                        }
                        result.Add(new
                        {
                            rootItem.Name,
                            rootItem.MenuUrl,
                            rootItem.Icon,
                            rootItem.ClassName,
                            ChildList = secondList
                        });
                    }
                }
            }
            return result;
        }

        /// <summary>
        /// 新增修改网站前台菜单
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public bool SetWebMenuModule(RB_Web_Menu_ViewModel model)
        {
            bool flag = false;
            if (model.Id > 0)
            {
                Dictionary<string, object> fileds = new Dictionary<string, object>()
                {
                    {nameof(RB_Web_Menu_ViewModel.Name),model.Name },
                    {nameof(RB_Web_Menu_ViewModel.MenuUrl),model.MenuUrl },
                    {nameof(RB_Web_Menu_ViewModel.ParentId),model.ParentId },
                    {nameof(RB_Web_Menu_ViewModel.MenuLevel),model.MenuLevel },
                    {nameof(RB_Web_Menu_ViewModel.ClassName),model.ClassName },
                    {nameof(RB_Web_Menu_ViewModel.Icon),model.Icon },
                    {nameof(RB_Web_Menu_ViewModel.SortNum),model.SortNum },
                };
                flag = web_MenuRepository.Update(fileds, new WhereHelper(nameof(RB_Web_Menu_ViewModel.Id), model.Id));
            }
            else
            {
                var newId = web_MenuRepository.Insert(model);
                model.Id = newId;
                flag = newId > 0;
            }
            return flag;
        }

        /// <summary>
        /// 根据编号获取网站菜单详情
        /// </summary>
        /// <param name="Id"></param>
        /// <returns></returns>
        public RB_Web_Menu_ViewModel GetWebMenuModule(object Id)
        {
            return web_MenuRepository.GetEntity<RB_Web_Menu_ViewModel>(Id);
        }

        /// <summary>
        /// 修改网站菜单状态
        /// </summary>
        /// <param name="Id"></param>
        /// <param name="Status">0-正常,1-删除</param>
        /// <returns></returns>
        public bool RemoveWebMenuStatusModule(int Id, int Status)
        {
            Dictionary<string, object> fileds = new Dictionary<string, object>()
            {
                {nameof(RB_Web_Menu_ViewModel.Status),Status },
            };
            var flag = web_MenuRepository.Update(fileds, new WhereHelper(nameof(RB_Web_Menu_ViewModel.Id), Id));
            return flag;
        }
    }
}
