DELIMITER $$
USE `qrsoft_dyj_db`$$
DROP PROCEDURE IF EXISTS `proc_withdraw_approve`$$
CREATE PROCEDURE `proc_withdraw_approve`(
IN p_apply_id INT,
IN p_handleuserid INT,
IN p_handlestate INT,
IN p_handletype INT,
IN p_bankreceipt VARCHAR(200),
IN p_apply_mark VARCHAR(2000),
IN p_cash_serial VARCHAR(200))
label_pro : BEGIN
DECLARE apply_userid INT;
DECLARE apply_balance DECIMAL;
DECLARE apply_code VARCHAR(200);
DECLARE before_avabalance DECIMAL;
START TRANSACTION;
-- 校验审核记录ID
IF p_apply_id = 0 THEN
SELECT -1002 AS RESULT_CODE;
ROLLBACK;
LEAVE label_pro ;
END IF;
-- 校验审核状态
IF p_handlestate <> 2 && p_handlestate <> 3 THEN
SELECT -1001 AS RESULT_CODE;
ROLLBACK;
LEAVE label_pro;
END IF;
-- 查询用户ID 提现订单号 提现金额
SELECT applyrecord_ref_userid ,
applyrecord_balance ,
applyrecord_code INTO apply_userid, apply_balance ,apply_code
FROM qr_apply_record
WHERE applyrecord_id = p_apply_id;
IF p_handlestate = 2 THEN -- 审核成功
-- 更新提现信息表
UPDATE qr_apply_record SET
applyrecord_handlestate = p_handlestate,
applyrecord_handletime = NOW(),
applyrecord_handleuserid = p_handleuserid,
applyrecord_handletype = p_handletype,
applyrecord_bankreceipt = p_bankreceipt ,
applyrecord_mark = p_apply_mark
WHERE applyrecord_id = p_apply_id;
-- 修改用户账号信息表中累计提现金额值
UPDATE qr_useraccount SET account_drawalsbalance = account_drawalsbalance + apply_balance
WHERE account_ref_userid = apply_userid;
ELSE -- 审核失败
-- 更新提现信息表
UPDATE qr_apply_record SET
applyrecord_handlestate = p_handlestate,
applyrecord_handletime = NOW(),
applyrecord_handleuserid = p_handleuserid,
applyrecord_mark = p_apply_mark
WHERE applyrecord_id = p_apply_id;
-- 返还提现金额到账户余额和添加退款记录
-- 1.查询用户变动前可用余额
SELECT account_avabalance INTO before_avabalance FROM qr_useraccount
WHERE account_ref_userid = apply_userid;
-- 2.更新账户可用余额
UPDATE qr_useraccount SET account_avabalance = before_avabalance + apply_balance
WHERE account_ref_userid = apply_userid;
-- 3.添加退款记录
-- 交易类型 (4:退款(旅游订单申请退款) 5:退款(提现申请被拒绝退款))
-- 交易方式 1:余额支付
INSERT INTO `qr_cash_record` (`cashrecord_serial`,
`cashrecord_ref_userid`,
`cashrecord_beforebalance`,
`cashrecord_balance`,
`cashrecord_afterbalance`,
`cashrecord_type`,
`cashrecord_time`,
cashrecord_ref_id,
cashrecord_model)
VALUES (
p_cash_serial,
apply_userid,
before_avabalance,
apply_balance,
(before_avabalance + apply_balance), 5, NOW(), p_apply_id, 1);
END IF;
COMMIT;
SELECT 1 AS RESULT_CODE;
END$$
DELIMITER ;