<html xmlns="http://www.w3.org/1999/xhtml">
<!--#include file="../Inc/Conn.asp" -->
<!--#include file="../Inc/SK.Session.asp" -->
<!--#include file="../Inc/SK.Funtion.asp" -->
<%if Not (Session("Manager")="1" OR (JudgePopedomTF(Session("UserName"),"SK900000000") AND JudgePopedomTF(Session("UserName"),"SK910000006"))) then Call ReturnError1()%>
<%
dim Action,ParentID,i,FoundErr,ErrMsg
Action=F("Action")
ParentID=F("ParentID")
if ParentID="" then
ParentID=0
else
ParentID=CLng(ParentID)
end if
%>
<head>
<title>部门信息管理</title>
<meta http-equiv="Content-Type" content="text/html; charset=GB2312">
<LINK href="../images/style.css" type=text/css rel=stylesheet>
<script type="text/javascript" src="../Inc/Check.js"></script>
<script type="text/javascript" src="../Inc/Calendar.js"></script>
<style type="text/css">
<!--
body {
margin: 5px 0px 0px 0px;
overflow-x: hidden;
}
-->
</style>
</head>
<body>
<table width="100%" border="0" align="center" cellpadding="0" cellspacing="0">
<TBODY>
  <tr>
    <td class="SK_Td_Top"><table width="100%" border="0" cellspacing="0" cellpadding="0">
      <tr>
        <td width="12" height="25"><img src="../images/tab_03.gif" width="12" height="25" /></td>
        <td><table width="100%" border="0" cellspacing="0" cellpadding="0">
<tr>
<td width="46%" valign="middle">部门信息管理</td>
<td width="54%" align="right"><strong><a href="SK.Dept.asp">部门信息首页</a> | 
<a href="?Action=Add">新建部门</a>&nbsp;|&nbsp;
<a href="?Action=Order">一级排序</a><!--&nbsp;|&nbsp;<a href="?Action=OrderN">N级排序</a>
&nbsp;|&nbsp;<a href="?Action=Reset">复位所有区域</a>&nbsp;|&nbsp;<a href="?Action=Unite">部门合并</a>--></strong></td>
</tr>
</table></td>
<td width="16"><img src="../images/tab_07.gif" width="16" height="25" /></td>
</tr>
</table></td>
</tr>
<tr>
<td>

<table width="100%" border="0" cellspacing="0" cellpadding="0">
<tr>
<td width="8" background="../images/tab_12.gif"></td>
<td>
<%
if Action="Add" then
call AddClass()
elseif Action="SaveAdd" then

dim LName,PrevOrderID
LName=F("LName")
dim Vote_Array,conts,i123
conts=F("textaread")
if conts<>"" then
Vote_Array = split(conts,"|")
For i123=0 To Ubound(Vote_Array)
LName=Vote_Array(i123)
call SaveAdd()
Next
else
call SaveAdd()
end if

Response.Redirect "SK.Dept.asp#end" 

elseif Action="Modify" then
call Modify()
elseif Action="SaveModify" then
call SaveModify()
elseif Action="Move" then
call MoveClass()
elseif Action="SaveMove" then
call SaveMove()
elseif Action="Del" then
call DeleteClass()
elseif Action="Clear" then
call ClearClass()
elseif Action="UpOrder" then 
call UpOrder() 
elseif Action="DownOrder" then 
call DownOrder() 
elseif Action="Order" then
call Order()
elseif Action="UpOrderN" then 
call UpOrderN() 
elseif Action="DownOrderN" then 
call DownOrderN() 
elseif Action="OrderN" then
call OrderN()
elseif Action="Reset" then
call Reset()
elseif Action="SaveReset" then
call SaveReset()
elseif Action="Unite" then
call Unite()
elseif Action="SaveUnite" then
call SaveUnite()
else
call main()
end if

sub main()

dim arrShowLine(10)
for i=0 to ubound(arrShowLine)
arrShowLine(i)=False
next
dim sqlClass,rsClass,i,iDepth
sqlClass="select * From SK_SystemDept order by RootID,OrderID"
set rsClass=server.CreateObject("adodb.recordset")
rsClass.open sqlClass,conn,1,1
%>
<TABLE borderColor=#ffffff cellSpacing=0 borderColorDark=#ffffff cellPadding=3 width="100%" borderColorLight=#dfdfdf border=1>
<tr bgcolor=#efefef> 
<td width="735" height="22" align="center"><strong>部门名称</strong></td>
<td width="210" height="22" align="center"><strong>操作选项</strong></td>
</tr>
<% 
do while not rsClass.eof 
%>
<tr onMouseOver="changeto()" onMouseOut="changeback()"> 
<td><% 
iDepth=rsClass("Depth")
if rsClass("NextID")>0 then
arrShowLine(iDepth)=True
else
arrShowLine(iDepth)=False
end if
if iDepth>0 then
  for i=1 to iDepth 
if i=iDepth then 
if rsClass("NextID")>0 then 
response.write "├" 
else 
response.write "└" 
end if 
else 
if arrShowLine(i)=True then
response.write "│" 
else
response.write "&nbsp;&nbsp;" 
end if
end if 
  next 
  end if 
  if rsClass("Child")>0 then 
  response.write "┼" 
  else 
  response.write "─" 
  end if 
  if rsClass("Depth")=0 then 
  response.write "<b>" 
  end if 
  response.write "<a href='SK.Dept.asp?Action=Modify&LID=" & rsClass("LID") & "'>" & rsClass("LName") & "</a>"
  if rsClass("Child")>0 then 
  response.write "(" & rsClass("Child") & ")" 
  end if
  %></td>
<td align="center">
<%if RsClass("Depth")<2 Then%>
<a href="SK.Dept.asp?Action=Add&ParentID=<%=rsClass("LID")%>">新建下级</a>
<%end if%>
<a href="SK.Dept.asp?Action=Del&LID=<%=rsClass("LID")%>" onClick="<%if rsClass("Child")>0 then%>return ConfirmDel1();<%else%>return ConfirmDel2();<%end if%>">删除</a>
<%
if RsClass("Depth")>1 Then
Response.write"<a href=""javascript:void(0)"" onClick=""javascript:var win=window.open('SK.DeptPopList.asp?id="&rsClass("LID")&"','Mod','width=820,height=400,top=170,left=161,center:yes,status:no,help:no,toolbar=no,location=no,directories=no,status=no,menubar=no,resizable=yes,scrollbars=yes'); win.focus()"">设置权限</a>"
end if
%>
</td>
</tr>
<% 
rsClass.movenext 
loop 
%>
</table>
<script language="JavaScript" type="text/JavaScript">
function ConfirmDel1()
{
   alert("此部门下还有子部门，必须先删除下属子部门后才能删除此部门！");
   return false;
}

function ConfirmDel2()
{
   if(confirm("删除部门将同时删除此部门中的所有，并且不能恢复！确定要删除此部门吗？"))
     return true;
   else
     return false;
 
}
function ConfirmDel3()
{
   if(confirm("清空部门将把部门(包括子部门)的所有放入回收站中！确定要清空此部门吗？"))
     return true;
   else
     return false;
 
}
</script>
<%
end sub
sub AddClass()
%>
<form name="form1" method="post" action="SK.Dept.asp" onSubmit="return check()">
<TABLE borderColor=#ffffff cellSpacing=0 borderColorDark=#ffffff cellPadding=3 width="100%" borderColorLight=#dfdfdf border=1>
    <tr bgcolor=#efefef> 
      <td height="22" colspan="2" align="center"><strong>新建部门</strong></td>
    </tr>
    <tr > 
      <td width="350"><strong>所属上级：</strong></td>
      <td> <select name="ParentID">
          <%call Admin_ShowClass_Option(0,ParentID)%>
        </select></td>
    </tr>
    <tr> 
      <td width="350"><strong>部门名称：</strong><br>
        可一次增加多个部门，部门之前用|做为分隔符</td>
      <td>
      <textarea name="textaread" cols="90%"  rows="7" id="textaread"></textarea></td>
    </tr>

    <tr > 
      <td height="40" colspan="2" align="center"><input name="Action" type="hidden" id="Action" value="SaveAdd"> 
