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

import com.jay.monitor.data.core.enums.MsgTypeEnums;
import com.jay.monitor.data.core.model.response.PageResponseModel;
import com.jay.monitor.data.server.enums.StoreType;
import com.jay.monitor.data.server.utils.DataUtils;
import com.jay.monitor.data.server.utils.JayDataUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.InitializingBean;

import java.util.List;
import java.util.Map;

public class TranceJsonManager implements InitializingBean {

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

    private List<TraceSearchProcess> storeProcessList;
    private StoreType storeType;

    public TranceJsonManager(StoreType storeType, List<TraceSearchProcess> storeProcessList) {
        this.storeProcessList = storeProcessList;
        this.storeType = storeType;
    }

    private StoreType defaultStoreType = StoreType.MYSQL;

    private Map<MsgTypeEnums, TraceSearchProcess> processMap;

    /**
     * 获取配置文件中的默认存储配置
     *
     * @return
     */
    private StoreType getStoreType() {
        return DataUtils.getDefaultValue(this.storeType, this.defaultStoreType);
    }

    @Override
    public void afterPropertiesSet() throws Exception {
        this.processMap = JayDataUtils.builderInitMap(storeProcessList);
    }

    /**
     * 查询数据集合
     *
     * @param msgTypeEnums
     * @param date
     * @param pageNo
     * @param pageSize
     * @param entity
     * @return
     * @throws Exception
     */
    public <R> PageResponseModel<R> searchDataByList(MsgTypeEnums msgTypeEnums, String date, Integer pageNo,
        Integer pageSize, Object entity) throws Exception {
        TraceSearchProcess traceSearchProcess = processMap.get(msgTypeEnums);
        PageResponseModel pageResponseModel = traceSearchProcess.searchDataByList(date, pageNo, pageSize, entity);
        return pageResponseModel;
    }

}

