VB.net版机房收费系统——结账功能实现(代码部分)

  实现结账功能的时候,被选项卡控件整的有点郁闷,瞬间脑袋就凌乱了,听上去自己好像很可笑的样子……于是,便去爬巨人的肩膀了~

  看了欢哥的博客,发现她的结账博客是按照“索引”思想写的,在结账实体中添加了新的属性过程(我添加的叫CheckDetail)结账表中并没有这个字段,是为了实现显示所有结账需要的信息(售卡、充值、退卡、汇总)而添加的。

VB.net版机房收费系统——结账功能实现(代码部分)_第1张图片

  U层代码如下,其中部分注释是功能实现中的一些错,我就顺便把相应的错误信息和解决办法写成了注释~

<span style="font-family:KaiTi_GB2312;font-size:18px;">Public Class frmCheck
    Private Sub frmCheck_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Dim UserInfo As New Entity.eUserLogin
        '反向思维,查询操作员和管理员
        UserInfo.UserLevel = "一般用户"
        Dim UserFac As New Facade.CheckFacade
        Dim mylist As List(Of Entity.eUserLogin)
        mylist = UserFac.ShowUserInfo(UserInfo)
        Dim i As Integer
        '显示所有的操作员和管理员
        For i = 0 To mylist.Count - 1
            cmbCheckUserID.Items.Add(Val(Trim((mylist(i).UserID))))
        Next
    End Sub
    Private Sub cmbCheckUserID_SelectedIndexChanged(sender As Object, e As EventArgs) Handles cmbCheckUserID.SelectedIndexChanged
        DgvCancelCard.Rows.Clear()
        DgvRecharge.Rows.Clear()
        DgvSellCard.Rows.Clear()
        '选择用户名后,显示用户真实姓名
        Dim eUser As New Entity.eUserLogin
        eUser.UserID = Trim(cmbCheckUserID.Text)
        Dim UserNameFac As New Facade.CheckFacade
        Dim mylist As List(Of Entity.eUserLogin)
        Dim i As Integer
        mylist = UserNameFac.ShowUserName(eUser)
        txtCheckUserName.Text = Trim(mylist(i).UserName)

        '显示结账前的不同信息
        Dim CheckInfo As New Entity.eCheck
        Dim CheckFac As New Facade.CheckFacade
        Dim dt As New DataTable
        CheckInfo.CheckDate = Format(Now, "yyyy-MM-dd")
        CheckInfo.CheckTime = Format(Now, "HH:mm:ss")
        CheckInfo.Head = GlobalUserID
        CheckInfo.UserID = cmbCheckUserID.Text
        CheckInfo.IsCheck = "N"
        '定义变量,并赋予初值
        Dim SellCardNum As Integer = 0
        Dim CancelCardNum As Integer = 0
        Dim ActualSellCardNum As Integer = 0
        Dim RechargeSum As String = 0
        Dim CancelCashSum As String = 0
        Dim ActualRechargeSum As String = 0

        '给实体赋初值,要是没有,报错:没给存储过程赋参数
        CheckInfo.RechargeCash = RechargeSum
        CheckInfo.CancelCash = CancelCashSum
        CheckInfo.Benefit = ActualRechargeSum

        '显示售卡信息
        <span style="color:#ff0000;">'在实体中添加了新的属性过程CheckDetail,结账表中并没有这个字段,为了实现显示所有结账信息的需求添加的</span>
        CheckInfo.CheckDetail = "SellCard"
        dt = CheckFac.Check(CheckInfo)
        SellCardNum = SellCardNum + dt.Rows.Count
        If dt.Rows.Count <> 0 Then
            lblSelltip.Visible = False
            DgvSellCard.Rows.Add(dt.Rows.Count)
            For i = 0 To (dt.Rows.Count - 1)
                DgvSellCard.Rows(i).Cells(0).Value = dt.Rows(i).Item(0)
                DgvSellCard.Rows(i).Cells(1).Value = dt.Rows(i).Item(1)
                DgvSellCard.Rows(i).Cells(2).Value = Mid(dt.Rows(i).Item(2), 1, 10)
                DgvSellCard.Rows(i).Cells(3).Value = dt.Rows(i).Item(3)
            Next
            DgvSellCard.AllowUserToAddRows = False
        Else
            lblSelltip.Visible = True
            'Exit Sub  '不能有这句代码,因为此处的需求是连续将相关的记录查找出来,不能间断
        End If

        '显示充值信息
        CheckInfo.CheckDetail = "Recharge"
        dt = CheckFac.Check(CheckInfo)
        If dt.Rows.Count <> 0 Then
            lblRechargetip.Visible = False
            DgvRecharge.Rows.Add(dt.Rows.Count)
            For i = 0 To (dt.Rows.Count - 1)
                DgvRecharge.Rows(i).Cells(0).Value = dt.Rows(i).Item(0)
                DgvRecharge.Rows(i).Cells(1).Value = dt.Rows(i).Item(1)
                DgvRecharge.Rows(i).Cells(2).Value = Mid(dt.Rows(i).Item(2), 1, 10)
                DgvRecharge.Rows(i).Cells(3).Value = dt.Rows(i).Item(3)
                '若是如下方式写,会报错从字符串“088        33        11        ”到类型“Double”的转换无效
                'RechargeSum = RechargeSum + dt.Rows(i).Item(1)
                '避免上述错误的写法
                '“System.FormatException”类型的未经处理的异常在 mscorlib.dll 中发生,其他信息: 输入字符串的格式不正确。
                'RechargeSum = RechargeSum + Convert.ToInt32(dt.Rows(i).Item(1))
                '改成了以下写法
                RechargeSum = RechargeSum + Val(dt.Rows(i).Item(1))
            Next
            DgvRecharge.AllowUserToAddRows = False
        Else
            lblRechargetip.Visible = True
        End If


        '显示退卡信息
        CheckInfo.CheckDetail = "CancelCard"
        dt = CheckFac.Check(CheckInfo)
        CancelCardNum = CancelCardNum + dt.Rows.Count
        If dt.Rows.Count <> 0 Then
            lblCanceltip.Visible = False
            DgvCancelCard.Rows.Add(dt.Rows.Count)
            For i = 0 To (dt.Rows.Count - 1)
                DgvCancelCard.Rows(i).Cells(0).Value = dt.Rows(i).Item(0)
                DgvCancelCard.Rows(i).Cells(1).Value = dt.Rows(i).Item(1)
                DgvCancelCard.Rows(i).Cells(2).Value = Mid(dt.Rows(i).Item(2), 1, 10)
                DgvCancelCard.Rows(i).Cells(3).Value = dt.Rows(i).Item(3)
                '若是这样写,会报错
                'CancelCashSum = CancelCashSum +(dt.Rows(i).Item(1)
                'CancelCashSum = CancelCashSum + Convert.ToInt32(dt.Rows(i).Item(1))
                CancelCashSum = CancelCashSum + Val(dt.Rows(i).Item(1))
            Next
            DgvCancelCard.AllowUserToAddRows = False
        Else
            lblCanceltip.Visible = True
        End If
        '如果没有结账需要的信息,结账按钮不可用,并给出相应的信息
        If DgvCancelCard.Rows.Count = 0 And DgvRecharge.Rows.Count = 0 And DgvSellCard.Rows.Count = 0 Then
            lblChecktip.Visible = True
            btnCheck.Enabled = False
        Else
            lblChecktip.Visible = False
            '汇总界面的显示信息
            btnCheck.Enabled = True
            txtSellNum.Text = SellCardNum
            txtCancelNum.Text = CancelCardNum
            txtCancelCashSum.Text = CancelCashSum
            txtRechargeSum.Text = RechargeSum
            '下面的代码是否加ToString并未影响运行
            'txtActualSellNum.Text = SellCardNum - CancelCardNum
            txtActualSellNum.Text = (SellCardNum - CancelCardNum).ToString
            'txtActualGainCashSum.Text = RechargeSum - CancelCashSum
            txtActualGainCashSum.Text = (RechargeSum - CancelCashSum).ToString
        End If
    End Sub
    '正式结账
    '注意:由于结账信息显示和正式结账用的是同一个存储过程,所以为了照顾存储过程里边的字段,就把
    '一些字段即使用不到,也写了一遍
    Private Sub btnCheck_Click(sender As Object, e As EventArgs) Handles btnCheck.Click
        '由于优化的想法是不选择被结账用户,结账按钮不能用,故此处不用进行如下提示
        'If cmbCheckUserID.SelectedIndex = -1 Then
        '    MsgBox("请选择被结账用户!", MsgBoxStyle.OkOnly + MsgBoxStyle.Information, "提示")
        'End If
        Dim CheckInfo As New Entity.eCheck
        Dim CheckFac As New Facade.CheckFacade
        CheckInfo.UserID = Trim(cmbCheckUserID.Text)
        CheckInfo.Head = GlobalUserID
        CheckInfo.RechargeCash = Trim(txtRechargeSum.Text)
        CheckInfo.CancelCash = Trim(txtCancelCashSum.Text)
        CheckInfo.Benefit = Trim(txtRechargeSum.Text) - Trim(txtCancelCashSum.Text)
        CheckInfo.CheckDate = Format(Now, "yyyy-MM-dd")
        CheckInfo.CheckTime = Format(Now, "HH:mm:ss")
        CheckInfo.IsCheck = "Y"
        CheckInfo.CheckDetail = "InfoSum"
        Dim IsCheckSucceed As Boolean
        IsCheckSucceed = CheckFac.IsCheck(CheckInfo)
        If IsCheckSucceed = True Then
            MsgBox("恭喜你,结账成功!", MsgBoxStyle.OkOnly + MsgBoxStyle.Information, "提示")
            txtActualGainCashSum.Text = "0"
            txtActualSellNum.Text = "0"
            txtCancelCashSum.Text = "0"
            txtCancelNum.Text = "0"
            txtRechargeSum.Text = "0"
            txtSellNum.Text = "0"
            btnCheck.Enabled = False
            Exit Sub
        Else
            MsgBox("抱歉,结账失败!", MsgBoxStyle.OkOnly + MsgBoxStyle.Information, "提示")
        End If
    End Sub
