using System;
using System.Linq;
using System.Linq.Expressions;
using System.Threading.Tasks;
using Hncore.Infrastructure.Data;
using Hncore.Infrastructure.DDD;
using Hncore.Infrastructure.EntitiesExtension;
using Hncore.Infrastructure.Extension;
using Hncore.Infrastructure.WebApi;
using Hncore.Pass.Manage.Request;
using Microsoft.EntityFrameworkCore;

namespace Hncore.Pass.Manage.Domain  
{
    public class  ManagerTag : AggregateRoot<int> 

    {

        /// <summary>
        /// 用户数据库ID
        /// <summary>
        public override 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>
        /// 创建人ID
        /// <summary>
        public int CreatorId { get; set; }

        /// <summary>
        /// 更新人ID
        /// <summary>
        public int UpdatorId { get; set; }

        /// <summary>
        /// 软删除标记，0.代表正常，1.代表已删除
        /// <summary>
        public   int DeleteTag { get; set; }

        /// <summary>
        /// 管理员id
        /// <summary>
        public int ManagerId { get; set; }  

        /// <summary>
        /// 管理员标签 
        /// <summary>
        public  string  Tag  { get; set; }
        /// <summary>
        /// 管理员标签类型
        /// <summary>
        public int  TagType  { get; set; }

        private static async Task EditCheckAsync(IQueryable<ManagerTag> queryable, string tag ,int managerId, int id=0 )
        {
         
            if(string.IsNullOrWhiteSpace(tag))
            {
                BusinessException.Throw("管理员类型名称不能为空");
            }
            Expression<Func<ManagerTag, bool>> condition = t => t.Tag == tag && t.ManagerId== managerId;
            if (id > 0)
            {
                condition = condition.And(t => t.Id != id);
            }
            if (await queryable.AnyAsync(condition))
            {
                BusinessException.Throw("管理员类型重名");
            }
        }

        /// <summary>
        /// 创建管理员标签
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        public static async Task<ManagerTag> Create(RequestBase<EditManagerTagRequest> request,
            IQueryable<ManagerTag> queryable)
        {
             await EditCheckAsync(queryable, request.Data.Tag, request.Data.ManagerId);

             await CheckCountAsync(queryable, request.Data.ManagerId);

            var ManagerTag = new ManagerTag()
            {
                Tag = request.Data.Tag,
                ManagerId=request.Data.ManagerId,
                OwnerId = request.TenantId,
                CreatorId = request.OperaterId,
                UpdatorId = request.OperaterId,
                CreateTime = DateTime.Now,
                UpdateTime = DateTime.Now
            };

            return ManagerTag ;
        }

        private static async Task CheckCountAsync(IQueryable<ManagerTag> queryable, int ManagerId )
        {

            Expression<Func<ManagerTag, bool>> condition = t => t.ManagerId == ManagerId;
            int  totalcount=  queryable.Count(condition);
            if (totalcount >= 5)
            {
                BusinessException.Throw("管理员最多可添加5个类型");
            }
        }


        /// <summary>
        /// 编辑管理员标签
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        public async Task<ManagerTag> Edit(RequestBase<EditManagerTagRequest> request, IQueryable<ManagerTag> queryable)
        {
            await EditCheckAsync(queryable, request.Data.Tag, request.Data.Id);

            Tag = request.Data.Tag;
            UpdatorId = request.OperaterId;
            UpdateTime = DateTime.Now;

            return this;
        }

        /// <summary>
        /// 删除管理员标签
        /// </summary>
        /// <param name="updatorId"></param>
        /// <returns></returns>
        public ManagerTag Delete(int updatorId)
        {

            DeleteTag = 1;
            UpdatorId = updatorId;
            return this;
        }
 

    }

}
