handler.go 1.6 KB
package goqueue

import (
	rawjson "encoding/json"
	"fmt"
	"github.com/linmadan/egglib-go/utils/json"
	"gitlab.fjmaimaimai.com/allied-creation/allied-creation-user/pkg/application/blockChain/command"
	"gitlab.fjmaimaimai.com/allied-creation/allied-creation-user/pkg/application/blockChain/service"
	"gitlab.fjmaimaimai.com/allied-creation/allied-creation-user/pkg/log"
)

func UpToChainHandler(k, v string) error {
	blockChainService := service.NewBlockChainService(nil)
	upChainCommand := &command.UpChainCommand{}
	err := json.UnmarshalFromString(v, upChainCommand)
	if err != nil {
		log.Logger.Error(err.Error(), map[string]interface{}{"info": "UpToChainHandler 解析json错误", "data": v})
		return err
	}
	_, err = blockChainService.UpChain(upChainCommand)
	return err
}

func FilterTopicHandler(topic string, handler func(string, string) error) func(string, string) error {
	return func(k string, v string) error {
		raw := &RawData{}
		err := json.UnmarshalFromString(v, raw)
		if err != nil {
			log.Logger.Error("【GoQueue】消息解析错误:"+err.Error(), map[string]interface{}{"info": "FilterTopicHandler 解析json错误", "data": v})
			return err
		}
		log.Logger.Debug(fmt.Sprintf("【GoQueue】收到消息 Topic:%s", raw.Topic), map[string]interface{}{"data": v})
		if raw.Topic != topic {
			log.Logger.Debug(fmt.Sprintf("【GoQueue】 topic not equal get:%v want:%v", raw.Topic, topic), map[string]interface{}{"data": v})
			return nil
		}
		return handler(k, string(raw.Data))
	}
}

type RawData struct {
	Topic string             `json:"topic"`
	Data  rawjson.RawMessage `json:"data"`
}