<!--#include file="../Inc/Conn.asp" -->
<!--#include file="../Inc/SK.Session.asp" -->
<!--#include file="../Inc/SK.Funtion.asp" -->
<%if Not (Getconfig("SK_Product")="0" AND (Session("Manager")="1" OR (JudgePopedomTF(Session("UserName"),"SK900000000") AND JudgePopedomTF(Session("UserName"),"SK900000008")))) then Call ReturnError()%>
<%
dim Action,ParentID,i,FoundErr,ErrMsg
Action=F("Action")
ParentID=F("ParentID")
if ParentID="" then
ParentID=0
else
ParentID=CLng(ParentID)
end if
%>
<html>
<head>
<title>产品分类管理</title>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<LINK href="../images/style.css" type=text/css rel=stylesheet>
<style type="text/css">
<!--
body {
margin: 5px 0px 5px 0px;
overflow-x:hidden;
overflow-y:auto;
}
-->
</style>
</head>
<body>
<table width="99%" 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.ProClass.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 ProClassName,PrevOrderID
ProClassName=trim(request("ProClassName"))
dim Vote_Array,conts,i123
conts=request.Form("textaread")
if conts<>"" then
Vote_Array = split(conts,"|")
For i123=0 To Ubound(Vote_Array)
ProClassName=Vote_Array(i123)
call SaveAdd()
Next
else
call SaveAdd()
end if

Response.Redirect "SK.ProClass.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_ProClass 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="99%" borderColorLight=#dfdfdf border=1>
  <tr bgcolor=#efefef> 
    <td height="22" align="center"><strong>分类名称</strong></td>
    <td width="300" height="22" align="center"><strong>操作选项</strong></td>
  </tr>
<% 
do while not rsClass.eof 
%>
<tr> 
<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.ProClass.asp?Action=Modify&ProClassID=" & rsClass("ProClassID") & "'>" & rsClass("ProClassName") & "</a>"
  if rsClass("Child")>0 then 
  response.write "(" & rsClass("Child") & ")" 
  end if
  %></td>
<td align="center">
<a href="SK.ProClass.asp?Action=Add&ParentID=<%=rsClass("ProClassID")%>">新建下级</a>
<a href="SK.ProClass.asp?Action=Del&ProClassID=<%=rsClass("ProClassID")%>" onClick="<%if rsClass("Child")>0 then%>return ConfirmDel1();<%else%>return ConfirmDel2();<%end if%>">删除</a>
</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.ProClass.asp" onSubmit="return check()">
<TABLE borderColor=#ffffff cellSpacing=0 borderColorDark=#ffffff cellPadding=3 width="99%" 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.ProClass.asp'" style="cursor:hand;" class="button1"> 
      </td>
    </tr>
  </table>
</form>
<script language="JavaScript" type="text/JavaScript">
function check()
{
//  if (document.form1.ProClassName.value=="")
//  {
//    alert("分类名称不能为空！");
//document.form1.ProClassName.focus();
//return false;
//  }
}
</script>
<%
end sub

sub Modify()
dim ProClassID,sql,rsClass,i
ProClassID=trim(request("ProClassID"))
if ProClassID="" then
response.Write "<br>参数不足！"
else
ProClassID=CLng(ProClassID)
end if
sql="select * From SK_ProClass where ProClassID=" & ProClassID
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.ProClass.asp" onSubmit="return check()">
<TABLE borderColor=#ffffff cellSpacing=0 borderColorDark=#ffffff cellPadding=3 width="99%" 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&ProClassID=<%=ProClassID%>'>点此移动分类</a></b></td>
      <td> <%
if rsClass("ParentID")<=0 then
  response.write "无(作为一级分类)"
else
    dim rsParentClass,sqlParentClass
sqlParentClass="Select * From SK_ProClass where ProClassID 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("ProClassName") & "<br>"
rsParentClass.movenext
loop
rsParentClass.close
set rsParentClass=Nothing
end if
%> </select></td>
    </tr>
    <tr > 
      <td width="350"><strong>分类名称：</strong></td>
      <td><input name="ProClassName" type="text" value="<%=rsClass("ProClassName")%>" size="37" maxlength="20"> 
        <input name="ProClassID" type="hidden" id="ProClassID" value="<%=rsClass("ProClassID")%>"></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.ProClass.asp'" style="cursor:hand;" class="button1"> 
