using Ropin.Inspection.Model;
using Ropin.Inspection.Model.Entities;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Ropin.Inspection.Repository
{
    public class TsysLicenseRepository : RepositoryBase<TSYS_License, Guid>, ITsysLicenseRepository
    {
        public TsysLicenseRepository(InspectionDbContext dbContext) : base(dbContext)
        {

        }
        public IList<TSYS_LicenseType> GetALLLicenseType()
        {
            return  DbContext.Set<TSYS_LicenseType>().Where(i => i.C_Status == "1").OrderByDescending(t=>t.I_Sort).ToList();
        }
        public TSYS_LicenseType GetLicenseTypeByCode(string code)
        {
            return DbContext.Set<TSYS_LicenseType>().Where(i => i.C_Code == code).FirstOrDefault();
        }
        public async Task CreateLicenseTypeAsync(TSYS_LicenseType entity)
        {
            await DbContext.Set<TSYS_LicenseType>().AddAsync(entity);
        }
        public void UpdateLicenseType(TSYS_LicenseType entity)
        {
             DbContext.Set<TSYS_LicenseType>().Update(entity);
        }
        public Task<IEnumerable<LicenseTypePrivViewModel>> GetLicensePrivsByTypeIdAsync(string typeId)
        {
            MySqlConnector.MySqlParameter[] parameters = new[] { new MySqlConnector.MySqlParameter("typeId", typeId) };
            string sql = "SELECT * FROM TSYS_LicenseTypePriv where C_LicenseTypeCode = @typeId";
            IEnumerable<LicenseTypePrivViewModel> result = EntityFrameworkCoreExtensions.GetList<LicenseTypePrivViewModel>(DbContext.Database, sql, parameters);
            return Task.FromResult(result);
        }
        public Task<TsysLicenseTypePrivViewModel> GetLicensePrivByType(string typeCode)
        {
                var query = from a in DbContext.TSYS_LicenseTypePriv
                            join b in DbContext.TSYS_Priv
                            on a.C_PrivilegeCode equals b.C_Code
                            where a.C_LicenseTypeCode == typeCode && b.C_Status == "1"
                            select new TSYS_Priv
                            {
                                C_Code = b.C_Code,
                                C_ParentCode = b.C_ParentCode,
                                C_Module = b.C_Module,
                                C_Type = b.C_Type,
                                C_Name = b.C_Name,
                                I_Sort = b.I_Sort,
                                C_ImageUrl = b.C_ImageUrl,
                                C_PageUrl = b.C_PageUrl,
                                C_Remark = b.C_Remark,
                                C_Status = b.C_Status,
                            };
            var licenseTypePrivList = query.ToList();
            var treeList = new List<LicenseTypePrivTree>();
            //foreach (var item in licenseTypePrivList.Where(m => m.C_ParentCode == null).OrderBy(m => m.I_Sort))
            //{
            //    //获得子级
            //    var children = RecursionRoleTypePriv(licenseTypePrivList.Where(m => m.C_ParentCode != null).ToList(), new List<LicenseTypePrivTree>(), item.C_Code);
            //    treeList.Add(new LicenseTypePrivTree()
            //    {
            //        C_Code = item.C_Code,
            //        C_Name = item.C_Name,
            //        C_ParentCode = item.C_ParentCode,
            //        C_Type = item.C_Type,
            //        I_Sort = item.I_Sort,
            //        C_Remark = item.C_Remark,
            //        C_ImageUrl = item.C_ImageUrl,
            //        C_PageUrl = item.C_PageUrl,
            //        C_Module = item.C_Module,
            //        Open = children.Count > 0,
            //        Children = children.Count == 0 ? null : children
            //    });
            //}
            //var model =  new TsysLicenseTypePrivViewModel {LicenseTypeCode = typeCode,LicenseTypePrivS = treeList };
            //return Task.FromResult(model);



            //var treeList = new List<LicenseTypePrivTree>();
            var SysPriv = DbContext.TSYS_Priv.ToList();
            foreach (var item in SysPriv.Where(m => m.C_ParentCode == null))
            {
                List<LicenseTypePrivTree> Part2Children = new List<LicenseTypePrivTree>();
                foreach (var item2 in SysPriv.Where(m => m.C_ParentCode == item.C_Code))
                {
                    List<LicenseTypePrivTree> Part3Children = new List<LicenseTypePrivTree>();
                    foreach (var item3 in SysPriv.Where(m => m.C_ParentCode == item2.C_Code))
                    {
                        List<LicenseTypePrivTree> Part4Children = new List<LicenseTypePrivTree>();
                        foreach (var item4 in SysPriv.Where(m => m.C_ParentCode == item3.C_Code))
                        {
                            var treeList4 = new List<LicenseTypePrivTree>();
                            List<LicenseTypePrivTree> v3 = LicenseRoleTypePriv(licenseTypePrivList.Where(m => m.C_ParentCode != null).ToList(), new List<LicenseTypePrivTree>(), item4.C_Code);
                            var query4 = from p in SysPriv
                                         join pp in licenseTypePrivList
                                         on p.C_Code equals pp.C_Code
                                         where p.C_Code == item4.C_Code
                                         select new
                                         {
                                             pp.C_Code
                                         };
                            if (v3.Count > 0 || query4.Any())
                                treeList4.Add(new LicenseTypePrivTree()
                                {
                                    C_Code = item4.C_Code,
                                    C_Name = item4.C_Name,
                                    C_ParentCode = item4.C_ParentCode,
                                    C_Type = item4.C_Type,
                                    I_Sort = item4.I_Sort,
                                    C_Remark = item4.C_Remark,
                                    C_ImageUrl = item4.C_ImageUrl,
                                    C_PageUrl = item4.C_PageUrl,
                                    C_Module = item4.C_Module,
                                    Open = v3.Count > 0,
                                    Children = v3.Count == 0 ? null : v3
                                });
                            Part4Children.AddRange(treeList4);
                        }
                        var treeList3 = new List<LicenseTypePrivTree>();
                        var query3 = from p in SysPriv
                                     join pp in licenseTypePrivList
                                     on p.C_Code equals pp.C_Code
                                     where p.C_Code == item3.C_Code
                                     select new
                                     {
                                         pp.C_Code
                                     };
                        if (Part4Children.Count > 0 || query3.Any())
                            treeList3.Add(new LicenseTypePrivTree()
                            {
                                C_Code = item3.C_Code,
                                C_Name = item3.C_Name,
                                C_ParentCode = item3.C_ParentCode,
                                C_Type = item3.C_Type,
                                I_Sort = item3.I_Sort,
                                C_Remark = item3.C_Remark,
                                C_ImageUrl = item3.C_ImageUrl,
                                C_PageUrl = item3.C_PageUrl,
                                C_Module = item3.C_Module,
                                Open = Part4Children.Count > 0,
                                Children = Part4Children.Count == 0 ? null : Part4Children
                            });
                        Part3Children.AddRange(treeList3);
                    }
                    var treeList2 = new List<LicenseTypePrivTree>();
                    var query2 = from p in SysPriv
                                 join pp in licenseTypePrivList
                                 on p.C_Code equals pp.C_Code
                                 where p.C_Code == item2.C_Code
                                 select new
                                 {
                                     pp.C_Code
                                 };
                    if (Part3Children.Count > 0 || query2.Any())
                        treeList2.Add(new LicenseTypePrivTree()
                        {
                            C_Code = item2.C_Code,
                            C_Name = item2.C_Name,
                            C_ParentCode = item2.C_ParentCode,
                            C_Type = item2.C_Type,
                            I_Sort = item2.I_Sort,
                            C_Remark = item2.C_Remark,
                            C_ImageUrl = item2.C_ImageUrl,
                            C_PageUrl = item2.C_PageUrl,
                            C_Module = item2.C_Module,
                            Open = Part3Children.Count > 0,
                            Children = Part3Children.Count == 0 ? null : Part3Children
                        });
                    Part2Children.AddRange(treeList2);
                }
                var query1 = from p in SysPriv
                            join pp in licenseTypePrivList
                            on p.C_Code equals pp.C_Code
                            where p.C_Code == item.C_Code
                            select new
                            {
                                pp.C_Code
                            };
                if (Part2Children.Count > 0 || query1.Any())
                    treeList.Add(new LicenseTypePrivTree()
                    {
                        C_Code = item.C_Code,
                        C_Name = item.C_Name,
                        C_ParentCode = item.C_ParentCode,
                        C_Type = item.C_Type,
                        I_Sort = item.I_Sort,
                        C_Remark = item.C_Remark,

                        C_ImageUrl = item.C_ImageUrl,
                        C_PageUrl = item.C_PageUrl,
                        C_Module = item.C_Module,
                        Open = Part2Children.Count > 0,
                        Children = Part2Children.Count == 0 ? null : Part2Children
                    });
            }
            var model = new TsysLicenseTypePrivViewModel { LicenseTypeCode = typeCode, LicenseTypePrivS = treeList };
            
            return Task.FromResult(model);
        }

        List<LicenseTypePrivTree> LicenseRoleTypePriv(List<TSYS_Priv> sourceList, List<LicenseTypePrivTree> list, string guid)
        {
            foreach (var item in sourceList.Where(m => m.C_ParentCode.Equals(guid)))
            {
                var res = LicenseRoleTypePriv(sourceList, new List<LicenseTypePrivTree>(), item.C_Code);
                list.Add(new LicenseTypePrivTree()
                {
                    C_Code = item.C_Code,
                    C_Name = item.C_Name,
                    C_ParentCode = item.C_ParentCode,
                    C_Type = item.C_Type,
                    I_Sort = item.I_Sort,
                    C_Remark = item.C_Remark,
                    //LicenseTypeCode = item.LicenseTypeCode,
                    //LicenseTypeName = item.LicenseTypeName,
                    C_ImageUrl = item.C_ImageUrl,
                    C_PageUrl = item.C_PageUrl,
                    C_Module = item.C_Module,
                    Open = res.Count > 0,
                    Children = res.Count > 0 ? res : null
                });
            }
            return list;
        }

        List<LicenseTypePrivTree> RecursionRoleTypePriv(List<TSYS_Priv> sourceList, List<LicenseTypePrivTree> list, string guid)
        {
            foreach (var item in sourceList.Where(m => m.C_ParentCode.Equals(guid)))
            {
                var res = RecursionRoleTypePriv(sourceList, new List<LicenseTypePrivTree>(), item.C_Code);
                list.Add(new LicenseTypePrivTree()
                {
                    C_Code = item.C_Code,
                    C_Name = item.C_Name,
                    C_ParentCode = item.C_ParentCode,
                    C_Type = item.C_Type,
                    I_Sort = item.I_Sort,
                    C_Remark = item.C_Remark,
                    C_ImageUrl = item.C_ImageUrl,
                    C_PageUrl = item.C_PageUrl,
                    C_Module = item.C_Module,
                    Open = res.Count > 0,
                    Children = res.Count > 0 ? res : null
                });
            }
            return list;
        }


    }
}