package com.yihu.hos.resource.dao;

import com.yihu.hos.resource.model.RsDemensionCategory;
import com.yihu.hos.resource.model.RsResourceCategory;
import com.yihu.hos.web.framework.dao.SQLGeneralDAO;
import com.yihu.hos.web.framework.model.Result;
import org.springframework.stereotype.Repository;
import org.springframework.util.StringUtils;

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

@Repository("rsCategoryDao")
public class RsCategoryDaoImpl extends SQLGeneralDAO {
    
    public static final String BEAN_ID = "rsCategoryDao";
    
   
    public RsDemensionCategory[] getAllCategory() throws Exception {
        return (RsDemensionCategory[]) super.hibernateTemplate.find("from RsResourceCategory").toArray();
    }

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

   
    public RsResourceCategory getCategoryByNameAndPid(String name, String pid) throws Exception {
        List<RsResourceCategory> r = (List<RsResourceCategory>) super.hibernateTemplate.find("from RsResourceCategory r where r.name=? and r.pid=?", name, pid);
        if (r != null && r.size() > 0)
            return r.get(0);
        else
            return null;
    }

   
    public RsResourceCategory getCategoryByNameAndPidWithOutThis(String name, String pid, String id) throws Exception {
        List<RsResourceCategory> r = (List<RsResourceCategory>) super.hibernateTemplate.find("from RsResourceCategory r where r.name=? and r.pid = ? and r.id != ?", name, pid, id);
        if (r != null && r.size() > 0)
            return r.get(0);
        else
            return null;
    }

   
    public List<RsResourceCategory> getCategoryByPid(String id) throws Exception {
        return (List<RsResourceCategory>) super.hibernateTemplate.find("from RsResourceCategory r where r.pid =?", id);
    }

    public List<RsResourceCategory> getCategoryWithOutId(String id) throws Exception {
        String sql = " from RsResourceCategory r where 1=1 ";
        if (!StringUtils.isEmpty(id)) {
            sql += " and r.id !='" + id + "'";
        }
        return (List<RsResourceCategory>) super.hibernateTemplate.find(sql);
    }
}
