package com.sp2p.dao.admin;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.List;
import java.util.Map;
import org.apache.commons.lang.StringUtils;
import com.shove.data.DataException;
import com.shove.data.DataSet;
import com.shove.util.BeanMapUtils;
import com.sp2p.constants.IConstants;
import com.sp2p.database.Dao;
public class RoleDao {
/**
* 添加角色
* @Title: addRole
* @param conn
* @param roleName 角色名
* @param description 描述
* @throws SQLException
* @return Long
*/
public Long addRole(Connection conn ,String roleName,String description) throws SQLException{
Dao.Tables.t_role role = new Dao().new Tables().new t_role();
role._name.setValue(roleName);
role.description.setValue(description);
return role.insert(conn);
}
/**
* 修改角色
* @Title: addRole
* @param conn
* @param roleName 角色名
* @param description 描述
* @throws SQLException
* @return Long
*/
public Long updateRole(Connection conn ,long id,String name,String description) throws SQLException{
Dao.Tables.t_role role = new Dao().new Tables().new t_role();
if(StringUtils.isNotBlank(name)){
role._name.setValue(name);
}
if(StringUtils.isNotBlank(description)){
role.description.setValue(description);
}
return role.update(conn," id="+id);
}
/**
* 查询角色集合
* @return
* @throws DataException
* @throws SQLException
*/
public List<Map<String,Object>> queryRoleList(Connection conn) throws SQLException, DataException{
Dao.Tables.t_role role = new Dao().new Tables().new t_role();
DataSet dataSet = role.open(conn, "", "", " id "+IConstants.SORT_TYPE_DESC, -1, -1);
dataSet.tables.get(0).rows.genRowsMap();
return dataSet.tables.get(0).rows.rowsMap;
}
/**
* 根据编号查询角色信息
* @param conn
* @param id 角色
* @return
* @throws SQLException
* @throws DataException
*/
public Map<String, String> queryRoleById(Connection conn,long id) throws SQLException, DataException{
Dao.Tables.t_role role = new Dao().new Tables().new t_role();
DataSet dataSet = role.open(conn, "", " id = "+id, "", -1, -1);
return BeanMapUtils.dataSetToMap(dataSet);
}
public Long deleteRole(Connection conn ,long id) throws SQLException{
Dao.Tables.t_role role = new Dao().new Tables().new t_role();
Dao.Tables.t_role_rights roleRights = new Dao().new Tables().new t_role_rights();
roleRights.delete(conn, " roleId="+id);
return role.delete(conn, " id="+id);
}
}