<!--#include file="../inchuayuwebrlzywzhyweb/CONN.ASP"-->
<!--#include file="../inchuayuwebrlzywzhyweb/cls_huayuweb()^191563631.asp"-->
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<%oa.chkadmin()%>
<%
select case oa.act
       case "add"  
	   call class_add()
	   case "mod" 
	   call class_mod()
	   case "clear"
	   call class_clear()
	   case "del" 
	   
	   
       call class_del()
	   case "down"
	   call class_down()
	   case "up"
	   call class_up()
end select
sub class_clear()
dim classid,parentpath
classid=request.QueryString("classid")

if classid="" or not isnumeric(classid) then oa.msgshow("非法操作！")
set rs=oa.execute("select classid,parentpath from classlist where classid="&classid)
if rs.eof then oa.msgshow("该频道名称不存在！请返回！")
parentpath=rs(1)
rs.close
 oa.execute("delete from c_article where classid in(select classid from classlist where parentpath like '"&parentpath&"%' or classid="&classid&")")
 oa.msgshowurl "操作成功！",oa.tmpstr

end sub

sub class_del()
dim classid,child
dim sql,PrevID,NextID
classid=request.QueryString("classid")

if classid="" or not isnumeric(classid) then oa.msgshow("非法操作！")

set rs=oa.execute("select classid,child from classlist where classid="&classid)
if rs.eof then oa.msgshow("该频道名称不存在！请返回！")
child=rs(1)
rs.close

	sql="select ClassID,RootID,Depth,ParentID,Child,PrevID,NextID From ClassList where ClassID="&ClassID
	set rs=oa.execute(sql)

	if rs.bof and rs.eof then
		oa.msgshow("频道不存在，或者已经被删除")
	else
		if rs("Child")>0 then
			oa.msgshow("该频道含有子频道，请删除其子频道后再进行删除本频道的操作")
		end if
	end if
	
	PrevID=rs("PrevID")
	NextID=rs("NextID")
	if rs("Depth")>0 then
		oa.execute("update ClassList set child=child-1 where ClassID=" & rs("ParentID"))
	end if
	oa.execute("delete from classlist where classid="&classid)
	
	rs.close
	set rs=nothing
	'删除本频道的所有文章和评论
		
	'修改上一频道的NextID和下一频道的PrevID
	if PrevID>0 then
		oa.execute "update ClassList set NextID=" & NextID & " where ClassID=" & PrevID
	end if
	if NextID>0 then
		oa.execute "update ClassList set PrevID=" & PrevID & " where ClassID=" & NextID
	end if
	
	oa.execute("delete from c_article where classid="&classid)


	 oa.msgshowurl "操作成功",oa.tmpstr

end sub


sub class_mod()
dim Parentid,classid,classname,class_show,class_row,class_pic,class_link,class_sfdh,class_intro
Parentid=oa.sqlrequest("Parentid")
classid=oa.sqlrequest("classid")
classname=request("classname")
class_sfdh=oa.sqlrequest("class_sfdh")
class_show=oa.sqlrequest("class_show")
class_row=oa.sqlrequest("class_row")
class_pic=oa.sqlrequest("class_pic")
class_link=oa.sqlrequest("class_link")
class_intro=oa.sqlrequest("class_intro")
class_sfcp=oa.sqlrequest("class_sfcp")
if classid="" or not isnumeric(classid) then oa.msgshow("非法操作！")
set rs=oa.execute("select classid from classlist where classid="&classid)
if rs.eof then oa.msgshow("该频道名称不存在！请返回！")
rs.close
    set rs=oa.execute("select classid from classlist where parentid="&Parentid&" and classname='"&classname&"' and classid<>"&classid)
	if not rs.eof then oa.msgshow("该频道名称已经存在！请返回重新输入！")
	rs.close
    set rs=server.createobject("adodb.recordset")
	rs.open "select * from classlist where classid="&classid,conn,1,3
	rs("classname")=classname
	rs("class_sfdh")=class_sfdh
	rs("class_sfcp")=class_sfcp
	rs("class_show")=class_show
	rs("width")=oa.sqlrequest("width")
	rs("height")=oa.sqlrequest("height")
	rs("class_cell")=oa.sqlrequest("class_cell")
	rs("class_title")=oa.sqlrequest("class_title")
	rs("class_time")=oa.sqlrequest("class_time")
	rs("class_row")=class_row
	rs("class_pic")=class_pic
	rs("class_link")=class_link
	rs("class_intro")=class_intro
	rs("ctitle")=request("ctitle")
	rs("ckeywords")=request("ckeywords")
	rs("ccontent")=request("ccontent")
	rs("zdylj")=request("zdylink")
	rs("ename")=request("ename")
	rs.update()
	rs.close
