vote-admin-api/api/service/SystemTime.go
2024-08-30 11:19:25 +08:00

50 lines
938 B
Go

package service
import (
"context"
"time"
"vote-admin-api/api/dao"
"vote-admin-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
}