package com.yihu.hos.standard.dao;

import com.yihu.hos.standard.model.standard.StdMetaDataModel;
import com.yihu.hos.web.framework.dao.SQLGeneralDAO;
import com.yihu.hos.web.framework.util.sql.BeanTransformer;
import org.hibernate.Query;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * Created by Administrator on 2016/5/4.
 */
@Repository("stdeMetadataDao")
public class StdMetaDataModelDao extends SQLGeneralDAO implements IStdMetaDataModelDao {
    public List<StdMetaDataModel> getMetadata(String schemaName,String adapter_std_version, String orgMetadataName,String orgDatasetName){
        StringBuffer sql = new StringBuffer();
        sql.append("SELECT m.*").append(" FROM std_metadata_"+adapter_std_version).append(" m inner join std_dataset_"+adapter_std_version )
                .append(" d on d.id= m.dataset_id where m.name like ? and d.name like ?");

        String myCatAnnotation = "/*#mycat\\:schema=" + schemaName + "*/ ";
        String completeSql = myCatAnnotation + sql;

        try {
            Query query = getCurrentSession().createSQLQuery(completeSql);
            query.setParameter(0,"%" + orgMetadataName + "%");
            query.setParameter(1,"%" + orgDatasetName + "%");
            query.setResultTransformer(new BeanTransformer(StdMetaDataModel.class));
            List<StdMetaDataModel> list = query.list();
            return list;
        }catch (Exception e){
            e.printStackTrace();
            System.out.println("错误Like SQL："+sql.toString());
            return null;
        }

    }
}
