package com.yihu.hos.crawler.model.adapter;

import com.yihu.hos.core.datatype.NumberUtil;
import com.yihu.hos.crawler.model.transform.DictDataType;
import com.yihu.hos.standard.model.adapter.AdapterDictEntryModel;
import com.yihu.hos.standard.model.adapter.AdapterDictModel;
import com.yihu.hos.standard.model.adapter.AdapterMetadataModel;
import com.yihu.hos.standard.service.adapter.AdapterDictService;
import com.yihu.hos.standard.service.bo.AdapterVersion;
import com.yihu.hos.web.framework.util.springutil.SpringBeanUtil;

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

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

    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())) {
            AdapterDictService adapterDictService = SpringBeanUtil.getService(AdapterDictService.BEAN_ID);
            /**
             * 默认适配字典的id与数据元的标准id一致
             */
            AdapterDictModel adapterDictModel = (AdapterDictModel) adapterDictService.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];
    }
}
