package backstage_service

import (
	"errors"
	"github.com/gogf/gf/encoding/gjson"
	"github.com/gogf/gf/frame/g"
	"github.com/gogf/gf/net/ghttp"
	"github.com/gogf/gf/util/gconv"
	"github.com/gogf/gf/util/guid"
	"youngmini_server/app/model/backstage_model"
	"youngmini_server/app/utils"
)

// Token  系统用户token操作类
var Token = TokenService{}

type TokenService struct {
}

// TokenCreate 创建用户token
func (s *TokenService) TokenCreate(sysUserData *backstage_model.SysUsersServiceSignReq, tokenDefine interface{}, r *ghttp.Request) (interface{}, error) {
	if sysUserData == nil {
		return nil, errors.New("用户信息不存在")
	}
	var token interface{}
	if tokenDefine != nil {
		token = tokenDefine
	} else {
		//tokenString := "young_user:" + gconv.String(sysUserData.User)
		//md5String, err := gmd5.EncryptString(tokenString)
		//if err != nil {
		//	return nil, err
		//}
		//token = md5String
		token = guid.S()
	}
	//配置缓存,sys_user
	// cache := utils.Cache.New()
	//缓存服务
	//if err := cache.Set(cacheKey, sysUserData, 2*time.Hour); err != nil {
	//	return nil, err
	//}
	//_, err := cache.Get(cacheKey)
	//if err != nil {
	//	return nil, err
	//}

	// 生成唯一token
	cacheKey := "sys_user::" + gconv.String(token)
	_, err := g.Redis().Do("SET", cacheKey, sysUserData)
	if err != nil {
		return nil, err
	}
	// 设置延时
	_, err = g.Redis().Do("EXPIRE", cacheKey, 86400)
	if err != nil {
		return nil, err
	}
	return token, nil
}

// TokenGetData 通过token获取用户数据
func (s *TokenService) TokenGetData(token string, r *ghttp.Request) (*backstage_model.User, error) {
	//缓存服务
	cacheKey := "sys_user::" + gconv.String(token)
	userDataJsonStr, err := g.Redis().DoVar("GET", cacheKey)
	if err != nil {
		return nil, errors.New("用户登录信息已过期")
	}
	if userDataJsonStr == nil {
		return nil, errors.New("用户登录信息已过期")
	}
	userDataJson, err := gjson.DecodeToJson(userDataJsonStr)
	if err != nil {
		return nil, err
	}
	userDataMapStruct := new(backstage_model.User)
	if err := userDataJson.Struct(userDataMapStruct); err != nil {
		return nil, err
	}
	if userDataMapStruct.User != "" {
		if err != nil {
			panic(err)
		}
	}
	return userDataMapStruct, nil
}

// TokenDelete 删除清除token缓存
func (s *TokenService) TokenDelete(token string) (interface{}, error) {
	//缓存服务
	cache := utils.Cache.New()
	cacheKey := "young_user:" + gconv.String(token)
	result, err := cache.Remove(cacheKey)
	if err != nil {
		return nil, err
	}
	return result, nil
}

// AuthorizationGetUserData 通过分析请求中的head值获取当前请求用户数据
func (s *TokenService) AuthorizationGetUserData(r *ghttp.Request) (*backstage_model.User, error) {
	// var authorizationArr []string
	//过滤分析用户token值
	authorization := r.Header.Get("Authorization")
	//if authorization != "" {
	//	authorizationArr = gstr.Explode(" ", authorization)
	//}
	//if len(authorizationArr) < 2 {
	//	return nil, errors.New("授权凭证不存在")
	//}
	if authorization == "" {
		return nil, errors.New("授权凭证不存在")
	}
	//userToken := authorizationArr[1]
	userData, err := s.TokenGetData(authorization, r)
	if err != nil {
		return nil, err
	}
	if userData == nil {
		return nil, errors.New("用户不存在")
	}
	return userData, nil
}
