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

import com.jay.monitor.data.core.model.serializable.base.BaseDTO;
import com.jay.monitor.data.server.componts.ElasticSearchPartition;
import com.jay.monitor.data.server.daos.PartitionOperation;
import com.jay.monitor.data.server.enums.StoreType;
import com.jay.monitor.data.server.store.AbstractStoreProcess;
import com.jay.monitor.data.server.utils.JayDataUtils;
import org.elasticsearch.action.bulk.BulkResponse;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

public abstract class AbstractElasticSearchStoreProcess<T extends BaseDTO, E> extends AbstractStoreProcess<T, E>
    implements PartitionOperation<E> {

    private Logger logger = LoggerFactory.getLogger(getClass());
    @Autowired
    protected ElasticSearchPartition<E, String> elasticSearchPartition;

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

    @Override
    protected PartitionOperation<E> getMapper() {
        return this;
    }

    @Override
    public int batchPartitionInsert(String date, List<E> monitorList) {
        BulkResponse[] bulkResponses = null;
        try {
            bulkResponses = elasticSearchPartition
                .saveBatchPartition(monitorList, JayDataUtils.getTableName(tableName(), getPartitionId()));
        } catch (Exception e) {
            logger.error("插入es:" + tableName() + "异常", e);
        }
        return bulkResponses == null ? 0 : bulkResponses.length;
    }

    @Override
    public E selectPartitionById(String date, Integer id) {
        return null;
    }

    @Override
    public List<E> selectPartitionByList(String date, Integer pageSize, E record) throws Exception {
        return this.elasticSearchPartition.searchPartition(tableName() + "_" + date, record);
    }

}
