package service

import (
	"gin-vue-admin/global"
	"time"
)

func GetWxToken() string {
	cTime := time.Now().Add(-time.Minute * 119)
	var token string

	sqlStr := "SELECT token " +
		" FROM wx_token" +
		" where create_time>? and status=1"

	global.GVA_DB.Raw(sqlStr, cTime).Find(&token)
	if global.GVA_DB.Error != nil {
		return ""
	}
	return token
}

func CreateWxToken(token string) error {
	sqlStr := "INSERT INTO wx_token(token) VALUE(?)"
	global.GVA_DB.Exec(sqlStr, token)
	return global.GVA_DB.Error
}

func DelWxToken() error {
	cTime := time.Now().Add(-time.Minute * 119)
	sqlStr := "UPDATE wx_token SET status = 2 where create_time>?"
	global.GVA_DB.Exec(sqlStr, cTime)
	return global.GVA_DB.Error
}
