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

import com.yihu.hos.standard.dao.IStdDataSetModelDao;
import com.yihu.hos.standard.dao.IStdMetaDataModelDao;
import com.yihu.hos.standard.match.matchModel.MetadataMatchVO;
import com.yihu.hos.standard.model.adapter.AdapterMetadataModel;
import com.yihu.hos.standard.service.adapter.AdapterMetadataService;
import org.apache.commons.collections4.CollectionUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2016/5/18.
 * 数据元匹配执行器
 */

public class MetadataStrategyExecute implements Runnable {
    private AdapterMetadataModel metadata;
    private MetadataMatchVO matchVO;
    public static Map<String, List<Integer>> size = new HashMap<String, List<Integer>>();
    private String std_version;
    private String adapter_std_version;
    private String version;
    private MetatdataStandardExistStrategy standardExistStrategy;
    private AdapterMetadataService adapterMetadataService;
    private MetatdataStandardNameStrategy standardNameStrategy;
    private IStdDataSetModelDao stdDataSetModelDao;
    private String schemaName;

    public MetadataStrategyExecute(
            String schemaName,
            AdapterMetadataModel metadata,
            MetadataMatchVO matchVO,
            String std_version,
            String adapter_std_version,
            String version,
            IStdMetaDataModelDao stdMetaDataModelDao,
            IStdDataSetModelDao stdDataSetModelDao,
            AdapterMetadataService adapterMetadataService) {
        this.metadata = metadata;
        this.matchVO = matchVO;
        this.std_version = std_version;
        this.adapter_std_version = adapter_std_version;
        this.version = version;
        this.adapterMetadataService = adapterMetadataService;
        this.stdDataSetModelDao = stdDataSetModelDao;
        this.schemaName = schemaName;

        //项目之间学习适配
        standardExistStrategy = new MetatdataStandardExistStrategy();
        standardExistStrategy.setStdMetaDataModelDao(stdMetaDataModelDao);
        standardExistStrategy.setAdapterMetadataService(adapterMetadataService);
        standardExistStrategy.setStdDataSetModelDao(stdDataSetModelDao);
        standardExistStrategy.setSchemaName(schemaName);
        standardNameStrategy = new MetatdataStandardNameStrategy();
        standardNameStrategy.setAdapterMetadataService(adapterMetadataService);
        standardNameStrategy.setStdDataSetModelDao(stdDataSetModelDao);
        standardNameStrategy.setSchemaName(schemaName);

    }

    @Override
    public void run() {
        try {
            //按名字相似度来适配
            if (standardNameStrategy.match(metadata, version, std_version)) {
                updateAdapt(version);
                return;
            }

            if (matchVO != null) {
                //项目间学习
                if (standardExistStrategy.match(metadata, matchVO,std_version,adapter_std_version, version)) {
                    updateAdapt(version);
                    return;
                }
            }

            updateAdapt(version);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static Map<String, List<Integer>> getSize() {
        return size;
    }

    public synchronized static void updateAdapt(String version) {
        List<Integer> sizeList = size.get(version);
        sizeList.add(1, sizeList.get(1) + 1);
    }

    public synchronized static void setFirst(String version, Integer allCount) {
        List<Integer> sizeList = new ArrayList<Integer>();
        sizeList.add(0, allCount);
        sizeList.add(1, 0);
        size.put(version, sizeList);
    }

    public synchronized static void setVersionYes(String version) {
        List<Integer> sizeList = new ArrayList<Integer>();
        size.put(version, sizeList);
    }


    public synchronized static void removeAdapt(String version) {
        size.remove(version);
    }

    public synchronized static boolean isAdapt(String version) {
        if (size.get(version) == null) {
            return false;
        } else {
            return true;
        }
    }

    public synchronized static Map getAllNumAndAdaptNum(String version) {
        Map<String, Integer> adapt = new HashMap<String, Integer>();
        List<Integer> list = size.get(version);
        if(CollectionUtils.isNotEmpty(list)){
            adapt.put("all", list.get(0));
            adapt.put("adapt", list.get(1));
        }
        return adapt;
    }
}
