package com.yihu.hos.standard.dao;

import com.yihu.hos.standard.model.standard.StdDataSetModel;
import com.yihu.hos.web.framework.dao.SQLGeneralDAO;
import com.yihu.hos.web.framework.util.sql.BeanTransformer;
import org.hibernate.Query;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Repository;

/**
 * Created by Administrator on 2016/5/4.
 */
@Repository("stdeDatasetDao")
@CacheConfig(cacheNames = "StdDataSetModelCache")
public class StdDataSetModelDao extends SQLGeneralDAO implements IStdDataSetModelDao {
    @Override
    @Cacheable
    public StdDataSetModel getDatasetName(String schemaName,String version ,Integer id){
        StringBuffer sql = new StringBuffer();
        sql.append("SELECT d.*").append(" FROM std_dataset_").append(version).append(" d where d.id ="+id);

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

        try {
            Query query = getCurrentSession().createSQLQuery(completeSql);
            query.setResultTransformer(new BeanTransformer(StdDataSetModel.class));
            StdDataSetModel stdDataSetModel =(StdDataSetModel) query.uniqueResult();
            return stdDataSetModel;
        }catch (Exception e){
            System.out.println("错误SQL："+sql.toString());
            e.printStackTrace();
            return null;
        }

    }
}
