package gm_services

import (
	"encoding/json"
	"github.com/nats-io/nats.go"
	"github.com/sirupsen/logrus"
)

func HandleGMSubscribe(c *nats.Conn) {
	_, err := c.Subscribe("gm", func(message *nats.Msg) {

		// 消息回复
		reply := func(res interface{}) {
			marshal, err := json.Marshal(&res)
			if err != nil {
				logrus.Warnf("gm reply Marshal error. %v", err)
				return
			}
			if err = c.Publish(message.Reply, marshal); err != nil {
				logrus.Warnf("gm reply error. %v", err)
				return
			}
		}

		switch message.Header.Get("method") {
		case "RedeemGoodsIs": //兑换券码
			/*var request *msg.RedeemGoodsIs
			if err := json.Unmarshal(message.Data, &request); err != nil {
				logrus.Infof("RedeemGoodsIs json.Unmarshal error. %v", err)
				return
			}

			logrus.WithField("from", "RedeemGoodsIs").Infof("request:%+v", request)

			exchange, err := service.Cdk.Exchange(context.TODO(), request)
			if err != nil {
				logrus.WithField("from", "handlerRedeemGoodsIs Exchange err ").Warnln(err)
			}
			reply(&exchange)*/
			reply(nil)
		default:
			logrus.Warnf("gm messages are not bound to handle routes. method:%+v, data:%+v", message.Header.Get("method"), string(message.Data))
			return
		}

	})

	if err != nil {
		logrus.Fatalf("gm nsClient.Subscribe error. %v", err)
	}

}
