package mq

import (
	"encoding/json"
	"mongo.games.com/game/dbproxy/svc"
	"mongo.games.com/game/model"
	"mongo.games.com/game/mq"
	"mongo.games.com/goserver/core/broker"
	"mongo.games.com/goserver/core/broker/rabbitmq"
)

func init() {
	mq.RegisterSubscriber(model.FriendRecordLogCollName, func(e broker.Event) (err error) {
		msg := e.Message()
		if msg != nil {
			defer func() {
				if err != nil {
					mq.BackUp(e, err)
				}

				e.Ack()

				recover()
			}()

			var log model.FriendRecord
			err = json.Unmarshal(msg.Body, &log)
			if err != nil {
				return
			}
			err = svc.InsertFriendRecordLog(log)
			return
		}
		return nil
	}, broker.Queue(model.FriendRecordLogCollName), broker.DisableAutoAck(), rabbitmq.DurableQueue())
}
