<!--#include file="conn.asp"-->
<!--#include file="inc/const.asp"-->
<!--#include file="inc/ex_stock/ex_sm_conn.asp"-->
<!--#include file="inc/ex_stock/ex_sm_const.asp"-->
<!--#include file="inc/ex_stock/ex_admin/ex_admin_index.asp"-->
<%
	if not exsm.stockadmin then
		exsm.errmess="您没有管理权限！请返回。"
		call exsm.endinfo("no")
	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
	sub main()
		adminmenutop "数据库管理"
		session("ex_sm")=0
%>
		<table class="tableborder1" cellspacing="1" cellpadding="3" align="center" border="0" style="width:100%">
			<tr>
				<th colspan="2" height="25">-=> 数 据 库 刷 新 <=-</th>
			</tr>
			<form method="post" action="?action=update1" name="f1">
			<tr>
				<td class="tablebody1" width="50%"><u>重新计算股票经营权</u><br>根据股民持股数重新计算股票的经营者<br>建议每隔一段时间运行一次</td>
				<td class="tablebody1" width="50%">&nbsp;<input type="submit" name="submit" value="执行"></td>
			</tr>
			</form>
			<form method="post" action="?action=update2" name="f2">
			<tr>
				<td class="tablebody2"><u>重新计算资产排名</u><br>重新计算用户持股数量、持股现值、总资产值<br>请不要频繁使用该功能</td>
				<td class="tablebody2">&nbsp;<input type="submit" name="submit" value="执行"></td>
			</tr>
			</form>
			<form method="post" action="?action=update3" name="f3">
			<tr>
				<td class="tablebody1"><u>股票数据刷新</u><br>强制刷新股票最近[30]天走势图数据<br>建议股份数量混乱时使用</td>
				<td class="tablebody1">&nbsp;<input type="submit" name="submit" value="执行"></td>
			</tr>
			</form>
		</table>
		<br>
		<table class="tableborder1" cellspacing="1" cellpadding="3" align="center" border="0" style="width:100%">
			<tr>
				<th colspan="2" height="25"><a name="data_setting"></a>-=> 数 据 库 管 理 <=-</th>
			</tr>
			<form method="post" action="ex_sm_admin_setting.asp?action=exconfig" name="f5">	
			<tr>
				<td class="tablebody1" width="50%"><u>还原基本设置</u><br>如果“<a href="ex_sm_admin_setting.asp"><font color="red">基本设置</font></a>”设置混乱建议恢复默认[ex_config]</td>
				<td class="tablebody1" width="50%">&nbsp;<input type=submit name="submit" value="执行"></td>
			</tr>
			</form>
			<form method="post" action="?action=ex_announcement" name="f6">
			<tr>
				<td class="tablebody2"><u>重建公告表</u><br>删除所有公告数据，重新建立公告表[ex_announcement]</td>
				<td class="tablebody2">&nbsp;<input type="submit" name="submit" value="执行"></td>
			</tr>
			</form>
			<form method="post" action="?action=clearevent" name="f7">
			<tr>
				<td class="tablebody1"><u>清空新闻</u><br>清空股市实时新闻数据，建议新闻过多时运行一次</td>
				<td class="tablebody1">&nbsp;<input type=submit name="submit" value="执行"></td>
			</tr>
			</form>
			<form method="post" action="?action=clearevent&reaction=created" name="f8">
			<tr>
				<td class="tablebody2"><u>重建随机事件表</u><br>删除所有的随机事件记录，重新建立随机事件表[ex_news]</td>
				<td class="tablebody2">&nbsp;<input type="submit" name="submit" value="执行"></td>
			</tr>
			</form>
		</table>
		<%if issqlsmdatabase=0 then%>
		<br>
		<table class="tableborder1" cellspacing="1" cellpadding="3" align="center" border="0" style="width:100%">
			<tr>
				<th colspan="2" height="25"><a name=data_setting></a>-=> 数 据 库 操 作 <=-</th>
			</tr>
			<form method="post" action="?action=savedata" name="f9">
			<tr>
				<td class="tablebody1" width="50%"><u>备份股市数据库</u><br>&nbsp;&nbsp;当前数据库路径(相对路径)：<br>&nbsp;&nbsp;备份数据库目录(相对路径)：<br>&nbsp;&nbsp;备份数据库名称(填写名称)：</td>
				<td class="tablebody1" width="50%">&nbsp;<input type="text" name="dbpath" value="<%=stock_db%>"><br>&nbsp;<input type="text" name="bkfolder" value="databackup"><br>&nbsp;<input type="text" name="bkdbname" value="ex_stock.mdb"></td>
			</tr>
			<tr>
				<td colspan="2" class="tablebody2" align="center"><input type="submit" name="submit" value="执行备份"></td></tr>
			</form>
			<form method="post" action="?action=restore" name="f10">
			<tr>
				<td class="tablebody1"><u>恢复股市数据库</u><br>&nbsp;&nbsp;备份数据库路径(相对)：<br>&nbsp;&nbsp;目标数据库路径(相对)：</td>
				<td class="tablebody1">&nbsp;<input type="text" name="dbpath" value="databackup\ex_stock.mdb"><br>&nbsp;<input type="text" name="backpath" value="<%=stock_db%>"><br></td>
			</tr>
			<tr>
				<td colspan="2" class="tablebody2" align="center"><input type="submit" name="submit" value="执行恢复"></td></tr>
			</form>
		</table>
		<%end if%>
		<br>
		<table class="tableborder1" cellspacing="1" cellpadding="3" align="center" border="0" style="width:100%">
			<tr>
				<th colspan="2" height="25"><a name="data_setting"></a>-=> sol 操 作 <=-</th>
			</tr>
			<form method="post" action="?action=executesql" name="f11">
			<tr>
				<td class="tablebody1" width="50%"><u>sql语句执行操作</u><br>本项为高级操作、对sql编程比较熟悉的用户，可以直接输入sql执行语句，本操作直接对股票数据库进行操作，操作不可逆！</td>
				<td class="tablebody1" width="50%">&nbsp;<textarea name="sql" cols="50" rows="3"></textarea></td>
			</tr>
			<tr>
				<td colspan="2" class="tablebody2" align="center"><input type="submit" name="submit" value="执行操作"></td></tr>
			</tr>
			</form>	
		</table>