oa.msgshowurl "操作成功！",oa.tmpstr
end sub


sub class_add()
    dim sql
	dim ParentDepth,ParentPath,ParentStr,ParentName,MaxClassID,MaxRootID,PrevOrderID
	dim ClassID,RootID,ParentID,PrevID,NextID,Child,Depth,OrderID
	dim classname
	ClassID=oa.getid("classlist")
	classname=request("classname")
	ParentID=oa.sqlrequest("ParentID")
	
	set rs=oa.execute("select classid from classlist where parentid="&ParentID&" and classname='"&classname&"'")

	if not rs.eof then oa.msgshow("该栏目名称已经存在！请返回重新输入！")
	rs.close
		set rc=server.CreateObject("adodb.recordset")
	rc.open"select  Depth  from classlist  where classid="&ParentID&" order by rootid,orderid",conn,1,1
	if not rc.eof then
	if rc(0)=2 then 
	oa.msgshow("该栏目是最低端类，下面不能再添加小类!")
	end if
	end if
	rc.close
	set rc=nothing
If ParentID>0 then
        sql="select * From classlist where ClassID=" & ParentID & ""
		set rs=oa.execute(sql)
		if rs.bof and rs.eof then
		oa.msgshow("所属频道已经被删除！")
		end if
	
			RootID=rs("RootID")
			ParentName=rs("ClassName")
			ParentDepth=rs("Depth")
			ParentPath=rs("ParentPath")
			Child=rs("Child")			
			PrevOrderID=rs("OrderID")
		rs.close
		
			if Child>0 then	
			
				set rs=oa.execute("select Max(OrderID) From classlist where ParentID=" & ParentID)
			
				PrevOrderID=rs(0)
				rs.close
				set rs=oa.execute("select ClassID from classlist where ParentID=" & ParentID & " and OrderID=" & PrevOrderID)
				PrevID=rs(0)
				rs.close
					
				'得到同一父频道但比本频道级数大的子频道的最大OrderID，如果比前一个值大，则改用这个值。
				set rs=oa.execute("select Max(OrderID) From classlist where ParentPath like '" & ParentPath & ",%'")
				if (not(rs.bof and rs.eof)) then
					if not IsNull(rs(0))  then
				 		if rs(0)>PrevOrderID then
							PrevOrderID=rs(0)
						end if
					end if
				end if
				rs.close
			else
				PrevID=0
			end if
	Depth=ParentDepth+1
	OrderID=PrevOrderID
	ParentPath=ParentPath & "," & ParentID     '得到此频道的父级频道路径
Else
	set rs=oa.execute("select max(rootid) from classlist")	
	RootID=rs(0)
	rs.close		
	if RootID="" or isnull(rootid) then rootid=0	
	RootID=RootID+1	
	    if rootid>1 then
			set rs=oa.execute("select ClassID from classlist where RootID=" & (rootid-1) & " and Depth=0")
			PrevID=rs(0)
			rs.close
		else
			PrevID=0
		end if
		
		ParentPath=0
		Depth=0		
		OrderID=0
End If	
	set rs=server.createobject("adodb.recordset")
	rs.open "select * from classlist",conn,1,3
	rs.addnew()
	rs("classid")=classid
	rs("classname")=ClassName
	rs("parentid")=ParentID
	rs("parentpath")=ParentPath
	rs("depth")=Depth
	rs("rootid")=rootid
	rs("child")=0
	rs("PrevID")=PrevID
	rs("NextID")=0
	rs("orderid")=OrderID	
	rs("class_show")=oa.sqlrequest("class_show")
	rs("width")=oa.sqlrequest("width")
	rs("height")=oa.sqlrequest("height")
	rs("class_cell")=oa.sqlrequest("class_cell")
	rs("class_title")=oa.sqlrequest("class_title")
	rs("class_time")=oa.sqlrequest("class_time")
	rs("class_sfdh")=oa.sqlrequest("class_sfdh")
	rs("class_sfcp")=oa.sqlrequest("class_sfcp")
	rs("class_row")=oa.sqlrequest("class_row")
	rs("class_pic")=oa.sqlrequest("class_pic")
	rs("class_link")=oa.sqlrequest("class_link")
	rs("class_intro")=oa.sqlrequest("class_intro")
	rs("ctitle")=request("ctitle")
	rs("ckeywords")=request("ckeywords")
	rs("ccontent")=request("ccontent")
	rs("zdylj")=request("zdylink")
	rs("ename")=request("ename")
	rs("class_article")=0
	rs.update()
	rs.close
	
