package com.yihu.hos.qc.dao;

import com.yihu.hos.qc.model.RuleModel;
import com.yihu.hos.web.framework.dao.SQLGeneralDAO;
import com.yihu.hos.web.framework.model.Result;
import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.criterion.Restrictions;
import org.springframework.stereotype.Repository;
import org.springframework.util.StringUtils;

import java.util.List;
import java.util.Map;

/**
 * @author CD
 * @vsrsion 1.0
 * Created at 2017/05/05.
 */
@Repository("ruleDao")
public class RuleDao extends SQLGeneralDAO {
    public static final String BEAN_ID = "ruleDao";

    public Result getRuleList(Map<String, Object> params) throws Exception {
        StringBuilder sb = new StringBuilder("from RuleModel r where 1=1 ");
        Object name = params.get("name");
        if (!StringUtils.isEmpty(name)) {
            sb.append(" and r.name like '%" + name + "%'");
        }
        return super.getDataGridResult(sb.toString(), Integer.valueOf(params.get("page").toString()), Integer.valueOf(params.get("rows").toString()));

    }

    public List<RuleModel> getRuleList(String name) throws Exception {
        StringBuilder sb = new StringBuilder("from RuleModel r where 1=1 ");
        if (!StringUtils.isEmpty(name)) {
            sb.append(" and r.name like '%" + name + "%'");
        }
        return (List<RuleModel>) super.hibernateTemplate.getSessionFactory().getCurrentSession().createQuery(sb.toString());
    }

    public List<RuleModel> getAllByIdList(List<String> idList) {
        Session session = getCurrentSession();
        Criteria criteria = session.createCriteria(RuleModel.class);
        criteria.add(Restrictions.in("id", idList));
        return criteria.list();
    }
}
