???????????????????????2???α???洢????
set ANSI_NULLS ON
set QUOTED_IDENTIFIER ON
go
ALTER PROCEDURE [dbo].[CancelOrderBySystem]
AS
BEGIN
declare    /*????????*/
@Status varchar(100)??  --??
@TimeNow datetime??   --??????
@TradeID varchar(50)??  --????????
@GoodsID int??   --???ID
@Num int??   --????
@SkuID int  --???ID
set @Status='TRADE_CLOSED_BY_SYSTEM'   /*????????*/
set @TimeNow=getdate()
begin transaction; --??????????
--????????????????
--??????????????????????????ж????????й??
--?????й????????ж??????????й????????????
--????й??????ж?ù??????????????????????????
--??????????
declare cancelOrder_Cursor cursor for  --?????α?
select TradeID from WxTrade where  Status='WAIT_BUYER_PAY' and OutTime<=@TimeNow
OPEN cancelOrder_Cursor    --???α?
FETCH NEXT FROM cancelOrder_Cursor    --????α???????????
into @TradeID    --???????????α?????е????????
----------------------???α?????????begin --------------------------
WHILE (@@FETCH_STATUS = 0)    --FETCH?????г??
BEGIN
--??????????
update WxTrade set Status=@Status??CloseTime=@TimeNow where TradeID=@TradeID
--???????????棬???????????????????????
declare orderDetail_Cursor cursor for  --?????α?
select GoodsID??SkuID??Num from WxOrder where  TradeID=@TradeID
OPEN orderDetail_Cursor    --???α????????
FETCH NEXT FROM orderDetail_Cursor    --????α???????????
into @GoodsID??@SkuID??@Num   --???????????α?????е????ID?????ID??????
----------------------???????α????????begin ----------------------
------------------------------------------------------------------------
WHILE (@@FETCH_STATUS = 0)    --FETCH?????г??
BEGIN
if(@SkuID is null)
BEGIN
--??й????????ж??????????й???????????棬??????
if not exists(select SkuID from [Sku] where GoodsID=@GoodsID)
BEGIN
update Goods set Quantity=Quantity+@Num where GoodsID=@GoodsID
END
END
else
BEGIN
--????й??????ж?ù??????????????????????????
if exists(select SkuID from [Sku] where SkuID=@SkuID)
BEGIN
update [Sku] set ItemQuantity=ItemQuantity+@Num where SkuID=@SkuID
update Goods set Quantity=Quantity+@Num where GoodsID=@GoodsID
END
END
FETCH NEXT FROM orderDetail_Cursor     --????α??????У????????
into @GoodsID??@SkuID??@Num   --???????????α?????е????ID?????ID??????
END
CLOSE orderDetail_Cursor    --????α????????
DEALLOCATE orderDetail_Cursor     --????α????????
----------------------???????α????????end ----------------------
----------------------------------------------------------------------
FETCH NEXT FROM cancelOrder_Cursor     --????α??????У?????????
into @TradeID    --???????????α?????е????????
End
CLOSE cancelOrder_Cursor    --????α?????????
DEALLOCATE cancelOrder_Cursor    --????α?????????
----------------------???α?????????end --------------------------
if(@@error>0)
begin
rollback transaction
return 0
end
else
begin
commit transaction
return 1
end
END