if PrevID>0 then
	oa.execute("update classlist set NextID=" & ClassID & " where ClassID=" & PrevID)
end if
IF ParentID>0 then
	'更新其父类的子频道数
		oa.execute("update classlist set child=child+1 where ClassID="&ParentID)
		
		'更新该频道排序以及大于本需要和同在本分类下的频道排序序号
		oa.execute("update classlist set OrderID=OrderID+1 where rootid=" & rootid & " and OrderID>" & PrevOrderID)
		oa.execute("update classlist set OrderID=" & PrevOrderID & "+1 where ClassID=" & ClassID)
		
Else	
	oa.execute("insert into d_manage_ulevel (classid,adminid) values ("&classid&","&oa.adminid&")")
	
End If	
oa.msgshowurl "操作成功",oa.tmpstr
end sub

Sub class_down()
dim ClassID,sqlOrder,rsOrder,rsChild,MoveNum,cRootID,tRootID,i,ii,PrevID,NextID,MaxRootID,ParentID,OrderID,Child,ParentPath
    i=0:ii=0:tRootID=0
	ClassID=trim(request("ClassID"))
	if not isnumeric(ClassID) then oa.msgshow("非法操作！")
	set rs=oa.execute("select PrevID,NextID,rootid,Parentid,OrderID,child,ParentPath from ClassList where ClassID=" & ClassID)
	if rs.eof then oa.msgshow("该栏目不存在或已经被管理员删除！请返回重新选择！")
	PrevID=rs(0)
	NextID=rs(1)
	cRootID=rs(2)
	ParentID=rs(3)
	OrderID=rs(4)
	Child=rs(5)
	ParentPath=rs(6)
	rs.close
	set rs=nothing
If ParentID>0 then
    if oa.execute("select count(classid) from classlist where ParentID="&ParentID&" and OrderID>"&OrderID)(0)<1 then
    response.Redirect(oa.tmpstr)
	exit sub
	end if
	if PrevID > 0 then
      oa.execute("update ClassList set NextID=" & NextID & " where ClassID=" & PrevID)
	end if
	if NextID > 0 then
      oa.execute("update ClassList set PrevID=" & PrevID & " where ClassID=" & NextID)
	end if
	
    sqlOrder="select * From ClassList where ParentID="&ParentID&" and OrderID>"&OrderID&" order by OrderID"	

	set rsOrder=oa.execute(sqlOrder)	
	if rsOrder.bof and rsOrder.eof then
		response.Redirect(oa.tmpstr)    '如果当前频道已经在最上面，则无需移动
		exit Sub    
	end if	
	do while not rsOrder.eof
      i = i + 1
	  if i=1 then
	     oa.execute("update ClassList set OrderID=" & OrderID & " where ClassID=" & rsOrder("ClassID"))
         oa.execute("update ClassList set NextID=" & ClassID & " where ClassID=" & rsOrder("ClassID"))
         oa.execute("update ClassList set PrevID=" & rsOrder("ClassID") & " where ClassID=" & ClassID)
		 if rsOrder("Child")>0 then
		    set rsChild=oa.execute("select * from classlist where ParentPath like '%" & rsOrder("ParentPath") & "," & rsOrder("classid") & "%' order by OrderID")
			
			do while not rsChild.eof
			  ii=ii+1
			  oa.execute("update Classlist set OrderID=" & (orderid + ii) & " where Classid=" & rsChild("classid"))
			  rsChild.movenext
			loop
			rsChild.close
		  end if
	  else
	     oa.execute("update Classlist set PrevID=" & ClassID & " where ClassID=" & rsOrder("ClassID"))
         oa.execute("update Classlist set NextID=" & rsOrder("ClassID") & " where ClassID=" & ClassID)
         exit do   
	  end if
	   oa.execute("update Classlist set NextID=0 where ClassID=" + ClassID)
   rsOrder.movenext
   loop	  
   rsOrder.close  

  oa.execute("update Classlist set OrderID=" & (OrderID + ii + 1) & " where ClassID="&ClassID)

               if Child > 0 then
                  i = 0
				  
				  set rs=oa.execute("select * from classlist where ParentPath like '%" & ParentPath & "," & ClassID & "%' order by OrderID")
				 
                   do while not rs.eof
                     i = i+1					
					 oa.execute("update classlist set OrderID=" & (OrderID + i + ii + 1) & " where Classid=" & rs("classid"))
                   rs.movenext
				   loop
                end if

