<!--#include file="../Include/Conn.jsp" -->
<%
if Request.Cookies("aspxnet")("CurUsername")="" then
response.write "<script language=javascript>alert('对不起，您还没有登陆！');window.close();</script>"
response.End
end if
dim jpid,username,action
action=request.QueryString("action")
username=trim(request.Cookies("aspxnet")("CurUsername"))
jpid=request.QueryString("id")


if action="add" then
'判断用户是否已换这个奖品
set rs_s=server.CreateObject("adodb.recordset")
rs_s.open "select count(*) as rec_count from T_action_jp where jpid="&jpid&" and zhuangtai=7 and username='"&username&"'",conn,1,1
if rs_s("rec_count")>0 then
	rs_s.close
	set rs_s=nothing
	response.write "<script language=javascript>alert('这个奖品您已经选了，请再选其他奖品！');history.go(-1);</script>"
	response.end
end if
rs_s.close
set rs_s=nothing
'判断用户的现有金币
set rs_s=server.CreateObject("adodb.recordset")
rs_s.open "select Points from LeadBBS_User where UserName='"&username&"'",conn,1,1
jifen_xy=rs_s("Points")
rs_s.close
set rs_s=nothing
'判断用户的已准备换的金币
set rs_s=server.CreateObject("adodb.recordset")
rs_s.open "select point from T_action_jp where username='"&username&"' and zhuangtai=7",conn,1,1
jifen_yy=0
while not rs_s.eof
jifen_yy=jifen_yy+rs_s("point")
rs_s.movenext
wend
rs_s.close
set rs_s=nothing
'判断奖品需要的金币
set rs_s=server.CreateObject("adodb.recordset")
rs_s.open "select point,count from T_jp where jpid="&jpid,conn,1,1
jifen_y=rs_s("point")
counter=rs_s("count")
rs_s.close
set rs_s=nothing

if counter=0 then
	response.write "<script language=javascript>alert('这个奖品数量不够兑换，请再选其他奖品！');history.go(-1);</script>"
	response.end
end if
if jifen_xy<(jifen_yy+jifen_y) then
	response.write "<script language=javascript>alert('您现有的金币不够兑换本奖品，请更换其他奖品！');history.go(-1);</script>"
	response.end
end if

m_Sql = "Update T_jp set count = count - 1 Where jpid="&jpid
			Set Rs = conn.Execute(m_Sql)

m_Sql = "Update LeadBBS_User set Points = Points-"&jifen_y&" Where UserName='" & username & "'"
			Set Rs = conn.Execute(m_Sql)

'//奖品，判断是否存在
set rs=server.CreateObject("adodb.recordset")
rs.open "select * from T_action_jp",conn,1,3
rs.addnew
rs("jpid")=jpid
rs("username")=username
rs("actiondate")=now() '加入日期
rs("zhuangtai")=7
rs("point")=jifen_y
rs("jpcount")=1
rs.update
rs.close
set rs=nothing
response.Redirect "Center_wealth.jsp"
end if
%>
