package service

import (
	"encoding/json"
	"fengkuangGameServerJob/dao"
	"time"
)

type NoticeService struct {
}

func NewNoticeService() *NotifyService {
	return &NotifyService{}
}

func (n *NotifyService) GetNewest() *NoticeShow {
	show := NoticeShow{}
	//1 从缓存中读取
	cache := dao.GetSingleNoticeFromCache()
	if cache != "" {
		json.Unmarshal([]byte(cache), &show)
		if show.Title != "" && show.Content != "" {
			return &show
		}
	}
	//2 没有则读库
	curUnix := time.Now().Unix()
	notice := dao.GetSingleNotice(curUnix)
	if notice == nil || notice.Title == "" || notice.Content == "" {
		return &show
	}
	//3 读取到数据写缓存
	show.Title = notice.Title
	show.Content = notice.Content
	showByte, err := json.Marshal(&show)
	if err == nil {
		dao.CacheSingleNotice(string(showByte))
	}
	return &show
}

type NoticeShow struct {
	Title, Content string
}
