package com.yihu.hos.standard.dao;

import com.yihu.hos.standard.model.standard.StdDictionaryEntryModel;
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;

import java.util.List;

/**
 * Created by Administrator on 2017/4/19 0019.
 */
@Repository("stdDictionaryEntryDao")
@CacheConfig(cacheNames = "StdDictionaryEntryModelCache")
public class StdDictionaryEntryModelDao extends SQLGeneralDAO implements IStdDictionaryEntryModelDao {
    @Override
    @Cacheable
    public List<StdDictionaryEntryModel> getDictionaryEntry(String schemaName,String adapter_std_version, String orgDicEntryValue, String dictName) {
        StringBuffer sql = new StringBuffer();
        sql.append("SELECT m.*").append(" FROM std_dictionary_entry_" + adapter_std_version).append(" m inner join std_dictionary_" + adapter_std_version)
                .append(" d on d.id= m.dict_id where m.value like ? and d.name like ?");

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

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