using Hncore.Infrastructure.Data;
using Hncore.Infrastructure.DDD;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Data;
using System.Linq.Expressions;
using Microsoft.EntityFrameworkCore;
using Hncore.Infrastructure.WebApi;
using Hncore.Pass.Manage.Request;
using Hncore.Pass.Manage.Domain;

namespace Hncore.Pass.Manage.Response
{
    public class QueryItemManagerResponse 

    {

        /// <summary>
        /// 用户数据库ID
        /// <summary>
        public int Id { get; set; }

        /// <summary>
        /// 所属物业ID
        /// <summary>
        public int OwnerId { get; set; }

        /// <summary>
        /// 创建时间
        /// <summary>
        public DateTime CreateTime { get; set; }

        /// <summary>
        /// 更新时间
        /// <summary>
        public DateTime UpdateTime { get; set; }

        /// <summary>
        /// 删除标记
        /// <summary>
        public int DeleteTag { get; set; }
        /// <summary>
        /// 管理员登录名
        /// </summary>
        public string LoginCode { get; set; }

        /// <summary>
        /// 管理员手机号
        /// </summary>
        public string Phone { get; set; }

        /// <summary>
        /// 头像地址
        /// </summary>
        public string Photourl { get; set; }

        /// <summary>
        /// 项目关联
        /// </summary>
        public string ProjectContact { get; set; }

        /// <summary>
        /// 管理员姓名
        /// </summary>
        public string RealName { get; set; }

        public  int Isroot { get; set; }

        public int RoleCode { get; set; }


        public List<PermissionResponse> Permission  =new List<PermissionResponse>();
        public List<DomainResponse> Domain = new List<DomainResponse>();

        public static async Task<QueryItemManagerResponse> Query(
      IQueryable<Domain.Manager> ManagerQueryable
    , IQueryable<Domain.ManagerToPermission> ManagerToPermissionQueryable
     , IQueryable<Domain.AuthorityManagerDataDomain> DomainQueryable

    , QueryByIdRequest param
)
        {
            var search = from dw in ManagerQueryable.Where(p => p.DeleteTag == 0 && p.Id == param.Id)
                         join mtp in ManagerToPermissionQueryable.Where(p => p.DeleteTag == 0)
                         on dw.Id equals mtp.ManagerId into Permissions
                         select new { dw, Permissions };
            var list = search.ToList();

            QueryItemManagerResponse pm  = new QueryItemManagerResponse();
            foreach (var item in list)
            {
                pm.Id = item.dw.Id;
                pm.CreateTime = item.dw.CreateTime;
                pm.UpdateTime = item.dw.UpdateTime;
                pm.DeleteTag = item.dw.DeleteTag;
                pm.OwnerId = item.dw.TenantId;
                pm.LoginCode = item.dw.LoginCode;
                pm.RealName = item.dw.RealName;
                pm.Phone = item.dw.Phone;
                pm.Photourl = item.dw.PhotoUrl;
                pm.RoleCode = item.dw.RoleId;
              //  pm.ProjectContact = item.dw.ProjectContact;

                foreach (var itemper in item.Permissions)
                {
                    PermissionResponse mtp =new PermissionResponse() ;
                    mtp.ManagerId = itemper.ManagerId;
                    mtp.Id = itemper.Id;
                    mtp.AllowAdd = itemper.AllowAdd;
                    mtp.AllowDel = itemper.AllowDel;
                    mtp.AllowEdit = itemper.AllowEdit;
                    mtp.AllowView = itemper.AllowView;
                    mtp.PermissionCode = itemper.PermissionCode;
                    mtp.OwnerId = item.dw.TenantId;
                    pm.Permission.Add(mtp);
                }      
            }

            //var projectList=  DomainQueryable.Where(p => p.DeleteTag == 0 && p.ManagerId == param.Id && p.TenantId == param.TenantId);
            //foreach(var project in projectList)
            //{
            //    pm.Domain.Add(new DomainResponse() {
            //         Id= project.Id,
            //         ProjectCode= project.ProjectCode
            //    });
            //}

            return pm;

        }

    }
}
