package cn.axzo.msg.center.message.service.card.broadcast;

import cn.axzo.basics.common.BeanMapper;
import cn.axzo.msg.center.api.mq.CardInfo;
import cn.axzo.msg.center.api.mq.CardPresetButtonPressedMessage;
import cn.axzo.msg.center.dal.CardDao;
import cn.axzo.msg.center.domain.entity.Card;
import cn.axzo.msg.center.mq.MqMessageRecord;
import cn.axzo.msg.center.mq.MqProducer;
import cn.axzo.msg.center.service.enums.MqMessageType;
import cn.axzo.msg.center.service.enums.PresetButtonType;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * @author yanglin
 */
@Component
@RequiredArgsConstructor
public class CardBroadcaster {

    private final CardDao cardDao;
    private final MqProducer mqProducer;

    public void firePresetButtonPressed(
            List<Card> cards, Long operatorId, PresetButtonType presetButtonType) {
        for (Card card : cardDao.reloadCards(cards)) {
            CardPresetButtonPressedMessage message = new CardPresetButtonPressedMessage();
            message.setPresetButtonType(presetButtonType);
            message.setCardInfo(BeanMapper.copyBean(card, CardInfo.class));
            mqProducer.send(MqMessageRecord
                    .builder(MqMessageType.CARD_PRESET_BUTTON_PRESSED, message)
                    .messageKey(card.getId())
                    .operatorId(operatorId)
                    .shardingKey(card.getTemplateCode())
                    .build());
        }
    }

}