</td>
</tr>
</table>
</form>
<script language="JavaScript" type="text/JavaScript">
function check()
{
  if (document.form1.ProClassName.value=="")
  {
    alert("分类名称不能为空！");
document.form1.ProClassName.focus();
return false;
  }
}
</script>
<%
end if
rsClass.close
set rsClass=Nothing
end sub

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

sql="select * From SK_ProClass where ProClassID=" & ProClassID
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.ProClass.asp">
<TABLE borderColor=#ffffff cellSpacing=0 borderColorDark=#ffffff cellPadding=3 width="99%" 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("ProClassName")%> <input name="ProClassID" type="hidden" id="ProClassID" value="<%=rsClass("ProClassID")%>"></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_ProClass where ProClassID 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("ProClassName") & "<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.ProClass.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_ProClass 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="99%" 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("ProClassName")%></td> 
<% 
if j>1 then 
  response.write "<form action='SK.ProClass.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=ProClassID value="&rsClass("ProClassID")&">"
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.ProClass.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=ProClassID value="&rsClass("ProClassID")&">"
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_ProClass 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="99%" 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("ProClassName") 
if rsClass("Child")>0 then 
response.write "(" & rsClass("Child") & ")" 
end if 
%></td> 
<% 
if rsClass("ParentID")>0 then   '如果不是一级分类，则算出相同深度的分类数目，得到该分类在相同深度的分类中所处位置(之上或者之下的分类数) 
'所能提升最大幅度应为For i=1 to 该版之上的版面数 
set trs=conn.execute("select count(ProClassID) From SK_ProClass 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.ProClass.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=ProClassID value="&rsClass("ProClassID")&">&nbsp;<input type=submit name=Submit value=修改>" 
response.write "</td></form>" 
else 
response.write "<td width='150'>&nbsp;</td>" 
end if 
trs.close 
'所能降低最大幅度应为For i=1 to 该版之下的版面数 
set trs=conn.execute("select count(ProClassID) From SK_ProClass 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.ProClass.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=ProClassID value="&rsClass("ProClassID")&">&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="99%" 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 height="150"><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.ProClass.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="99%" 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="ProClassID" id="ProClassID">
        <%call Admin_ShowClass_Option(1,0)%>
        </select>
        合并到
        <select name="TargetProClassID" id="TargetProClassID">
        <%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.ProClass.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.ProClassID.value==document.myform.TargetProClassID.value)
  {
    alert("请不要在相同分类内进行操作！");
document.myform.TargetProClassID.focus();
return false;
  }
  if (document.myform.TargetProClassID.value=="")
  {
    alert("目标分类不能指定为含有子分类的分类！");
document.myform.TargetProClassID.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>
<%Call CloseDB()%>
</html>
<% 
sub SaveAdd()
dim sql,rs,trs
dim RootID,ParentDepth,ParentPath,ParentStr,ParentName,MaxProClassID,MaxRootID
dim PrevID,NextID,Child
if ProClassName="" then
Response.Write("<script>alert('分类名称不能为空！');location.href=""javascript:history.go(-1)"";</script>")
Response.End 
end if
set rs = conn.execute("select Max(ProClassID) From SK_ProClass")
MaxProClassID=rs(0)
if isnull(MaxProClassID) then
MaxProClassID=0
end if
rs.close
ProClassID=MaxProClassID+1
set rs=conn.execute("select max(rootid) From SK_ProClass")
MaxRootID=rs(0)
if isnull(MaxRootID) then
MaxRootID=0
end if
rs.close
RootID=MaxRootID+1

if ParentID>0 then
sql="select * From SK_ProClass where ProClassID=" & 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("ProClassName")
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_ProClass where ParentID=" & ParentID)
PrevOrderID=rsPrevOrderID(0)
set trs=conn.execute("select ProClassID From SK_ProClass where ParentID=" & ParentID & " and OrderID=" & PrevOrderID)
PrevID=trs(0)

'得到同一父分类但比本分类级数大的子分类的最大OrderID，如果比前一个值大，则改用这个值。
set rsPrevOrderID=conn.execute("select Max(OrderID) From SK_ProClass 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 ProClassID From SK_ProClass 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_ProClass Where ParentID=" & ParentID & " AND ProClassName='" & ProClassName & "'"
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>已经存在一级分类：" & ProClassName & ""
else
ErrMsg=ErrMsg & "<br>“" & ParentName & "”中已经存在子分类“" & ProClassName & "”！"
end if
rs.close
set rs=Nothing
exit sub
end if
rs.close

sql="Select top 1 * From SK_ProClass"
rs.open sql,conn,1,3
    rs.addnew
rs("ProClassID")=ProClassID
   rs("ProClassName")=ProClassName
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_ProClass set NextID=" & ProClassID & " where ProClassID=" & PrevID)
end if

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

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

sub SaveModify()
dim ProClassName
dim trs,rs
dim ProClassID,sql,rsClass,i
dim SkinCount,LayoutCount
ProClassID=trim(request("ProClassID"))
if ProClassID="" then
response.Write "<br>参数不足！"
response.End()
else
ProClassID=CLng(ProClassID)
end if
ProClassName=F("ProClassName")
if ProClassName="" then
Response.Write("<script>alert('分类名称不能为空！');location.href=""javascript:history.go(-1)"";</script>")
Response.End 
end if

if FoundErr=True then
exit sub
end if

sql="select * From SK_ProClass where ProClassID=" & ProClassID
set rsClass=server.CreateObject ("Adodb.recordset")
rsClass.open sql,conn,1,3
if rsClass.bof and rsClass.eof then
Response.Write("<script>alert('找不到指定的分类！');location.href=""javascript:history.go(-1)"";</script>")
Response.End 
rsClass.close
set rsClass=Nothing
exit sub
end if
if rsClass("Child")>0 then
Response.Write("<script>alert('本分类有子分类，不能进行修改操作！');location.href=""javascript:history.go(-1)"";</script>")
Response.End 
end if
rsClass("ProClassName")=ProClassName
rsClass.update
rsClass.close
set rsClass=Nothing
set rs=Nothing
set trs=Nothing
Response.Redirect "SK.ProClass.asp"  
end sub

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

sql="select ProClassID,RootID,Depth,ParentID,Child,PrevID,NextID From SK_ProClass where ProClassID="&ProClassID
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_ProClass set child=child-1 where ProClassID=" & rs("ParentID"))
end if
rs.delete
rs.update
rs.close
set rs=Nothing
'删除本分类的所有产品
conn.execute("delete from SK_Product where SK_ProType=" & ProClassID)

'修改上一分类的NextID和下一分类的PrevID
if PrevID>0 then
conn.execute "update SK_ProClass set NextID=" & NextID & " where ProClassID=" & PrevID
end if
if NextID>0 then
conn.execute "update SK_ProClass set PrevID=" & PrevID & " where ProClassID=" & NextID
end if
call CloseConn()
response.redirect "SK.ProClass.asp"

end sub

sub ClearClass()
dim strProClassID,rs,trs,SuccessMsg,ProClassID
ProClassID=trim(Request("ProClassID"))
if ProClassID="" then
FoundErr=True
ErrMsg=ErrMsg & "<br>参数不足！"
exit sub
else
ProClassID=CLng(ProClassID)
end if
strProClassID=cstr(ProClassID)
set rs=conn.execute("select ProClassID,Child,ParentPath From SK_ProClass where ProClassID=" & ProClassID)
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 ProClassID From SK_ProClass where ParentID=" & rs(0))
do while not trs.eof
strProClassID=strProClassID & "," & trs(0)
trs.movenext
loop
trs.close
set trs=conn.execute("select ProClassID From SK_ProClass where ParentPath like '" & rs(2) & "," & rs(0) & ",%'")
do while not trs.eof
strProClassID=strProClassID & "," & 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 (" & strProClassID & ")")
'conn.execute("delete from SK_Recycle where ID in (" & strProClassID & ")")
SuccessMsg="此分类(包括子分类)的所有文章已经被移到回收站中！"
call WriteSuccessMsg(SuccessMsg)
end sub

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

sql="select * From SK_ProClass where ProClassID=" & ProClassID
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("ProClassID") 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_ProClass where ProClassID="&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 ProClassID From SK_ProClass where ParentPath like '"&rsClass("ParentPath")&"," & rsClass("ProClassID") & "%' and ProClassID="&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("ProClassID")
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

'假如更改了所属分类
'需要更新其原来所属分类信息，包括深度、父级ID、分类数、排序、继承版主等数据
'需要更新当前所属分类信息
'继承版主数据需要另写函数进行更新--取消，在前台可用ProClassID in ParentPath来获得

dim mrs,MaxRootID
set mrs=conn.execute("select max(rootid) From SK_ProClass")
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  '假如更改了所属分类
'更新原来同一父分类的上一个分类的NextID和下一个分类的PrevID
if PrevID>0 then
conn.execute "update SK_ProClass set NextID=" & NextID & " where ProClassID=" & PrevID
end if
if NextID>0 then
conn.execute "update SK_ProClass set PrevID=" & PrevID & " where ProClassID=" & NextID
end if

if iParentID>0 and rParentID=0 then  '如果原来不是一级分类改成一级分类
'得到上一个一级分类分类
sql="select ProClassID,NextID From SK_ProClass where RootID=" & MaxRootID & " and Depth=0"
set rs=server.CreateObject("Adodb.recordset")
rs.open sql,conn,1,3
PrevID=rs(0)      '得到新的PrevID
rs(1)=ProClassID     '更新上一个一级分类分类的NextID的值
rs.update
rs.close
set rs=Nothing

MaxRootID=MaxRootID+1
'更新当前分类数据
conn.execute("update SK_ProClass set depth=0,OrderID=0,rootid="&maxrootid&",parentid=0,ParentPath='0',PrevID=" & PrevID & ",NextID=0 where ProClassID="&ProClassID)
'如果有下属分类，则更新其下属分类数据。下属分类的排序不需考虑，只需更新下属分类深度和一级排序ID(rootid)数据
if child>0 then
i=0
ParentPath=ParentPath & ","
set rs=conn.execute("select * From SK_ProClass where ParentPath like '%"&ParentPath & ProClassID&"%'")
do while not rs.eof
i=i+1
mParentPath=replace(rs("ParentPath"),ParentPath,"")
conn.execute("update SK_ProClass set depth=depth-"&depth&",rootid="&maxrootid&",ParentPath='"&mParentPath&"' where ProClassID="&rs("ProClassID"))
rs.movenext
loop
rs.close
set rs=Nothing
end if

'更新其原来所属分类的分类数，排序相当于剪枝而不需考虑
conn.execute("update SK_ProClass set child=child-1 where ProClassID="&iParentID)

elseif iParentID>0 and rParentID>0 then    '如果是将一个分分类移动到其他分分类下
'得到当前分类的下属子分类数
ParentPath=ParentPath & ","
set rs=conn.execute("select count(*) From SK_ProClass where ParentPath like '%"&ParentPath & ProClassID&"%'")
ClassCount=rs(0)
if isnull(ClassCount) then
ClassCount=1
end if
rs.close
set rs=Nothing

'获得目标分类的相关信息
set trs=conn.execute("select * From SK_ProClass where ProClassID="&rParentID)
if trs("Child")>0 then
'得到与本分类同级的最后一个分类的OrderID
set rsPrevOrderID=conn.execute("select Max(OrderID) From SK_ProClass where ParentID=" & trs("ProClassID"))
PrevOrderID=rsPrevOrderID(0)
'得到与本分类同级的最后一个分类的ProClassID
sql="select ProClassID,NextID From SK_ProClass where ParentID=" & trs("ProClassID") & " and OrderID=" & PrevOrderID
set rs=server.createobject("adodb.recordset")
rs.open sql,conn,1,3
PrevID=rs(0)    '得到新的PrevID
rs(1)=ProClassID     '更新上一个分类的NextID的值
rs.update
rs.close
set rs=Nothing

'得到同一父分类但比本分类级数大的子分类的最大OrderID，如果比前一个值大，则改用这个值。
set rsPrevOrderID=conn.execute("select Max(OrderID) From SK_ProClass where ParentPath like '" & trs("ParentPath") & "," & trs("ProClassID") & ",%'")
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_ProClass set OrderID=OrderID+" & ClassCount & "+1 where rootid=" & trs("rootid") & " and OrderID>" & PrevOrderID)

'更新当前分类数据
conn.execute("update SK_ProClass set depth="&trs("depth")&"+1,OrderID="&PrevOrderID&"+1,rootid="&trs("rootid")&",ParentID="&rParentID&",ParentPath='" & trs("ParentPath") & "," & trs("ProClassID") & "',PrevID=" & PrevID & ",NextID=0 where ProClassID="&ProClassID)

'如果有子分类则更新子分类数据，深度为原来的相对深度加上当前所属分类的深度
set rs=conn.execute("select * From SK_ProClass where ParentPath like '%"&ParentPath&ProClassID&"%' order by OrderID")
i=1
do while not rs.eof
i=i+1
iParentPath=trs("ParentPath") & "," & trs("ProClassID") & "," & replace(rs("ParentPath"),ParentPath,"")
conn.execute("update SK_ProClass set depth=depth-"&depth&"+"&trs("depth")&"+1,OrderID="&PrevOrderID&"+"&i&",rootid="&trs("rootid")&",ParentPath='"&iParentPath&"' where ProClassID="&rs("ProClassID"))
rs.movenext
loop
rs.close
set rs=Nothing
trs.close
set trs=Nothing

'更新所指向的上级分类的子分类数
conn.execute("update SK_ProClass set child=child+1 where ProClassID="&rParentID)

'更新其原父类的子分类数
conn.execute("update SK_ProClass set child=child-1 where ProClassID="&iParentID)
else    '如果原来是一级分类改成其他分类的下属分类
'得到移动的分类总数
set rs=conn.execute("select count(*) From SK_ProClass where rootid="&rootid)
ClassCount=rs(0)
rs.close
set rs=Nothing

'获得目标分类的相关信息
set trs=conn.execute("select * From SK_ProClass where ProClassID="&rParentID)
if trs("Child")>0 then
'得到与本分类同级的最后一个分类的OrderID
set rsPrevOrderID=conn.execute("select Max(OrderID) From SK_ProClass where ParentID=" & trs("ProClassID"))
PrevOrderID=rsPrevOrderID(0)
sql="select ProClassID,NextID From SK_ProClass where ParentID=" & trs("ProClassID") & " and OrderID=" & PrevOrderID
set rs=server.createobject("adodb.recordset")
rs.open sql,conn,1,3
PrevID=rs(0)
rs(1)=ProClassID
rs.update
set rs=Nothing

'得到同一父分类但比本分类级数大的子分类的最大OrderID，如果比前一个值大，则改用这个值。
set rsPrevOrderID=conn.execute("select Max(OrderID) From SK_ProClass where ParentPath like '" & trs("ParentPath") & "," & trs("ProClassID") & ",%'")
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_ProClass set OrderID=OrderID+" & ClassCount &"+1 where rootid=" & trs("rootid") & " and OrderID>" & PrevOrderID)

conn.execute("update SK_ProClass set PrevID=" & PrevID & ",NextID=0 where ProClassID=" & ProClassID)
set rs=conn.execute("select * From SK_ProClass 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("ProClassID")
conn.execute("update SK_ProClass set depth=depth+"&trs("depth")&"+1,OrderID="&PrevOrderID&"+"&i&",rootid="&trs("rootid")&",ParentPath='"&ParentPath&"',parentid="&rParentID&" where ProClassID="&rs("ProClassID"))
else
ParentPath=trs("ParentPath") & "," & trs("ProClassID") & "," & replace(rs("ParentPath"),"0,","")
conn.execute("update SK_ProClass set depth=depth+"&trs("depth")&"+1,OrderID="&PrevOrderID&"+"&i&",rootid="&trs("rootid")&",ParentPath='"&ParentPath&"' where ProClassID="&rs("ProClassID"))
end if
rs.movenext
loop
rs.close
set rs=Nothing
trs.close
set trs=Nothing
'更新所指向的上级分类分类数
conn.execute("update SK_ProClass set child=child+1 where ProClassID="&rParentID)

end if
  end if

  call CloseConn()
  Response.Redirect "SK.ProClass.asp"  
end sub

sub UpOrder()
dim ProClassID,sqlOrder,rsOrder,MoveNum,cRootID,tRootID,i,rs,PrevID,NextID
ProClassID=trim(request("ProClassID"))
cRootID=Trim(request("cRootID"))
MoveNum=trim(request("MoveNum"))
if ProClassID="" then
FoundErr=True
ErrMsg=ErrMsg & "<br>参数不足！"
else
ProClassID=CLng(ProClassID)
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_ProClass where ProClassID=" & ProClassID)
PrevID=rs(0)
NextID=rs(1)
rs.close
set rs=Nothing
'先修改上一分类的NextID和下一分类的PrevID
if PrevID>0 then
conn.execute "update SK_ProClass set NextID=" & NextID & " where ProClassID=" & PrevID
end if
if NextID>0 then
conn.execute "update SK_ProClass set PrevID=" & PrevID & " where ProClassID=" & NextID
end if

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

'然后将位于当前分类以上的分类的RootID依次加一，为要提升的数字
sqlOrder="select * From SK_ProClass 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_ProClass set RootID=RootID+1 where RootID=" & tRootID)
i=i+1
if i>MoveNum then
rsOrder("PrevID")=ProClassID
rsOrder.update
conn.execute("update SK_ProClass set NextID=" & rsOrder("ProClassID") & " where ProClassID=" & ProClassID)
exit do
end if
rsOrder.movenext
loop
rsOrder.movenext
if rsOrder.eof then
conn.execute("update SK_ProClass set PrevID=0 where ProClassID=" & ProClassID)
else
rsOrder("NextID")=ProClassID
rsOrder.update
conn.execute("update SK_ProClass set PrevID=" & rsOrder("ProClassID") & " where ProClassID=" & ProClassID)
end if
rsOrder.close
set rsOrder=Nothing

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

sub DownOrder()
dim ProClassID,sqlOrder,rsOrder,MoveNum,cRootID,tRootID,i,rs,PrevID,NextID
ProClassID=trim(request("ProClassID"))
cRootID=Trim(request("cRootID"))
MoveNum=trim(request("MoveNum"))
if ProClassID="" then
FoundErr=True
ErrMsg=ErrMsg & "<br>参数不足！"
else
ProClassID=CLng(ProClassID)
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_ProClass where ProClassID=" & ProClassID)
PrevID=rs(0)
NextID=rs(1)
rs.close
set rs=Nothing
'先修改上一分类的NextID和下一分类的PrevID
if PrevID>0 then
conn.execute "update SK_ProClass set NextID=" & NextID & " where ProClassID=" & PrevID
end if
if NextID>0 then
conn.execute "update SK_ProClass set PrevID=" & PrevID & " where ProClassID=" & NextID
end if

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

'然后将位于当前分类以下的分类的RootID依次减一，为要下降的数字
sqlOrder="select * From SK_ProClass 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")       '得到要提升位置的RootID，包括子分类
conn.execute("update SK_ProClass set RootID=RootID-1 where RootID=" & tRootID)
i=i+1
if i>MoveNum then
rsOrder("NextID")=ProClassID
rsOrder.update
conn.execute("update SK_ProClass set PrevID=" & rsOrder("ProClassID") & " where ProClassID=" & ProClassID)
exit do
end if
rsOrder.movenext
loop
rsOrder.movenext
if rsOrder.eof then
conn.execute("update SK_ProClass set NextID=0 where ProClassID=" & ProClassID)
else
rsOrder("PrevID")=ProClassID
rsOrder.update
conn.execute("update SK_ProClass set NextID=" & rsOrder("ProClassID") & " where ProClassID=" & ProClassID)
end if
rsOrder.close
set rsOrder=Nothing

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

sub UpOrderN()
dim sqlOrder,rsOrder,MoveNum,ProClassID,i
dim ParentID,OrderID,ParentPath,Child,PrevID,NextID
ProClassID=Trim(request("ProClassID"))
MoveNum=trim(request("MoveNum"))
if ProClassID="" then
FoundErr=true
ErrMsg=ErrMsg & "<br>错误参数！"
else
ProClassID=CLng(ProClassID)
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_ProClass where ProClassID="&ProClassID)
ParentID=rs(0)
OrderID=rs(1)
ParentPath=rs(2) & "," & ProClassID
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_ProClass where ParentPath like '%"&ParentPath&"%'")
oldorders=rs(0)
rs.close
set rs=Nothing
else
oldorders=0
end if
'先修改上一分类的NextID和下一分类的PrevID
if PrevID>0 then
conn.execute "update SK_ProClass set NextID=" & NextID & " where ProClassID=" & PrevID
end if
if NextID>0 then
conn.execute "update SK_ProClass set PrevID=" & PrevID & " where ProClassID=" & NextID
end if

'和该分类同级且排序在其之上的分类------更新其排序，为要提升的数字
sql="select ProClassID,OrderID,child,ParentPath,PrevID,NextID From SK_ProClass 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_ProClass set OrderID="&tOrderID+oldorders+i&" where ProClassID="&rs(0))
if rs(2)>0 then
ii=i+1
set trs=conn.execute("select ProClassID,OrderID From SK_ProClass 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_ProClass set OrderID="&tOrderID+oldorders+ii&" where ProClassID="&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)=ProClassID
rs.update
conn.execute("update SK_ProClass set NextID=" & rs(0) & " where ProClassID=" & ProClassID)
exit do
end if
rs.movenext
loop
rs.movenext
if rs.eof then
conn.execute("update SK_ProClass set PrevID=0 where ProClassID=" & ProClassID)
else
rs(5)=ProClassID
rs.update
conn.execute("update SK_ProClass set PrevID=" & rs(0) & " where ProClassID=" & ProClassID)
end if
rs.close
set rs=Nothing

'更新所要排序的分类的序号
conn.execute("update SK_ProClass set OrderID="&tOrderID&" where ProClassID="&ProClassID)
'如果有下属分类，则更新其下属分类排序
if child>0 then
i=1
set rs=conn.execute("select ProClassID From SK_ProClass where ParentPath like '%"&ParentPath&"%' order by OrderID")
do while not rs.eof
conn.execute("update SK_ProClass set OrderID="&tOrderID+i&" where ProClassID="&rs(0))
i=i+1
rs.movenext
loop
rs.close
set rs=Nothing
end if
call CloseConn()
response.Redirect "SK.ProClass.asp?Action=OrderN"
end sub

sub DownOrderN()
dim sqlOrder,rsOrder,MoveNum,ProClassID,i
dim ParentID,OrderID,ParentPath,Child,PrevID,NextID
ProClassID=Trim(request("ProClassID"))
MoveNum=trim(request("MoveNum"))
if ProClassID="" then
FoundErr=true
ErrMsg=ErrMsg & "<br>错误参数！"
exit sub
else
ProClassID=Cint(ProClassID)
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_ProClass where ProClassID="&ProClassID)
ParentID=rs(0)
OrderID=rs(1)
ParentPath=rs(2) & "," & ProClassID
child=rs(3)
PrevID=rs(4)
NextID=rs(5)
rs.close
set rs=Nothing

'先修改上一分类的NextID和下一分类的PrevID
if PrevID>0 then
conn.execute "update SK_ProClass set NextID=" & NextID & " where ProClassID=" & PrevID
end if
if NextID>0 then
conn.execute "update SK_ProClass set PrevID=" & PrevID & " where ProClassID=" & NextID
end if

'和该分类同级且排序在其之下的分类------更新其排序，为要下降的数字
sql="select ProClassID,OrderID,child,ParentPath,PrevID,NextID From SK_ProClass 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_ProClass set OrderID="&OrderID+ii&" where ProClassID="&rs(0))
if rs(2)>0 then
set trs=conn.execute("select ProClassID,OrderID From SK_ProClass 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_ProClass set OrderID="&OrderID+ii&" where ProClassID="&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)=ProClassID
rs.update
conn.execute("update SK_ProClass set PrevID=" & rs(0) & " where ProClassID=" & ProClassID)
exit do
end if
rs.movenext
loop
rs.movenext
if rs.eof then
conn.execute("update SK_ProClass set NextID=0 where ProClassID=" & ProClassID)
else
rs(4)=ProClassID
rs.update
conn.execute("update SK_ProClass set NextID=" & rs(0) & " where ProClassID=" & ProClassID)
end if
rs.close
set rs=Nothing

