LeftMoney ,当前余额 , 需要加上充值额
DepositSum ,充值总额 , 需要加上充值额
ConsumeNo ,卡操作序号 ,需要加1
ForceAct 用leftmoney去强制同步cardleftmoney,是一个“卡片修复的行为”
UnInformedMoney 若ForceAct的值是单数,不需要变动;若是0或双数,则要加上充值额
USE [CASH]
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROC [dbo].[pr_addMoney]
@Cash money, --充值金额
@CardIDH varchar(16) , --需要充值的卡号
@AccExist int OUTPUT, --用户存在或不存在标记
@LogID int OUTPUT --充值成功的tb_OperLog记录LogID
AS
BEGIN
DECLARE @ERROR INT --定义
SET @ERROR=0 --事务回滚判断条件:错误计数
SET XACT_ABORT ON
BEGIN TRAN tran_addMoney --开始事务
IF exists
(SELECT AccountNo
FROM [CASH].[dbo].[tb_Customer]
WHERE CardIDH=@CardIDH) --判断用户是否存在,并且印刷卡号和物理卡号一致
--WHERE CardIDH=@CardIDH AND CardIDH=CardID) --测试卡号的发卡卡号和物理卡号不一致,临时注释掉
BEGIN
SET @AccExist =1 --存在标记1
--局部变量
DECLARE
@AccountNo int, --用户编号
@CustomerName varchar(32), --用户名
@deptNo varchar(12), --班级号
@ClassNo tinyint, --人员类别
@LeftMoney money, --原余额
@ConsumeNo int, --序号
@ConsumeNoC int, --卡操作序号
@CardLeftMoney money, --卡中余额
@ExChgDate datetime, --充值日期
@TempID int, --临时ID值
@TempAcc int --临时账户值
SET @ExChgDate=convert(datetime,convert(char(10),GetDate(),120), 20); --为一个特殊格式的字段赋初值
--查询出充值人名字
SELECT @AccountNo=AccountNo,
@CustomerName=CustomerName,
@deptNo=DeptNo,
@ClassNo=ClassNo,
@LeftMoney=LeftMoney,
@ConsumeNo=ConsumeNo,
@ConsumeNoC=ConsumeNoC,
@CardLeftMoney=CardLeftMoney
FROM [CASH].[dbo].[tb_Customer]
WHERE CardIDH=@CardIDH;
BEGIN
SELECT @TempID=MAX(LogID) FROM [CASH].[dbo].[tb_OperLog]; --查询添加充值记录前最大LOGID
--在tb_OperLog添加充值明细
INSERT INTO [CASH].[dbo].[tb_OperLog]
(EDate,OperatorNo,AccountNo,CustomerName,deptNo,ClassNo,Times,AccCount,OldLeftMoney,LeftMoney,AccLeftMoney,GLF,YJ,CardFee,Cash,ItemNo,ConsumeNo,ConsumeNoC,CardLeftMoney,LocaAreaNo,OperAreaNo,ExChgDate)
VALUES
(GETDATE(),100,@AccountNo,@CustomerName,@deptNo,@ClassNo,1,0,@LeftMoney,@LeftMoney+@Cash,@Cash,0,0,0,0,11,@ConsumeNo+1,@ConsumeNoC,@CardLeftMoney,1,1,@ExChgDate);
SELECT @LogID=MAX(LogID)
FROM [CASH].[dbo].[tb_OperLog]; --查询添加充值记录后最大LOGID
SELECT @TempAcc=AccountNo
FROM [CASH].[dbo].[tb_OperLog]
WHERE LogID=@LogID; --查询添加充值记录后最大LOGID所对应的用户账号
IF ( (@LogID>@TempID) AND (@TempAcc=@AccountNo) ) --添加记录成功后再执行充值操作
BEGIN
PRINT ‘在tb_OperLog添加充值明细成功@LogID是‘+CAST(@LogID as nvarchar(10));
--tb_Customer表充值(先添加充值记录成功,后执行充值操作)
UPDATE [CASH].[dbo].[tb_Customer]
SET LeftMoney=LeftMoney+@Cash,
DepositSum=DepositSum+@Cash,
ConsumeNo=ConsumeNo+1,
UnInformedMoney=UnInformedMoney+@Cash
WHERE CardIDH=@CardIDH;
END
ELSE
BEGIN
PRINT ‘在tb_OperLog添加充值明细失败,充值失败,请查明原因‘;
SET @ERROR=@ERROR+1
END
END
END
ELSE
BEGIN
SET @AccExist=0 --不存在标记0
PRINT ‘查无此人,或账户异常,充值失败‘;
SET @ERROR=@ERROR+1
END
SET @ERROR=@ERROR+@@ERROR --系统报错和逻辑报错累加
IF @ERROR<>0
BEGIN
ROLLBACK TRAN;
PRINT ‘充值失败,@@ERROR是:‘+cast(@@ERROR as nvarchar(10))
END
ELSE
BEGIN
COMMIT TRAN;
PRINT ‘账户为‘+cast(@AccountNo as nvarchar(8))+‘的‘+@CustomerName+‘充值‘+cast(@Cash as nvarchar(7))+‘成功‘ --打印充值成功信息
END
END
**
原文地址:http://blog.51cto.com/13710467/2105326