package com.jay.monitor.data.server.store.mysql;

import com.cat.file.message.internal.DefaultMessageTree;
import com.cat.file.message.internal.MessageTree;
import com.jay.monitor.data.core.enums.MsgTypeEnums;
import com.jay.monitor.data.core.model.serializable.MQDataDTO;
import com.jay.monitor.data.server.daos.JayMonitorMqMapperExt;
import com.jay.monitor.data.server.daos.PartitionOperation;
import com.jay.monitor.data.server.enums.StoreType;
import com.jay.monitor.data.server.models.entity.JayMonitorMq;
import com.jay.monitor.data.server.store.BigTextStoreProcess;
import com.jay.monitor.data.server.utils.JayDataUtils;
import com.jay.monitor.data.server.utils.PrefixUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.atomic.LongAdder;

/**
 * @Module 数据存储器
 * @Description Mysql的MQ存储实现
 * @Author liukaixiong
 * @Date 2020/10/29 13:36
 */
public class MysqlMQStoreProcess extends BigTextStoreProcess<MQDataDTO, JayMonitorMq> {

    private Logger logger = LoggerFactory.getLogger(getClass());

    private LongAdder counter = new LongAdder();

    /**
     * 队列的大小
     */
    public int defaultSize = 5000;

    @Autowired
    @Qualifier("jayMonitorMqMapperExt")
    private JayMonitorMqMapperExt mqMapper;

    @Override
    public StoreType storeType() {
        return StoreType.MYSQL;
    }

    @Override
    public MsgTypeEnums msgTypeEnums() {
        return MsgTypeEnums.MQ;
    }

    @Override
    public String tableName() {
        return "jay_monitor_mq";
    }

    @Override
    protected PartitionOperation<JayMonitorMq> getMapper() {
        return mqMapper;
    }

    /**
     * 加速消费,单条记录插入会造成大量的并发写
     *
     * @param dataList
     */
    @Override
    public List<JayMonitorMq> speedUpConsumerList(List<MQDataDTO> dataList) {
        List<JayMonitorMq> monitorList = new ArrayList<>();

        for (int i = 0; i < dataList.size(); i++) {
            MQDataDTO request = dataList.get(i);
            JayMonitorMq mqData = builderJayMonitor(request);
            monitorList.add(mqData);
        }

        mqMapper.batchPartitionInsert(getPartitionId(), monitorList);

        return monitorList;
    }

    @Override
    public String searchContentById(String date, String id) throws Exception {
        JayMonitorMq jayMonitorMq = mqMapper.selectPartitionById(date, Integer.valueOf(id));
        MessageTree messageTree =
            new DefaultMessageTree(jayMonitorMq.getApplicationName(), jayMonitorMq.getApplicationIp(),
                jayMonitorMq.getId(), jayMonitorMq.getCreatedTime(), null);
        String messageId = messageTree.getMessageId();
        return this.messageManagerProcess.getMessage(messageId);
    }

    @Override
    public String searchErrorById(String date, String id) throws Exception {
        JayMonitorMq jayMonitorMq = mqMapper.selectPartitionById(date, Integer.valueOf(id));
        MessageTree messageTree =
            new DefaultMessageTree(PrefixUtils.prefixErrorMessage(jayMonitorMq.getApplicationName()),
                jayMonitorMq.getApplicationIp(), jayMonitorMq.getId(), jayMonitorMq.getCreatedTime(), null);
        String messageId = messageTree.getMessageId();
        return this.messageManagerProcess.getMessage(messageId);
    }

    @Override
    public List<JayMonitorMq> searchDataByList(String date, Integer pageSize, JayMonitorMq entity) throws Exception {
        List<JayMonitorMq> resultList = mqMapper.selectPartitionByList(date, pageSize, entity);
        return resultList;
    }

    @Override
    public JayMonitorMq builderJayMonitor(MQDataDTO request) {
        return JayDataUtils.builderMonitorMQ(request);
    }

}