Else
	MaxRootID = oa.execute("select max(RootID) from ClassList")(0)+1	
	if oa.execute("select count(classid) from classlist where ParentID=0 and RootID>"& cRootID)(0)<1 then
    response.Redirect(oa.tmpstr)
	exit sub
	end if
	
	oa.execute("update ClassList set RootID=" & MaxRootID & " where RootID=" & cRootID)
	
	'先修改上一频道的NextID和下一频道的PrevID
	if PrevID>0 then
		oa.execute "update ClassList set NextID=" & NextID & " where ClassID=" & PrevID
	end if
	if NextID>0 then
		oa.execute "update ClassList set PrevID=" & PrevID & " where ClassID=" & NextID
	end if
	
	'然后将位于当前频道以上的频道的RootID依次加一，范围为要提升的数字
	sqlOrder="select * From ClassList where ParentID=0 and RootID>" & cRootID & " and Rootid<>"&MaxRootID&" order by RootID"
	
	set rsOrder=oa.execute(sqlOrder)
	
	if rsOrder.bof and rsOrder.eof then
		response.Redirect(oa.tmpstr)    '如果当前频道已经在最上面，则无需移动
		exit Sub    
	end if	
	
	do while not rsOrder.eof
	
	    i=i+1
		if i=1 then		
		tRootID=rsOrder("RootID")       '得到要提升位置的RootID，包括子频道
	
		oa.execute("update ClassList set RootID=RootID-1 where RootID=" & tRootID)
		 oa.execute("update ClassList set NextID=" & ClassID & " where ClassID=" & rsOrder("ClassID"))
		oa.execute("update ClassList set PrevID=" & rsOrder("ClassID") & " where ClassID=" & ClassID)
		 
		else		
		 oa.execute("update ClassList set PrevID=" & ClassID & " where ClassID=" & rsOrder("ClassID"))
		 oa.execute("update ClassList set NextID=" & rsOrder("ClassID") & " where ClassID=" & ClassID)
		 exit do
		end if			
		oa.execute("update ClassList set NextID=0  where ClassID=" & ClassID)		
		rsOrder.movenext
	loop
	
		oa.execute("update ClassList set RootID=" & tRootID & " where RootID=" & MaxRootID)
	rsOrder.close
	set rsOrder=nothing
	
End IF
	response.Redirect(oa.tmpstr)


End Sub

Sub class_up()
dim ClassID,sqlOrder,rsOrder,rsChild,cRootID,tRootID,i,ii,PrevID,NextID,MaxRootID,ParentID,OrderID,Child,ParentPath,oldorders,TorderID
    i=0:ii=0:tRootID=0
	ClassID=trim(request("ClassID"))
	if not isnumeric(ClassID) then oa.msgshow("非法操作！")
	set rs=oa.execute("select PrevID,NextID,rootid,Parentid,OrderID,child,ParentPath from ClassList where ClassID=" & ClassID)
	if rs.eof then oa.msgshow("该栏目不存在或已经被管理员删除！请返回重新选择！")
	PrevID=rs(0)
	NextID=rs(1)
	cRootID=rs(2)
	ParentID=rs(3)
	OrderID=rs(4)
	Child=rs(5)
	ParentPath=rs(6)
	rs.close
	set rs=nothing
