package service

import (
	"encoding/json"
	"errors"
	
	"tower_api/dao"
)

// NotifyService 消息通知
type NotifyService struct {
}

func NewNotifyService() *NotifyService {
	return &NotifyService{}
}

// OrderPaid 订单支付成功后写入有序集合中
func (n *NotifyService) OrderPaid(orderSn string, playerId int) error {
	//1 参数基础校验
	if playerId <= 0 {
		return errors.New("玩家ID信息不正确")
	}
	if orderSn == "" {
		return errors.New("订单号信息不正确")
	}
	//2 获取玩家所在区服的tcpId
	tcpId, err := n.GetPlayerRealTcpId(playerId)
	if err != nil {
		return err
	}
	//3 支付完成的订单写入定时任务中
	content := TimerData{
		PlayerId: playerId,
		Event:    "order",
		Body:     orderSn,
	}
	cByte, _ := json.Marshal(&content) //json任务内容
	return dao.AddZSetMsg(tcpId, string(cByte))
}

type TimerData struct {
	PlayerId int    `json:"pid"`
	Event    string `json:"event"`
	Body     string `json:"body"`
}