<input name="Add" type="submit" value=" 添 加 " style="cursor:hand;" class="button1"> 
&nbsp; <input name="Cancel" type="button" id="Cancel" value=" 取 消 " onClick="window.location.href='SK.Dept.asp'" style="cursor:hand;" class="button1"> 
      </td>
    </tr>
  </table>
</form>
<script language="JavaScript" type="text/JavaScript">
function check()
{
//  if (document.form1.LName.value=="")
//  {
//    alert("部门名称不能为空！");
//document.form1.LName.focus();
//return false;
//  }
}
</script>
<%
end sub

sub Modify()
dim LID,sql,rsClass,i
LID=F("LID")
if LID="" then
response.Write "<br>参数不足！"
else
LID=CLng(LID)
end if
sql="select * From SK_SystemDept where LID=" & LID
set rsClass=server.CreateObject ("Adodb.recordset")
rsClass.open sql,conn,1,3
if rsClass.bof and rsClass.eof then
response.Write "<br>找不到指定的部门！"
else
%>
<form name="form1" method="post" action="SK.Dept.asp" onSubmit="return check()">
<TABLE borderColor=#ffffff cellSpacing=0 borderColorDark=#ffffff cellPadding=3 width="100%" borderColorLight=#dfdfdf border=1>
    <tr  bgcolor=#efefef> 
      <td height="22" colspan="2" align="center"><strong>修改部门</strong></td>
    </tr>
    <tr > 
      <td width="350"><strong>所属部门：</strong><br>
        如果你想改变所属部门，请<b><a href='?Action=Move&LID=<%=LID%>'>点此移动部门</a></b></td>
      <td> <%
if rsClass("ParentID")<=0 then
  response.write "无(作为一级部门)"
else
    dim rsParentClass,sqlParentClass
sqlParentClass="Select * From SK_SystemDept where LID in ("&rsClass("ParentPath")&") order by Depth"
set rsParentClass=server.CreateObject("adodb.recordset")
rsParentClass.open sqlParentClass,conn,1,1
do while not rsParentClass.eof
for i=1 to rsParentClass("Depth")
response.write "&nbsp;&nbsp;&nbsp;"
next
if rsParentClass("Depth")>0 then
response.write "└"
end if
response.write "&nbsp;"&rsParentClass("LName")&"<br>"
rsParentClass.movenext
loop
rsParentClass.close
set rsParentClass=Nothing
end if
%> </select></td>
    </tr>
    <tr > 
      <td width="350"><strong>部门名称：</strong></td>
      <td><input name="LName" type="text" value="<%=rsClass("LName")%>" size="37" maxlength="20"> 
        <input name="LID" type="hidden" id="LID" value="<%=rsClass("LID")%>"></td>
    </tr>

    <tr > 
      <td height="40" colspan="2" align="center"><input name="Action" type="hidden" id="Action" value="SaveModify"> 
<input name="Submit" type="submit" value=" 保存 " style="cursor:hand;" class="button1"> 
&nbsp; <input name="Cancel" type="button" id="Cancel" value=" 取 消 " onClick="window.location.href='SK.Dept.asp'" style="cursor:hand;" class="button1"> 
</td>
</tr>
</table>
</form>
<script language="JavaScript" type="text/JavaScript">
function check()
{
  if (document.form1.LName.value=="")
  {
    alert("部门名称不能为空！");
document.form1.LName.focus();
return false;
  }
}
</script>
<%
end if
rsClass.close
set rsClass=Nothing
end sub

sub MoveClass()
dim LID,sql,rsClass,i
LID=F("LID")
if LID="" then
FoundErr=True
ErrMsg=ErrMsg & "<br>参数不足！"
exit sub
else
LID=CLng(LID)
end if

sql="select * From SK_SystemDept where LID="&LID
set rsClass=server.CreateObject ("Adodb.recordset")
rsClass.open sql,conn,1,3
if rsClass.bof and rsClass.eof then
FoundErr=True
ErrMsg=ErrMsg & "<br>找不到指定的部门！"
else
%>
<form name="form1" method="post" action="SK.Dept.asp">
<TABLE borderColor=#ffffff cellSpacing=0 borderColorDark=#ffffff cellPadding=3 width="100%" borderColorLight=#dfdfdf border=1>
    <tr  bgcolor=#efefef> 
      <td height="22" colspan="2" align="center"><strong>移动部门</strong></td>
    </tr>
    <tr > 
      <td width="200"><strong>部门名称：</strong></td>
      <td><%=rsClass("LName")%> <input name="LID" type="hidden" id="LID" value="<%=rsClass("LID")%>"></td>
    </tr>
    <tr >
      <td width="200"><strong>当前所属部门：</strong></td>
      <td>
        <%
if rsClass("ParentID")<=0 then
  response.write "无(作为一级部门)"
else
    dim rsParent,sqlParent
sqlParent="Select * From SK_SystemDept where LID in ("&rsClass("ParentPath")&") order by Depth"
set rsParent=server.CreateObject("adodb.recordset")
rsParent.open sqlParent,conn,1,1
do while not rsParent.eof
for i=1 to rsParent("Depth")
response.write "&nbsp;&nbsp;&nbsp;"
next
if rsParent("Depth")>0 then
response.write "└"
end if
response.write "&nbsp;"&rsParent("LName")&"<br>"
rsParent.movenext
loop
rsParent.close
set rsParent=Nothing
end if
%>
</td>
</tr>
<tr > 
<td width="200"><strong>移动到：</strong><br>
不能指定为当前部门的下属子部门<br>
不能指定为外部部门</td>
<td><select name="ParentID" size="2" style="height:300px;width:500px;"><%call Admin_ShowClass_Option(0,rsClass("ParentID"))%></select></td>
</tr>
<tr > 
<td height="40" colspan="2" align="center"><input name="Action" type="hidden" id="Action" value="SaveMove"> 
<input name="Submit" type="submit" value=" 保存 " style="cursor:hand;" class="button1">
&nbsp; 
<input name="Cancel" type="button" id="Cancel" value=" 取 消 " onClick="window.location.href='SK.Dept.asp'" style="cursor:hand;" class="button1"></td></tr>
</table>
</form>
<%
end if
rsClass.close
set rsClass=Nothing
end sub

sub Order() 
dim sqlClass,rsClass,i,iCount,j 
sqlClass="select * From SK_SystemDept where ParentID=0 order by RootID" 
set rsClass=server.CreateObject("adodb.recordset") 
rsClass.open sqlClass,conn,1,1 
iCount=rsClass.recordcount 
%>
<TABLE borderColor=#ffffff cellSpacing=0 borderColorDark=#ffffff cellPadding=3 width="100%" borderColorLight=#dfdfdf border=1>
<tr bgcolor=#efefef>  
<td height="22" colspan="4" align="center"><strong>一级栏目排序</strong></td> 
</tr> 
<% 
j=1 
do while not rsClass.eof 
%> 
<tr onMouseOut="this.style.backgroundColor=''" onMouseOver="this.style.backgroundColor='#BFDFFF'">  
<td width="200">&nbsp;<%=rsClass("LName")%></td> 
<% 
if j>1 then 
response.write "<form action='SK.Dept.asp?Action=UpOrder' method='post'><td width='150'>" 
response.write "<select name=MoveNum size=1><option value=0>向上移动</option>" 
for i=1 to j-1 
response.write "<option value="&i&">"&i&"</option>" 
next 
response.write "</select>" 
response.write "<input type=hidden name=LID value="&rsClass("LID")&">"
response.write "<input type=hidden name=cRootID value="&rsClass("RootID")&">&nbsp;<input type=submit name=Submit value=修改>" 
response.write "</td></form>" 
else 
response.write "<td width='150'>&nbsp;</td>" 
end if 
if iCount>j then 
  response.write "<form action='SK.Dept.asp?Action=DownOrder' method='post'><td width='150'>" 
