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

import com.yihu.hos.standard.dao.IStdDictionaryModelDao;
import com.yihu.hos.standard.dao.StdDictionaryEntryModelDao;
import com.yihu.hos.standard.match.matchModel.DictItemMatchVO;
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.service.adapter.AdapterDictService;
import com.yihu.hos.standard.util.GetChineseFirst;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * Created by Administrator on 2016/6/24.
 */
@Scope("prototype")
@Component("dictitemStandardExistStrategy")
public class DictitemStandardExistStrategy {
    private IStdDictionaryModelDao stdDictionaryDao;

    private StdDictionaryEntryModelDao stdDictionaryEntryDao;

    private AdapterDictEntryService adapterDictEntryService;
    private AdapterDictService adapterDictService;
    private String schemaName;

    public DictitemStandardExistStrategy(String schemaName,IStdDictionaryModelDao stdDictionaryDao, StdDictionaryEntryModelDao stdDictionaryEntryDao, AdapterDictEntryService adapterDictEntryService) {
        this.schemaName = schemaName;
        this.stdDictionaryDao = stdDictionaryDao;
        this.stdDictionaryEntryDao = stdDictionaryEntryDao;
        this.adapterDictEntryService = adapterDictEntryService;
    }

    public IStdDictionaryModelDao getStdDictionaryDao() {
        return stdDictionaryDao;
    }

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

    public StdDictionaryEntryModelDao getStdDictionaryEntryDao() {
        return stdDictionaryEntryDao;
    }

    public void setStdDictionaryEntryDao(StdDictionaryEntryModelDao stdDictionaryEntryDao) {
        this.stdDictionaryEntryDao = stdDictionaryEntryDao;
    }

    public AdapterDictEntryService getAdapterDictEntryService() {
        return adapterDictEntryService;
    }

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

    public AdapterDictService getAdapterDictService() {
        return adapterDictService;
    }

    public void setAdapterDictService(AdapterDictService adapterDictService) {
        this.adapterDictService = adapterDictService;
    }

    public String getSchemaName() {
        return schemaName;
    }

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

    public DictitemStandardExistStrategy() {

    }


    @Transactional
    public boolean match(AdapterDictEntryModel adapterDictEntryModel, DictItemMatchVO matchVO,String std_version, String adapter_std_version,String version) throws Exception {
        boolean flag = false;
        try {
        String dictitemNameFirstCode = GetChineseFirst.cn2py(adapterDictEntryModel.getStdEntryValue());//待匹配
        List<AdapterDictEntryModel> matchList = matchVO.getCodeAdapter().get(dictitemNameFirstCode);//已存在
        if (matchList != null && matchList.size() > 0) {
            Integer unAdaptDicId = adapterDictEntryModel.getStdDictId();
            StdDictionaryModel stdDictionaryModel = stdDictionaryDao.getDictionaryName(schemaName,std_version,unAdaptDicId);
            if(stdDictionaryModel!=null){
                String dictName = stdDictionaryModel.getName();
                for (AdapterDictEntryModel adapterDictEntry : matchList) {
                    if(adapterDictEntry.getStdEntryValue().contains(adapterDictEntryModel.getStdEntryValue())){
                        String orgDicEntryValue= adapterDictEntry.getAdapterEntryValue();
                        //根据值查找出 该项目下面的字典项
                        List<StdDictionaryEntryModel> dictionaryEntryTemps = stdDictionaryEntryDao.getDictionaryEntry(schemaName,adapter_std_version,orgDicEntryValue,dictName);
                        if(dictionaryEntryTemps!=null&&dictionaryEntryTemps.size()>0){
                            String adapterInfo = "1";//查找到就默认疑似适配
                            if(dictionaryEntryTemps.size()==1){//查找到的条数为1,则为完全适配
                                adapterInfo = "2";
                            }
                            StdDictionaryEntryModel stdDictionaryEntryModel = dictionaryEntryTemps.get(0);
                            //保存适配
                            adapterDictEntryService.saveAdapt(schemaName,adapterDictEntryModel, stdDictionaryEntryModel, version,adapterInfo);
                            flag=true;
                            break;
                        }
                    }
                }
            }
        }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return flag;
    }
}
