机房收费系统重构(7)——下机

背景:整理完《机房收费系统重构(6)——上机》,觉得好事应该成双,再谈一下下机的内容吧。自我感觉下机的内容比上机的内容复杂那么一点点,因为下机要涉及到“钱”的问题,这个很重要,既不能让购买这款软件的人亏本,又要保证持卡人的利益,本着全心全意为人民服务的宗旨,才能打造出更好的软件。


一、流程图


机房收费系统重构(7)——下机_第1张图片


二、代码展示


1、SqlHelper(同上机)

2、实体层(同上机)

3、接口方法层

Imports Entity
Public Interface IOfflineDAO
    '创建检查用户是否存在的接口方法
    Function checkcard(ByVal card As Entity.StudentInfo) As DataTable
    '创建检查用户是否正在上机的接口方法
    Function checkonline(ByVal line As Entity.OnlineInfo) As DataTable
    '创建获取基本数据的接口方法
    Function acquirebasic() As DataTable
    '创建更新student表的接口方法
    Function updatestu(ByVal card As Entity.StudentInfo) As Boolean
    '创建更新online表的接口方法
    Function deletecard(ByVal card As Entity.OnlineInfo) As Boolean
    '创建更新line表的接口方法
    Function updateline(ByVal card As Entity.LineInfo) As Boolean
End Interface

4、工厂+反射层

Imports IDAL
Imports System.Reflection
Imports System.Configuration
'/************************************************************************************
'类 名 称:SqlServerFactory
'命名空间:Factory
'内    容:读配置文件,来给DB字符串赋值,在配置文件中写明是SqlServer还是Access,想要
'更换数据库直接改配置文件就行。前提是:SqlServer前缀的DAL层类里写的时访问SQLServer
'数据库的代码。Access前缀的DAL层类里写的时访问Access数据库的代码
'功    能:用反射+配置文件+抽象工厂,方便更换数据库
'创建时间:2015/3/29 15:22:54
'作    者:何丽杰
'修改时间:
'修 改 人:
'版 本 号:v1.0.0
'******************************************************************************************/
Public Class SqlServerFactory
    '利用反射+配置文件+抽象工厂
    Private Shared ReadOnly AssemblyName As String = "DAL"
    '定义程序集名称变量,D层命名空间的名字
    Private Shared ReadOnly db As String = ConfigurationManager.AppSettings("DB")

'下机
    Public Function Offline() As IDAL.IOfflineDAO
        Return CType(Assembly.Load(AssemblyName).CreateInstance(AssemblyName + "." + db + "OfflineDAO"), IOfflineDAO)
    End Function

5、D层

Imports IDAL
Imports Entity
Imports System
Imports System.Data
Imports System.Data.SqlClient
Imports System.Configuration
Imports System.Reflection

