package com.yihu.hos.standard.match.metadata;

import com.yihu.hos.standard.dao.IStdDataSetModelDao;
import com.yihu.hos.standard.model.adapter.AdapterMetadataModel;
import com.yihu.hos.standard.model.standard.StdDataSetModel;
import com.yihu.hos.standard.model.standard.StdMetaDataModel;
import com.yihu.hos.standard.service.adapter.AdapterMetadataService;
import com.yihu.hos.standard.util.LikeHashMap;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.util.Map;

/**
 * Created by Administrator on 2016/7/4.
 */

@Scope("prototype")
@Component("metatdataStandardNameStrategy")
public class MetatdataStandardNameStrategy {

    private static LikeHashMap<String, StdMetaDataModel> nameMap = new LikeHashMap<String, StdMetaDataModel>();//机构名称的map
    private static LikeHashMap<String, StdMetaDataModel> remarkMap = new LikeHashMap<String, StdMetaDataModel>();//机构remark的map

    private AdapterMetadataService adapterMetadataService;

    private IStdDataSetModelDao stdDataSetModelDao;

    private String schemaName;

    public MetatdataStandardNameStrategy(AdapterMetadataService adapterMetadataService, IStdDataSetModelDao stdDataSetModelDao,String schemaName) {

        this.adapterMetadataService = adapterMetadataService;
        this.stdDataSetModelDao = stdDataSetModelDao;
        this.schemaName = schemaName;
    }

    public MetatdataStandardNameStrategy() {

    }

    public String getSchemaName() {
        return schemaName;
    }

    public void setSchemaName(String schemaName) {
        this.schemaName = schemaName;
    }

    public static Map<String, StdMetaDataModel> getRemarkMap() {
        return remarkMap;
    }

    public static void setRemarkMap(LikeHashMap<String, StdMetaDataModel> remarkMap) {
        MetatdataStandardNameStrategy.remarkMap = remarkMap;
    }

    public static Map<String, StdMetaDataModel> getNameMap() {
        return nameMap;
    }

    public static void setNameMap(LikeHashMap<String, StdMetaDataModel> nameMap) {
        MetatdataStandardNameStrategy.nameMap = nameMap;
    }

    public AdapterMetadataService getAdapterMetadataService() {
        return adapterMetadataService;
    }

    public void setAdapterMetadataService(AdapterMetadataService adapterMetadataService) {
        this.adapterMetadataService = adapterMetadataService;
    }

    public IStdDataSetModelDao getStdDataSetModelDao() {
        return stdDataSetModelDao;
    }

    public void setStdDataSetModelDao(IStdDataSetModelDao stdDataSetModelDao) {
        this.stdDataSetModelDao = stdDataSetModelDao;
    }

    /**
     * 对传进来的metadata进行匹配
     *
     * @param metadata
     * @param version
     * @return
     * @throws Exception
     */
    @Transactional
    public boolean match(AdapterMetadataModel metadata, String version, String std_version) throws Exception {
        StdDataSetModel stdDataset = stdDataSetModelDao.getDatasetName(schemaName,std_version, metadata.getStdDatasetId());
        String name = metadata.getStdMetadataName();
        StdMetaDataModel orgStdeMetadata = nameMap.get(name + "_" + stdDataset.getName(), true);
        if (orgStdeMetadata != null) {
            adapterMetadataService.saveAdapt(schemaName,metadata, orgStdeMetadata, version, "2");
            return true;
        }
        orgStdeMetadata = remarkMap.get(name + "_" + stdDataset.getName(), true);
        if (orgStdeMetadata != null) {
            adapterMetadataService.saveAdapt(schemaName,metadata, orgStdeMetadata, version, "2");
            return true;
        }
        return false;
    }
}