'更新所要排序的分类的序号
conn.execute("update SK_ProClass set OrderID="&OrderID+ii&" where ProClassID="&ProClassID)
'如果有下属分类，则更新其下属分类排序
if child>0 then
i=1
set rs=conn.execute("select ProClassID From SK_ProClass where ParentPath like '%"&ParentPath&"%' order by OrderID")
do while not rs.eof
conn.execute("update SK_ProClass set OrderID="&OrderID+ii+i&" where ProClassID="&rs(0))
i=i+1
rs.movenext
loop
rs.close
set rs=Nothing
end if
call CloseConn()
response.Redirect "SK.ProClass.asp?Action=OrderN"
end sub

sub SaveReset()
dim i,sql,rs,SuccessMsg,iCount,PrevID,NextID
sql="select ProClassID From SK_ProClass 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_ProClass set RootID=" & i & ",OrderID=0,ParentID=0,Child=0,ParentPath='0',Depth=0,PrevID=" & PrevID & ",NextID=" & NextID & " where ProClassID=" & 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 ProClassID,TargetProClassID,ParentPath,iParentPath,Depth,iParentID,Child,PrevID,NextID
dim rs,trs,i,SuccessMsg
ProClassID=trim(request("ProClassID"))
TargetProClassID=trim(request("TargetProClassID"))
if ProClassID="" then
FoundErr=True
ErrMsg=ErrMsg & "<br>请指定要合并的分类！"
else
ProClassID=CLng(ProClassID)
end if
if TargetProClassID="" then
FoundErr=True
ErrMsg=ErrMsg & "<br>请指定目标分类！"
else
TargetProClassID=CLng(TargetProClassID)
end if
if ProClassID=TargetProClassID then
FoundErr=True
ErrMsg=ErrMsg & "<br>请不要在相同分类内进行操作"
end if
if FoundErr=True then
exit sub
end if
'判断目标分类是否有子分类，如果有，则报错。
set rs=conn.execute("select Child From SK_ProClass where ProClassID=" & TargetProClassID)
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 ProClassID,ParentID,ParentPath,PrevID,NextID,Depth From SK_ProClass where ProClassID="&ProClassID)
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 ProClassID From SK_ProClass where ProClassID="&TargetProClassID&" 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 ProClassID From SK_ProClass 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=ProClassID
end if