Public Class SqlServerOfflineDAO : Implements IOfflineDAO
    '检查用户是否存在
    Public Function checkcard(card As StudentInfo) As DataTable Implements IOfflineDAO.checkcard
        Dim sql As String
        Dim table As New DataTable
        Dim paras As SqlParameter() = {New SqlParameter("@cardno", card.Cardno)}
        sql = "select * from Student_Info where Cardno=@cardno "
        table = SqlHelper.ExecSelect(sql, CommandType.Text, paras)
        Return table
    End Function

    '检查用户是否正在上机
    Public Function checkonline(online As Entity.OnlineInfo) As DataTable Implements IOfflineDAO.checkonline
        Dim sql As String
        Dim table As New DataTable
        Dim paras As SqlParameter() = {New SqlParameter("@cardno", online.Cardno)}
        sql = "select * from Online_Info where Cardno=@cardno "
        table = SqlHelper.ExecSelect(sql, CommandType.Text, paras)
        Return table
    End Function

    '获取基本数据
    Public Function acquirebasic() As DataTable Implements IOfflineDAO.acquirebasic
        Dim sql As String
        Dim table As New DataTable
        sql = "select * from BasicData_Info "
        table = SqlHelper.ExecSelectNo(sql, CommandType.Text)
        Return table
    End Function

    '更新student表
    Public Function updatestu(ByVal card As Entity.StudentInfo) As Boolean Implements IOfflineDAO.updatestu
        Dim sql As String
        Dim flag As Boolean
        Dim paras As SqlParameter() = {New SqlParameter("@cardno", card.Cardno),
                                       New SqlParameter("@cash", card.Cash)}
        sql = "update Student_Info set Cash=@cash where Cardno=@cardno"
        flag = SqlHelper.ExecAddDelUpdate(sql, CommandType.Text, paras)
        Return flag
    End Function

    '更新online表
    Public Function deletecard(ByVal online As Entity.OnlineInfo) As Boolean Implements IOfflineDAO.deletecard
        Dim sql As String
        Dim flag As Boolean
        Dim paras As SqlParameter() = {New SqlParameter("@cardno", online.Cardno)}
        sql = "delete from Online_Info where Cardno=@cardno"
        flag = SqlHelper.ExecAddDelUpdate(sql, CommandType.Text, paras)
        Return flag
    End Function

    '更新line表
    Public Function updateline(ByVal line As Entity.LineInfo) As Boolean Implements IOfflineDAO.updateline
        Dim sql As String
        Dim flag As Boolean
        Dim paras As SqlParameter() = {New SqlParameter("@cardno", line.Cardno),
                                       New SqlParameter("@cardtype", line.Cardtype),
                                    New SqlParameter("@studentno", line.Studentno),
                                    New SqlParameter("@studentname", line.Studentname),
                                    New SqlParameter("@sex", line.Sex),
                                       New SqlParameter("@department", line.Department),
                                       New SqlParameter("@ondate", line.Ondate),
                                       New SqlParameter("@ontime", line.Ontime),
                                       New SqlParameter("@offdate", line.Offdate),
                                       New SqlParameter("@offtime", line.Offtime),
                                       New SqlParameter("@consumetime", line.Consumetime),
                                       New SqlParameter("@consume", line.Consume),
                                       New SqlParameter("@cash", line.Cash),
                                       New SqlParameter("@remark", line.Remark),
                                       New SqlParameter("@ischeck", line.Ischeck),
                                       New SqlParameter("@computer", line.Computer)}
        sql = "insert into Line_Info values(@cardno,@cardtype,@studentno,@studentname,@sex,@department,@ondate,@ontime,@offdate,@offtime,@consumetime,@consume,@cash,@remark,@ischeck,@computer)"
        flag = SqlHelper.ExecAddDelUpdate(sql, CommandType.Text, paras)
        Return flag
    End Function
End Class

6、B层

Imports IDAL
Imports Entity
Imports Factory

Public Class BOffline
    '检查用户是否存在,获取用户余额
    Public Function Checkcard(ByVal card As Entity.StudentInfo) As DataTable
        '定义并实例化一个工厂
        Dim factory As New Factory.SqlServerFactory
        Dim icard As IDAL.IOfflineDAO
        Dim table As New DataTable
        icard = factory.Offline
        table = icard.checkcard(card)
        Return table
    End Function

    '检查该卡是否正在上机
    Public Function checkonline(ByVal online As Entity.OnlineInfo) As DataTable
        '定义并实例化一个工厂
        Dim factory As New Factory.SqlServerFactory
        Dim iline As IDAL.IOfflineDAO
        Dim table As New DataTable
        iline = factory.Offline
        table = iline.checkonline(online)
        Return table
    End Function

    '获取基本数据
    Public Function acquirebasic() As DataTable
        Dim factory As New Factory.SqlServerFactory
        Dim ibasic As IDAL.IOfflineDAO
        Dim table As New DataTable
        ibasic = factory.Offline
        table = ibasic.acquirebasic()
        Return table
    End Function

    '更新student表
    Public Function updatestu(ByVal card As Entity.StudentInfo) As Boolean
        Dim factory As New Factory.SqlServerFactory
        Dim icard As IDAL.IOfflineDAO
        Dim flag As Boolean
        icard = factory.Offline
        flag = icard.updatestu(card)
        If flag = True Then
            Return True
        Else
            Return False
        End If
        Return flag
    End Function

    '更新online表,删除已经下机的用户
    Public Function deletecard(ByVal online As Entity.OnlineInfo) As Boolean
        Dim factory As New Factory.SqlServerFactory
        Dim icard As IDAL.IOfflineDAO
        Dim flag As Boolean
        icard = factory.Offline
        flag = icard.deletecard(online)
        If flag = True Then
            Return True
        Else
            Return False
        End If
        Return flag
    End Function

    '更新line表,将下机的用户更新到line表
    Public Function updateline(ByVal line As Entity.LineInfo) As Boolean
        Dim factory As New Factory.SqlServerFactory
        Dim icard As IDAL.IOfflineDAO
        Dim flag As Boolean
        icard = factory.Offline
        flag = icard.updateline(line)
        If flag = True Then
            Return True
        Else
            Return False
        End If
        Return flag
    End Function
