删除函数
function deleteRegion() {
if(window.confirm("确认删除吗?")){
with(document.forms[0]){
action = "client_node_crud.jsp";
submit();
}
}
}
jsp里取得表单值,调用删除分销商方法
<%
int id = Integer.parseInt(request.getParameter("id"));
Client client = ClientManager.getInstance().findClientOrRegionById(id);
//删除区域
String command = request.getParameter("command");
if(Constants.Del.equals(command)){
ClientManager.getInstance().delClientOrRegion(id);
%>
<script type="text/javascript">
alert("用户【<%=client.getName()%>】删除成功!");
window.parent.clientTreeFrame.location.reload();
</script>
<%
}
%>
删除分销商方法
/**
* 删除分销商或区域
* @param id
*/
public void delClientOrRegion(int id){
Connection conn = null;
try{
conn = DbUtil.getConnection();
//手动控制事务
DbUtil.beginTransaction(conn);
//保存当前分销商
Client client = findClientOrRegionById(id);
//递归删除树节点
recursionDelNode(conn, id);
//如果父节点下没有子节点,修改为叶子
if(getChildren(conn,client.getPid())==0){
modifyIsLeafField(conn, client.getPid(), Constants.YES);
}
//提交事务
DbUtil.commitTransaction(conn);
}catch(Exception e){
e.printStackTrace();
DbUtil.rollbackTransaction(conn);
}finally{
DbUtil.resetConnection(conn);
DbUtil.close(conn);
}
}
读取分销商
// 读取分销商
public Client findClientOrRegionById(int id) {
StringBuffer sbSql = new StringBuffer();
sbSql.append("select a.id,pid,a.name,client_level_id,b.name as client_level_name,client_id,bank_acct_no,contact_tel,address,zip_code,")
.append("is_leaf,is_client").append(" from t_client a").append(" left join t_data_dict b on a.client_level_id=b.id")
.append(" where a.id = ?");
Connection conn = null;
PreparedStatement pstmt = null;
ResultSet rs = null;
conn = DbUtil.getConnection();
Client client = null;
try {
pstmt = conn.prepareStatement(sbSql.toString());
pstmt.setInt(1, id);
rs = pstmt.executeQuery();
while (rs.next()) {
client = new Client();
client.setId(rs.getInt("id"));
client.setPid(rs.getInt("pid"));
client.setName(rs.getString("name"));
ClientLevel cl = new ClientLevel();
cl.setId(rs.getString("client_level_id"));
cl.setName(rs.getString("client_level_name"));
client.setClientLevel(cl);
client.setClientId(rs.getString("client_id"));
client.setBankAcctNo(rs.getString("bank_acct_no"));
client.setContactTel(rs.getString("contact_tel"));
client.setAddress(rs.getString("address"));
client.setZipCode(rs.getString("zip_code"));
client.setIsLeaf(rs.getString("is_leaf"));
client.setIsClient(rs.getString("is_client"));
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
DbUtil.close(rs);
DbUtil.close(pstmt);
DbUtil.close(conn);
}
return client;
}
递归删除节点
/**
* 递归删除,首先删除下面的子节点
* @param conn
* @param id
* @throws SQLException
*/
private void recursionDelNode(Connection conn, int id) throws SQLException {
String sql = "select * from t_client where pid=?";
PreparedStatement pstmt = null;
ResultSet rs = null;
try{
pstmt = conn.prepareStatement(sql);
pstmt.setInt(1, id);
rs = pstmt.executeQuery();
while(rs.next()){
if(Constants.NO.equals(rs.getString("is_leaf"))){
recursionDelNode(conn,rs.getInt("id"));
}
delNode(conn, rs.getInt("id"));
}
//删除自身
delNode(conn, id);
}finally{
DbUtil.close(rs);
DbUtil.close(pstmt);
}
}
获取子节点数目
/**
* 获取孩子数目
* @param conn
* @param pid
* @return
* @throws SQLException
*/
private int getChildren(Connection conn, int pid) throws SQLException {
String sql = "select count(*) as c from t_client where pid=?";
PreparedStatement pstmt = null;
ResultSet rs = null;
int count = 0;
try{
pstmt = conn.prepareStatement(sql);
pstmt.setInt(1, pid);
rs = pstmt.executeQuery();
rs.next();
count = rs.getInt("c");
}finally{
DbUtil.close(rs);
DbUtil.close(pstmt);
}
return count;
}
修改叶子
/**
* 修改isLeaf字段
*
* @param conn
* 连接
* @param id
* @param leaf
* 是否为叶子Y/N
*/
private void modifyIsLeafField(Connection conn, int id, String leaf) {
String sql = "update t_client t set t.is_leaf=? where id=?";
PreparedStatement pstmt = null;
try {
pstmt = conn.prepareStatement(sql);
pstmt.setString(1, leaf);
pstmt.setInt(2, id);
pstmt.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
} finally {
DbUtil.close(pstmt);
}
}