CREATE OR REPLACE FUNCTION "public"."tradegift3(src_id int4, dst_id int4, gift_id int4, gift_count i"(IN src_id int4, IN dst_id int4, IN gift_id int4, IN gift_count int4, IN gift_type int4, IN total_out int4, IN total_in int4, IN trade_type int4, IN room_id int4, OUT ret_sucess int4, OUT src_nk int4, OUT src_nb int4, OUT dst_nk int4, OUT dst_nb int4, OUT dst_nchestnum int4)
RETURNS "pg_catalog"."record" AS $BODY$
DECLARE
r0 RECORD;
todaycost integer;
bnewchest integer;
today date;
BEGIN
src_nk:=0;
src_nb:=0;
dst_nk:=0;
dst_nb:=0;
dst_nchestnum:=0;
todaycost:=0;
bnewchest:=0;
select now() as val into r0;
select now() into today;
update tbuseraccount set nk=nk-total_out where nuserid=src_id and nk>=total_out;
if FOUND then
if trade_type=1 then
update tbuseraccount set nb=nb+total_in where nuserid=dst_id;
else
update tbuseraccount set nk=nk+total_in where nuserid=dst_id;
end if;
insert into tbusertradelog(nuserid,nbuddyid,ngiftid,ncount,dtime,ngifttype,nvcbid,nusermoney,nbuddymoney) VALUES(src_id,dst_id,gift_id,gift_count,r0.val,gift_type,room_id,total_out,total_in);
select ncost into todaycost from tbusertodaycost where nuserid=src_id and ddate=today;
if FOUND then
todaycost := todaycost + total_out;
update tbusertodaycost set ncost=ncost + total_out where nuserid=src_id;
else
todaycost := total_out;
update tbusertodaycost set ncost=total_out, ddate=today where nuserid=src_id;
if not FOUND then
insert into tbusertodaycost(nuserid,ncost,ddate) values(src_id,total_out,today);
end if;
end if;
if todaycost >= 10000000 then
bnewchest :=1;
update tbusertodaycost set ncost=0 where nuserid=src_id;
update tbuseraccount set nchestnum=nchestnum+1 where nuserid=src_id;
select nchestnum into dst_nchestnum from tbuseraccount where nuserid=src_id;
end if;
select nk,nb from tbuseraccount where nuserid=src_id into src_nk,src_nb;
select nk,nb from tbuseraccount where nuserid=dst_id into dst_nk,dst_nb;
ret_sucess=0;
if bnewchest=1 then
ret_sucess=1;
end if;
RETURN;
end if;
ret_sucess=-1;
RETURN;
END;
$BODY$
LANGUAGE 'plpgsql' VOLATILE COST 100
;
ALTER FUNCTION "public"."tradegift3(src_id int4, dst_id int4, gift_id int4, gift_count i"(IN src_id int4, IN dst_id int4, IN gift_id int4, IN gift_count int4, IN gift_type int4, IN total_out int4, IN total_in int4, IN trade_type int4, IN room_id int4, OUT ret_sucess int4, OUT src_nk int4, OUT src_nb int4, OUT dst_nk int4, OUT dst_nb int4, OUT dst_nchestnum int4) OWNER TO "ksbox";