response.write "<select name=MoveNum size=1><option value=0>向下移动</option>" 
for i=1 to iCount-j 
response.write "<option value="&i&">"&i&"</option>" 
next 
response.write "</select>" 
response.write "<input type=hidden name=LID value="&rsClass("LID")&">"
response.write "<input type=hidden name=cRootID value="&rsClass("RootID")&">&nbsp;<input type=submit name=Submit value=修改>" 
response.write "</td></form>" 
else 
response.write "<td width='150'>&nbsp;</td>" 
end if 
%> 
<td>&nbsp;</td>
</tr> 
<% 
j=j+1 
rsClass.movenext 
loop 
%> 
</table>
<% 
rsClass.close 
set rsClass=Nothing 
end sub 

sub OrderN() 
dim sqlClass,rsClass,i,iCount,trs,UpMoveNum,DownMoveNum 
sqlClass="select * From SK_SystemDept order by RootID,OrderID" 
set rsClass=server.CreateObject("adodb.recordset") 
rsClass.open sqlClass,conn,1,1 
%>
<TABLE borderColor=#ffffff cellSpacing=0 borderColorDark=#ffffff cellPadding=3 width="100%" borderColorLight=#dfdfdf border=1>
<tr bgcolor=#efefef>  
<td height="22" colspan="4" align="center"><strong>N 级 栏 目 排 序</strong></td> 
</tr> 
<% 
do while not rsClass.eof 
%> 
<tr onMouseOut="this.style.backgroundColor=''" onMouseOver="this.style.backgroundColor='#BFDFFF'">  
<td width="300"> 
<% 
for i=1 to rsClass("Depth") 
  response.write "&nbsp;&nbsp;&nbsp;" 
next 
if rsClass("Child")>0 then 
response.write "<img src='../Images/tree_folder4.gif' width='15' height='15' valign='abvmiddle'>" 
else 
  response.write "<img src='../Images/tree_folder3.gif' width='15' height='15' valign='abvmiddle'>" 
end if 
if rsClass("ParentID")=0 then 
response.write "<b>" 
end if 
response.write rsClass("LName") 
if rsClass("Child")>0 then 
response.write "(" & rsClass("Child") & ")" 
end if 
%></td> 
<% 
if rsClass("ParentID")>0 then

set trs=conn.execute("select count(LID) From SK_SystemDept where ParentID="&rsClass("ParentID")&" and OrderID<"&rsClass("OrderID")&"") 
UpMoveNum=trs(0) 
if isnull(UpMoveNum) then UpMoveNum=0 
if UpMoveNum>0 then 
  response.write "<form action='SK.Dept.asp?Action=UpOrderN' method='post'><td width='150'>" 
response.write "<select name=MoveNum size=1><option value=0>向上移动</option>" 
for i=1 to UpMoveNum 
response.write "<option value="&i&">"&i&"</option>" 
next 
response.write "</select>" 
response.write "<input type=hidden name=LID value="&rsClass("LID")&">&nbsp;<input type=submit name=Submit value=修改>" 
response.write "</td></form>" 
else 
response.write "<td width='150'>&nbsp;</td>" 
end if 
trs.close 

set trs=conn.execute("select count(LID) From SK_SystemDept where ParentID="&rsClass("ParentID")&" and orderID>"&rsClass("orderID")&"") 
DownMoveNum=trs(0) 
if isnull(DownMoveNum) then DownMoveNum=0 
if DownMoveNum>0 then 
  response.write "<form action='SK.Dept.asp?Action=DownOrderN' method='post'><td width='150'>" 
response.write "<select name=MoveNum size=1><option value=0>向下移动</option>" 
for i=1 to DownMoveNum 
response.write "<option value="&i&">"&i&"</option>" 
next 
response.write "</select>" 
response.write "<input type=hidden name=LID value="&rsClass("LID")&">&nbsp;<input type=submit name=Submit value=修改>" 
response.write "</td></form>" 
else 
response.write "<td width='150'>&nbsp;</td>" 
end if 
trs.close 
else 
response.write "<td colspan=2>&nbsp;</td>" 
end if 
%> 
<td>&nbsp;</td>
</tr> 
<% 
UpMoveNum=0 
DownMoveNum=0 
rsClass.movenext 
loop 
%> 
</table>
<% 
rsClass.close 
set rsClass=Nothing 
end sub 
sub Reset() 
%>
<TABLE borderColor=#ffffff cellSpacing=0 borderColorDark=#ffffff cellPadding=3 width="100%" borderColorLight=#dfdfdf border=1>
  <tr bgcolor=#efefef>  
    <td height="22" colspan="3" align="center"><strong>复位所有部门</strong></td> 
  </tr> 
    <tr >  
    <td align="center">  
      <form name="form1" method="post" action="?Action=SaveReset"> 
        <table width="100%" border="0" cellspacing="0" cellpadding="0"> 
          <tr>  
            <td><font color="#FF0000"><strong>注意：</strong></font><br> 
              &nbsp;&nbsp;如果选择复位所有部门，则所有部门都将作为一级部门，这时您需要重新对各个部门进行归属的基本设置。不要轻易使用该功能，仅在做出了错误的设置而无法复原部门之间的关系和排序的时候使用。  
            </td> 
          </tr> 
        </table> 
        <input type="submit" name="Submit" value="复位" class="button1"> &nbsp; <input name="Cancel" type="button" id="Cancel" value=" 取 消 " onClick="window.location.href='SK.Dept.asp'" style="cursor:hand;" class="button1">
      </form></td>
    </tr>
</table>
<%
end sub
sub Unite()
%>
<br>
<TABLE borderColor=#ffffff cellSpacing=0 borderColorDark=#ffffff cellPadding=3 width="100%" borderColorLight=#dfdfdf border=1>
  <tr  bgcolor=#efefef> 
    <td height="22" colspan="3" align="center"><strong>部门合并</strong></td>
  </tr>
  <tr > 
    <td height="100"><form name="myform" method="post" action="" onSubmit="return ConfirmUnite();">
        &nbsp;&nbsp;将部门 
        <select name="LID" id="LID">
        <%call Admin_ShowClass_Option(1,0)%>
        </select>
        合并到
        <select name="TargetLID" id="TargetLID">
        <%call Admin_ShowClass_Option(1,0)%>
        </select>
        <br> <br>
        &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
        <input name="Action" type="hidden" id="Action" value="SaveUnite">
        <input type="submit" name="Submit" value=" 合并 " style="cursor:hand;" class="button1">
        &nbsp;&nbsp; 
        <input name="Cancel" type="button" id="Cancel" value=" 取 消 " onClick="window.location.href='SK.Dept.asp'" style="cursor:hand;" class="button1">
      </form>
</td>
  </tr>
  <tr > 
    <td height="60"><strong>注意事项：</strong><br>
      &nbsp;&nbsp;所有操作不可逆，请慎重操作！！！<br>
      &nbsp;&nbsp;不能在同一个部门内进行操作，不能将一个部门合并到其下属部门中。目标部门中不能含有子部门。<br>
      &nbsp;&nbsp;合并后您所指定的部门(或者包括其下属部门)将被删除，所有将转移到目标部门中。</td>
  </tr>