<%
		adminmenubottom
		exsm.rootshowa
	end sub
	sub update1()
		dim rst,l_content
		if exsm.outsiderefer() then
			exsm.errmess="请不要从外部提交数据。"
			call exsm.endinfo("stop.gif")
			exit sub
		end if
		if exsm.repeatrefer() then
			exsm.errmess="您提交的数据已经保存，请不要重复提交。"
			call exsm.endinfo("stop.gif")
			exit sub
		end if
		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("数据库管理：重新计算股票经营权 ","'","")
			call exsm.savelog(exsm.scriptname,l_content,2)
			session("ex_sm")=1
			exsm.sucmess="数据库更新成功。"
			call exsm.endinfo("ok")
		else
			exsm.errmess="数据库中没有股票数据！更新失败。"
			call exsm.endinfo("no")
			exit sub
		end if
	end sub
	sub update2
		dim l_content
		if exsm.outsiderefer() then
			exsm.errmess="请不要从外部提交数据。"
			call exsm.endinfo("stop.gif")
			exit sub
		end if
		if exsm.repeatrefer() then
			exsm.errmess="您提交的数据已经保存，请不要重复提交。"
			call exsm.endinfo("stop.gif")
			exit sub
		end if
		call exsm.calculatefund(0,0)
		exsm.delstockcahe("exsm_imitate_usersequence")
		l_content=replace("数据库管理：重新计算资产排名 ","'","")
		call exsm.savelog(exsm.scriptname,l_content,2)
		session("ex_sm")=1
		exsm.sucmess="用户持股数量、持股现值、总资产值更新成功。"
		call exsm.endinfo("ok")
	end sub
	sub update3()
		dim stocknum,tongji,l_content
		if exsm.outsiderefer() then
			exsm.errmess="请不要从外部提交数据。"
			call exsm.endinfo("stop.gif")
			exit sub
		end if
		if exsm.repeatrefer() then
			exsm.errmess="您提交的数据已经保存，请不要重复提交。"
			call exsm.endinfo("stop.gif")
			exit sub
		end if
		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("数据库管理：股票数据刷新 ","'","")
			call exsm.savelog(exsm.scriptname,l_content,2)
			session("ex_sm")=1
			exsm.sucmess="股票数据刷新成功。"
			call exsm.endinfo("ok")
		else
			exsm.errmess="数据库中没有股票数据！更新失败。"
			call exsm.endinfo("no")
			exit sub
		end if
	end sub
	sub ex_announcement()
		dim l_content
		if exsm.outsiderefer() then
			exsm.errmess="请不要从外部提交数据。"
			call exsm.endinfo("stop.gif")
			exit sub
		end if
		if exsm.repeatrefer() then
			exsm.errmess="您提交的数据已经保存，请不要重复提交。"
			call exsm.endinfo("stop.gif")
			exit sub
		end if
		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("数据库管理：重建公告表 ","'","")
		call exsm.savelog(exsm.scriptname,l_content,2)
		session("ex_sm")=1
		exsm.sucmess="重建公告表成功，公告已经清空。"
		call exsm.endinfo("ok")
	end sub
	sub clearevent()
		dim l_content
		if exsm.outsiderefer() then
			exsm.errmess="请不要从外部提交数据。"
			call exsm.endinfo("stop.gif")
			exit sub
		end if
		if exsm.repeatrefer() then
			exsm.errmess="您提交的数据已经保存，请不要重复提交。"
			call exsm.endinfo("stop.gif")
			exit sub
		end if
		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("数据库管理：清空新闻事件 ","'","")
			call exsm.savelog(exsm.scriptname,l_content,2)
			exsm.delstockcahe("exsm_imitate_news")
			session("ex_sm")=1
			exsm.sucmess="重建事件表成功。"
			call exsm.endinfo("ok")
		else
			stock_conn.execute("delete from ex_news")
			l_content=replace("数据库管理：清空新闻事件 ","'","")
			call exsm.savelog(exsm.scriptname,l_content,2)
			exsm.delstockcahe("exsm_imitate_news")
			session("ex_sm")=1
			exsm.sucmess="所有随机事件已经清空。"
			call exsm.endinfo("ok")
		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")
		if exsm.outsiderefer() then
			exsm.errmess="请不要从外部提交数据。"
			call exsm.endinfo("stop.gif")
			exit sub
		end if
		if exsm.repeatrefer() then
			exsm.errmess="您提交的数据已经保存，请不要重复提交。"
			call exsm.endinfo("stop.gif")
			exit sub
		end if
		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("数据库管理：数据库备份 ","'","")
			call exsm.savelog(exsm.scriptname,l_content,2)
			session("ex_sm")=1
			exsm.sucmess="备份数据库成功。"
			call exsm.endinfo("ok")
		else
			exsm.errmess="找不到您所需要备份的文件。"
			call exsm.endinfo("no")
			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 exsm.outsiderefer() then
			exsm.errmess="请不要从外部提交数据。"
			call exsm.endinfo("stop.gif")
			exit sub
		end if
		if exsm.repeatrefer() then
			exsm.errmess="您提交的数据已经保存，请不要重复提交。"
			call exsm.endinfo("stop.gif")
			exit sub
		end if
		if dbpath="" then
			exsm.errmess="请输入您要恢复成的数据库全名。"
			call exsm.endinfo("no")
			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("数据库管理：数据库恢复 ","'","")
			call exsm.savelog(exsm.scriptname,l_content,2)
			session("ex_sm")=1
			exsm.sucmess="成功恢复数据。"
			call exsm.endinfo("ok")
		else
			exsm.errmess="备份目录下并无您的恢复文件！"
			call exsm.endinfo("no")
			exit sub
		end if
	end sub
	sub executesql()
		dim l_content
		if exsm.outsiderefer() then
			exsm.errmess="请不要从外部提交数据。"
			call exsm.endinfo("stop.gif")
			exit sub
		end if
		if exsm.repeatrefer() then
			exsm.errmess="您提交的数据已经保存，请不要重复提交。"
			call exsm.endinfo("stop.gif")
			exit sub
		end if
		if request("sql")="" then
			exsm.errmess="请输入sql语句！"
			call exsm.endinfo("no")
			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")&" ","'","")
			call exsm.savelog(exsm.scriptname,l_content,2)
			session("ex_sm")=1
			exsm.sucmess="语句执行成功。"
			call exsm.endinfo("ok")
		else
			exsm.errmess="语句有问题，未能执行！具体错误："&err.description&""
			call exsm.endinfo("no")
			err.clear
		end if
	end sub
%>