<!--#INCLUDE FILE="setup.asp" -->
<!--#include file=checkuser.asp-->
<title><%=StrVar(1)%>--管理页面</title>
<link href="css.asp" rel="stylesheet" type="text/css">
<!--#include file=theme.css-->
<body alink=#333333 vlink=#333333 link=#333333 topmargin=0 class=adminbody>
<%
if  not sysadmin or session("adminlogin")<>"0K" Then
	Errmsg=Errmsg+"<br>"+"<li>本页面为管理员专用，请<a href=superindex.asp target=_top>登陆</a>后进入。"
	Call Error("Information",ErrMsg)
Else
	Call main()
End if
Call endconnection()


Sub main()
%>
<table cellpadding=5 cellspacing=1 border=0 width=100% Class=TableBorder align=center>
<tr>
	<th>欢迎<b><%=membername%></b>进入管理页面</th>
</tr></table>
		<%
		if Request("action")="merge" Then
			Call merge()
		Else
			Call classinfo()
		End if
End Sub


Sub classinfo()
%>
<SCRIPT LANGUAGE="JavaScript">
function checkinput(cls,aobj)
{
	var nv;
	if (cls==2)
	{nv=main.class2.value;}
	else
	{nv=main.class1.value;}
	if(nv!=""&&!isNaN(nv))
	{
		aobj.href="MyClass.asp?ClassID="+nv;
		return true;
	}
	window.alert("您输入了错误的班级ID");
	return false;
}
</SCRIPT>

<BR>
<TABLE border=0 width=600 align=center>
<TR>
	<TD class=mp><b>说明：</b><br>
	(1)如果合并前班级2的开放等级为班长审核请改为完全开放后再执行合并操作！<BR>
	(2)合并后的班级将保留班级1的班级信息。<BR>
	(3)合并后的班级需要重新任命或申请班委。</TD>
</TR>
</TABLE>
<form method="POST" action=sysadm_merge.asp name=main>
<input type=hidden name=action value=merge>
<table width="600" border="0" cellspacing="1" cellpadding="5"  align=center Class=TableBorder>
<tr> 
	<th height="11" colspan="2" ><b>请输入要合并的班级信息</b></th>
</tr>
<tr Class=LightTableBody> 
	<td width="41%" height="18">班级1(请输入班级ID)</td>
	<td width="59%" height="18">
	<input type="text" name="class1" size="35">&nbsp;<a href="" target=_blank name=a1 onclick="return checkinput(1,this);">>>转到班级页面</a>
	</td>
</tr>
<tr Class=LightTableBody >
	<td width="41%" height="18">班级2(请输入班级ID)</td>
	<td width="59%" height="18"> 
	<input type="text" name="class2" size="35">&nbsp;<a href="" target=_blank name=b1 onclick="return checkinput(2,this);">>>转到班级页面</a>
	</td>
</tr>
<tr Class=LightTableBody >
	<td width="41%" height="18">合并后班级的班长(用户名)</td>
	<td width="59%" height="18"><input type="text" name="clsadm1" size="35"></td>
</tr>
<tr Class=LightTableBody >
	<td width="41%" height="18">合并后班级的副班长(用户名)</td>
	<td width="59%" height="18"><input type="text" name="clsadm2" size="35"></td>
</tr>
<tr Class=DarkTableBody> 
	<td height="23" colspan="2" align=right > 
	<input type="submit" name="Submit" value="执行班级合并" class=button>
	</td>
</tr>
</table>
</form>
<%
End Sub