If ParentID>0 then
  oldorders = oa.execute("select count(ClassID) from ClassList where ParentPath like '%" &ParentPath & "," + ClassID + "%'")(0)
  
    if oa.execute("select count(classid) from classlist where ParentID="&ParentID&" and OrderID<"&OrderID)(0)<1 then
    response.Redirect(oa.tmpstr)
	exit sub
	end if
	if PrevID > 0 then
      oa.execute("update ClassList set NextID=" & NextID & " where ClassID=" & PrevID)
	end if
	if NextID > 0 then
      oa.execute("update ClassList set PrevID=" & PrevID & " where ClassID=" & NextID)
	end if
	
    sqlOrder="select * From ClassList where ParentID="&ParentID&" and OrderID<"&OrderID&" order by OrderID desc"	

	set rsOrder=oa.execute(sqlOrder)	
	if rsOrder.bof and rsOrder.eof then
		response.Redirect(oa.tmpstr)    '如果当前频道已经在最上面，则无需移动
		exit Sub    
	end if	
	do while not rsOrder.eof
      i = i + 1
	  if i=1 then
	 
	     TorderID=rsOrder("OrderID")
		
		
		 
	     oa.execute("update ClassList set OrderID=" & (Torderid + oldorders + i) & " where ClassID=" & rsOrder("ClassID"))
         oa.execute("update ClassList set PrevID=" & ClassID & " where ClassID=" & rsOrder("ClassID"))
         oa.execute("update ClassList set NextID=" & rsOrder("ClassID") & " where ClassID=" & ClassID)
		 if rsOrder("Child")>0 then
		    set rsChild=oa.execute("select * from classlist where ParentPath like '%" & rsOrder("ParentPath") & "," & rsOrder("classid") & "%' order by OrderID")
			ii = i + 1
			do while not rsChild.eof
			  
			  oa.execute("update Classlist set OrderID=" & (Torderid + oldorders + ii) & " where Classid=" & rsChild("classid"))
			  ii=ii+1
			  rsChild.movenext
			loop
			rsChild.close
		  end if
	  else
	     oa.execute("update Classlist set NextID=" & ClassID & " where ClassID=" & rsOrder("ClassID"))
         oa.execute("update Classlist set PrevID=" & rsOrder("ClassID") & " where ClassID=" & ClassID)
         exit do   
	  end if
	   oa.execute("update Classlist set PrevID=0 where ClassID=" + ClassID)
   rsOrder.movenext
   loop	  
   rsOrder.close  

  oa.execute("update Classlist set OrderID=" & Torderid  & " where ClassID="&ClassID)

               if Child > 0 then
                  i = 0
				  
				  set rs=oa.execute("select * from classlist where ParentPath like '%" & ParentPath & "," & ClassID & "%' order by OrderID")
				 
                   do while not rs.eof
                     i = i+1					
					 oa.execute("update classlist set OrderID=" & (Torderid + i) & " where Classid=" & rs("classid"))
                   rs.movenext
				   loop
                end if

Else
    Trootid =0
	MaxRootID = oa.execute("select max(RootID) from ClassList")(0)+1	
	if oa.execute("select count(classid) from classlist where ParentID=0 and RootID<"& cRootID)(0)<1 then
    response.Redirect(oa.tmpstr)
	exit sub
	end if
	
	oa.execute("update ClassList set RootID=" & MaxRootID & " where RootID=" & cRootID)
	
	'先修改上一频道的NextID和下一频道的PrevID
	if PrevID>0 then
		oa.execute "update ClassList set NextID=" & NextID & " where ClassID=" & PrevID
	end if
	if NextID>0 then
		oa.execute "update ClassList set PrevID=" & PrevID & " where ClassID=" & NextID
	end if
	
	'然后将位于当前频道以上的频道的RootID依次加一，范围为要提升的数字
	sqlOrder="select * From ClassList where ParentID=0 and RootID<" & cRootID & " and Rootid<>"&MaxRootID&" order by RootID desc"
	
	set rsOrder=oa.execute(sqlOrder)
	
	if rsOrder.bof and rsOrder.eof then
		response.Redirect(oa.tmpstr)    '如果当前频道已经在最上面，则无需移动
		exit Sub    
	end if	
	
	do while not rsOrder.eof
	
	    i=i+1
		if i=1 then		
		tRootID=rsOrder("RootID")       '得到要提升位置的RootID，包括子频道
	
		oa.execute("update ClassList set RootID=RootID+1 where RootID=" & tRootID)
		 oa.execute("update ClassList set PrevID=" & ClassID & " where ClassID=" & rsOrder("ClassID"))
		oa.execute("update ClassList set NextID=" & rsOrder("ClassID") & " where ClassID=" & ClassID)
		 
		else		
		 oa.execute("update ClassList set NextID=" & ClassID & " where ClassID=" & rsOrder("ClassID"))
		 oa.execute("update ClassList set PrevID=" & rsOrder("ClassID") & " where ClassID=" & ClassID)
		 exit do
		end if			
		oa.execute("update ClassList set PrevID=0  where ClassID=" & ClassID)		
		rsOrder.movenext
	loop
	
		oa.execute("update ClassList set RootID=" & tRootID & " where RootID=" & MaxRootID)
	rsOrder.close
	set rsOrder=nothing
	
End IF
	response.Redirect(oa.tmpstr)


End Sub
%>