</table> 
<script language="JavaScript" type="text/JavaScript">
function ConfirmUnite()
{
  if (document.myform.LID.value==document.myform.TargetLID.value)
  {
    alert("请不要在相同部门内进行操作！");
document.myform.TargetLID.focus();
return false;
  }
  if (document.myform.TargetLID.value=="")
  {
    alert("目标部门不能指定为含有子部门的部门！");
document.myform.TargetLID.focus();
return false;
  }
}
</script>
<% 
end sub 
%> 
<a name="end"></a>
</td>
<td width="8" background="../images/tab_15.gif"></td>
</tr>
</table></td>
</tr>
<tr>
<td class="SK_Td_Foot">
<table width="100%" border="0" cellspacing="0" cellpadding="0">
<tr>
<td width="1%" height="20"><img src="../images/tab_18.gif" width="12" height="20" /></td>
<td width="98%" class="SK_Td_Page"></td>
<td width="1%"><img src="../images/tab_20.gif" width="16" height="20" /></td>
</tr>
</table>

</td>
</tr>
</TBODY>
</table>
</body>
</html>
<% 
sub SaveAdd()
dim sql,rs,trs
dim RootID,ParentDepth,ParentPath,ParentStr,ParentName,MaxLID,MaxRootID
dim PrevID,NextID,Child

if LName="" then
response.Write "<br>部门名称不能为空！"
response.End()

end if
set rs = conn.execute("select Max(LID) From SK_SystemDept")
MaxLID=rs(0)
if isnull(MaxLID) then
MaxLID=0
end if
rs.close
LID=MaxLID+1
set rs=conn.execute("select max(rootid) From SK_SystemDept")
MaxRootID=rs(0)
if isnull(MaxRootID) then
MaxRootID=0
end if
rs.close
RootID=MaxRootID+1

if ParentID>0 then
sql="select * From SK_SystemDept where LID=" & ParentID & ""
rs.open sql,conn,1,1
if rs.bof and rs.eof then
FoundErr=True
ErrMsg=ErrMsg & "<br>所属部门已经被删除！"
end if
if FoundErr=True then
rs.close
set rs=Nothing
exit sub
else
RootID=rs("RootID")
ParentName=rs("LName")
ParentDepth=rs("Depth")
ParentPath=rs("ParentPath")
Child=rs("Child")
ParentPath=ParentPath & ","&ParentID     '得到此部门的父级部门路径
PrevOrderID=rs("OrderID")
if Child>0 then
dim rsPrevOrderID
'得到与本部门同级的最后一个部门的OrderID
set rsPrevOrderID=conn.execute("select Max(OrderID) From SK_SystemDept where ParentID="&ParentID)
PrevOrderID=rsPrevOrderID(0)
set trs=conn.execute("select LID From SK_SystemDept where ParentID="&ParentID&" and OrderID="&PrevOrderID)
PrevID=trs(0)

'得到同一父部门但比本部门级数大的子部门的最大OrderID，如果比前一个值大，则改用这个值。
set rsPrevOrderID=conn.execute("select Max(OrderID) From SK_SystemDept where ParentPath like '"&ParentPath&",%'")
if (not(rsPrevOrderID.bof and rsPrevOrderID.eof)) then
if not IsNull(rsPrevOrderID(0))  then
 if rsPrevOrderID(0)>PrevOrderID then
PrevOrderID=rsPrevOrderID(0)
end if
end if
end if
else
PrevID=0
end if

end if
rs.close
else
if MaxRootID>0 then
set trs=conn.execute("select LID From SK_SystemDept where RootID="&MaxRootID&" and Depth=0")
PrevID=trs(0)
trs.close
else
PrevID=0
end if
PrevOrderID=0
ParentPath="0"
end if

sql="Select * From SK_SystemDept Where ParentID="&ParentID&" AND LName='"&LName&"'"
set rs=server.CreateObject("adodb.recordset")
rs.open sql,conn,1,1
if not(rs.bof and rs.eof) then
FoundErr=True
if ParentID=0 then
ErrMsg=ErrMsg & "<br>已经存在一级部门："&LName&""
else
ErrMsg=ErrMsg & "<br>“"&ParentName&"”中已经存在子部门“"&LName&"”！"
end if
rs.close
set rs=Nothing
exit sub
end if
rs.close

sql="Select top 1 * From SK_SystemDept"
rs.open sql,conn,1,3
    rs.addnew
rs("LID")=LID
   rs("LName")=LName
rs("RootID")=RootID
rs("ParentID")=ParentID
if ParentID>0 then
rs("Depth")=ParentDepth+1
else
rs("Depth")=0
end if
rs("ParentPath")=ParentPath
rs("OrderID")=PrevOrderID
rs("Child")=0
rs("PrevID")=PrevID
rs("NextID")=0
rs.update
rs.Close
if PrevID>0 then
conn.execute("update SK_SystemDept set NextID=" & LID & " where LID=" & PrevID)
end if

if ParentID>0 then
'更新其父类的子部门数
conn.execute("update SK_SystemDept set child=child+1 where LID="&ParentID)

'更新该部门排序以及大于本需要和同在本部门下的部门排序序号
conn.execute("update SK_SystemDept set OrderID=OrderID+1 where rootid=" & rootid & " and OrderID>" & PrevOrderID)
conn.execute("update SK_SystemDept set OrderID=" & PrevOrderID & "+1 where LID=" & LID)
end if
end sub

sub SaveModify()
dim LName
dim trs,rs
dim LID,sql,rsClass,i
dim SkinCount,LayoutCount
LID=trim(request("LID"))
if LID="" then
response.Write "<br>参数不足！"
response.End()
else
LID=CLng(LID)
end if
LName=trim(request("LName"))
if LName="" then
response.Write "<br>部门名称不能为空！"
response.End()
end if

if FoundErr=True then
exit sub
end if

sql="select * From SK_SystemDept where LID=" & LID
set rsClass=server.CreateObject ("Adodb.recordset")
rsClass.open sql,conn,1,3
if rsClass.bof and rsClass.eof then
response.Write "<br>找不到指定的部门！"
response.End()
rsClass.close
set rsClass=Nothing
exit sub
end if
rsClass("LName")=LName
rsClass.update
rsClass.close
set rsClass=Nothing

set rs=Nothing
set trs=Nothing
Response.Redirect "SK.Dept.asp"  
end sub

sub DeleteClass()
dim sql,rs,PrevID,NextID,LID
LID=trim(Request("LID"))
if LID="" then
FoundErr=True
ErrMsg=ErrMsg & "<br>参数不足！"
exit sub
else
LID=CLng(LID)
end if

sql="select LID,RootID,Depth,ParentID,Child,PrevID,NextID From SK_SystemDept where LID="&LID
set rs=server.CreateObject ("Adodb.recordset")
rs.open sql,conn,1,3
if rs.bof and rs.eof then
FoundErr=True
ErrMsg=ErrMsg & "<br>部门不存在，或者已经被删除"
else
if rs("Child")>0 then
FoundErr=True
ErrMsg=ErrMsg & "<br>该部门含有子部门，请删除其子部门后再进行删除本部门的操作"
end if
end if
if FoundErr=True then
rs.close
set rs=Nothing
exit sub
end if
PrevID=rs("PrevID")
NextID=rs("NextID")
if rs("Depth")>0 then
conn.execute("update SK_SystemDept set child=child-1 where LID=" & rs("ParentID"))
end if
rs.delete
rs.update
rs.close
set rs=Nothing
'删除本部门的所有内容
'conn.execute("delete from product where LID=" & LID)
'conn.execute("delete from productComment where LID=" & LID)

'修改上一部门的NextID和下一部门的PrevID
if PrevID>0 then
conn.execute "update SK_SystemDept set NextID=" & NextID & " where LID=" & PrevID
end if
if NextID>0 then
conn.execute "update SK_SystemDept set PrevID=" & PrevID & " where LID=" & NextID
end if
call CloseConn()
response.redirect "SK.Dept.asp"

end sub

