package com.yihu.hos.rest.models.crawler.adapter;

import com.yihu.hos.core.datatype.NumberUtil;
import com.yihu.hos.rest.common.format.AdapterBase;
import com.yihu.hos.rest.models.crawler.transform.DictDataType;
import com.yihu.hos.rest.models.standard.adapter.AdapterDictEntryModel;
import com.yihu.hos.rest.models.standard.adapter.AdapterDictModel;
import com.yihu.hos.rest.models.standard.adapter.AdapterMetadataModel;
import com.yihu.hos.rest.models.standard.bo.AdapterVersion;

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


/**
 * @author Air
 * @version 1.0
 * @created 2015.08.11 11:31
 */
public class AdapterMetaData extends AdapterBase {

    private AdapterMetadataModel adapterMetadataModel;
    private AdapterDict adapterDict;
    private AdapterVersion adapterVersion;

    public AdapterMetaData(AdapterMetadataModel adapterMetadataModel, AdapterVersion adapterVersion, Map<Integer, List<AdapterDictEntryModel>> entryMap) {
        this.adapterMetadataModel = adapterMetadataModel;
        this.adapterVersion = adapterVersion;
        prepareData(entryMap);
    }

    public AdapterMetadataModel getAdapterMetadataModel() {
        return adapterMetadataModel;
    }

    public void prepareData(Map<Integer, List<AdapterDictEntryModel>> entryMap) {
        if (!NumberUtil.isZero(adapterMetadataModel.getStdDictId())) {
            /**
             * 默认适配字典的id与数据元的标准id一致
             */
            AdapterDictModel adapterDictModel = (AdapterDictModel) getAdapterDictService().get(AdapterDictModel.class, adapterVersion.getDictTableName(), adapterMetadataModel.getStdDictId());
            if (adapterDictModel != null) {
                adapterDict = new AdapterDict(adapterDictModel, adapterVersion, entryMap);
            }
        }
    }

    public AdapterDict getAdapterDict() {
        return adapterDict;
    }

    public DictDataType getAdapterDictDataType() {
        Integer orgDictDataType = adapterMetadataModel.getAdapterDataType();
        if (orgDictDataType == null){
            return DictDataType.VALUE;
        }

        return DictDataType.values()[orgDictDataType];
    }
}
