---
title: "一个小抽奖程序处理"
author: "yinter"
date: "2008-12-25T09:55:07Z"
tags: []
slug: "untitled"
draft: false
lastmod: "2008-12-25T10:01:06Z"
id: 301204
url: "https://yinter.iteye.com/blog/301204"
---

SET QUOTED\_IDENTIFIER ON  
GO  
SET ANSI\_NULLS ON  
GO

ALTER   PROCEDURE \[dbo].\[wishwall\_init\_prize\_data]

as  
/*  
select * from dbo.wall\_prize\_basedatas

select * from dbo.wall\_set\_prizies

--exec \[wishwall\_init\_prize\_data]

select count(\*) as count from wall\_set\_prizies

select count(prizetype) as count from wall\_set\_prizies GROUP BY prizetype

select * from dbo.wall\_prize\_infos

\*/  
    declare @prizetype        int  
    declare @allcount        int  
    declare @startdate        datetime  
    declare @enddate        datetime  
    declare @lucknumber        int  
    declare @curdate        datetime  
    declare @curallcount        int  
    declare @everycount        int  
    declare @time            int  
     
    set @time=0

    start:

    truncate table wall\_set\_prizies

    set @time=@time+1  
     
     
    DECLARE cur CURSOR FOR  
    SELECT type,allcount,startdate,enddate  
    FROM wall\_prize\_basedatas where type&lt;&gt;3  
     
    OPEN cur  
    FETCH next FROM cur INTO @prizetype, @allcount,    @startdate,    @enddate

   
    WHILE @@FETCH\_STATUS = 0  
    BEGIN  
        set @curdate=@startdate

        while @curdate&lt;=@enddate  
        begin  
            select @curallcount = sum(edatecount) from wall\_set\_prizies where prizetype=@prizetype  
            if @curallcount is null  
            begin  
                set @curallcount = 0  
            end

            if @prizetype=1  
            begin  
                set @lucknumber = cast(500\*rand() as int)  
            end  
            else  
            begin  
                set @lucknumber = cast(500\*rand() as int)  
            end

            if @curallcount&lt;@allcount  
            begin  
                if @prizetype=1  
                begin  
                    select top 1 @everycount=ecount from  
                    (select 2 as 'ecount' union select 3 as 'ecount' union select 1 as 'ecount')as temptable order by newid()  
                end  
                else  
                begin  
                    select top 1 @everycount=ecount from  
                    (select 1 as 'ecount' union select 0 as 'ecount' union select 1 as 'ecount' union select 1 as 'ecount')as temptable  
                    order by newid()  
                end

                insert wall\_set\_prizies (prizetype,lucknumber,edatecount,prizedate,created\_at)  
                values(@prizetype,@lucknumber,@everycount,@curdate,getdate())  
            end  
            else  
            begin  
                insert wall\_set\_prizies (prizetype,lucknumber,edatecount,prizedate,created\_at)  
                values(@prizetype,@lucknumber,0,@curdate,getdate())  
            end

            set @curdate = @curdate + 1  
        end

        FETCH next FROM cur INTO @prizetype, @allcount,    @startdate,    @enddate  
    END

    --\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\**  
    
    DECLARE temp CURSOR FOR  
    select sum(edatecount) as 'allcount',prizetype from dbo.wall\_set\_prizies group by prizetype  
    
    OPEN temp  
    FETCH next FROM temp INTO @allcount, @prizetype  
     
    WHILE @@FETCH\_STATUS = 0  
    BEGIN  
        if @prizetype=1  
        begin  
            if @allcount&lt;&gt;80  
            begin  
                truncate table wall\_set\_prizies  
                CLOSE temp  
                DEALLOCATE temp   

                CLOSE cur  
                DEALLOCATE cur     
                goto start     
            end  
        end  
        else if @prizetype=2  
        begin  
            if @allcount&lt;&gt;20  
            begin  
                truncate table wall\_set\_prizies  
                CLOSE temp  
                DEALLOCATE temp   

                CLOSE cur  
                DEALLOCATE cur     
                goto start     
            end  
        end

        FETCH next FROM temp INTO @allcount, @prizetype  
    end

    CLOSE temp  
    DEALLOCATE temp   

    CLOSE cur  
    DEALLOCATE cur               

    select @time '@time'

