package com.izouma.nineth.listener;

import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.izouma.nineth.config.EventNames;
import com.izouma.nineth.service.AdapayMerchantService;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.rocketmq.spring.annotation.ConsumeMode;
import org.apache.rocketmq.spring.annotation.MessageModel;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.springframework.stereotype.Service;

@Service
@Slf4j
@AllArgsConstructor
@RocketMQMessageListener(
        messageModel = MessageModel.BROADCASTING,
        consumerGroup = "${general.broadcast-event-group}",
        topic = "${general.broadcast-event-topic}",
        consumeMode = ConsumeMode.CONCURRENTLY)
public class BroadcastEventListener implements RocketMQListener<JSONObject> {
    private AdapayMerchantService adapayMerchantService;

    @Override
    public void onMessage(JSONObject message) {
        log.info("接收到广播事件 {}", JSONObject.toJSONString(message, SerializerFeature.PrettyFormat));
        String name = message.getString("name");
        if (name != null) {
            switch (name) {
                case EventNames.SWITCH_ACCOUNT:
                    try {
                        Long id = message.getLong("data");
                        adapayMerchantService.select(id);
                    } catch (Exception e) {
                        log.error("event error", e);
                    }
                    break;
            }
        }
    }
}