'先修改上一分类的NextID和下一分类的PrevID
if PrevID>0 then
conn.execute "update SK_ProClass set NextID=" & NextID & " where ProClassID=" & PrevID
end if
if NextID>0 then
conn.execute "update SK_ProClass set PrevID=" & PrevID & " where ProClassID=" & NextID
end if

'更新文章及评论所属分类
'conn.execute("update Article set ProClassID="&TargetProClassID&" where ProClassID in ("&ParentPath&")")
'conn.execute("update ArticleComment set ProClassID="&TargetProClassID&" where ProClassID in ("&ParentPath&")")

'删除被合并分类及其下属分类
'conn.execute("delete From SK_ProClass where ProClassID in ("&ParentPath&")")

'更新其原来所属分类的子分类数，排序相当于剪枝而不需考虑
if Depth>0 then
conn.execute("update SK_ProClass set Child=Child-1 where ProClassID="&iParentID)
end if

SuccessMsg="分类合并成功！已经将被合并分类及其下属子分类的所有数据转入目标分类中。<br><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_ProClass 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("ProClassID") & "'"
end if
elseif ShowType=4 then
if rsClass("Child")>0 then
strTemp="<option value=''"
strTemp="<option value='0'"
else
strTemp="<option value='" & rsClass("ProClassID") & "'"
end if
else
strTemp="<option value='" & rsClass("ProClassID") & "'"
end if
if CurrentID>0 and rsClass("ProClassID")=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("ProClassName")
strTemp=strTemp & "</option>"
response.write strTemp
rsClass.movenext
loop
end if
rsClass.close
set rsClass=Nothing
end sub

function CloseConn()
conn.close
end function
%>