using Furion.DependencyInjection;
using Nirvana.Common;
using SqlSugar;
using System;
using System.Threading.Tasks;
using Waste.Domain;

namespace Waste.Application
{
    public class AccountService : IAccountService, ITransient
    {
        private readonly ISqlSugarRepository<W_Account> repository;
        private readonly SqlSugarClient dbClient;
        public AccountService(ISqlSugarRepository<W_Account> sqlSugarRepository)
        {
            repository = sqlSugarRepository;
            dbClient = repository.Context;
        }
        public async Task<ResultInfo> LoginAsync(LoginModel model)
        {
            //如果是系统维护人员
            if (OperatorProvider.Provider.IsSupperAdmin(model.username, model.pwd))
            {
                OperatorProvider.Provider.AddCurrent(new OperatorModel
                {
                    AccountType = (int)AccountType.platform,
                    UserId = Guid.Empty,
                    IsSuper = true,
                    BusinessId = Guid.Empty,
                    LoginIPAddress = Net.Ip,
                    LoginTime = DateTime.Now,
                    RealName = "系统维护人员",
                    RoleId = Guid.Empty,
                    BusinessCode=""
                });
                return new ResultInfo { code = ResultState.SUCCESS, message = "登录成功" };
            }
            var userdata = await dbClient.Queryable<W_Account>().FirstAsync(x => x.UserName == model.username);
            if (userdata == null)
            {
                return new ResultInfo { code = ResultState.FAIL, message = "账户未找到" };
            }
            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 = "密码不正确" };
            }
            await dbClient.Updateable<W_Account>().SetColumns(x => new W_Account
            {
                LastVisitIP = Net.Ip,
                LastVisitTime = DateTime.Now
            }).Where(x => x.Id == userdata.Id).ExecuteCommandAsync();
            string businesscode = "";
            if(userdata.BusinessId != Guid.Empty)
            {
                var buss = await dbClient.Queryable<W_Business>().FirstAsync(x => x.Id == userdata.BusinessId);
                businesscode = buss != null ? buss.Code : "";
            }
            //记录登录信息到cookie和session
            OperatorModel logindata = new OperatorModel
            {
                UserId = userdata.Id,
                RoleId = userdata.RoleId,
                AccountType = userdata.AccountType,
                BusinessId = userdata.BusinessId,
                IsSuper = false,
                LoginIPAddress = Net.Ip,
                LoginTime = DateTime.Now,
                RealName = userdata.RealName,
                BusinessCode= businesscode
            };
            OperatorProvider.Provider.AddCurrent(logindata);
            return new ResultInfo { code = ResultState.SUCCESS, message = "登录成功"};
        }
    }
}
