<!--#include file="setup.asp"-->
<!--#include file="plus/Ex_Stock/Exsm_Plus_Conn.asp"-->
<!--#include file="plus/Ex_Stock/Exsm_Plus_Const.asp"-->
<%
	'检查用户名和ＩＤ是否合法
	If Exsm.UserID="" or Exsm.UserID=0 or not isnumeric(Exsm.UserID) or Exsm.StockHolder="" Then
		Response.Redirect "Exsm_Plus_Gupiao.asp"
	Else
		set Rs=stock_conn.execute("select UserName,LockUser from Ex_User where ID="&Exsm.UserID&"")
		'是否存在用户
		If Rs.eof and Rs.bof Then
			Response.Redirect "Exsm_Plus_Gupiao.asp"
		'用户是否锁定
		Elseif Rs(1)>1 Then
			Response.Redirect "Exsm_Plus_Gupiao.asp"
		'是否已经登录
		Elseif not Exsm.Members Then
			Response.Redirect "Exsm_Plus_Gupiao.asp"
		'检查用户等级
		Elseif not Exsm.StockAdmin Then
			Response.Redirect "Exsm_Plus_Gupiao.asp"
		'再次验证身份
		Elseif not Rs(0)=Exsm.StockHolder Then
			Response.Redirect "Exsm_Plus_Gupiao.asp"
		Else
			select case request("action")
				case "Update1"
					call Update1()
				case "Update2"
					call Update2()
				case "Update3"
					call Update3()
				case "EX_Announcement"
					call EX_Announcement()
				case "ClearEvent"
					call ClearEvent()
				case "SaveData"
					call SaveData()
				case "Restore"
					call Restore()
				case "ExecuteSql"
					call ExecuteSql()
				case else
					call main()
			End select
		End if
	End if
	Sub main()
		Exsm.Title 1,"数据库管理"
