<!--#include file="setup.asp"-->
<%top
if CookieUserName=empty then error("您还未登录，请先<a href=login.asp>登录</a>。")
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
datafile    ="database/prize.asp"      '数据库的文件名及路径

connstring="DBQ="&server.mappath(""&datafile&"")&";DRIVER={Microsoft Access Driver (*.mdb)};"
Set prizeconn=Server.CreateObject("ADODB.Connection")
prizeconn.open connstring,1,3

datecode=prizeconn.execute("select count(id) from [information]")(0)+1

price=prizeconn.execute("select price from [prizeconfig] where id=1")(0) '获得彩票单注金额
prize=prizeconn.execute("select prizesum from [prizeconfig] where id=1")(0) '取得上期奖池基数
prizebase=prizeconn.execute("select prizebase from [prizeconfig] where id=1")(0)'取得最小奖池基数

if Prize <prizebase then Prize=prizebase  '如果奖金少于最小基数，显示更改奖金基数为最小基数

Prize=prize+prizeconn.execute("select count(id) from [buyz] where datecode="&datecode)(0)*price '奖金设置，卖出的注数*单注金额

<!---------历史信息--------------->
sub history
infonum=prizeconn.execute("select count(id) from [information]")(0)

if infonum>10 then
   infonum=10
end if

if infonum<>0 then
   set prizers=prizeconn.execute("select  * from [information] order by id DESC")
   response.write "<table  cellspacing=1 cellpadding=4 width=95% class=a2><tr class=a1><td width=20% >奖金</td><td width=40% >中奖者</td><td width=20% >期数</td><td width=20% >号码</td></tr>"
   for i=1 to infonum
      prizeman=prizers("whichone")
	  
      if isnull(prizeman) then
         prizeman="本期无人中奖"
      end if
      response.write "<tr class=a3><td>"&prizers("prizemoney")&"</td><td>"&prizeman&"</td><td>"&prizers("datecode")&"</td><td>"&prizers("prizecode")&"</td></tr>"
      prizers.movenext
   next
   
   prizers.close
else 
   response.write "<table align=center cellspacing=1 cellpadding=4 width=95% class=a2><tr class=a1><td width=20% >奖金</td><td width=40% >中奖者</td><td width=20% >期数</td><td width=20% >号码</td></tr>"
   response.write "<tr class=a3><td colspan=4>未曾开奖</td></tr>"
end if
response.write "</table>"
end sub
<!---------投注验收---------->
if request("action")="buy" then
   if conn.execute("select usermoney from [BBSXP_Users] where username='"&CookieUserName&"'")(0)<price then
      error("对不起，你的金钱不够。")
   else
      buycode=request.form("num1")+request.form("num2")+request.form("num3")+request.form("num4")
      prizeconn.execute "insert into [buyz](datecode,code,username) values("&datecode&",'"&buycode&"','"&CookieUserName&"')"
      sql="select * from [BBSXP_Users] where username='"&CookieUserName&"'"
      rs.Open sql,Conn,1,3
      rs("usermoney")=rs("usermoney")-price
      rs.update
      rs.close


      message=message&"<li>投注成功！！<li><a href=piao.asp>返回社区彩票</a>"
      succeed(""&message&"<meta http-equiv=refresh content=3;url=piao.asp>")
   end if
end if


'''''''''''''''''''''''''''''''''''开奖验收''''''''''''''''''''''''
if request.querystring("action")="out" then
   DIM SinglePrize
   DIM WinSinglePriceMax		 
   DIM WinPrizeSum

   if request.form("or")="rnd" then
      randomize
      finalcode=CStr(int(rnd*9000+1000))
		  if len(finalcode)<4 then
		    do while len(finalcode)=4
		     finalcode="0"+finalcode
		    loop
		  end if
   else
	  if isnull(request.form("final1")) or isnull(request.form("final2")) or isnull(request.form("final3")) or  isnull(request.form("final4")) then
	     error("请输入完整的开奖号码")
	  else
	     finalcode=request.form("final1")+request.form("final2")+request.form("final3")+request.form("final4")
	  end if

 
  
  '=======================取得中奖人数并设置单注最高奖金==============================
       WinPrizeNum=prizeconn.execute("select count(username) from buyz where code='"&finalcode&"' and datecode="&datecode)(0)
       if winPrizeNum <> 0 then
	     Singleprize = prize/WinprizeNum   '如果多人中奖，要平分奖金
		 

		 WinSinglePriceMax = prizeconn.Execute("Select SinglePriceMax from [PrizeConfig] where id=1")(0)    '取出单注最大值
		 
		 if SinglePrize > WinSinglePriceMax  then SinglePrize=WinSinglePriceMax   '如果单注奖金大于最大奖金数，设单注为最大奖金数
	  '   end if 
			
		 Prize = Prize - SinglePrize*WinPrizeNum   '奖金余额等于奖金总数-单注奖金*中奖人数
			
	     set yeah=prizeconn.execute("select * from buyz where code='"&finalcode&"' and datecode="&datecode)
		 prizeok="|"
		 do while not yeah.eof
		 cksql="select * from [BBSXP_Users] where username='"&yeah("username")&"'"
         rs.Open cksql,Conn,1,3
		 rs("usermoney")=rs("usermoney")+SinglePrize
		 rs.update
		 prizeok=prizeok+yeah("username")+"|"
		 yeah.movenext
		 rs.close   
		 loop
		 yeah.close
       end if             			    
	 end if  
 
    '奖金累积
        prizesumsql="select prizesum,prizebase from [prizeconfig] where id=1"
    set dbprizesum=server.createobject("adodb.recordset")
     	dbprizesum.Open prizesumsql,Connstring,1,3
		dbprizesum("prizesum")=prize          '下次奖池累积等于本期余数
		dbprizesum.update
		dbprizesum.close
    '奖金累积

	  
   cksql="select * from [information]"
   set dbs=server.createobject("adodb.recordset")
   dbs.Open cksql,Connstring,1,3
   dbs.addnew
   dbs("datecode")=datecode
   dbs("prizecode")=finalcode

   if WinPrizeNum=0 then 
     dbs("PrizeMoney")=Prize
   else
     dbs("prizemoney")=SinglePrize*WinPrizeNum    '实发奖金等于中奖数*单注奖金额度，如果不中为当前奖池总数
     dbs("whichone")=prizeok
   end if 

   dbs.update
   dbs.close    
   
   '''OK''''''''''''''''''发送短信通知用户''''''''''''''''''''''
   oksql="select username from [BBSXP_Users]"
   Set Rs=Conn.Execute(oksql)
   do while not rs.eof
   conn.Execute "insert into BBSXP_Messages (UserName,incept,content) values ('"&CookieUserName&"','"&rs("username")&"','雨易社区彩票中心：大奖已经开出，请速到‘社区服务’->‘社区彩票’看看你有没有中奖！！')"
   conn.execute "update [BBSXP_Users] set newmessage=newmessage+1 where username='"&rs("username")&"'"
   rs.movenext
   loop
   rs.close
   succeed("开奖成功！！<meta http-equiv=refresh content=3;url=piao.asp><br><a href=piao.asp>返回彩票中心</a>")
   
end if

''''''OK''''''''''''''''''''''''''''自身情况'''''''''''''''''''''''''
sub self
if prizeconn.execute("select count(id) from [buyz] where datecode="&datecode&" and username='"&CookieUserName&"'")(0)=0 then 
   selfmessage="您未曾购买彩票！"
