package com.elab.mq.rocket.msg;

import com.elab.mq.rocket.msg.entity.RmqConsumerEntity;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

/**
 * 数据库存储
 *
 * @author ： liukx
 * @time ： 2019/9/16 - 11:21
 */
public class MysqlDataStore implements ConsumerDataStore {

    @Autowired
    private IRmqConsumerDao consumerDao;

    @Override
    public boolean add(RmqConsumerEntity keyModel) throws Exception {
        return consumerDao.insert(keyModel) > 0;
    }

    @Override
    public RmqConsumerEntity getMsgKey(String group, String key) throws Exception {
        RmqConsumerEntity entity = new RmqConsumerEntity();
        entity.setConsuemrGroup(group);
        entity.setMsgKey(key);
        List<RmqConsumerEntity> rmqConsumerEntities = consumerDao.selectByList(entity);
        if (rmqConsumerEntities.size() > 0) {
            return rmqConsumerEntities.get(0);
        }
        return null;
    }

    @Override
    public boolean update(RmqConsumerEntity entity) throws Exception {
        return consumerDao.updateById(entity) > 0;
    }

    @Override
    public RmqConsumerEntity getMsgId(String group, String msgId) throws Exception {
        RmqConsumerEntity entity = new RmqConsumerEntity();
        entity.setConsuemrGroup(group);
        entity.setMsgId(msgId);
        List<RmqConsumerEntity> rmqConsumerEntities = consumerDao.selectByList(entity);


        if (rmqConsumerEntities.size() > 0) {
            return rmqConsumerEntities.get(0);
        }

        return null;
    }
}