%>
		<table class=a2 width=<%=Exsm.AssignWidth%> cellspacing=1 cellpadding=3 align=center border=0>
			<tr>
				<td colspan="2" align=center height=25 class=a1>-=> 数 据 库 刷 新 <=-</td>
			</tr>
			<form method="POST" action="?action=Update1" name="f1">
			<tr>
				<td class=a3 width="50%"><u>重新计算股票经营权</u><br>根据股民持股数重新计算股票的经营者<br>建议每隔一段时间运行一次</td>
				<td class=a3 width="50%">&nbsp;<input type=submit name="submit" value="执行"></td>
			</tr>
			</form>
			<form method="POST" action="?action=Update2" name="f2">
			<tr>
				<td class=a4 width="50%"><u>重新计算资产排名</u><br>重新计算用户持股数量、持股现值、总资产值<br>请不要频繁使用该功能</td>
				<td class=a4 width="50%">&nbsp;<input type=submit name="submit" value="执行"></td>
			</tr>
			</form>
			<form method="POST" action="?action=Update3" name="f3">
			<tr>
				<td class=a3 width="50%"><u>股票数据刷新</u><br>强制刷新股票最近[30]天走势图数据<br>建议股份数量混乱时使用</td>
				<td class=a3 width="50%">&nbsp;<input type=submit name="submit" value="执行"></td>
			</tr>
			</form>
		</table>
		<br>
		<table class=a2 width=<%=Exsm.AssignWidth%> cellspacing=1 cellpadding=3 align=center border=0>
			<tr>
				<td colspan="2" align=center height=25 class=a1><a name=data_Setting></a>-=> 数 据 库 管 理 <=-</td>
			</tr>
			<form method="POST" action="Exsm_Plus_Admin_Setting.asp?action=EXConfig" name="f5">	
			<tr>
				<td class=a3 width="50%"><u>还原基本设置</u><br>如果“<a href="Exsm_Plus_Admin_Setting.asp"><font color=red>基本设置</font></a>”设置混乱建议恢复默认[EX_Config]</td>
				<td class=a3 width="50%">&nbsp;<input type=submit name="submit" value="执行"></td>
			</tr>
			</form>
			<form method="POST" action="?action=EX_Announcement" name="f6">
			<tr>
				<td class=a4><u>重建公告表</u><br>删除所有公告数据，重新建立公告表[EX_Announcement]</td>
				<td class=a4>&nbsp;<input type=submit name="submit" value="执行"></td>
			</tr>
			</form>
			<form method="POST" action="?action=ClearEvent" name="f7">
			<tr>
				<td class=a3><u>清空新闻</u><br>清空股市实时新闻数据，建议新闻过多时运行一次</td>
				<td class=a3>&nbsp;<input type=submit name="submit" value="执行"></td>
			</tr>
			</form>
			<form method="POST" action="?action=ClearEvent&reaction=created" name="f8">
			<tr>
				<td class=a4><u>重建随机事件表</u><br>删除所有的随机事件记录，重新建立随机事件表[EX_News]</td>
				<td class=a4>&nbsp;<input type=submit name="submit" value="执行"></td>
			</tr>
			</form>
		</table>
		<%If IsSqlSmDataBase=0 Then%>
		<br>
		<table class=a2 width=<%=Exsm.AssignWidth%> cellspacing=1 cellpadding=3 align=center border=0>
			<tr>
				<td colspan="2" align=center height=25 class=a1><a name=data_Setting></a>-=> 数 据 库 操 作 <=-</td>
			</tr>
			<form method="POST" action="?action=SaveData" name="f9">
			<tr>
				<td class=a3 width="50%"><u>备份股市数据库</u><br>&nbsp;&nbsp;当前数据库路径(相对路径)：<BR>&nbsp;&nbsp;备份数据库目录(相对路径)：<br>&nbsp;&nbsp;备份数据库名称(填写名称)：</td>
				<td class=a3 width="50%">&nbsp;<input type=text name=DBpath value="<%=stock_db%>"><BR>&nbsp;<input type=text name=bkfolder value=plus/Ex_Stock/Data/><br>&nbsp;<input type=text name=bkDBname value=#bak(ExStock)ohoh.MDB></td>
			</tr>
			<tr>
				<td colspan="2" class=a4 align="center"><input type=submit name=submit value=执行备份></td></tr>
			</form>
			<form method="POST" action="?action=Restore" name="f10">
			<tr>
				<td class=a3 width="50%"><u>恢复股市数据库</u><br>&nbsp;&nbsp;备份数据库路径(相对)：<br>&nbsp;&nbsp;目标数据库路径(相对)：</td>
				<td class=a3 width="50%">&nbsp;<input type=text name=DBpath value="plus/Ex_Stock/Data/#bak(ExStock)ohoh.MDB"><br>&nbsp;<input type=text name=backpath value="<%=stock_db%>"><br></td>
			</tr>
			<tr>
				<td colspan="2" class=a4 align="center"><input type=submit name=submit value=执行恢复></td></tr>
			</form>
		</table>
		<%End if%>
		<br>
		<table class=a2 width=<%=Exsm.AssignWidth%> cellspacing=1 cellpadding=3 align=center border=0>
			<tr>
				<td colspan="2" align=center height=25 class=a1><a name=data_Setting></a>-=> SOL 操 作 <=-</td>
			</tr>
			<form method="POST" action="?action=ExecuteSql" name="f11">
			<tr>
				<td class=a3 width="50%"><u>SQL语句执行操作</u><br>本项为高级操作、对SQL编程比较熟悉的用户，可以直接输入SQL执行语句，本操作直接对股票数据库进行操作，操作不可逆！</td>
				<td class=a3 width="50%">&nbsp;<textarea name=sql cols=50 rows=3></textarea></td>
			</tr>
			<tr>
				<td colspan="2" class=a4 align="center"><input type=submit name=submit value=执行操作></td></tr>
			</tr>
			</form>	
		</table>
