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

import com.cat.file.message.MessageManagerProcess;
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.URLDataDTO;
import com.jay.monitor.data.server.daos.JayMonitorUrlMapperExt;
import com.jay.monitor.data.server.daos.PartitionOperation;
import com.jay.monitor.data.server.enums.StoreType;
import com.jay.monitor.data.server.models.entity.JayMonitorUrl;
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;

/**
 * @Module 执行器
 * @Description Mysql的URL请求类型处理器
 * @Author liukaixiong
 * @Date 2020/10/29 13:36
 */
public class MysqlUrlStoreProcess extends BigTextStoreProcess<URLDataDTO, JayMonitorUrl> {

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

    @Autowired
    @Qualifier("jayMonitorUrlMapperExt")
    private JayMonitorUrlMapperExt urlMapper;

    @Autowired
    private MessageManagerProcess messageManagerProcess;

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

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

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

    @Override
    protected PartitionOperation<JayMonitorUrl> getMapper() {
        return urlMapper;
    }

    @Override
    protected JayMonitorUrl builderJayMonitor(URLDataDTO request) {
        return JayDataUtils.builderJayMonitorUrl(request);
    }

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

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

    @Override
    public List<JayMonitorUrl> speedUpConsumerList(List<URLDataDTO> requestList) {
        List<JayMonitorUrl> monitorList = new ArrayList<>();
        for (int i = 0; i < requestList.size(); i++) {
            URLDataDTO request = requestList.get(i);
            monitorList.add(builderJayMonitor(request));
        }

        urlMapper.batchPartitionInsert(getPartitionId(), monitorList);

        return monitorList;
    }

    @Override
    public List<JayMonitorUrl> searchDataByList(String date, Integer pageSize, JayMonitorUrl entity) throws Exception {
        return urlMapper.selectPartitionByList(date, pageSize, entity);
    }
}