sub ClearClass()
dim strLID,rs,trs,SuccessMsg,LID
LID=trim(Request("LID"))
if LID="" then
FoundErr=True
ErrMsg=ErrMsg & "<br>参数不足！"
exit sub
else
LID=CLng(LID)
end if
strLID=cstr(LID)
set rs=conn.execute("select LID,Child,ParentPath From SK_SystemDept where LID=" & LID)
if rs.bof and rs.eof then
FoundErr=True
ErrMsg=ErrMsg & "<br>部门不存在，或者已经被删除"
exit sub
end if
if rs(1)>0 then
set trs=conn.execute("select LID From SK_SystemDept where ParentID=" & rs(0))
do while not trs.eof
strLID=strLID & "," & trs(0)
trs.movenext
loop
trs.close
set trs=conn.execute("select LID From SK_SystemDept where ParentPath like '" & rs(2) & "," & rs(0) & ",%'")
do while not trs.eof
strLID=strLID & "," & trs(0)
trs.movenext
loop
trs.close
set trs=Nothing
end if
rs.close
set rs=Nothing
'conn.execute("update SK_Recycle set Deleted=True where ID in (" & strLID & ")")
'conn.execute("delete from SK_Recycle where ID in (" & strLID & ")")
SuccessMsg="此部门(包括子部门)的所有文章已经被移到回收站中！"
call WriteSuccessMsg(SuccessMsg)
end sub

sub SaveMove()
dim LID,sql,rsClass,i
dim rParentID
dim trs,rs
dim ParentID,RootID,Depth,Child,ParentPath,ParentName,iParentID,iParentPath,PrevOrderID,PrevID,NextID
LID=trim(request("LID"))
if LID="" then
FoundErr=True
ErrMsg=ErrMsg & "<br>参数不足！"
exit sub
else
LID=CLng(LID)
end if

sql="select * From SK_SystemDept where LID=" & LID
set rsClass=server.CreateObject ("Adodb.recordset")
rsClass.open sql,conn,1,3
if rsClass.bof and rsClass.eof then
FoundErr=True
ErrMsg=ErrMsg & "<br>找不到指定的部门！"
rsClass.close
set rsClass=Nothing
exit sub
end if

rParentID=trim(request("ParentID"))
if rParentID="" then
rParentID=0
else
rParentID=CLng(rParentID)
end if

if rsClass("ParentID")<>rParentID then   '更改了所属部门，则要做一系列检查
if rParentID=rsClass("LID") then
FoundErr=True
ErrMsg=ErrMsg & "<br>所属部门不能为自己！"
end if
'判断所指定的部门是否为外部部门或本部门的下属部门
if rsClass("ParentID")=0 then
if rParentID>0 then
set trs=conn.execute("select rootid From SK_SystemDept where LID="&rParentID)
if trs.bof and trs.eof then
FoundErr=True
ErrMsg=ErrMsg & "<br>不能指定外部部门为所属部门"
else
if rsClass("rootid")=trs(0) then
FoundErr=True
ErrMsg=ErrMsg & "<br>不能指定该部门的下属部门作为所属部门"
end if
end if
trs.close
set trs=Nothing
end if
else
set trs=conn.execute("select LID From SK_SystemDept where ParentPath like '"&rsClass("ParentPath")&"," & rsClass("LID") & "%' and LID="&rParentID)
if not (trs.eof and trs.bof) then
FoundErr=True
ErrMsg=ErrMsg & "<br>您不能指定该部门的下属部门作为所属部门"
end if
trs.close
set trs=Nothing
end if

end if

if FoundErr=True then
rsClass.close
set rsClass=Nothing
exit sub
end if

if rsClass("ParentID")=0 then
ParentID=rsClass("LID")
iParentID=0
else
ParentID=rsClass("ParentID")
iParentID=rsClass("ParentID")
end if
Depth=rsClass("Depth")
Child=rsClass("Child")
RootID=rsClass("RootID")
ParentPath=rsClass("ParentPath")
PrevID=rsClass("PrevID")
NextID=rsClass("NextID")
rsClass.close
set rsClass=Nothing

dim mrs,MaxRootID
set mrs=conn.execute("select max(rootid) From SK_SystemDept")
MaxRootID=mrs(0)
set mrs=Nothing
if isnull(MaxRootID) then
MaxRootID=0
end if
dim k,nParentPath,mParentPath
dim ParentSql,ClassCount
dim rsPrevOrderID
if clng(parentid)<>rParentID and not (iParentID=0 and rParentID=0) then  '假如更改了所属部门

if PrevID>0 then
conn.execute "update SK_SystemDept set NextID=" & NextID & " where LID=" & PrevID
end if
if NextID>0 then
conn.execute "update SK_SystemDept set PrevID=" & PrevID & " where LID=" & NextID
end if

if iParentID>0 and rParentID=0 then

sql="select LID,NextID From SK_SystemDept where RootID=" & MaxRootID & " and Depth=0"
set rs=server.CreateObject("Adodb.recordset")
rs.open sql,conn,1,3
PrevID=rs(0)
rs(1)=LID
rs.update
rs.close
set rs=Nothing

MaxRootID=MaxRootID+1

conn.execute("update SK_SystemDept set depth=0,OrderID=0,rootid="&maxrootid&",parentid=0,ParentPath='0',PrevID=" & PrevID & ",NextID=0 where LID="&LID)

if child>0 then
i=0
ParentPath=ParentPath & ","
set rs=conn.execute("select * From SK_SystemDept where ParentPath like '%"&ParentPath & LID&"%'")
do while not rs.eof
i=i+1
mParentPath=replace(rs("ParentPath"),ParentPath,"")
conn.execute("update SK_SystemDept set depth=depth-"&depth&",rootid="&maxrootid&",ParentPath='"&mParentPath&"' where LID="&rs("LID"))
rs.movenext
loop
rs.close
set rs=Nothing
end if

conn.execute("update SK_SystemDept set child=child-1 where LID="&iParentID)

elseif iParentID>0 and rParentID>0 then

ParentPath=ParentPath & ","
set rs=conn.execute("select count(*) From SK_SystemDept where ParentPath like '%"&ParentPath & LID&"%'")
ClassCount=rs(0)
if isnull(ClassCount) then
ClassCount=1
end if
rs.close
set rs=Nothing

set trs=conn.execute("select * From SK_SystemDept where LID="&rParentID)
if trs("Child")>0 then
set rsPrevOrderID=conn.execute("select Max(OrderID) From SK_SystemDept where ParentID=" & trs("LID"))
PrevOrderID=rsPrevOrderID(0)
sql="select LID,NextID From SK_SystemDept where ParentID=" & trs("LID") & " and OrderID=" & PrevOrderID
set rs=server.createobject("adodb.recordset")
rs.open sql,conn,1,3
PrevID=rs(0)
rs(1)=LID
rs.update
rs.close
set rs=Nothing

set rsPrevOrderID=conn.execute("select Max(OrderID) From SK_SystemDept where ParentPath like '" & trs("ParentPath") & "," & trs("LID") & ",%'")
if (not(rsPrevOrderID.bof and rsPrevOrderID.eof)) then
if not IsNull(rsPrevOrderID(0))  then
 if rsPrevOrderID(0)>PrevOrderID then
PrevOrderID=rsPrevOrderID(0)
end if
end if
end if
else
PrevID=0
PrevOrderID=trs("OrderID")
end if

conn.execute("update SK_SystemDept set OrderID=OrderID+" & ClassCount & "+1 where rootid=" & trs("rootid") & " and OrderID>" & PrevOrderID)

conn.execute("update SK_SystemDept set depth="&trs("depth")&"+1,OrderID="&PrevOrderID&"+1,rootid="&trs("rootid")&",ParentID="&rParentID&",ParentPath='" & trs("ParentPath") & "," & trs("LID") & "',PrevID=" & PrevID & ",NextID=0 where LID="&LID)

