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.core.model.serializable.SQLMonitorDTO;
import com.jay.monitor.data.server.daos.JayMonitorSqlMapperExt;
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.models.entity.JayMonitorSql;
import com.jay.monitor.data.server.store.AbstractStoreProcess;
import com.jay.monitor.data.server.utils.PrefixUtils;
import org.apache.commons.beanutils.ConvertUtils;
import org.apache.commons.beanutils.PropertyUtils;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.time.DateUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Component;

import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.concurrent.atomic.LongAdder;

/**
 * @Module 数据存储器
 * @Description Mysql的SQL存储实现
 * @Author liukaixiong
 * @Date 2020/10/29 13:36
 */
@Component
@Lazy
public class MysqlSQLStoreProcess extends AbstractStoreProcess<SQLMonitorDTO, JayMonitorSql> {

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

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

    @Autowired
    @Qualifier("jayMonitorSqlMapperExt")
    private JayMonitorSqlMapperExt sqlMapper;

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

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

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

    @Override
    protected PartitionOperation<JayMonitorSql> getMapper() {
        return sqlMapper;
    }

    @Override
    protected JayMonitorSql builderJayMonitor(SQLMonitorDTO request) {
        JayMonitorSql sqlData = new JayMonitorSql();
        try {
            BeanUtils.copyProperties(request, sqlData);
            sqlData.setApplicationName(request.getApplicationName());
            sqlData.setApplicationIp(request.getIp());
            sqlData.setCreatedTime(request.getCreateDate());
            sqlData.setDataGroupName(request.getGroupName());
            sqlData.setDataGroupKey(request.getGroupKeyName());
            sqlData.setDataId(request.getDataId());
            sqlData.setLogId(request.getLogId());
            sqlData.setInvokeType(request.getCommandType());
            sqlData.setRequestTime(request.getRequestTime().intValue());
            sqlData.setAffectedRows(request.getAffectedRows());
            sqlData.setTableName(request.getTable());
            sqlData.setSqlId(request.getSqlId());
        } catch (Exception e) {
            logger.error("builderJayMonitorSql 失败", e);
        }
        return sqlData;
    }

}
