package com.yihu.hos.system.dao;

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

/**
 * Created by chenweida on 2016/1/19.
 */
@Repository("systemParamDao")
public class SystemParamDao extends SQLGeneralDAO {

    public static final String BEAN_ID = "systemParamDao";

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

   
    public SystemParam getParamByKey(String paramKey) throws Exception {
        List<SystemParam> sParams = (List<SystemParam>) super.hibernateTemplate.find("from SystemParam s where s.paramKey = ?", paramKey);
        if (sParams != null && sParams.size() > 0) {
            return sParams.get(0);
        }
        return null;
    }

   
    public SystemParam getParamByKeyWithOutId(String paramKey, String id) throws Exception {
        List<SystemParam> sParams = (List<SystemParam>) super.hibernateTemplate.find("from SystemParam s where s.paramKey=? and s.id!=?", paramKey, id);
        if (sParams != null && sParams.size() > 0) {
            return sParams.get(0);
        }
        return null;
    }
}
