
using System;
using System.Collections.Generic;
using System.Linq;
using System.Data;
using MySystem.Models.Bs;
using Library;

namespace MySystem
{
    public class AdminRightList
    {
        public WebCMSEntities db = new WebCMSEntities();

        #region 获取权限数据

        public List<AdminRightListItem> GetRight(string myrole, string rightString)
        {
            List<AdminRightListItem> menuList1 = new List<AdminRightListItem>();
            List<RightDic> list = db.RightDic.ToList();
            List<MenuRight> btnList = db.MenuRight.ToList();
            List<RightDic> Level1 = list.Where(m => m.RightLevel == 1).OrderByDescending(m => m.Sort).ToList();
            foreach (RightDic sub1 in Level1)
            {
                AdminRightListItem menu1 = new AdminRightListItem();
                menu1.path = sub1.Path.Replace("\r", "");
                menu1.name = sub1.Name;
                // menu1.component = sub1.Component;
                menu1.meta = new AdminRightListItemMeta();
                menu1.meta.title = sub1.Title;
                menu1.meta.icon = sub1.Icon;
                menu1.meta.auths = GetAuths(btnList.Where(m => m.MenuId.StartsWith(sub1.Id)).OrderBy(m => m.MenuId).ToList().Select(m => m.MenuId).ToList(), rightString, sub1.OtherRight);
                if (menu1.meta.roles == null) menu1.meta.roles = new List<string>(){ "1" };
                if (!menu1.meta.roles.Contains(myrole) && rightString.Contains("," + sub1.Id + ",")) menu1.meta.roles.Add(myrole);
                List<AdminRightListItem> menuList2 = new List<AdminRightListItem>();
                List<RightDic> Level2 = list.Where(m => m.RightLevel == 2 && m.Id.StartsWith(sub1.Id)).OrderByDescending(m => m.Sort).ToList();
                if (Level2.Count > 0)
                {
                    foreach (RightDic sub2 in Level2)
                    {
                        AdminRightListItem menu2 = new AdminRightListItem();
                        menu2.path = sub2.Path.Replace("\r", "");
                        menu2.name = sub2.Name;
                        // menu2.component = sub2.Component;
                        menu2.meta = new AdminRightListItemMeta();
                        menu2.meta.title = sub2.Title;
                        menu2.meta.icon = sub2.Icon;
                        menu2.meta.auths = GetAuths(btnList.Where(m => m.MenuId.StartsWith(sub2.Id)).OrderBy(m => m.MenuId).ToList().Select(m => m.MenuId).ToList(), rightString, sub2.OtherRight);
                        if (menu2.meta.roles == null) menu2.meta.roles = new List<string>(){ "1" };
                        if (!menu2.meta.roles.Contains(myrole) && rightString.Contains("," + sub2.Id + ",")) menu2.meta.roles.Add(myrole);
                        List<RightDic> Level3 = list.Where(m => m.RightLevel == 3 && m.Id.StartsWith(sub2.Id)).OrderByDescending(m => m.Sort).ToList();
                        if (Level3.Count > 0)
                        {
                            List<AdminRightListItem> menuList3 = new List<AdminRightListItem>();
                            foreach (RightDic sub3 in Level3)
                            {
                                AdminRightListItem menu3 = new AdminRightListItem();
                                menu3.path = sub3.Path.Replace("\r", "");
                                menu3.name = sub3.Name;
                                // menu3.component = sub3.Component;
                                menu3.meta = new AdminRightListItemMeta();
                                menu3.meta.title = sub3.Title;
                                menu3.meta.icon = sub3.Icon;
                                menu3.meta.auths = GetAuths(btnList.Where(m => m.MenuId.StartsWith(sub3.Id)).OrderBy(m => m.MenuId).ToList().Select(m => m.MenuId).ToList(), rightString, sub3.OtherRight);
                                if (menu3.meta.roles == null) menu3.meta.roles = new List<string>(){ "1" };
                                if (!menu3.meta.roles.Contains(myrole) && rightString.Contains("," + sub3.Id + ",")) menu3.meta.roles.Add(myrole);
                                menuList3.Add(menu3);
                            }
                            menu2.children = menuList3;
                        }
                        menuList2.Add(menu2);
                    }
                    menu1.children = menuList2;
                }
                menuList1.Add(menu1);
            }
            return menuList1;
        }

        private List<string> GetAuths(List<string> auths, string rightString, string otherRight)
        {
            List<string> result = new List<string>();
            if(auths.Count <= 0)
            {
                return result;
            }
            var rute = auths[0].Substring(0, auths[0].LastIndexOf("_"));
            foreach (string auth in auths)
            {
                if (rightString.Contains(auth))
                {
                    result.Add(auth.Substring(auth.LastIndexOf("_") + 1));
                }
            }
            if (!string.IsNullOrEmpty(otherRight))
            {
                var other = otherRight.Split("\n");
                foreach (var right in other)
                {
                    var a = right.Substring(0, right.LastIndexOf("_"));
                    var check = rute + "_" + a;
                    if (rightString.Contains(check))
                    {
                        result.Add(a);
                    }
                }
            }
            return result;
        }

        private string GetEnName(string str)
        {
            return new PublicFunction().TranslateZHToEn(str).Replace(" ", "").Replace("'", "");
        }

        #endregion


    }
}
