using Nirvana.Common;
using Nirvana.Common.ApiBase;
using Nirvana.Data;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using YBDevice.Entity;

namespace YBDevice.Service.DBServices
{
    /// <summary>
    /// 账户管理
    /// </summary>
    public partial class AccountApp : Repository<YB_Account>
    {
        /// <summary>
        /// 账户登录
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public async Task<ResultInfo> LoginAsync(LoginModel model)
        {
            if (string.IsNullOrEmpty(model.username))
            {
                return new ResultInfo(ResultState.FAIL, "请填写登录账户");
            }
            if (string.IsNullOrEmpty(model.pwd))
            {
                return new ResultInfo(ResultState.FAIL, "请输入密码");
            }
            using (var dbClient = ReadDbContext.GetInstance())
            {
                var userdata = await dbClient.Queryable<YB_Account>().FirstAsync(x => x.UserName == model.username);
                if (userdata == null)
                {
                    return new ResultInfo { code = ResultState.FAIL, message = "账户未找到", data = null };
                }
                if (userdata.Status != 1)
                {
                    return new ResultInfo { code = ResultState.FAIL, message = "账户已禁用" };
                }
                var password = Md5.md5(DESEncrypt.Encrypt(Md5.md5(model.pwd, 32).ToLower(), userdata.Secret).ToLower(), 32).ToLower();
                if (password != userdata.Password)
                {
                    return new ResultInfo { code = ResultState.FAIL, message = "密码不正确", data = null };
                }
                await dbClient.Updateable<YB_Account>().SetColumns(x => new YB_Account
                {
                    LastVisitIP = Net.Ip,
                    LastVisitTime = DateTime.Now
                }).Where(x => x.Id == userdata.Id).ExecuteCommandAsync();
                var buss = await dbClient.Queryable<YB_Business>().FirstAsync(x => x.Id == userdata.BusinessId);
                //记录登录信息到cookie和session
                OperatorModel logindata = new OperatorModel
                {
                    UserId = userdata.Id,
                    RoleId = userdata.RoleId,
                    AccountType = userdata.AccountType,
                    BusinessId = userdata.BusinessId,
                    BusinessCode = buss != null ? buss.Code : "",
                    IsSuper = false,
                    LoginIPAddress = Net.Ip,
                    LoginTime = DateTime.Now,
                    RealName = userdata.RealName
                };
                OperatorProvider.Provider.AddCurrent(logindata);
                return new ResultInfo { code = ResultState.SUCCESS, message = "登录成功", data = null };
            }
        }

        /// <summary>
        /// 重置密码
        /// </summary>
        /// <param name="id">用户ID</param>
        /// <param name="pwd">重置的密码</param>
        /// <returns></returns>
        public async Task<ResultInfo> RevicePasswordAsync(int id, string pwd)
        {
            using (var dbClient = ReadDbContext.GetInstance())
            {
                if (string.IsNullOrEmpty(pwd))
                {
                    pwd = "123456";
                }
                var account = await dbClient.Queryable<YB_Account>().FirstAsync(x => x.Id == id);
                account.Secret = Md5.md5(Common.CreateNo(), 16).ToLower();
                account.Password = Md5.md5(DESEncrypt.Encrypt(Md5.md5(pwd, 32).ToLower(), account.Secret).ToLower(), 32).ToLower();
                dbClient.Updateable<YB_Account>().SetColumns(x => new YB_Account()
                {
                    Secret = account.Secret,
                    Password = account.Password
                }).Where(x => x.Id == account.Id).ExecuteCommand();
            }
            return new ResultInfo { code = ResultState.SUCCESS, message = "重置密码成功", data = null };
        }

        /// <summary>
        /// 修改密码
        /// </summary>
        /// <param name="oldpwd"></param>
        /// <param name="newpwd"></param>
        /// <param name="repwd"></param>
        /// <returns></returns>
        public async Task<ResultInfo> ChangePwdAsync(string oldpwd, string newpwd, string repwd)
        {
            using (var dbClient = ReadDbContext.GetInstance())
            {
                var currentUser = OperatorProvider.Provider.GetCurrent();
                var data = await dbClient.Queryable<YB_Account>().FirstAsync(x => x.Id == currentUser.UserId);
                if (data == null)
                {
                    return new ResultInfo { code = ResultState.FAIL, message = "账户未找到" };
                }
                if (newpwd != repwd)
                {
                    return new ResultInfo { code = ResultState.FAIL, message = "两次输入密码不一致", data = null };
                }
                var password = Md5.md5(DESEncrypt.Encrypt(Md5.md5(oldpwd, 32).ToLower(), data.Secret).ToLower(), 32).ToLower();
                if (password != data.Password)
                {
                    return new ResultInfo { code = ResultState.FAIL, message = "旧密码输入错误", data = null };
                }
                var newpassword = Md5.md5(DESEncrypt.Encrypt(Md5.md5(newpwd, 32).ToLower(), data.Secret).ToLower(), 32).ToLower();
                await dbClient.Updateable<YB_Account>().SetColumns(x => new YB_Account
                {
                    Password = newpassword
                }).Where(x => x.Id == data.Id).ExecuteCommandAsync();
                return new ResultInfo { code = (int)ResultState.SUCCESS, message = "密码修改成功", data = null };
            }
        }

        /// <summary>
        /// 越权登录
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public async Task<ResultInfo> UserEnterAsync(int id)
        {
            using (var dbClient = ReadDbContext.GetInstance())
            {
                var account = await dbClient.Queryable<YB_Account>().FirstAsync(x => x.Id == id);
                if (account != null)
                {
                    var buss = await dbClient.Queryable<YB_Business>().FirstAsync(x => x.Id == account.BusinessId);
                    if (buss == null)
                    {
                        return new ResultInfo { code = (int)ResultState.FAIL, message = "账户未找到", data = null };
                    }
                    //记录登录信息到cookie和session
                    OperatorModel logindata = new OperatorModel
                    {
                        UserId = account.Id,
                        RoleId = account.RoleId,
                        AccountType = account.AccountType,
                        BusinessId = account.BusinessId,
                        BusinessCode = buss != null ? buss.Code : "",
                        IsSuper = false,
                        LoginIPAddress = Net.Ip,
                        LoginTime = DateTime.Now,
                        RealName = account.RealName
                    };
                    OperatorProvider.Provider.AddCurrent(logindata);
                    return new ResultInfo { code = ResultState.SUCCESS, message = "成功", data = null };
                }
                return new ResultInfo { code = ResultState.FAIL, message = "账户未找到" };
            }
        }
    }
}