GO  
SET QUOTED\_IDENTIFIER OFF  
GO  
SET ANSI\_NULLS ON  
GO

 

 

 

 

SET QUOTED\_IDENTIFIER ON  
GO  
SET ANSI\_NULLS ON  
GO

ALTER  PROCEDURE \[dbo].\[wishwall\_start\_draw]  
(  
    @prizetype        int,  
    @mo\_content        nvarchar(50),  
    @fetion\_no        varchar(20),  
    @current\_date        varchar(21)  
    --,@can\_winning    int        --是否可以中奖    0不可以，1可以  
)  
as  
/*  
select * from dbo.wall\_prize\_basedatas

select * from dbo.wall\_set\_prizies

select * from dbo.wall\_prize\_infos

exec \[wishwall\_start\_draw] 1, 'c', '644513457', '2008-12-18 16:06:03.0'

\*/    declare @can\_winning    int     
    declare @luckNumber int  
    declare @randNumber int  
    declare @isPrize    int    --0没中奖    1中奖  
    declare @drawTime    int  
    declare @prizeAllCount        int  
    declare @curPrizeAllCount    int  
    declare @curDayPrizeCount    int  
    declare @everyDayPrizeCount    int

    set @can\_winning = 0  
    set @drawtime = 0  
    set @isprize = 0

    if NOT EXISTS (SELECT fetion\_no FROM wall\_prize\_infos  
    WHERE convert(varchar(10),created\_at,121)=convert(varchar(10),@current\_date,121)  
        and fetion\_no=@fetion\_no)  
    begin  
        --允许中奖总数  
        select @prizeallcount = allcount from dbo.wall\_prize\_basedatas where type=@prizetype  
        --已中奖数量  
        select @curprizeallcount = count(fetion\_no) from wall\_prize\_infos  
            where prizetype=@prizetype and isprize=1  
        --中奖名额少于限制总数  
        if  @curprizeallcount &lt; @prizeallcount  
        begin  
            set @can\_winning=1  
        end  
        else  
        begin  
            set @can\_winning=0  
        end

        begin tran  
            --当天充许的中奖号码  
            select @luckNumber=lucknumber, @everyDayPrizeCount=edatecount from wall\_set\_prizies  
            where prizetype=@prizetype and  
            convert(varchar(10),prizedate,121)=convert(varchar(10),@current\_date,121)

            --当天中奖数量  
            select @curprizeallcount = count(fetion\_no) from wall\_prize\_infos  
            with (tablockx)    --锁定  
                    where prizetype=@prizetype and isprize=1 and  
                convert(varchar(10),created\_at,121)=convert(varchar(10),@current\_date,121)  
      
            --当天还有中奖名额  
            if @curprizeallcount &lt; @everyDayPrizeCount  
            begin  
                set @can\_winning=1  
            end  
            else  
            begin  
                set @can\_winning=0  
            end

            if @can\_winning=1    --可以中奖  
            begin  
                set @randNumber = cast(500\*rand() as int)  
            end  
            else  
            begin  
                reDraw:  
                set @drawtime = @drawtime + 1  
                set @randNumber = cast(500\*rand() as int)  
                if @randNumber = @luckNumber  
                begin  
                    goto reDraw  
                end  
            end

            if @randNumber = @luckNumber  
            begin  
                set @isprize=1  
            end  
            else  
            begin  
                set @isprize=0  
            end  
             
            insert into wall\_prize\_infos (fetion\_no,mocontent,mtcontent,prizetype,isprize,lucknumber,created\_at)  
            values(@fetion\_no,@mo\_content,'',@prizetype,@isprize,@randNumber,cast(@current\_date as datetime))  
        commit

        print 'not exist'  
    end  
    else  
    begin  
        print 'exist'  
    end

    SELECT @isprize as 'prizevalue'  
    --SELECT @LUCKNUMBER 'lucknumber',@randnumber 'randnumber',@drawtime 'drawTime',@isprize 'isprize'  
   

