package com.yihu.hos.system.dao;

import com.yihu.hos.system.model.SystemDict;
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/20.
 */
@Repository("dictDao")
public class DictDao extends SQLGeneralDAO {

    public static final String BEAN_ID = "dictDao";

   
    public Result getDictList(Map<String, Object> params) throws Exception {
        StringBuilder sb = new StringBuilder(" from SystemDict s where 1=1 ");
        if (!StringUtils.isEmpty(params.get("dictId"))) {
            sb.append(" and  (s.dictName = '" + params.get("dictId") +"')");
        }
       else if (!StringUtils.isEmpty(params.get("name"))) {
            sb.append(" and  (s.code like '%" + params.get("name") + "%' or s.value like '%" + params.get("name")+"%')");
        }
        return super.getDataGridResult(sb.toString(), Integer.valueOf(params.get("page").toString()), Integer.valueOf(params.get("rows").toString()));

    }

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

   
    public SystemDict getDictItemByCode(String code) throws Exception {
        List<SystemDict> sds = (List<SystemDict>) super.hibernateTemplate.find("from SystemDict s where s.code=? ", code);
        if (sds != null && sds.size() > 0) {
            return sds.get(0);
        }
        return null;
    }

   
    public void delDictItemByDictName(String dictName) throws Exception
    {
        String sqlStr = "delete from system_dict where dict_name='" + dictName + "'";

        super.execute(sqlStr);
    }
}
