using Long.Common.Basedata;
using Long.Dapper;

namespace Long.DAO.Basedata
{
    /// <summary>
    /// 系统用户-数据库
    /// </summary>
    public class UserDatabase : Database<User>, IUserDatabase
    {
        /// <summary>
        /// 通过登录名获取一条系统用户数据
        /// </summary>
        /// <param name="userName">登录名</param>
        /// <returns>用户</returns>
        public async Task<User> GetUserAsync(string userName)
        {
            string sql = "SELECT Id, Code, IsActive, UserName, Password, Email, Name, Gender, Age, `Language`, City, Province, Country, AvatarUrl, Phone, PromotionCode, ParentPromotionCode, PromotionLevel, CreatedBy, CreatedOn, LastModifiedBy, LastModifiedOn FROM user WHERE UserName = @UserName OR Email = @Email OR Phone = @Phone;";
            return await GetFirstAsync(sql, new { 
                UserName = userName,
                Email = userName,
                Phone = userName
            });
        }

        /// <summary>
        /// 通过登录名获取一条系统用户数据
        /// </summary>
        /// <param name="userName">登录名</param>
        /// <returns>用户</returns>
        public async Task<User> GetByUserNameAsync(string userName)
        {
            string sql = "SELECT Id, Code, IsActive, UserName, Password, Email, Name, Gender, Age, `Language`, City, Province, Country, AvatarUrl, Phone, PromotionCode, ParentPromotionCode, PromotionLevel, CreatedBy, CreatedOn, LastModifiedBy, LastModifiedOn FROM user WHERE UserName = @UserName;";
            return await GetFirstAsync(sql, new { UserName = userName });
        }

        /// <summary>
        /// 通过推广码获取一条系统用户数据
        /// </summary>
        /// <param name="promotionCode">推广码</param>
        /// <returns>用户</returns>
        public async Task<User> GetByPromotionCodeAsync(string promotionCode)
        {
            string sql = "SELECT Id, Code, IsActive, UserName, Password, Email, Name, Gender, Age, `Language`, City, Province, Country, AvatarUrl, Phone, PromotionCode, ParentPromotionCode, PromotionLevel, CreatedBy, CreatedOn, LastModifiedBy, LastModifiedOn FROM user WHERE PromotionCode = @PromotionCode;";
            return await GetFirstAsync(sql, new { PromotionCode = promotionCode });
        }

        /// <summary>
        /// 判断推广码是否重复
        /// </summary>
        /// <param name="promotionCode">推广码</param>
        /// <returns>重复标记</returns>
        public async Task<bool> PromotionCodeIsRepeatAsync(string promotionCode)
        {
            string sql = "SELECT Count(Id) FROM user WHERE PromotionCode = @PromotionCode;";
            var count = await GetFirstAsync<int>(sql, new { PromotionCode = promotionCode });
            return count > 0 ? true : false;
        }

        /// <summary>
        /// 更新密码
        /// </summary>
        /// <param name="id">内码</param>
        /// <param name="password">新密码</param>
        /// <returns></returns>
        public async Task<int> UpdatePassword(long id, string password)
        {
            string sql = "UPDATE User SET Password = @Password WHERE Id = @Id;";
            return await UpdateAsync(sql, new { Id = id, Password = password });
        }

        /// <summary>
        /// 判断用户是否存在
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public async Task<bool> HasUser(long id)
        {
            return await CountAsync(id) > 0 ? true : false;
        }
    }
}
