50 lines
938 B
Go
50 lines
938 B
Go
package service
|
|
|
|
import (
|
|
"context"
|
|
"time"
|
|
"vote-video-api/api/dao"
|
|
"vote-video-api/global"
|
|
)
|
|
|
|
type SystemTimeService struct {
|
|
}
|
|
|
|
// CheckVoteValidStatus 检测投票有效期
|
|
// bool true:未结束 false:已结束
|
|
func (r *SystemTimeService) CheckVoteValidStatus() bool {
|
|
redisKey := "VoteSystemTime"
|
|
res, _ := global.Redis.Get(context.Background(), redisKey).Result()
|
|
if res == "" {
|
|
// 获取配置-时间
|
|
systemTimeDao := dao.SystemTimeDao{}
|
|
systemTime, err := systemTimeDao.GetSystemTimeById(1)
|
|
if err != nil {
|
|
return false
|
|
}
|
|
|
|
if systemTime.EndTime == nil {
|
|
return false
|
|
}
|
|
|
|
// 结束时间
|
|
endTime := time.Time(*systemTime.EndTime)
|
|
|
|
// 当前时间
|
|
now := time.Now()
|
|
|
|
duration := endTime.Sub(now)
|
|
if duration < 0 {
|
|
return false
|
|
}
|
|
|
|
// 添加缓存
|
|
_, err = global.Redis.Set(context.Background(), redisKey, "1", duration).Result()
|
|
if err != nil {
|
|
return false
|
|
}
|
|
}
|
|
|
|
return true
|
|
}
|