<!--#include file="inc_const.asp"-->
<%
response.expires=0
Response.CacheControl = "Private"
Set Conn=Server.CreateObject("ADODB.Connection")
conn.open Application(ConstStr & "_connstr")
sub voteend(vnum)
  dim j,rs1,end_time,temp_level,selstr,loop_num
  set rs1=conn.execute("select * from vote where votename='"&application(ConstStr & "_auto"&vnum&"name")&"' and exp_num=1 order by votenum desc,id")
  selstr=""
  end_time=dateAdd("d",cint(application(ConstStr & "_auto"&vnum&"timeout"))*3,now())
  if vnum=1 then
    conn.execute("UPDATE vrecord SET temp_level=0 WHERE votetype="&vnum)
    temp_level=application(ConstStr & "_l_kick")+2
    for j=1 to 6
      if j=2 then temp_level=application(ConstStr & "_l_kick")+1
      if j=4 then temp_level=application(ConstStr & "_l_kick")
      conn.execute("insert into vrecord (username,votetype,end_time,temp_level) values ('"&rs1("voteoptions")&"',"&vnum&",'"&end_time&"',"&temp_level&")")
      selstr=selstr&rs1("voteoptions")&" "
      rs1.movenext
    next
    occ_env="系统>系统> >"&application(ConstStr & "_auto"&vnum&"name")&">自动>"&"〖"&trim(selstr)&"〗被当选为新的管理员 ！"
    conn.Execute("INSERT INTO record (stype,occ_time,occ_env) VALUES (8,'"&now()&"','"&occ_env&">终止投票事件%%')")
  else
    loop_num=cint(application(ConstStr & "_auto"&vnum&"num"))
    for j=1 to loop_num
      conn.execute("insert into vrecord (username,votetype,end_time,temp_level) values ('"&rs1("voteoptions")&"',"&vnum&",'"&end_time&"',0)")
      conn.Execute("UPDATE user SET user_level=user_level+1 WHERE username='"&rs1("voteoptions")&"'")
      selstr=selstr&rs1("voteoptions")&" "
      rs1.movenext
    next
    occ_env="系统>系统> >"&application(ConstStr & "_auto"&vnum&"name")&">自动>"&"〖"&trim(selstr)&"〗获得了升一级的奖励 ！"
    conn.Execute("INSERT INTO record (stype,occ_time,occ_env) VALUES (8,'"&now()&"','"&occ_env&">终止投票事件%%')")
  end if
  conn.execute("UPDATE vote SET end_flag='"&now()&"' WHERE votename='"&application(ConstStr & "_auto"&vnum&"name")&"' and exp_num<>1")
  rs1.close
  set rs1=nothing
end sub
sub votestart(vnum)
  dim rootRs
  dim j,rs1
  Set rootRs=Server.CreateObject("ADODB.RecordSet")
  conn.execute("delete from vote WHERE votename='"&application(ConstStr & "_auto"&vnum&"name")&"'")
  conn.execute("delete from vrecord WHERE (DATEDIFF('s',end_time,now())>0)")
  set rs1=conn.execute("select username from vrecord where votetype="&vnum)
  filterstr=""
  do while not rs1.eof
    filterstr=filterstr&",'"&rs1("username")&"'"
    rs1.movenext
  loop
  exp_num=application(ConstStr & "_auto"&vnum&"exp")
  sqlstr="user_level in ("&application(ConstStr & "_auto"&vnum&"level")&")"
  total=10
  if vnum=1 then total=20
  sqlstr="select username from user where "&sqlstr&" and username not in (''"&filterstr&") order by exp_num desc"
  rootRs.Open sqlstr,conn,1,1
  if rootRs.recordcount>total then
    conn.Execute("INSERT INTO vote (votename,voteoptions,exp_num,begin_time,end_flag) VALUES ('"&application(ConstStr & "_auto"&vnum&"name")&"','"&application(ConstStr & "_auto"&vnum&"name")&"',"&exp_num&",'"&now()&"','"&now()&"')")
    for j=1 to 20
      conn.Execute("INSERT INTO vote (votename,voteoptions,exp_num) VALUES ('"&application(ConstStr & "_auto"&vnum&"name")&"','"&rootRs("username")&"',1)")
      rootRs.movenext
      if rootRs.eof then exit for
    next
    occ_env="系统>系统> >"&application(ConstStr & "_auto"&vnum&"name")&">自动>系统自动产生"
    conn.Execute("INSERT INTO record (stype,occ_time,occ_env) VALUES (7,'"&now()&"','"&occ_env&">产生投票事件%%')")
  end if
  rootRs.close
  set rootRs=nothing
  rs1.close
  set rs1=nothing
end sub

on error resume next
for i=1 to 4
  set rs=conn.execute("select * from vote where votename='"&application(ConstStr & "_auto"&i&"name")&"' and exp_num<>1")
  daynum=cint(application(ConstStr & "_auto"&i&"timeout"))-1
  if (rs.eof or rs.bof) and application(ConstStr & "_disablevoteauto")-0=0 then
    call votestart(i)
  elseif (DATEDIFF("d",rs("begin_time"),now())>(daynum-1) and rs("begin_time")=rs("end_flag")) then
    call voteend(i)
  elseif DATEDIFF("d",rs("begin_time"),now())>daynum and application(ConstStr & "_disablevoteauto")-0=0 then
    call votestart(i)
  end if
next
rs.close
set rs=nothing
conn.close
set conn=nothing
Response.Redirect("tpxt.asp")%>