set rs=conn.execute("select * From SK_SystemDept where ParentPath like '%"&ParentPath&LID&"%' order by OrderID")
i=1
do while not rs.eof
i=i+1
iParentPath=trs("ParentPath") & "," & trs("LID") & "," & replace(rs("ParentPath"),ParentPath,"")
conn.execute("update SK_SystemDept set depth=depth-"&depth&"+"&trs("depth")&"+1,OrderID="&PrevOrderID&"+"&i&",rootid="&trs("rootid")&",ParentPath='"&iParentPath&"' where LID="&rs("LID"))
rs.movenext
loop
rs.close
set rs=Nothing
trs.close
set trs=Nothing
conn.execute("update SK_SystemDept set child=child+1 where LID="&rParentID)
conn.execute("update SK_SystemDept set child=child-1 where LID="&iParentID)
else
set rs=conn.execute("select count(*) From SK_SystemDept where rootid="&rootid)
ClassCount=rs(0)
rs.close
set rs=Nothing
set trs=conn.execute("select * From SK_SystemDept where LID="&rParentID)
if trs("Child")>0 then
set rsPrevOrderID=conn.execute("select Max(OrderID) From SK_SystemDept where ParentID=" & trs("LID"))
PrevOrderID=rsPrevOrderID(0)
sql="select LID,NextID From SK_SystemDept where ParentID=" & trs("LID") & " and OrderID=" & PrevOrderID
set rs=server.createobject("adodb.recordset")
rs.open sql,conn,1,3
PrevID=rs(0)
rs(1)=LID
rs.update
set rs=Nothing
set rsPrevOrderID=conn.execute("select Max(OrderID) From SK_SystemDept where ParentPath like '" & trs("ParentPath") & "," & trs("LID") & ",%'")
if (not(rsPrevOrderID.bof and rsPrevOrderID.eof)) then
if not IsNull(rsPrevOrderID(0))  then
 if rsPrevOrderID(0)>PrevOrderID then
PrevOrderID=rsPrevOrderID(0)
end if
end if
end if
else
PrevID=0
PrevOrderID=trs("OrderID")
end if
conn.execute("update SK_SystemDept set OrderID=OrderID+" & ClassCount &"+1 where rootid=" & trs("rootid") & " and OrderID>" & PrevOrderID)
conn.execute("update SK_SystemDept set PrevID=" & PrevID & ",NextID=0 where LID=" & LID)
set rs=conn.execute("select * From SK_SystemDept where rootid="&rootid&" order by OrderID")
i=0
do while not rs.eof
i=i+1
if rs("parentid")=0 then
ParentPath=trs("ParentPath") & "," & trs("LID")
conn.execute("update SK_SystemDept set depth=depth+"&trs("depth")&"+1,OrderID="&PrevOrderID&"+"&i&",rootid="&trs("rootid")&",ParentPath='"&ParentPath&"',parentid="&rParentID&" where LID="&rs("LID"))
else
ParentPath=trs("ParentPath") & "," & trs("LID") & "," & replace(rs("ParentPath"),"0,","")
conn.execute("update SK_SystemDept set depth=depth+"&trs("depth")&"+1,OrderID="&PrevOrderID&"+"&i&",rootid="&trs("rootid")&",ParentPath='"&ParentPath&"' where LID="&rs("LID"))
end if
rs.movenext
loop
rs.close
set rs=Nothing
trs.close
set trs=Nothing
conn.execute("update SK_SystemDept set child=child+1 where LID="&rParentID)
end if
end if
call CloseConn()
Response.Redirect "SK.Dept.asp"  
end sub
sub UpOrder()
dim LID,sqlOrder,rsOrder,MoveNum,cRootID,tRootID,i,rs,PrevID,NextID
LID=trim(request("LID"))
cRootID=Trim(request("cRootID"))
MoveNum=trim(request("MoveNum"))
if LID="" then
FoundErr=True
ErrMsg=ErrMsg & "<br>参数不足！"
else
LID=CLng(LID)
end if
if cRootID="" then
FoundErr=true
ErrMsg=ErrMsg & "<br>错误参数！"
else
cRootID=Cint(cRootID)
end if
if MoveNum="" then
FoundErr=true
ErrMsg=ErrMsg & "<br>错误参数！"
else
MoveNum=Cint(MoveNum)
if MoveNum=0 then
FoundErr=True
ErrMsg=ErrMsg & "<br>请选择要提升的数字！"
end if
end if
if FoundErr=True then
exit sub
end if

'得到本部门的PrevID,NextID
set rs=conn.execute("select PrevID,NextID From SK_SystemDept where LID=" & LID)
PrevID=rs(0)
NextID=rs(1)
rs.close
set rs=Nothing
'先修改上一部门的NextID和下一部门的PrevID
if PrevID>0 then
conn.execute "update SK_SystemDept set NextID=" & NextID & " where LID=" & PrevID
end if
if NextID>0 then
conn.execute "update SK_SystemDept set PrevID=" & PrevID & " where LID=" & NextID
end if

dim mrs,MaxRootID
set mrs=conn.execute("select max(rootid) From SK_SystemDept")
MaxRootID=mrs(0)+1
'先将当前部门移至最后，包括子部门
conn.execute("update SK_SystemDept set RootID=" & MaxRootID & " where RootID=" & cRootID)

'然后将位于当前部门以上的部门的RootID依次加一，为要提升的数字
sqlOrder="select * From SK_SystemDept where ParentID=0 and RootID<" & cRootID & " order by RootID desc"
set rsOrder=server.CreateObject("adodb.recordset")
rsOrder.open sqlOrder,conn,1,3
if rsOrder.bof and rsOrder.eof then
exit sub        '如果当前部门已经在最上面，则无需移动
end if
i=1
do while not rsOrder.eof
tRootID=rsOrder("RootID")       '得到要提升位置的RootID，包括子部门
conn.execute("update SK_SystemDept set RootID=RootID+1 where RootID=" & tRootID)
i=i+1
if i>MoveNum then
rsOrder("PrevID")=LID
rsOrder.update
conn.execute("update SK_SystemDept set NextID=" & rsOrder("LID") & " where LID=" & LID)
exit do
end if
rsOrder.movenext
loop
rsOrder.movenext
if rsOrder.eof then
conn.execute("update SK_SystemDept set PrevID=0 where LID=" & LID)
else
rsOrder("NextID")=LID
rsOrder.update
conn.execute("update SK_SystemDept set PrevID=" & rsOrder("LID") & " where LID=" & LID)
end if
rsOrder.close
set rsOrder=Nothing

'然后再将当前部门从最后移到相应位置，包括子部门
conn.execute("update SK_SystemDept set RootID=" & tRootID & " where RootID=" & MaxRootID)
call CloseConn()
response.Redirect "SK.Dept.asp?Action=Order"
end sub

sub DownOrder()
dim LID,sqlOrder,rsOrder,MoveNum,cRootID,tRootID,i,rs,PrevID,NextID
LID=trim(request("LID"))
cRootID=Trim(request("cRootID"))
MoveNum=trim(request("MoveNum"))
if LID="" then
FoundErr=True
ErrMsg=ErrMsg & "<br>参数不足！"
else
LID=CLng(LID)
end if
if cRootID="" then
FoundErr=true
ErrMsg=ErrMsg & "<br>错误参数！"
else
cRootID=Cint(cRootID)
end if
if MoveNum="" then
FoundErr=true
ErrMsg=ErrMsg & "<br>错误参数！"
else
MoveNum=Cint(MoveNum)
if MoveNum=0 then
FoundErr=True
ErrMsg=ErrMsg & "<br>请选择要提升的数字！"
end if
end if
if FoundErr=True then
exit sub
end if