Sub merge()

	dim class1,class2,clsadm1,clsadm2
	dim sql,rs
	dim errmsg,founderr
	dim classname2

	class1=trim(Request.form("class1"))
	if class1="" or not isnumeric(class1) then
		ErrMsg=ErrMsg+"<br>"+"<li>班级1的ID输入有误！"
		founderr=True
	else
		sql="Select classid from class where classid="&class1
		Set rs=conn.execute(sql)
		if rs.eof and rs.bof Then
			ErrMsg=ErrMsg+"<br>"+"<li>班级1不存在。"
			founderr=True
		End if
		set rs=nothing
	end if

	class2=trim(Request.form("class2"))
	if class2="" or not isnumeric(class2) then
		ErrMsg=ErrMsg+"<br>"+"<li>班级1的ID输入有误！"
		founderr=True
	else
		sql="Select classname from class where classid="&class2
		Set rs=conn.execute(sql)
		if rs.eof and rs.bof Then
			ErrMsg=ErrMsg+"<br>"+"<li>班级2不存在。"
			founderr=True
		else
			classname2=rs(0)
		End if
		set rs=nothing
	end if

	clsadm1=trim(Request.form("clsadm1"))

	if clsadm1<>"" Then
		sql="Select userid from joinclass where userid='"&clsadm1&"' and (classid="&class1&" or classid="&class2&")"
		Set	rs=conn.execute(sql)
		if rs.eof Then
			ErrMsg=ErrMsg+"<br>"+"<li>对不起，"&clsadm1&"不是这两个班级的成员。"
			founderr=True
		End if

	End if

	clsadm2=trim(Request.form("clsadm2"))


	if clsadm2<>"" Then
		sql="Select userid from joinclass where userid='"&clsadm2&"' and (classid="&class1&" or classid="&class2&")"
		Set	rs=conn.execute(sql)

		if rs.eof Then
			ErrMsg=ErrMsg+"<br>"+"<li>对不起，"&clsadm2&"不是这两个班级的成员。"
			founderr=True
		End if

	End if

	if founderr=True Then
		Call Error("Information",ErrMsg)
		Exit Sub
	End if

	sql="update classboard Set classid="&class1&" where classid="&class2
	conn.execute(sql)

	sql="update photo Set classid="&class1&" where classid="&class2
	conn.execute(sql)

	sql="update append Set classid="&class1&" where classid="&class2
	conn.execute(sql)

	sql="update votebox Set classid="&class1&" where classid="&class2
	conn.execute(sql)

	sql="delete from log where classid="&class2
	conn.execute(sql)

	'write log
	IP=Request.ServerVariables("REMOTE_ADDR") 
	sql="insert into log(userid,content,classid,ip)"&_
		" values('"&membername&"','将班级“"&classname2&"”合并入本班',"&class1&",'"&IP&"')"
	Conn.Execute(sql)

	sql="select userid,degree from joinclass where (classid="&class1&" or classid="&class2&") and degree>1"
	set rs =conn.execute(sql)
	do until rs.eof
		ResignPoint=Round(PointSet(13)*rs(1)/8)
		conn.execute("update student Set point=point+"&ResignPoint&" where userid='"&rs(0)&"'")
		rs.movenext
	loop

	sql="update student Set point=point+"&PointSet(12)&" where userid='"&clsadm1&"' or userid='"&clsadm2&"'"
	conn.execute(sql)

	sql="delete from joinclass where classid="&class2&" and userid in (Select userid from joinclass where classid="&class1&")"
	conn.execute(sql)

	sql="update joinclass Set classid="&class1&" where classid="&class2
	conn.execute(sql)

	sql="update joinclass set degree=1 where classid="&class1&" and degree>1"
	conn.execute(sql)
	
	sql="update joinclass set degree=8 where classid="&class1&" and userid='"&clsadm1&"'"
	conn.execute(sql)

	sql="update joinclass set degree=7 where classid="&class1&" and userid='"&clsadm2&"'"
	conn.execute(sql)

	sql="Select count(userid) from joinclass where classid="&class1
	Set rs=conn.execute(sql)

	sql="update class Set admin1='"&clsadm1&"',admin2='"&clsadm2&"',studcount=studcount+"&rs(0)&" where classid="&class1
	conn.execute(sql)

	sql="delete from class where classid="&class2
	conn.execute(sql)

	sql="update config set ClassNum=ClassNum-1"
	conn.execute(sql)

		Set rs=Nothing
	%>
	<center><p><b>班级合并成功！建议到数据维护中更新每个用户注册的班级数</b>
<%End Sub%>