else
   set selfrs=prizeconn.execute("select * from [buyz] where datecode="&datecode&" and username='"&CookieUserName&"'")
   i=0
   do while not selfrs.eof
   i=i+1
     response.write selfrs("code")&"&nbsp;&nbsp;&nbsp;&nbsp;"
   if i mod 15=0 then
   response.write "<br>"
	  end if
 
   selfrs.movenext
   loop
   selfrs.close
end if
end sub
''''''OK''''''''''''''''''''''''''开奖过程''''''''''''''''''''''''''
sub prizego%>
<form name="form1" method="post" action="?action=out">
  <table width="97%" border="0" cellspacing="1" cellpadding="1" class=a2 align=center>
    <tr class=a1> 
      <td width="20%"><div align="center">管理选项：</div></td>
      <td width="20%"><div align="center"> 
          <input type="radio" value="rnd" name="or">
          随机开号</div></td>
      <td><div align="center"> 
          <input name="or" type="radio" value="manmade" checked>
          自选开号 
<select name="final1">
  <option selected>1</option>
  <option>2</option>
  <option>3</option>
  <option>4</option>
  <option>5</option>
  <option>6</option>
  <option>7</option>
  <option>8</option>
  <option>9</option>
  <option>0</option>
</select>
<select name="final2">
  <option selected>1</option>
  <option>2</option>
  <option>3</option>
  <option>4</option>
  <option>5</option>
  <option>6</option>
  <option>7</option>
  <option>8</option>
  <option>9</option>
  <option>0</option>
</select>
<select name="final3">
  <option selected>1</option>
  <option>2</option>
  <option>3</option>
  <option>4</option>
  <option>5</option>
  <option>6</option>
  <option>7</option>
  <option>8</option>
  <option>9</option>
  <option>0</option>
</select>
<select name="final4">
  <option selected>1</option>
  <option>2</option>
  <option>3</option>
  <option>4</option>
  <option>5</option>
  <option>6</option>
  <option>7</option>
  <option>8</option>
  <option>9</option>
  <option>0</option>
</select>
        </div></td>
      <td width="20%"><div align="center"> 
          <input type="submit" name="Submit" value="开奖">
        </div></td>
    </tr>
	<tr> 
      <td colspan="4" class=a3>提示：
        <br>&nbsp;&nbsp;&nbsp;&nbsp;为了吸引用户继续投注，增加趣味性。请区长做到经常开奖，当然，要在一定的限度内，保持新鲜的感觉。为了使用户继续投注，尽量使用自选开号，每期至少使一名用户中奖（当然，偶尔大奖空缺也是可以的。）。</td>
    </tr>
  </table>
</form>
<%end sub
'''''''OK''''''''''''''''''''''''''''''''各用户投注情况总汇''''''''''''''''
sub userp
if prizeconn.execute("select count(id) from [buyz] where datecode="&datecode)(0)=0 then
   response.write "<table align=center class=a2 width=97%  border=0 cellspacing=1 cellpadding=1 ><tr class=a1 height=25><td colspan=8 class=a1>目前本期用户投注情况：</td></tr><tr>"
   response.write "<td colspan=8 class=a3>本期目前无人购买</td></tr>"
   response.write "</table>"
else
   set userrs=prizeconn.execute("select * from [buyz] where datecode="&datecode)
   response.write "<table align=center class=a2 width=97%  border=0 cellspacing=1 cellpadding=1 ><tr class=a1 height=25><td colspan=8 class=a1>目前本期用户投注情况：</td></tr><tr>"
   i=0
   do while not userrs.eof
   i=i+1
   response.write "<td width=12% class=a3 height=23>"&userrs("code")&"("&userrs("username")&")</td>"
   if i mod 8=0 then
      response.write "</tr>"
	  end if
   if i mod 8=0 then
      response.write "<tr>"
	  end if
   userrs.movenext
   loop
   response.write "</tr></table>"
   userrs.close
end if
end sub
''''''''''''''''''''''''''''''''''''''''''''''''''''''
%>