'得到本部门的PrevID,NextID
set rs=conn.execute("select PrevID,NextID From SK_SystemDept where LID=" & LID)
PrevID=rs(0)
NextID=rs(1)
rs.close
set rs=Nothing
'先修改上一部门的NextID和下一部门的PrevID
if PrevID>0 then
conn.execute "update SK_SystemDept set NextID=" & NextID & " where LID=" & PrevID
end if
if NextID>0 then
conn.execute "update SK_SystemDept set PrevID=" & PrevID & " where LID=" & NextID
end if

dim mrs,MaxRootID
set mrs=conn.execute("select max(rootid) From SK_SystemDept")
MaxRootID=mrs(0)+1

conn.execute("update SK_SystemDept set RootID=" & MaxRootID & " where RootID=" & cRootID)

sqlOrder="select * From SK_SystemDept where ParentID=0 and RootID>" & cRootID & " order by RootID"
set rsOrder=server.CreateObject("adodb.recordset")
rsOrder.open sqlOrder,conn,1,3
if rsOrder.bof and rsOrder.eof then
exit sub
end if
i=1
do while not rsOrder.eof
tRootID=rsOrder("RootID")
conn.execute("update SK_SystemDept set RootID=RootID-1 where RootID=" & tRootID)
i=i+1
if i>MoveNum then
rsOrder("NextID")=LID
rsOrder.update
conn.execute("update SK_SystemDept set PrevID=" & rsOrder("LID") & " where LID=" & LID)
exit do
end if
rsOrder.movenext
loop
rsOrder.movenext
if rsOrder.eof then
conn.execute("update SK_SystemDept set NextID=0 where LID=" & LID)
else
rsOrder("PrevID")=LID
rsOrder.update
conn.execute("update SK_SystemDept set NextID=" & rsOrder("LID") & " where LID=" & LID)
end if
rsOrder.close
set rsOrder=Nothing

conn.execute("update SK_SystemDept set RootID=" & tRootID & " where RootID=" & MaxRootID)
call CloseConn()
response.Redirect "SK.Dept.asp?Action=Order"
end sub

sub UpOrderN()
dim sqlOrder,rsOrder,MoveNum,LID,i
dim ParentID,OrderID,ParentPath,Child,PrevID,NextID
LID=Trim(request("LID"))
MoveNum=trim(request("MoveNum"))
if LID="" then
FoundErr=true
ErrMsg=ErrMsg & "<br>错误参数！"
else
LID=CLng(LID)
end if
if MoveNum="" then
FoundErr=true
ErrMsg=ErrMsg & "<br>错误参数！"
else
MoveNum=Cint(MoveNum)
if MoveNum=0 then
FoundErr=True
ErrMsg=ErrMsg & "<br>请选择要提升的数字！"
end if
end if
if FoundErr=True then
exit sub
end if

dim sql,rs,oldorders,ii,trs,tOrderID

set rs=conn.execute("select ParentID,OrderID,ParentPath,child,PrevID,NextID From SK_SystemDept where LID="&LID)
ParentID=rs(0)
OrderID=rs(1)
ParentPath=rs(2) & "," & LID
child=rs(3)
PrevID=rs(4)
NextID=rs(5)
rs.close
set rs=Nothing
if child>0 then
set rs=conn.execute("select count(*) From SK_SystemDept where ParentPath like '%"&ParentPath&"%'")
oldorders=rs(0)
rs.close
set rs=Nothing
else
oldorders=0
end if

if PrevID>0 then
conn.execute "update SK_SystemDept set NextID=" & NextID & " where LID=" & PrevID
end if
if NextID>0 then
conn.execute "update SK_SystemDept set PrevID=" & PrevID & " where LID=" & NextID
end if

sql="select LID,OrderID,child,ParentPath,PrevID,NextID From SK_SystemDept where ParentID="&ParentID&" and OrderID<"&OrderID&" order by OrderID desc"
set rs=server.CreateObject("adodb.recordset")
rs.open sql,conn,1,3
i=1
do while not rs.eof
tOrderID=rs(1)
conn.execute("update SK_SystemDept set OrderID="&tOrderID+oldorders+i&" where LID="&rs(0))
if rs(2)>0 then
ii=i+1
set trs=conn.execute("select LID,OrderID From SK_SystemDept where ParentPath like '%"&rs(3)&","&rs(0)&"%' order by OrderID")
if not (trs.eof and trs.bof) then
do while not trs.eof
conn.execute("update SK_SystemDept set OrderID="&tOrderID+oldorders+ii&" where LID="&trs(0))
ii=ii+1
trs.movenext
loop
end if
trs.close
set trs=Nothing
end if
i=i+1
if i>MoveNum then
rs(4)=LID
rs.update
conn.execute("update SK_SystemDept set NextID=" & rs(0) & " where LID=" & LID)
exit do
end if
rs.movenext
loop
rs.movenext
if rs.eof then
conn.execute("update SK_SystemDept set PrevID=0 where LID=" & LID)
else
rs(5)=LID
rs.update
conn.execute("update SK_SystemDept set PrevID=" & rs(0) & " where LID=" & LID)
end if
rs.close
set rs=Nothing

conn.execute("update SK_SystemDept set OrderID="&tOrderID&" where LID="&LID)

if child>0 then
i=1
set rs=conn.execute("select LID From SK_SystemDept where ParentPath like '%"&ParentPath&"%' order by OrderID")
do while not rs.eof
conn.execute("update SK_SystemDept set OrderID="&tOrderID+i&" where LID="&rs(0))
i=i+1
rs.movenext
loop
rs.close
set rs=Nothing
end if
call CloseConn()
response.Redirect "SK.Dept.asp?Action=OrderN"
end sub

sub DownOrderN()
dim sqlOrder,rsOrder,MoveNum,LID,i
dim ParentID,OrderID,ParentPath,Child,PrevID,NextID
LID=Trim(request("LID"))
MoveNum=trim(request("MoveNum"))
if LID="" then
FoundErr=true
ErrMsg=ErrMsg & "<br>错误参数！"
exit sub
else
LID=Cint(LID)
end if
if MoveNum="" then
FoundErr=true
ErrMsg=ErrMsg & "<br>错误参数！"
exit sub
else
MoveNum=Cint(MoveNum)
if MoveNum=0 then
FoundErr=True
ErrMsg=ErrMsg & "<br>请选择要下降的数字！"
exit sub
end if
end if

dim sql,rs,oldorders,ii,trs,tOrderID

set rs=conn.execute("select ParentID,OrderID,ParentPath,child,PrevID,NextID From SK_SystemDept where LID="&LID)
ParentID=rs(0)
OrderID=rs(1)
ParentPath=rs(2) & "," & LID
child=rs(3)
PrevID=rs(4)
NextID=rs(5)
rs.close
set rs=Nothing

if PrevID>0 then
conn.execute "update SK_SystemDept set NextID=" & NextID & " where LID=" & PrevID
end if
if NextID>0 then
conn.execute "update SK_SystemDept set PrevID=" & PrevID & " where LID=" & NextID
end if

sql="select LID,OrderID,child,ParentPath,PrevID,NextID From SK_SystemDept where ParentID="&ParentID&" and OrderID>"&OrderID&" order by OrderID"
set rs=server.CreateObject("adodb.recordset")
rs.open sql,conn,1,3
i=0
ii=0
do while not rs.eof
conn.execute("update SK_SystemDept set OrderID="&OrderID+ii&" where LID="&rs(0))
if rs(2)>0 then
set trs=conn.execute("select LID,OrderID From SK_SystemDept where ParentPath like '%"&rs(3)&","&rs(0)&"%' order by OrderID")
if not (trs.eof and trs.bof) then
do while not trs.eof
ii=ii+1
conn.execute("update SK_SystemDept set OrderID="&OrderID+ii&" where LID="&trs(0))
trs.movenext
loop
end if
trs.close
set trs=Nothing
end if
ii=ii+1
i=i+1
if i>=MoveNum then
rs(5)=LID
rs.update
conn.execute("update SK_SystemDept set PrevID=" & rs(0) & " where LID=" & LID)
exit do
end if
rs.movenext
loop
rs.movenext
if rs.eof then
conn.execute("update SK_SystemDept set NextID=0 where LID=" & LID)
else
rs(4)=LID
rs.update
conn.execute("update SK_SystemDept set NextID=" & rs(0) & " where LID=" & LID)
end if
rs.close
set rs=Nothing

