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.model.standard.StdDataSetModel;
import com.yihu.hos.standard.model.standard.StdMetaDataModel;
import com.yihu.hos.standard.service.adapter.AdapterDatasetService;
import com.yihu.hos.standard.service.adapter.AdapterMetadataService;
import com.yihu.hos.standard.util.GetChineseFirst;
import com.yihu.hos.web.framework.dao.SQLGeneralDAO;
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/5/16.
 * 对已经存在的匹配
 */
@Scope("prototype")
@Component("metatdataStandardExistStrategy")
public class MetatdataStandardExistStrategy extends SQLGeneralDAO {
    private IStdMetaDataModelDao stdMetaDataModelDao;
    private AdapterMetadataService adapterMetadataService;
    private IStdDataSetModelDao stdDataSetModelDao;
    private AdapterDatasetService adapterDatasetService;
    private String schemaName;

    public AdapterDatasetService getAdapterDatasetService() {
        return adapterDatasetService;
    }

    public void setAdapterDatasetService(AdapterDatasetService adapterDatasetService) {
        this.adapterDatasetService = adapterDatasetService;
    }

    public MetatdataStandardExistStrategy(IStdMetaDataModelDao stdMetaDataModelDao) {
        this.stdMetaDataModelDao = stdMetaDataModelDao;
    }

    public IStdMetaDataModelDao getStdMetaDataModelDao() {
        return stdMetaDataModelDao;
    }

    public void setStdMetaDataModelDao(IStdMetaDataModelDao stdMetaDataModelDao) {
        this.stdMetaDataModelDao = stdMetaDataModelDao;
    }

    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;
    }

    public String getSchemaName() {
        return schemaName;
    }

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

    public MetatdataStandardExistStrategy() {
    }

    /**
     * 对传进来的StdeMetadata进行匹配
     *
     * @param strategyMetadata
     * @param matchVO
     * @return
     * @throws Exception
     */
    @Transactional
    public boolean match(AdapterMetadataModel strategyMetadata, MetadataMatchVO matchVO, String std_version, String adapter_std_version, String version) {
        boolean flag = false;
        try {
            String metadataNameFirstCode = GetChineseFirst.cn2py(strategyMetadata.getStdMetadataName());//待匹配
            List<AdapterMetadataModel> adapterMetadatas = matchVO.getCodeAdapter().get(metadataNameFirstCode);//已存在
            if (adapterMetadatas != null && adapterMetadatas.size() > 0) {
                //获取待适配的数据集名
                Integer unAdaptStdId = strategyMetadata.getStdDatasetId();
                StdDataSetModel stdDataset = stdDataSetModelDao.getDatasetName(schemaName,std_version, unAdaptStdId);
                //StdDataSetModel adapterDataset = stdDataSetModelDao.getDatasetName(schemaName,adapter_std_version, unAdaptStdId);
                if(stdDataset!=null){
                    String stdDatasetName = stdDataset.getName();
                    //List<String> sqlList = new ArrayList<String>();
                    for (AdapterMetadataModel stdeMetadataTemp : adapterMetadatas) {
                        //利用名字來匹配
                        if (stdeMetadataTemp.getStdMetadataName().contains(strategyMetadata.getStdMetadataName())) {
                            String orgMetadataName = stdeMetadataTemp.getAdapterMetadataName();
                            //根据名字查找出 该项目下面的数据元名称
                            List<StdMetaDataModel> metadataTemps = stdMetaDataModelDao.getMetadata(schemaName,adapter_std_version, orgMetadataName, stdDatasetName);
                            if (metadataTemps != null && metadataTemps.size() > 0) {
                                String adapterInfo = "1";//找到默认为疑似适配
                                if (metadataTemps.size() == 1) {//找到条数为1,默认为完全适配
                                    adapterInfo = "2";
                                }
                                StdMetaDataModel orgMetadata = metadataTemps.get(0);
                                //保存适配
                                adapterMetadataService.saveAdapt(strategyMetadata, orgMetadata, version, adapterInfo);
//                                String strSql = adapterMetadataService.saveAdaptSql(strategyMetadata, orgMetadata, version, adapterInfo);
//                                if(null != strSql && !"".equals(strSql)){
//                                    sqlList.add(strSql);
//                                }
                                flag = true;
                                break;
                            }
                        }
                    }

//                    if(CollectionUtils.isNotEmpty(sqlList)){
//                        adapterMetadataService.insertBatch(sqlList);
//                    }
                }
//                if (adapterDataset != null) {
//                    adapterDatasetService = SpringBeanUtil.getService(AdapterDatasetService.BEAN_ID);
//                    AdapterDatasetModel entity = adapterDatasetService.getAdapterDatasetByCode(version, adapterDataset.getCode());
//                    adapterDatasetService.saveAdaptDataset(entity,adapterDataset,version);
//                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return flag;
    }

}
