PROCEDURE

抢劵

DROP PROCEDURE coupon_rush;
CREATE PROCEDURE coupon_rush
    (in v_coupon_id bigint,         -- 优惠劵id
     in v_user_id bigint,           -- 用户id
     in v_status int,                              -- 状态
     in v_rush_time timestamp,      -- 抢劵时间
     out r_result int)
BEGIN
      DECLARE temp_insert_count int DEFAULT 0;   -- 修改记录
          DECLARE temp_rush_id int DEFAULT 0;        -- 抢劵id
          DECLARE temp_is_rush_sign int DEFAULT 0;   -- 是否抢劵   0-非  1-是
      DECLARE temp_is_free_sign int DEFAULT 1;     -- 是否收费   0-收费  1-免费
      DECLARE temp_buy_times int DEFAULT 0;      -- 限购次数
      DECLARE temp_rushed_num int DEFAULT 0;     -- 已抢个数
      DECLARE temp_coupon_use_type int DEFAULT 0;-- 优惠劵类型 1-优惠券 2-优惠码 3-优惠券+优惠码
      START TRANSACTION;

            -- 0.检查优惠劵类型
      SELECT is_rush,is_free,buy_times,use_type 
            INTO temp_is_rush_sign,temp_is_free_sign,temp_buy_times,temp_coupon_use_type 
            from pd_coupon where id = v_coupon_id;
            -- IF (temp_is_rush_sign = 0) THEN
            --         set r_result = -1;                   -- 非抢劵 (-1)
      IF (temp_coupon_use_type = 2) THEN
                    set r_result = -7;                     -- 不支持优惠码(-7)
            ELSE

                    -- 0.检查领取数量(免费一张, 收费多张)
                    SELECT COUNT(*) INTO temp_rushed_num 
                    from pd_coupon_rush 
                    where user_id = v_user_id and coupon_id = v_coupon_id and status != 3;  -- 排除已经撤销的抢劵
                  IF(temp_is_free_sign = 1 AND temp_rushed_num > 0) THEN
                            ROLLBACK;
                            set r_result = -6;    -- 不可多抢(-6)              
                    ELSEIF(temp_is_free_sign = 0 AND temp_rushed_num >= temp_buy_times) THEN
                            ROLLBACK;
                          set r_result = -6;    -- 不可多抢(-6)    
                    ELSE
                        
                                -- 1.记录明细
                                insert into pd_coupon_rush
                                    (user_id, coupon_id, status, created_time, updated_time)
                                values
                                    (v_user_id, v_coupon_id, v_status, v_rush_time, v_rush_time);
                                SELECT LAST_INSERT_ID() INTO temp_rush_id; -- 获取自增ID
                                select row_count() into temp_insert_count;
                                IF (temp_insert_count = 0) THEN                       -- 记录明细失败(-2)
                                    ROLLBACK;
                                    set r_result = -2;
                                ELSEIF (temp_insert_count < 0) THEN              -- 记录明细系统异常(-3)
                                    ROLLBACK;
                                    set r_result = -3;
                                ELSE  

                                            -- 2.减库存
                                            UPDATE
                                                pd_coupon
                                            SET remain_num = remain_num - 1
                                            WHERE
                                                use_type = 1
                                            -- AND is_rush = 1       --支持优惠劵, 码加劵
                                            AND remain_num>0
                                            AND code_start_time < v_rush_time
                                            AND code_end_time > v_rush_time
                                            AND `status` = 0
                                            AND preheat = 0
                                            AND is_recom = 0
                                            AND is_lottery = 0;

                                            select row_count() into temp_insert_count;
                                            IF (temp_insert_count = 0) THEN                  -- 减库存失败, 抢劵结束 (-4)
                                                ROLLBACK;
                                                set r_result = -4;
                                            ELSEIF (temp_insert_count < 0) THEN          -- 减库存失败, 系统异常 (-5)
                                                ROLLBACK;
                                                set r_result = -5;
                                            ELSE                                                              -- 减库存成功(主键id)
                                                COMMIT;
                                                set r_result = temp_rush_id;
                                            END IF;

                                END IF;
                    END IF;
          END IF;
END;

 

posted @ 2017-08-09 10:37  wanhua.wu  阅读(163)  评论(0编辑  收藏  举报