
using System;
using System.Collections.Generic;
using System.Text;
using DFS.Core.Mvc.Jwt.Impl;
using DFS.Core.Mvc.Jwt.Model;
using DFS.Infrastructure.Redis;
using Newtonsoft.Json;


namespace DFS.Core.Mvc.Jwt
{
    public class JwtBuildHelp
    {
        public JwtBuildHelp()
        {

        }

        /// <summary>
        /// build jwt 
        /// </summary>
        /// <param name="model"></param>
        /// <param name="auhUser"></param>
        /// <returns></returns>
        /// <exception cref="Exception"></exception>
        public string BuildJwt(TokenModel model, IAuhUser auhUser)
        {
            var tokenManage = new JWTManager();
            var tokenGuid = Guid.NewGuid().ToString("N");
            model.ExpireTime = DateTime.Now.AddHours(2);
            model.Token = tokenGuid;
            var token = tokenManage.BuildToken(model, JwtConfig.JwtTokenKey);
            if (auhUser != null)
            {
                var authClient = CoreRedisHelper.GetRedisHelper(JwtConfig.RedisCon);
                if (authClient == null)
                {
                    throw new Exception($"授权配置redis不能为空,配置节点:{JwtConfig.RedisCon}");
                }
                var dicValue = auhUser.ToMapDictionary();
                authClient.Set(tokenGuid, JsonConvert.SerializeObject(dicValue), 6250);
            }
            return token;
        }
    }
}
