package com.yihu.hos.resource.dao;

import com.yihu.hos.resource.model.RsResourceRestDetail;
import com.yihu.hos.web.framework.dao.SQLGeneralDAO;
import com.yihu.hos.web.framework.model.DataGridResult;
import org.springframework.stereotype.Repository;
import org.springframework.util.StringUtils;

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

/**
 * Created by chenweida on 2016/1/30.
 */
@Repository("resourceRestDetailDao")
public class RsResourceRestDetailDaoImpl extends SQLGeneralDAO {

    public static final String BEAN_ID = "resourceRestDetailDao";

   
    public DataGridResult getResourceRestDetilList(Map<String, Object> conditionMap, Integer page, Integer rows) throws Exception {
        StringBuilder sb = new StringBuilder();
        sb.append(" from RsResourceRestDetail r ");
        sb.append(" where 1=1 ");
        if (!StringUtils.isEmpty(conditionMap.get("resourceRestName"))) {
            sb.append(" and r.name like '%" + conditionMap.get("resourceRestName") + "%'");
        }
        if (!StringUtils.isEmpty(conditionMap.get("resourceRestId"))) {
            sb.append(" and r.resourceRestId = '" + conditionMap.get("resourceRestId") + "'");
        }
        return super.getDataGridResult(sb.toString(), page, rows);
    }

   
    public RsResourceRestDetail getResourceRestDetailByCode(String code) throws Exception {
        return (RsResourceRestDetail) super.hibernateTemplate.find("from RsResourceRestDetail r where r.code=?", code).get(0);
    }

   
    public RsResourceRestDetail getResourceRestDetailByCodeAndResourceRestId(String code, String resourceRestId) throws Exception {
        List<RsResourceRestDetail> rrrds = (List<RsResourceRestDetail>) super.hibernateTemplate.find("from RsResourceRestDetail r where r.code=? ", code);
        if (rrrds != null && rrrds.size() > 0) {
            return rrrds.get(0);
        }
        return null;
    }

   
    public RsResourceRestDetail getResourceRestDetailByCodeAndResourceRestIdWithOutId(String code, String resourceRestId, String id) throws Exception {
        List<RsResourceRestDetail> rrrds = (List<RsResourceRestDetail>) super.hibernateTemplate.find("from RsResourceRestDetail r where r.code=? and r.id!=?", code, id);
        if (rrrds != null && rrrds.size() > 0) {
            return rrrds.get(0);
        }
        return null;
    }
}
