using MadRunFabric.Common;
using MadRunFabric.Common.Options;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using ConfigurationApi.IRepositories;
using MadRunFabric.Model;
using System;
using System.Collections.Generic;
using System.Text;
using MongoDB.Driver;
using System.Linq;

namespace ConfigurationApi.Repositories
{

    public class Sys_Role_FunctionRepository : BaseRepository<Sys_Role_Function, string>, ISys_Role_FunctionRepository
    {
        protected readonly ILogger<BaseRepository<Sys_Role_Function, string>> _logger;
        protected readonly IMongoCollection<Sys_Module_Info> _collection_sys_module_info;
        protected readonly IMongoCollection<Sys_ModuleButton_Info> _collection_sys_modulebutton_info;

        public Sys_Role_FunctionRepository(IOptions<MongodbOptions> settings, ILogger<BaseRepository<Sys_Role_Function, string>> logger) : base(settings, logger)
        {
            _collection_sys_module_info = _context.GetCollection<Sys_Module_Info>();
            _collection_sys_modulebutton_info = _context.GetCollection<Sys_ModuleButton_Info>();
            _logger = logger;
        }

        /// <summary>
        /// 获取菜单和按钮
        /// </summary>
        /// <param name="roleid"></param>
        /// <returns></returns>
        public object GetMenu(string roleid)
        {
            var list = (from p in _collection.AsQueryable() where p.role_id == roleid  select p).ToList();
            var menulist = list.Where(p => p.function_type == (int)EnumRoleControlType.Moudle).Select(p => p.function_id).ToList();
            var buttonlist = list.Where(p => p.function_type == (int)EnumRoleControlType.Button).Select(p => p.function_id).ToList();

            var menuinfolist = (from p in _collection_sys_module_info.AsQueryable() where menulist.Contains(p.id) && p.state_flag == 1 orderby p.sort select p).ToList();
            var buttoninfolist = (from p in _collection_sys_modulebutton_info.AsQueryable() where buttonlist.Contains(p.id) && p.state_flag == 1 orderby p.sort select p).ToList();

            return TreeRecursion(menuinfolist, buttoninfolist);
        }

        /// <summary>
        /// 生成菜单数据方法
        /// </summary>
        /// <param name="data"></param>
        /// <param name="parentId"></param>
        /// <returns></returns>
        private object TreeRecursion(List<Sys_Module_Info> menuinfolist, List<Sys_ModuleButton_Info> buttoninfolist, string parentId = "000000000000000000000000")
        {
            List<Sys_Module_Info> item = menuinfolist.FindAll(t => t.parent_id == parentId);//data建议在调用此扩展方法前已经排序过

            var list = item.Select(p => new
            {
                p.id,
                name = p.module_name,
                code = p.module_code,
                url = p.opt_url,
                type = p.target,
                imgurl = p.img_url,
                ismenu = p.ismenu,
                p.sort,
                buttonstyle = p.button,
                buttongroup = buttoninfolist.Where(q => q.moudle_id == p.id).Select(q=>new {
                    q.id,
                    name=q.full_name,
                    code=q.encode,
                    q.icon,
                    q.jsevent,
                    q.layers,
                    q.location,
                    moudleid=q.moudle_id,
                    parentid=q.parent_id,
                    q.sort,
                    q.split,
                    url=q.url_address
                }),
                children = TreeRecursion(menuinfolist, buttoninfolist, p.id)
            });

            return list;
        }

    }
}
