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

namespace Hncore.Pass.Manage.Domain
{
    /// <summary>
    /// 物业管理员表
    /// </summary>
    public partial class Manager : EntityWithTime<int>, ITenant
    {
        /// <summary>
        /// 所属物业ID
        /// <summary>
        public int TenantId  { get; set; }

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

        /// <summary>
        /// 创建人ID
        /// <summary>
        public int CreatorId { get; set; }

        /// <summary>
        /// 管理员登录名[16
        /// </summary>
        public string LoginCode { get; set; }

        /// <summary>
        /// 登录密码[20]
        /// </summary>
        public string Password  { get; set; }

        /// <summary>
        /// 管理员角色
        /// </summary>
        public int RoleId { get; set; }

        /// <summary>
        /// 状态
        /// </summary>
        public int State { get; set; }

        /// <summary>
        /// 头像地址[30
        /// </summary>
        public string PhotoUrl { get; set; }

        /// <summary>
        /// 微信openid[50]
        /// </summary>
        public string WxOpenid { get; set; }

        /// <summary>
        /// 微信昵称
        /// </summary>
        public string WxNickName { get; set; }

        /// <summary>
        /// 微信头像
        /// </summary>
        public string WxImage { get; set; }


        /// <summary>
        /// 注册来源
        /// </summary>
        public int Source { get; set; }

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

        /// <summary>
        /// 管理员姓名
        /// </summary>
        public string RealName { get; set; }
        /// <summary>
        /// 电子邮箱
        /// </summary>
        public string Email  { get; set; }

        /// <summary>
        /// 是否主管理员权限
        /// </summary>
        public bool IsRoot { get; set; }

        public int SystemId { get; set; }

        private static async Task EditCheckAsync(IQueryable<Manager> queryable, EditManagerRequest request, int id = 0)
        {

            Expression<Func<Manager, bool>> condition = t => t.LoginCode == request.LoginCode || t.Phone== request.Phone;

            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<Manager> Create(EditManagerRequest request,
            IQueryable<Manager> queryable)
        {
            await EditCheckAsync(queryable, request, 0);

            var Manager = new Manager()
            {
                TenantId = request.TenantId,
                CreatorId = request.OperaterId,
                UpdatorId = request.OperaterId,
                CreateTime = DateTime.Now,
                UpdateTime = DateTime.Now,
                LoginCode = request.LoginCode,
                Password = SecurityHelper.HashPassword(request.Password),
                PhotoUrl = request.photourl,
                RealName = request.RealName,
              
                State = 1,
                Phone = request.Phone,
                RoleId = -100,  ///新规则的权限用-100标识
                SystemId = 100,

            };

            return Manager;
        }

        /// <summary>
        /// 编辑管理员
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        public async Task<Manager> Edit(EditManagerRequest request, IQueryable<Manager> queryable)
        {

           await EditCheckAsync(queryable, request, request.Id);
            TenantId = request.TenantId;
            UpdatorId = request.OperaterId;
            UpdateTime = DateTime.Now;
            LoginCode = request.LoginCode;
            if (!string.IsNullOrEmpty(request.Password))
            { Password = SecurityHelper.HashPassword(request.Password); }          
            PhotoUrl = request.photourl;
            RealName = request.RealName;          
            Phone = request.Phone;
            RoleId = request.roleid;
            return this;
        }


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

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