DELIMITER $$
DROP PROCEDURE IF EXISTS `p_user_redbag_grap` $$
CREATE PROCEDURE `p_user_redbag_grap`(
IN postRedbagId VARCHAR(128), -- 帖子与红包关联id
IN userId VARCHAR(128), -- 用户id
IN userRedbagInfoId VARCHAR(128), -- 用户红包详情id
IN redbagSummaryId VARCHAR(128), -- 用户红包统计id
IN svrTime TIMESTAMP, -- 服务器时间
IN amount BIGINT, -- 红包金额
IN type INT, -- 红包类型
IN postType VARCHAR(1), -- 帖子类型: 1-链接 2-日志 3- 推荐好友 4-登录
OUT resCode INT -- 结果编码 1-成功,2-帖子不存在或已被删除, 3-红包已被抢完 9-数据库异常
)
BEGIN
DECLARE redbagId VARCHAR(64);
DECLARE sourceId VARCHAR(64);
DECLARE CONTINUE HANDLER FOR SQLEXCEPTION SET resCode=9;
START TRANSACTION;
SET @count = 0;
SET @restNum = 0;
SET resCode = 1;
SET @makeAmount = 0;
SET @sysAmount = 0;
-- 查询帖子红包关联表信息
SELECT count(*) INTO @count from t_idbear_post_redbag where id = postRedbagId AND `status` = '1' and isavailable = '1';
IF @count > 0 THEN
SELECT rest_num, redbag_id, post_id INTO @restNum, redbagId, sourceId from t_idbear_post_redbag where id = postRedbagId AND `status` = '1' and isavailable = '1';
IF
@restNum < 1 THEN
-- SELECT '剩余红包小于1';
SET resCode = 3;
ELSE
-- SELECT '插入用户红包详情';
INSERT INTO
t_idbear_user_redbag_info (id, user_id, post_redbag_id,redbag_id, source_Id, post_type, amount, type, status, isavailable, create_by, create_time, update_by, update_time)
VALUES (userRedbagInfoId, userId, postRedbagId, redbagId, sourceId, postType, amount, type, '1', '1', userId, svrTime, userId, svrTime);
-- SELECT CONCAT('resCode=', resCode);
-- SELECT '修改红包与帖子关联表红包剩余数量';
UPDATE t_idbear_post_redbag SET rest_num = rest_num - 1, update_time =svrTime, update_by = user_id, update_time = svrTime where id = postRedbagId;
END IF;
ELSE
SET resCode = 2;
END IF;
IF resCode = 9 THEN
ROLLBACK;
ELSE
COMMIT;
END IF;
SELECT resCode;
END $$
DELIMITER ;