using System;
using System.Collections.Generic;
using System.Text;

namespace APT.BaseData.Domain.ApiModel.FM
{
    public class RolePermissionTreeModel
    {
       // public Guid ID { get; set; }
        public bool IsCheck { get; set; }
        public string title { get; set; }
        public string key { get; set; }
        public List<RolePermissionTreeModel> children { get; set; } = new List<RolePermissionTreeModel>();    

        public static List<RolePermissionTreeModel> GetRolePermissionTree(List<FormInfo> formInfos)
        {
            var res = new List<RolePermissionTreeModel>();//所有最终父节点
            var dic = new Dictionary<string, RolePermissionTreeModel>();//所有节点
            foreach (var item in formInfos)
            {
                RolePermissionTreeModel rolePermissionTreeModel, prolePermissionTreeModel;
                if (dic.ContainsKey(item.Code))
                {
                    rolePermissionTreeModel = dic[item.Code];
                    rolePermissionTreeModel.title = item.Title;
                    rolePermissionTreeModel.IsCheck = item.IsCheck;
                    if (res.Contains(rolePermissionTreeModel))                   
                        res.Remove(rolePermissionTreeModel);               
                }
                else
                {
                    rolePermissionTreeModel = new RolePermissionTreeModel()
                    {
                        key = item.Code,
                        title = item.Title,
                        IsCheck=item.IsCheck
                    };
                    dic.Add(rolePermissionTreeModel.key, rolePermissionTreeModel);
                }
                if (dic.ContainsKey(item.ParentCode))
                    prolePermissionTreeModel = dic[item.ParentCode];
                else
                {
                    prolePermissionTreeModel = new RolePermissionTreeModel() { key = item.ParentCode };
                    dic.Add(prolePermissionTreeModel.key, prolePermissionTreeModel);
                    if (!res.Contains(prolePermissionTreeModel))
                        res.Add(prolePermissionTreeModel);
                }
                prolePermissionTreeModel.children.Add(rolePermissionTreeModel);
            }
            return res;       
        }
    }
    public class FormInfo
    {
        public string Title { get; set; }
        public bool IsCheck { get; set; }
        public string Code { get; set; }
        public string ParentCode { get; set; }

    }
    public class RolePermissionResul
    {
        public List<string> checks { get; set; }
        public List<string> checkLastKey { get; set; }
        public List<string> orgChecks { get; set; }
        public List<string> orgCheckLastKey { get; set; }
        public List<string> checkTenants { get; set; }
        public List<RolePermissionTreeModel> rolePermissionTreeModels { get; set; }
        public List<RolePermissionTreeModel> orgRolePermissionTreeModels { get; set; }
        public bool isRoot { get; set; }
    }
}