conn.execute("update SK_SystemDept set OrderID="&OrderID+ii&" where LID="&LID)

if child>0 then
i=1
set rs=conn.execute("select LID From SK_SystemDept where ParentPath like '%"&ParentPath&"%' order by OrderID")
do while not rs.eof
conn.execute("update SK_SystemDept set OrderID="&OrderID+ii+i&" where LID="&rs(0))
i=i+1
rs.movenext
loop
rs.close
set rs=Nothing
end if
call CloseConn()
response.Redirect "SK.Dept.asp?Action=OrderN"
end sub

sub SaveReset()
dim i,sql,rs,SuccessMsg,iCount,PrevID,NextID
sql="select LID From SK_SystemDept order by RootID,OrderID"
set rs=server.CreateObject("adodb.recordset")
rs.open sql,conn,1,1
iCount=rs.recordcount
i=1
PrevID=0
do while not rs.eof
rs.movenext
if rs.eof then
NextID=0
else
NextID=rs(0)
end if
rs.moveprevious
conn.execute("update SK_SystemDept set RootID=" & i & ",OrderID=0,ParentID=0,Child=0,ParentPath='0',Depth=0,PrevID=" & PrevID & ",NextID=" & NextID & " where LID=" & rs(0))
PrevID=rs(0)
i=i+1
rs.movenext
loop
rs.close
set rs=Nothing

SuccessMsg="复位成功！请返回<a href=''>部门管理首页</a>做部门的归属设置。"
call WriteSuccessMsg(SuccessMsg)
end sub

sub SaveUnite()
dim LID,TargetLID,ParentPath,iParentPath,Depth,iParentID,Child,PrevID,NextID
dim rs,trs,i,SuccessMsg
LID=trim(request("LID"))
TargetLID=trim(request("TargetLID"))
if LID="" then
FoundErr=True
ErrMsg=ErrMsg & "<br>请指定要合并的部门！"
else
LID=CLng(LID)
end if
if TargetLID="" then
FoundErr=True
ErrMsg=ErrMsg & "<br>请指定目标部门！"
else
TargetLID=CLng(TargetLID)
end if
if LID=TargetLID then
FoundErr=True
ErrMsg=ErrMsg & "<br>请不要在相同部门内进行操作"
end if
if FoundErr=True then
exit sub
end if
'判断目标部门是否有子部门，如果有，则报错。
set rs=conn.execute("select Child From SK_SystemDept where LID=" & TargetLID)
if rs.bof and rs.eof then
FoundErr=True
ErrMsg=ErrMsg & "<br>目标部门不存在，可能已经被删除！"
else
if rs(0)>0 then
FoundErr=True
ErrMsg=ErrMsg & "<br>目标部门中含有子部门，不能合并！"
end if
end if
if FoundErr=True then
exit sub
end if

'得到当前部门信息
set rs=conn.execute("select LID,ParentID,ParentPath,PrevID,NextID,Depth From SK_SystemDept where LID="&LID)
iParentID=rs(1)
Depth=rs(5)
if iParentID=0 then
ParentPath=rs(0)
else
ParentPath=rs(2) & "," & rs(0)
end if
iParentPath=rs(0)
PrevID=rs(3)
NextID=rs(4)

'判断是否是合并到其下属部门中
set rs=conn.execute("select LID From SK_SystemDept where LID="&TargetLID&" and ParentPath like '"&ParentPath&"%'")
if not (rs.eof and rs.bof) then
FoundErr=true
ErrMsg=ErrMsg & "<br>不能将一个部门合并到其下属子部门中"
exit sub
end if

'得到当前部门的下属部门ID
set rs=conn.execute("select LID From SK_SystemDept where ParentPath like '"&ParentPath&"%'")
i=0
if not (rs.eof and rs.bof) then
do while not rs.eof
iParentPath=iParentPath & "," & rs(0)
i=i+1
rs.movenext
loop
end if
if i>0 then
ParentPath=iParentPath
else
ParentPath=LID
end if

'先修改上一部门的NextID和下一部门的PrevID
if PrevID>0 then
conn.execute "update SK_SystemDept set NextID=" & NextID & " where LID=" & PrevID
end if
if NextID>0 then
conn.execute "update SK_SystemDept set PrevID=" & PrevID & " where LID=" & NextID
end if

'更新文章及评论所属部门
'conn.execute("update Article set LID="&TargetLID&" where LID in ("&ParentPath&")")
'conn.execute("update ArticleComment set LID="&TargetLID&" where LID in ("&ParentPath&")")

conn.execute("delete From SK_SystemDept where LID in ("&ParentPath&")")

if Depth>0 then
conn.execute("update SK_SystemDept set Child=Child-1 where LID="&iParentID)
end if

SuccessMsg="部门合并成功！已经将被合并部门及其下属子部门的所有数据转入目标部门中。<br>同时删除了被合并的部门及其子部门。"
call WriteSuccessMsg(SuccessMsg)
set rs=Nothing
set trs=Nothing
end sub


sub Admin_ShowClass_Option(ShowType,CurrentID)
if ShowType=0 then
    response.write "<option value='0'"
if CurrentID=0 then response.write " selected"
response.write ">无(作为一级部门)</option>"
end if
dim rsClass,sqlClass,strTemp,tmpDepth,i
dim arrShowLine(20)
for i=0 to ubound(arrShowLine)
arrShowLine(i)=False
next
sqlClass="Select * From SK_SystemDept order by RootID,OrderID"
set rsClass=server.CreateObject("adodb.recordset")
rsClass.open sqlClass,conn,1,1
if rsClass.bof and rsClass.bof then
response.write "<option value=''>请先新建部门</option>"
else
dim UserLevel
if UserLevel="" then
UserLevel=5000
else
UserLevel=Cint(UserLevel)
end if
do while not rsClass.eof
tmpDepth=rsClass("Depth")
if rsClass("NextID")>0 then
arrShowLine(tmpDepth)=True
else
arrShowLine(tmpDepth)=False
end if
if ShowType=1 then

elseif ShowType=2 then

elseif ShowType=3 then
if rsClass("Child")>0 then
strTemp="<option value=''"
strTemp="<option value='0'"
else
strTemp="<option value='" & rsClass("LID") & "'"
end if
elseif ShowType=4 then
if rsClass("Child")>0 then
strTemp="<option value=''"
strTemp="<option value='0'"
else
strTemp="<option value='" & rsClass("LID") & "'"
end if
else
strTemp="<option value='" & rsClass("LID") & "'"
end if
if CurrentID>0 and rsClass("LID")=CurrentID then
 strTemp=strTemp & " selected"
end if
strTemp=strTemp & ">"

if tmpDepth>0 then
for i=1 to tmpDepth
strTemp=strTemp & "&nbsp;&nbsp;"
if i=tmpDepth then
if rsClass("NextID")>0 then
strTemp=strTemp & "├&nbsp;"
else
strTemp=strTemp & "└&nbsp;"
end if
else
if arrShowLine(i)=True then
strTemp=strTemp & "│"
else
strTemp=strTemp & "&nbsp;"
end if
end if
next
end if
strTemp=strTemp & rsClass("LName")
strTemp=strTemp & "</option>"
response.write strTemp
rsClass.movenext
loop
end if
rsClass.close
set rsClass=Nothing
end sub

Call CloseDB()
%>