End Class</span>

  上边的代码用的是if选择语句,根据选择去显示相应的信息,所以,为了对应,我们也会有一个相应的用if选择语句进行功能实现的存储过程:

<span style="font-family:KaiTi_GB2312;font-size:18px;">USE [ComputerCharge_sys]
GO
/****** Object:  StoredProcedure [dbo].[proc_Check]    Script Date: 2016/4/9 16:40:55 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER PROCEDURE [dbo].[proc_Check]	
	@Head char(11),
	@UserID char(11),
	@RechargeSum char(10),
	@CancelCashSum char(10) ,
	@ActualRechargeSum char(10),
	@CheckDate  char(10),
	@CheckTime  char(8),
	@IsCheck varchar(10),
	@CheckDetail char(10)
AS
BEGIN	
	declare @N varchar(2)	
	set @N='N'
	declare @sql char(500)

	--查询表T_Card_Info中的售卡记录
	if @CheckDetail ='SellCard'
	begin
	select @sql ='select CardNo ,StudentNo,RegisterDate ,RegisterTime  from T_Card_Info  where Head='+ char(39)+@UserID+ char(39)+char(32)+'and IsCheck='+char(39)+@IsCheck+char(39)
	 exec(@sql)
	end 
    --查询表T_CancelCard_Info中的退卡记录
	if @CheckDetail ='CancelCard'
	begin
	select @sql ='select  CardNo ,BackCash ,CancelDate ,CancelTime from T_CancelCard_Info  where Head='+ char(39)+@UserID+ char(39)+char(32)+'and IsCheck='+char(39)+@IsCheck+char(39)
	 exec(@sql)	
	end 
	--查询充值表的充值记录
	if @CheckDetail ='Recharge'
	begin
	select CardNo ,Recharge ,RechargeDate ,RechargeTime from T_Recharge_Info   where Head=@UserID  and IsCheck =@IsCheck 
	--select @sql ='select  CardNo ,Recharge ,RechargeDate ,RechargeTime from T_Recharge_Info  where Head='+ char(39)+@UserID+ char(39)+char(32)+'and IsCheck='+char(39)+@IsCheck+char(39)
	exec(@sql)	
	end 
	 
	if @CheckDetail ='InfoSum'
	begin 
		update T_card_Info set IsCheck =@IsCheck where Head =@UserID and IsCheck = @N
		update T_CancelCard_Info  set IsCheck =@IsCheck where Head =@UserID and IsCheck = @N
		update T_Recharge_Info  set IsCheck =@IsCheck where Head =@UserID and IsCheck = @N
		insert into T_Check_Info (RechargeCash,CancelCash,Benefit,CheckDate,CheckTime,Head)values(@RechargeSum ,@CancelCashSum ,@ActualRechargeSum,@CheckDate ,@CheckTime ,@Head )
		
	--set @error=@error+@@error
	--if @error<>0 
		--rollback transaction --如果不等于0,则回滚事务,不能执行
	--else 
		--commit transaction --等于0,则执行该事务
		end
	 END</span>

  上述内容只是结账功能的代码,纯属站在巨人的肩膀上学习、比葫芦画瓢而已,接下来才是自己的探索学习过程,无奈篇幅太长,请欣赏下一篇博客《VB.net版机房收费系统——结账功能实现(调错部分)》


你可能感兴趣的:(机房重构结账功能的实现)