package com.copy.trade.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.copy.trade.entity.po.KafkaMessage;
import com.copy.trade.dao.KafkaMessageMapper;
import com.copy.trade.service.IKafkaMessageService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.Synchronized;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * kafka消息发送 服务实现类
 * </p>
 *
 * @author wz
 * @since 2022-11-22
 */
@Service
public class KafkaMessageServiceImpl extends ServiceImpl<KafkaMessageMapper, KafkaMessage> implements IKafkaMessageService {

    @Autowired
    private KafkaTemplate kafkaTemplate;

    @Override
    public synchronized void messageSend() {
        QueryWrapper<KafkaMessage> kafkaMessageQueryWrapper = new QueryWrapper<>();
        kafkaMessageQueryWrapper.eq("status",1);
        List<KafkaMessage> list = list(kafkaMessageQueryWrapper);
        list.stream().forEach(kafkaMessage -> {
            kafkaTemplate.send(kafkaMessage.getTopic(),kafkaMessage.getMessage());
            kafkaMessage.setStatus(2);
            updateById(kafkaMessage);
        });
    }
}
