package com.yihu.hos.system.dao;

import com.yihu.hos.system.model.SystemApp;
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;

/**
 * Created by chenweida on 2016/1/21.
 */
@Repository("appDao")
public class AppDao extends SQLGeneralDAO {
    public static final String BEAN_ID = "appDao";

    public Result getAppList(Map<String, Object> params) throws Exception {
        StringBuilder sb = new StringBuilder("from SystemApp t where 1=1 ");
        if (!StringUtils.isEmpty(params.get("status"))) //是否有效
        {
            sb.append(" and t.status = '" + params.get("status") + "'");
        }
        Object name = params.get("name");
        if (!StringUtils.isEmpty(name)) {
            sb.append(" and (t.name like '%" + name + "%' or t.code like '%" + name + "%')");
        }
        sb.append(" order by t.createDate desc");
        return super.getDataGridResult(sb.toString(), Integer.valueOf(params.get("page").toString()), Integer.valueOf(params.get("rows").toString()));

    }

    public List<SystemApp> getAllApplist() throws Exception {
        return (List<SystemApp>) super.hibernateTemplate.find("from SystemApp");
    }

    public List<SystemApp> findByCode(String code) throws Exception {
        List<SystemApp> systemApps = (List<SystemApp>) super.hibernateTemplate.find("from SystemApp where code=?", code);
        return systemApps;
    }

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