using Hncore.Infrastructure.DDD;
using Hncore.Infrastructure.WebApi;
using Hncore.Pass.Manage.Domain;
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;
using Hncore.Infrastructure.Common;
using Hncore.Infrastructure.Serializer;
using Microsoft.VisualStudio.TestPlatform.CoreUtilities.Helpers;
using Hncore.Pass.Manage.Repository;
using Hncore.Infrastructure.EF;

namespace Hncore.Pass.Manage.Service
{


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

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

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

        /// <summary>
        /// 获取管理员标签
        /// </summary>
        /// <param name="request">请求对象</param>
        /// <returns>响应列表对象</returns>
        public async Task<(int total, List<ManagerTagResponse> list)> GetTagList(ManagerTagRequest request)
        {
             
             Expression<Func<ManagerTag, bool>> exp = c =>(c.OwnerId==request.TenantId && c.DeleteTag == 0 && c.ManagerId == request.ManagerId && c.TagType==0)|| c.TagType==1;
            var search = m_DbContext.Set<ManagerTag>().GetQueryable().Where(exp);

            if (!EnvironmentVariableHelper.IsAspNetCoreProduction)
            {
                LogHelper.Debug("获取管理员标签",request.ToJson(true));
            }
            
            int total = await search.CountAsync();//异步查询记录总数
            var paged = await search.OrderByDescending(c=>c.TagType).ToListAsync();
            List<ManagerTagResponse> list = new List<ManagerTagResponse>();
            foreach (var item in paged)
            {
                ManagerTagResponse mtr  =new ManagerTagResponse() ;
                mtr.Id = item.Id;
                mtr.Tag = item.Tag;
                mtr.TagType = item.TagType;
                mtr.ManagerId = item.ManagerId;
                mtr.CreateTime = item.CreateTime;
                mtr.UpdateTime = item.UpdateTime;
                list.Add(mtr);
            }    
            return (total, list);
        }
        /// <summary>
        /// 获取管理员标签权限信息
        /// </summary>
        /// <param name="request">请求对象</param>
        /// <returns>响应列表对象</returns>
        public async Task<(int total, List<ManagerTagToPermissionResponse> list)> GetTagToPerssionList(ManagerTagToPerRequest request)
        {

            Expression<Func<ManagerTagToPermission, bool>> exp = c => c.ManagerTagId == request.ManagerTagId;

            var search = m_DbContext.Set<ManagerTagToPermission>().GetQueryable().Where(exp);                                       
            int total = await search.CountAsync();//异步查询记录总数
            var paged = await search.OrderByDescending(c => c.Id).ToListAsync();
            List<ManagerTagToPermissionResponse> list = new List<ManagerTagToPermissionResponse>();
            foreach (var item in paged)
            {
                ManagerTagToPermissionResponse mp   = new ManagerTagToPermissionResponse();
                mp.Id = item.Id;
                mp.ManagerTagId = item.ManagerTagId;
                mp.PermissionCode = item.PermissionCode;            
                mp.AllowView = item.AllowView;
                mp.AllowAdd = item.AllowAdd;
                mp.AllowEdit = item.AllowEdit;
                mp.AllowDel = item.AllowDel;
                mp.CreateTime = item.CreateTime;
                mp.UpdateTime = item.UpdateTime;
                list.Add(mp);
            }
            return (total, list);
        }

    }
}