End Class

7、外观层

Imports Entity

Public Class OffLineFacade
    '检查学生卡号是否存在,获取用户余额
    Public Function Checkcard(ByVal card As Entity.StudentInfo) As DataTable
        Dim bllcard As New BLL.BOffline
        Dim table As New DataTable
        table = bllcard.Checkcard(card)
        Return table
    End Function

    '检查用户是否正在上机
    Public Function Checkonline(ByVal online As Entity.OnlineInfo) As DataTable
        Dim bllline As New BLL.BOffline
        Dim table As New DataTable
        table = bllline.checkonline(online)
        Return table
    End Function

    '获取基本数据
    Public Function acquirebasic() As DataTable
        Dim bllbasic As New BLL.BOffline
        Dim table As New DataTable
        table = bllbasic.acquirebasic()
        Return table
    End Function

    '更新student表
    Public Function updatestu(ByVal card As Entity.StudentInfo) As Boolean
        Dim bllcard As New BLL.BOffline
        Dim flag As Boolean
        flag = bllcard.updatestu(card)
        Return flag
    End Function

    '更新online表,删除已经下机的用户
    Public Function deletecard(ByVal online As Entity.OnlineInfo) As Boolean
        Dim bllcard As New BLL.BOffline
        Dim flag As Boolean
        flag = bllcard.deletecard(online)
        Return flag
    End Function

    '更新line表,将已经下机的用户添加到line表
    Public Function updateline(ByVal line As Entity.LineInfo) As Boolean
        Dim bllcard As New BLL.BOffline
        Dim flag As Boolean
        flag = bllcard.updateline(line)
        Return flag
    End Function
End Class

8、U层

