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


import com.yihu.hos.standard.dao.IStdDictionaryModelDao;
import com.yihu.hos.standard.model.adapter.AdapterDictEntryModel;
import com.yihu.hos.standard.model.standard.StdDictionaryEntryModel;
import com.yihu.hos.standard.model.standard.StdDictionaryModel;
import com.yihu.hos.standard.service.adapter.AdapterDictEntryService;
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("dictitemStandardNameStrategy")
public class DictitemStandardNameStrategy {

    private AdapterDictEntryService adapterDictEntryService;

    private IStdDictionaryModelDao stdDictionaryDao;

    public IStdDictionaryModelDao getStdDictionaryDao() {
        return stdDictionaryDao;
    }

    private String schemaName;

    public void setStdDictionaryDao(IStdDictionaryModelDao stdDictionaryDao) {
        this.stdDictionaryDao = stdDictionaryDao;
    }

    public AdapterDictEntryService getAdapterDictEntryService() {
        return adapterDictEntryService;
    }

    public void setAdapterDictEntryService(AdapterDictEntryService adapterDictEntryService) {
        this.adapterDictEntryService = adapterDictEntryService;
    }

    public DictitemStandardNameStrategy(AdapterDictEntryService adapterDictEntryService) {

        this.adapterDictEntryService = adapterDictEntryService;
    }

    public String getSchemaName() {
        return schemaName;
    }

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

    private static LikeHashMap<String,StdDictionaryEntryModel> nameMap=new LikeHashMap<String,StdDictionaryEntryModel>();//机构字典项值的map


    public DictitemStandardNameStrategy() {

    }


    @Transactional
    public boolean match(AdapterDictEntryModel adapterDictEntryModel, String version,String std_version) throws Exception {
        StdDictionaryModel dictionary = stdDictionaryDao.getDictionaryName(schemaName,std_version, adapterDictEntryModel.getStdDictId());

        StdDictionaryEntryModel orgDictItem = nameMap.get(adapterDictEntryModel.getStdEntryValue()+ "_" + dictionary.getCode(), true);
        if (orgDictItem != null ) {
            adapterDictEntryService.saveAdapt(schemaName,adapterDictEntryModel, orgDictItem, version,"2");
            return true;
        }
        return false;
    }


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

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