package com.yihu.hos.resource.dao;

import com.yihu.hos.resource.model.RsResource;
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/13.
 */
@Repository("rsResourceDao")
public class RsResourceDaoImpl extends SQLGeneralDAO  {

    public static final String BEAN_ID = "rsResourceDao";


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

  
    public DataGridResult getResourceFiled(Map<String, Object> conditionMap, Integer page, Integer rows) throws Exception {
        StringBuffer sql = new StringBuffer(" from RsResourceMetadata smd where 1=1 ");
        if (!StringUtils.isEmpty(conditionMap.get("resourceId"))) {
            sql.append(" and smd.resourceId = '" + conditionMap.get("resourceId") + "'");
        }
        return super.getDataGridResult(sql.toString(), page, rows);
    }

  
    public int getResourceListCount(Map<String, Object> conditionMap) throws Exception {
        StringBuilder sb = new StringBuilder();

        sb.append("  from RsResource r");
        sb.append("  where 1=1  ");
        if (!StringUtils.isEmpty(conditionMap.get("categoryId"))) {
            sb.append(" and r.category = '" + conditionMap.get("categoryId") + "'");
        }
        if (!StringUtils.isEmpty(conditionMap.get("resourceName"))) {
            sb.append(" and r.name like '%" + conditionMap.get("resourceName") + "%'");
        }
        return super.hibernateTemplate.find(sb.toString()).size();
    }

  
    public List<RsResource> getAllRsResource() throws Exception {
        return (List<RsResource>) super.hibernateTemplate.find(" from RsResource r ");
    }

    /**
     * 通过分类获取资源
     */
  
    public List<RsResource> getAllRsResourceByCategory(String category) throws Exception {
        return (List<RsResource>) super.hibernateTemplate.find(" from RsResource r where Category='"+category+"'");
    }

  
    public RsResource getResourceById(String id) throws Exception {
        return super.getEntity(RsResource.class, id);
    }

}
