
using System.Collections.Generic;
using System.Linq;
using MySystem.Models.Bs;
using System.Data;
namespace MySystem
{
    /// <summary>
    /// 基础功能方法
    /// </summary>
    public class BaseClass
    {


        #region 菜单权限json
        /// <summary>
        /// 菜单权限json
        /// </summary>
        /// <returns></returns>
        public static string GetRightJson()
        {
            WebCMSEntities bsdb = new WebCMSEntities();
            string result = "[";
            List<RightDic> list = bsdb.RightDic.ToList();
            List<RightDic> Level1 = list.Where(m => m.RightLevel == 1).ToList();
            foreach (RightDic sub1 in Level1)
            {
                result += "{\"title\": \"" + sub1.Title + "\",\"name\": \"" + sub1.Name + "\",\"id\": \"" + sub1.Id + "\", \"spread\": false, \"children\": [";
                List<RightDic> Level2 = list.Where(m => m.RightLevel == 2 && m.Id.StartsWith(sub1.Id)).ToList();
                if (Level2.Count > 0)
                {
                    foreach (RightDic sub2 in Level2)
                    {
                        result += "{\"title\": \"" + sub2.Title + "\",\"name\": \"" + sub2.Name + "\",\"id\": \"" + sub2.Id + "\", \"spread\": false, \"children\": [";
                        List<RightDic> Level3 = list.Where(m => m.RightLevel == 3 && m.Id.StartsWith(sub2.Id)).ToList();
                        if (Level3.Count > 0)
                        {
                            foreach (RightDic sub3 in Level3)
                            {
                                result += "{\"title\": \"" + sub3.Title + "\",\"name\": \"" + sub3.Name + "\",\"id\": \"" + sub3.Id + "\", \"spread\": false, \"children\": [";
                                string rightString = ForOperateRight(sub3.Id);
                                if (!string.IsNullOrEmpty(rightString))
                                {
                                    result += rightString;
                                }
                                // else
                                // {
                                //     result += "{title: '基本权限', id: '" + sub3.Id + "_base'}";
                                // }
                                result += "]},";
                            }
                            result = result.TrimEnd(',');
                        }
                        else
                        {
                            string rightString = ForOperateRight(sub2.Id);
                            if (!string.IsNullOrEmpty(rightString))
                            {
                                result += rightString;
                            }
                            // else
                            // {
                            //     result += "{title: '基本权限', id: '" + sub2.Id + "_base'}";
                            // }
                        }
                        result += "]},";
                    }
                    result = result.TrimEnd(',');
                }
                else
                {
                    result += ForOperateRight(sub1.Id);
                    string rightString = ForOperateRight(sub1.Id);
                    if (!string.IsNullOrEmpty(rightString))
                    {
                        result += rightString;
                    }
                    // else
                    // {
                    //     result += "{title: '基本权限', id: '" + sub1.Id + "_base'}";
                    // }
                }
                result += "]},";
            }
            result = result.TrimEnd(',');
            result += "]";
            return result;
        }
        //读取当前菜单有哪些权限
        public static string ForOperateRight(string Id)
        {
            WebCMSEntities bsdb = new WebCMSEntities();
            string result = "";
            List<MenuRight> rights = bsdb.MenuRight.Where(m => m.MenuId.StartsWith(Id)).OrderBy(m => m.MenuId).ToList();
            foreach (MenuRight right in rights)
            {
                result += "{\"title\": \"" + right.Name + "\",\"id\": \"" + right.MenuId + "\"},";
            }
            RightDic rightDic = bsdb.RightDic.FirstOrDefault(m => m.Id == Id) ?? new RightDic();
            if (!string.IsNullOrEmpty(rightDic.OtherRight))
            {
                string[] OtherRightList = rightDic.OtherRight.Split('\n');
                foreach (string SubOtherRight in OtherRightList)
                {
                    string[] SubOtherRightData = SubOtherRight.Split('_');
                    result += "{\"title\": \"" + SubOtherRightData[1] + "\",\"id\": \"" + Id + "_" + SubOtherRightData[0] + "\"},";
                }
            }
            result = result.TrimEnd(',');
            return result;
        }
        #endregion
    }
}