<%
		Exsm.RootShowA
	End sub
	Sub Update1()
		Dim Rst,l_content
		Set Rs=stock_conn.execute("select Sid from EX_Stock order by sid")
		If not rs.eof Then
			Do while not Rs.eof
				Set Rst=stock_conn.execute("select top 1 UserName,UserId from EX_Customer Where Sid="&Rs(0)&" And UserId>0 order by PossessStockSum desc")
				If Rst.eof then
					stock_conn.execute("update EX_Stock set ManageMent=null,ManageMentID=0 Where Sid="&Rs(0))
				Else
					stock_conn.execute("update EX_Stock set ManageMent='"&Rst(0)&"',ManageMentID="&Rst(1)&" Where Sid="&Rs(0))
				End if
				Rs.movenext
			Loop
			Rst.close
			Rs.close
			l_content=replace("数据库管理：重新计算股票经营权 ","'","")
			stock_conn.execute("insert into EX_Log (l_boardid,l_touser,l_username,l_content,l_addtime,l_ip,l_type) values('"&Exsm.UserID&"','"&Exsm.ScriptName&"','"&Exsm.StockHolder&"','"&l_content&"',"&SqlSmNowString&",'"&Exsm.UserIP&"',2)")
			Exsm.Sucmess="<li>数据库更新成功。"
			Call Exsm.Endinfo(1)
		Else
			Exsm.Errmess="<li>数据库中没有股票数据！更新失败。"
			Call Exsm.Endinfo(1)
			Exit Sub
		End if
	End Sub
	Sub Update2
		Dim l_content
		Call Exsm.CalculateFund(0,0)
		Exsm.DelStockCahe("ExSm_Imitate_UserSequence")
		l_content=replace("数据库管理：重新计算资产排名 ","'","")
		stock_conn.execute("insert into EX_Log (l_boardid,l_touser,l_username,l_content,l_addtime,l_ip,l_type) values('"&Exsm.UserID&"','"&Exsm.ScriptName&"','"&Exsm.StockHolder&"','"&l_content&"',"&SqlSmNowString&",'"&Exsm.UserIP&"',2)")
		Exsm.Sucmess="<li>用户持股数量、持股现值、总资产值更新成功。"
		Call Exsm.Endinfo(1)
	End Sub
	Sub Update3()
		Dim StockNum,TongJi,l_content
		Set Rs=stock_conn.execute("select Sid,Statement from EX_Stock order by Sid")
		If not rs.eof Then
			Do while not Rs.eof 
				TongJi=Rs(1)
				StockNum=stock_conn.execute("select sum(PossessStockSum) from EX_Customer where sid="&Rs(0))(0)
				If StockNum="" Or isnull(StockNum) Then StockNum=0
				If TongJi="" Or isnull(TongJi) Then TongJi="0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0"
				stock_conn.execute("update EX_stock set SurplusShare=StockSum-"&StockNum&",Statement='"&TongJi&"' Where Sid="&Rs(0))
				Rs.movenext
			Loop
			Rs.close
			l_content=replace("数据库管理：股票数据刷新 ","'","")
			stock_conn.execute("insert into EX_Log (l_boardid,l_touser,l_username,l_content,l_addtime,l_ip,l_type) values('"&Exsm.UserID&"','"&Exsm.ScriptName&"','"&Exsm.StockHolder&"','"&l_content&"',"&SqlSmNowString&",'"&Exsm.UserIP&"',2)")
			Exsm.Sucmess="<li>股票数据刷新成功。"
			Call Exsm.Endinfo(1)
		Else
			Exsm.Errmess="<li>数据库中没有股票数据！更新失败。"
			Call Exsm.Endinfo(1)
			Exit Sub
		End if
	End Sub
	Sub EX_Announcement()
		Dim l_content
		stock_conn.execute("drop table EX_Announcement")
		sql="CREATE TABLE EX_Announcement (ID int IDENTITY (1, 1) PRIMARY KEY,Title varchar (100),Content varchar (255),Color varchar (20),AddTime datetime default "&SqlSmNowString&",UserName varchar (50))"
		stock_conn.execute(sql)
		Exsm.DelStockCahe("Exsm_Imitate_News")
		l_content=replace("数据库管理：重建公告表 ","'","")
		stock_conn.execute("insert into EX_Log (l_boardid,l_touser,l_username,l_content,l_addtime,l_ip,l_type) values('"&Exsm.UserID&"','"&Exsm.ScriptName&"','"&Exsm.StockHolder&"','"&l_content&"',"&SqlSmNowString&",'"&Exsm.UserIP&"',2)")
		Exsm.Sucmess="<li>重建公告表成功，公告已经清空。"
		Call Exsm.Endinfo(1)
	End Sub
	Sub ClearEvent()
		Dim l_content
		If request("reaction")="created" Then
			stock_conn.execute("drop table EX_News")
			sql="CREATE TABLE EX_News (ID int IDENTITY (1, 1) NOT NULL CONSTRAINT PrimaryKey PRIMARY KEY,content text,Color varchar (20),AddTime datetime default "&SqlSmNowString&")"
			stock_conn.execute(sql)
			l_content=replace("数据库管理：清空新闻事件 ","'","")
			stock_conn.execute("insert into EX_Log (l_boardid,l_touser,l_username,l_content,l_addtime,l_ip,l_type) values('"&Exsm.UserID&"','"&Exsm.ScriptName&"','"&Exsm.StockHolder&"','"&l_content&"',"&SqlSmNowString&",'"&Exsm.UserIP&"',2)")
			Exsm.DelStockCahe("Exsm_Imitate_News")
			Exsm.Sucmess="<li>重建事件表成功。"
			Call Exsm.Endinfo(1)
		Else
			stock_conn.execute("delete from EX_News")
			l_content=replace("数据库管理：清空新闻事件 ","'","")
			stock_conn.execute("insert into EX_Log (l_boardid,l_touser,l_username,l_content,l_addtime,l_ip,l_type) values('"&Exsm.UserID&"','"&Exsm.ScriptName&"','"&Exsm.StockHolder&"','"&l_content&"',"&SqlSmNowString&",'"&Exsm.UserIP&"',2)")
			Exsm.DelStockCahe("Exsm_Imitate_News")
			Exsm.Sucmess="<li>所有随机事件已经清空。"
			Call Exsm.Endinfo(1)
		End if
	End Sub
	Sub SaveData()
		Dim dbpath,bkfolder,bkdbname,fso,l_content
		Dbpath=request.form("Dbpath")
		Dbpath=server.mappath(Dbpath)
		bkfolder=request.form("bkfolder")
		bkdbname=request.form("bkdbname")
		Set Fso=server.createobject("scripting.filesystemobject")
		If fso.fileexists(dbpath) Then
		If CheckDir(bkfolder)=True Then
			Fso.copyfile dbpath,bkfolder& "\"& bkdbname
			Else
			MakeNewsDir bkfolder
			Fso.copyfile dbpath,bkfolder& "\"& bkdbname
		End if
			l_content=replace("数据库管理：数据库备份 ","'","")
			stock_conn.execute("insert into EX_Log (l_boardid,l_touser,l_username,l_content,l_addtime,l_ip,l_type) values('"&Exsm.UserID&"','"&Exsm.ScriptName&"','"&Exsm.StockHolder&"','"&l_content&"',"&SqlSmNowString&",'"&Exsm.UserIP&"',2)")
			Exsm.Sucmess="<li>备份数据库成功。"
			Call Exsm.Endinfo(1)
		Else
			Exsm.Errmess="<li>找不到您所需要备份的文件。"
			Call Exsm.Endinfo(1)
			Exit Sub
		End if
	End Sub
	Function CheckDir(FolderPath)
		Dim fso1
		folderpath=Server.MapPath(".")&"\"&folderpath
		Set fso1 = CreateObject("Scripting.FileSystemObject")
		If fso1.FolderExists(FolderPath) Then
			CheckDir=True
		Else
			CheckDir=False
		End if
		Set fso1=nothing
	End Function
	Function MakeNewsDir(foldername)
		Dim f,fso1
		Set fso1=CreateObject("Scripting.FileSystemObject")
		Set f=fso1.CreateFolder(foldername)
		MakeNewsDir=True
		Set fso1=nothing
	End Function
	Sub Restore()
		Dim dbpath,backpath,fso,l_content
		Dbpath=request.form("Dbpath")
		backpath=request.form("backpath")
		If Dbpath="" Then
			Exsm.Errmess="<li>请输入您要恢复成的数据库全名。"
			Call Exsm.Endinfo(1)
			Exit Sub
		Else
			Dbpath=server.mappath(Dbpath)
		End if
		backpath=server.mappath(backpath)
		Set Fso=server.createobject("scripting.filesystemobject")
		If fso.fileexists(Dbpath) Then
			fso.copyfile Dbpath,Backpath
			l_content=replace("数据库管理：数据库恢复 ","'","")
			stock_conn.execute("insert into EX_Log (l_boardid,l_touser,l_username,l_content,l_addtime,l_ip,l_type) values('"&Exsm.UserID&"','"&Exsm.ScriptName&"','"&Exsm.StockHolder&"','"&l_content&"',"&SqlSmNowString&",'"&Exsm.UserIP&"',2)")
			Exsm.Sucmess="<li>成功恢复数据。"
			Call Exsm.Endinfo(1)
		Else
			Exsm.Errmess="<li>备份目录下并无您的恢复文件！"
			Call Exsm.Endinfo(1)
			Exit Sub
		End if
	End Sub
	Sub ExecuteSql()
		Dim l_content
		If request("sql")="" then
			Exsm.Errmess="<li>请输入SQL语句！"
			Call Exsm.Endinfo(1)
			Exit Sub
		Else
			sql=request("sql")
		End if
		On Error Resume Next 
		stock_conn.execute(sql)
		If err.number="0" Then
			l_content=replace("数据库管理：执行SQL语句："&request("sql")&" ","'","")
			stock_conn.execute("insert into EX_Log (l_boardid,l_touser,l_username,l_content,l_addtime,l_ip,l_type) values('"&Exsm.UserID&"','"&Exsm.ScriptName&"','"&Exsm.StockHolder&"','"&l_content&"',"&SqlSmNowString&",'"&Exsm.UserIP&"',2)")
			Exsm.Sucmess="<li>语句执行成功。"
			Call Exsm.Endinfo(1)
		Else
			Exsm.Errmess="<li>语句有问题，未能执行！具体错误："&Err.Description&""
			Call Exsm.Endinfo(1)
			err.clear
		End if
	End Sub
%>