using Hncore.Infrastructure.Common;
using Hncore.Infrastructure.DDD;
using Hncore.Infrastructure.EF;
using Hncore.Infrastructure.WebApi;
using Hncore.Pass.Manage.Domain;
using Hncore.Pass.Manage.Repository;
using Hncore.Pass.Manage.Request;
using Hncore.Pass.Manage.Response;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Threading.Tasks;

namespace Hncore.Pass.Manage.Service
{


    /// <summary>
    /// 管理员业务逻辑类
    /// </summary>
    ///
    public class ManagerService : BaseService
    {
        EfDbContext m_DbContext { get; set; }

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="">管理员标签查询对象</param>

        public ManagerService(EfDbContext _DbContext,           
            IHttpContextAccessor hca) : base(hca)
        {
            m_DbContext = _DbContext;
        }

        /// <summary>
        /// 获取管理员列表
        /// </summary>
        /// <param name="request">请求对象</param>
        /// <returns>响应列表对象</returns>
        public async Task<(int total, List<QueryListManagerResponse> list)> Get(QueryListManagerRequest request)
        {

            var root  =m_DbContext.Set<Manager>().GetOne(p=>p.Id==request.OperaterId && p.IsRoot == true);

            IQueryable<Manager> managers;
            if (null ==root)
            {
                 managers = m_DbContext.Set<Manager>().GetQueryable().Where(m => m.CreatorId == request.OperaterId && m.IsRoot == false);
            }else
            {
                
                 managers = m_DbContext.Set<Manager>().GetQueryable().Where(m => m.DeleteTag == 0);
                //是否过滤超级管理员
                if (request.IsRoot == 1)
                    managers = managers.Where(m => m.IsRoot == false);
            }

            var search = managers;            
            //过滤关键字
            if (!string.IsNullOrEmpty(request.KeyWord))
                search = search.Where(m => m.LoginCode.Contains(request.KeyWord) || m.RealName.Contains(request.KeyWord) || m.Phone.Contains(request.KeyWord));
          
            int total = await search.CountAsync();//异步查询记录总数
             //排序分页
            var list = search.OrderByDescending(m => m.UpdateTime).Skip((request.PageIndex-1) * request.PageSize).Take(request.PageSize).ToList();

            List<QueryListManagerResponse> result = new List<QueryListManagerResponse>();
            foreach (var item in list)
            {
                QueryListManagerResponse m = new QueryListManagerResponse();
                m.Id = item.Id;
                m.LoginCode = item.LoginCode;
                m.OwnerId = request.TenantId;
                m.Phone = item.Phone;
                m.Photourl = item.PhotoUrl;
                m.RealName = item.RealName;
                m.CreateTime = item.CreateTime;
                m.UpdateTime = item.UpdateTime;
                m.CreatorId = item.CreatorId;
                m.UpdatorId = item.UpdatorId;
                m.DeleteTag = item.DeleteTag;
                m.Wxopenid = item.WxOpenid;
                m.roleid = item.RoleId;
                result.Add(m);
            }     
            return (total,result);
        
        }      
    }
}