Private Sub btoff_Click(sender As Object, e As EventArgs) Handles btoff.Click
        '判断卡号是否输入
        If txtcardno.Text = "" Then
            MsgBox("请输入要下机的卡号!")
            txtcardno.Focus()
            Return
        End If

        '判断该卡是否存在
        '实例化实体层
        Dim card As New Entity.StudentInfo
        '将界面数据传入
        card.Cardno = Trim(txtcardno.Text)
        '实例化外观层
        Dim oldcash As Integer
        Dim offlinefacade As New Facade.OffLineFacade
        Dim table1 As New DataTable
        table1 = offlinefacade.Checkcard(card)
        If table1.Rows.count = 0 Then
            MsgBox("该卡号没有注册!")
            txtcardno.SelectAll()
            txtcardno.Focus()
            Return
        End If
        '获取用户原来的余额
        oldcash = Trim(table1.Rows(0).Item(7))

        '判断该卡是否正在上机
        Dim online As New Entity.OnlineInfo
        online.Cardno = Trim(txtcardno.Text)
        Dim table2 As New DataTable
        table2 = offlinefacade.Checkonline(online)
        If table2.Rows.Count = 0 Then
            MsgBox("该卡没有上机,不用下机!")
            txtcardno.SelectAll()
            txtcardno.Focus()
            Return
        Else
            MsgBox("下机成功!")
            txttype.Text = Trim(table2.Rows(0).Item(1))
            txtstudentno.Text = Trim(table2.Rows(0).Item(2))
            txtstuname.Text = Trim(table2.Rows(0).Item(3))
            txtsex.Text = Trim(table2.Rows(0).Item(4))
            txtdepartment.Text = Trim(table2.Rows(0).Item(5))
            '上机日期和时间
            txtondate.Text = Trim(table2.Rows(0).Item(6))
            txtontime.Text = Trim(table2.Rows(0).Item(7))
            '下机日期和时间
            txtoffdate.Text = Trim(DateTime.Now.ToLongDateString)
            txtofftime.Text = Trim(DateTime.Now.ToLongTimeString)
            '计算消费时间
            txtconsumetime.Text = DateDiff("n", Trim(txtontime.Text), Trim(txtofftime.Text))
            '获取基本数据
            Dim rate As String
            Dim tmprate As String
            Dim basicfacade As New Facade.OffLineFacade
            Dim table3 As New DataTable
            table3 = basicfacade.acquirebasic()
            '获取固定用户收费标准
            rate = Trim(table3.Rows(0).Item(1))
            '获取临时用户收费标准
            tmprate = Trim(table3.Rows(0).Item(2))
            '获取上机用户的用户类型
            Dim type As String
            type = Trim(txttype.Text)
            '计算消费金额
            If type = "固定用户" Then
                txtconsume.Text = Val(rate * Int(txtconsumetime.Text / 60))
            Else
                txtconsume.Text = Val(tmprate * Int(txtconsumetime.Text / 60))
            End If

            '计算余额
            txtcash.Text = oldcash - Trim(txtconsume.Text)
            '更新student表(cash)
            card.Cash = Trim(txtcash.Text)
            Dim flag1 As Boolean
            flag1 = offlinefacade.updatestu(card)

            '更新Online表,删除已经下机的用户     
            Dim flag2 As Boolean
            flag2 = offlinefacade.deletecard(online)

            '更新line表,将下机的用户添加到line表
            Dim line As New Entity.LineInfo
            '将界面数据传入
            line.Cardno = Trim(txtcardno.Text)
            line.CardType = Trim(txttype.Text)
            line.Studentno = Trim(txtstudentno.Text)
            line.Studentname = Trim(txtstuname.Text)
            line.Sex = Trim(txtsex.Text)
            line.Department = Trim(txtdepartment.Text)
            line.Ondate = Trim(txtondate.Text)
            line.Ontime = Trim(txtontime.Text)
            line.Offdate = Trim(txtoffdate.Text)
            line.Offtime = Trim(txtofftime.Text)
            line.Consumetime = Trim(txtconsumetime.Text)
            line.Consume = Trim(txtconsume.Text)
            line.Cash = Trim(txtcash.Text)
            line.Remark = Trim("正常下机")
            line.Ischeck = Trim("未结账")
            line.Computer = Trim(Environment.MachineName) '机器名

            Dim flag3 As Boolean
            flag3 = offlinefacade.updateline(line)
        End If

        '获取当前上机人数
        Dim acquirefacade As New Facade.OnLineFacade
        Dim table As New DataTable
        Dim i As Integer
        table = acquirefacade.acquire()
        i = table.Rows.Count
        lbnowpeople.Text = Val(i)

    End Sub


三、学习心得


    在敲代码的时候,感觉就像在使用魔法,小小的几行代码,就能得到自己想要的功能,简直是太赞了,想实现什么就实现什么的感觉真好。思路清晰,带给自己不仅仅是时间上的充足,还带来了强大的自信心。

    我的代码生活如此精彩,哈哈微笑


机房收费系统重构(7)——下机_第2张图片


你可能感兴趣的:(.net,软件,经验)