GO  
SET QUOTED\_IDENTIFIER OFF  
GO  
SET ANSI\_NULLS ON  
GO

 

 

 

 

SET QUOTED\_IDENTIFIER ON  
GO  
SET ANSI\_NULLS ON  
GO

ALTER   PROCEDURE \[dbo].\[wishwall\_user\_score]  
(  
    @promise\_id        int,  
    @recv\_fetion    varchar(20)  
)  
as  
/*  
select * from dbo.wall\_prize\_basedatas

select * from dbo.wall\_set\_prizies

select * from dbo.wall\_prize\_infos

select * from promise where pid = 81

exec \[wishwall\_user\_score] 81,'726534971'

select * from wall\_user\_scores

select * from dbo.wall\_user\_wish\_relations

delete from wall\_user\_wish\_relations

\*/    declare @promiseContent    nvarchar(1000)  
    -- status    0字条不存在    1查看者不正确 2查看者正确 3多次查询  
    declare @status            int  
    declare @send\_fetion    varchar(20)  
    declare @send\_score\_id    int  
    declare @recv\_score\_id    int  
    declare @recv\_date        varchar(10)  
   

    select top 1 @promiseContent = promiseContent from promise  
    where pid = @promise\_id  
     
    if @promiseContent is null  
    begin  
        set @status = 0  
    end  
    else  
    begin  
        select top 1 @send\_fetion = sendFetion ,@recv\_date = convert(varchar(10),sendTime,121)  
        from promise where pid=@promise\_id and acceptFetion = @recv\_fetion  
        --查看者不正确  
        if @send\_fetion is null    --NOT EXISTS()  
        begin  
            set @status = 1     
        end  
        else  
        begin  
            --第一次两者交互  
            if NOT EXISTS(select top 1 id from dbo.wall\_user\_wish\_relations  
                where send\_fetion=@send\_fetion and recv\_fetion=@recv\_fetion)  
            begin  
                set @status = 2

                insert into wall\_user\_wish\_relations (send\_fetion, recv\_fetion)  
                values(@send\_fetion, @recv\_fetion)

                select top 1 @send\_score\_id = id from wall\_user\_scores  
                    where fetion\_no = @send\_fetion

                select top 1 @recv\_score\_id = id from wall\_user\_scores  
                    where fetion\_no = @recv\_fetion

                --发送者积分处理  
                if @send\_score\_id is null  
                begin  
                    insert into wall\_user\_scores (fetion\_no,send\_score)  
                    values(@send\_fetion,1)  
                end  
                else  
                begin  
                    update wall\_user\_scores set send\_score = send\_score + 1  
                    where id = @send\_score\_id  
                end

                --查看者积分处理  
                if @recv\_score\_id is null  
                begin  
                    insert into wall\_user\_scores (fetion\_no,recv\_score)  
                    values(@recv\_fetion, 1)  
                end  
                else  
                begin  
                    update wall\_user\_scores set recv\_score = recv\_score + 1  
                    where id = @recv\_score\_id  
                end  
            end  
            else  
            begin  
                set @status = 3  
            end  
        end  
    end

    if @status not in (2,3)  
    begin  
        set @promiseContent = ''  
    end

    select @status as 'status', @promiseContent as 'content',  
        month(@recv\_date) as 'month',year(@recv\_date) as 'year',  
        day(@recv\_date) as 'day' ,@send\_fetion as 'send\_fetion'

GO  
SET QUOTED\_IDENTIFIER OFF  
GO  
SET ANSI\_NULLS ON  
GO