package com.yihu.hos.standard.dao;

import com.yihu.hos.standard.model.standard.StdDictionaryModel;
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 2017/4/19 0019.
 */
@Repository("stdDictionaryDao")
@CacheConfig(cacheNames = "StdDictionaryModelCache")
public class StdDictionaryModelDao extends SQLGeneralDAO implements IStdDictionaryModelDao {

    public final static String BEAN_ID = "stdDictionaryDao";

    @Override
    @Cacheable
    public StdDictionaryModel getDictionaryName(String schemaName,String version, Integer id) {
        StringBuffer sql = new StringBuffer();
        sql.append("SELECT d.*").append(" FROM std_dictionary_").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(StdDictionaryModel.class));
            StdDictionaryModel stdDictionaryModel = (StdDictionaryModel) query.uniqueResult();
            return stdDictionaryModel;
        } catch (Exception e) {
            System.out.println("错误Dict SQL：" + sql.toString());
            e.printStackTrace();
            return null;
        }
    }
}
