package com.yihu.hos.system.dao;

import com.yihu.hos.system.model.SystemRole;
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("roleDao")
public class RoleDao extends SQLGeneralDAO {

    public static final String BEAN_ID = "roleDao";

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

  
    public void activityRole(String roleid, String activityFlag) throws Exception {
        String sql = "update System_role set valid='" + activityFlag + "' where Id='" + roleid + "'";
        super.execute(sql);
    }

  
    public SystemRole getRoleByName(String name) throws Exception {
        List<SystemRole> srs = (List<SystemRole>) super.hibernateTemplate.find("from SystemRole s where s.name=? ", name);
        if (srs != null && srs.size() > 0) {
            return srs.get(0);
        }
        return null;
    }

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