張 超 李鴻杰 王 琴 米 瑩 周璐瓊
(甘肅林業職業技術學院,甘肅 天水 741020)
經過調查、對比、分析,甘肅特色果品溯源智慧云平臺的開發采用面向對象的VB.NET程序設計語言和面向大中型企業的數據庫管理系統作為開發工具,具體信息如下:
開發工具:Microsoft Visual Studio 2010
數據庫管理系統:SQL Server 2008
運行環境:Microsoft .NET Framework 4.0
甘肅特色果品區塊鏈溯源智慧云平臺主要包括溯源信息采集功能和果品質量安全追溯功能,即溯源信息的輸入和輸出兩個方面。溯源信息的輸入主要由特色果品種植主體、加工主體、質檢主體和銷售主體實時完成;而溯源信息的輸出主要由特色果品消費者檢索查詢獲取。具體信息流如圖1所示:
圖1 溯源平臺信息流圖
使用VB.NET編程語言和SQL Server數據庫管理系統對甘肅特色果品質量安全信息進行全流程管理,主要包括質量安全因子的增、刪、改、查等操作。22個質量管理環節和全流程信息均可以通過各自的管理模塊進行采集和追蹤,而果品編碼則是貫穿整個數據采集、管理的唯一標識,果品編碼模塊核心代碼如下:
Private Subguopinbianma_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
bd = False
Dimselectstr As String
selectstr = "select (select count(果品編碼) from [guopinbianma] as [guopinbianma2] where [guopinbianma2].ID<=[guopinbianma].ID) as 序號,果園標識,果品類別,果品名稱,果品等級,果品編碼,查詢次數,果品狀態,備注,ID from [guopinbianma] order by 序號"
sqladapter = New SqlDataAdapter(selectstr, sqlconn)
Dim guopinbianmatmp1 As NewDataTable
If ds.Tables("guopinbianmatmp1") IsNot Nothing Then
ds.Tables("guopinbianmatmp1").Clear()
End If
sqladapter.Fill(ds, "guopinbianmatmp1")
DataGridView1.DataSource =ds.Tables("guopinbianmatmp1")
Fori As Int32 = 0 To DataGridView1.Columns.Count - 1
DataGridView1.Columns(i).SortMode = DataGridViewColumnSortMode.NotSortable
Next
DataGridView1.Columns("ID").Visible = False
Ifds.Tables("guopinbianmatmp1").Rows.Count > 0 Then
Select Case flag
Case "add"
DataGridView1.CurrentCell = DataGridView1.Rows(DataGridView1.Rows.Count - 1).Cells("序號").value
DataGridView1.Rows(DataGridView1.Rows.Count - 1).Selected = True
Case "modify"
DataGridView1.CurrentCell = DataGridView1.Rows(rec).Cells("序號").value
DataGridView1.Rows(rec).Selected = True
Case "delete"
If rec = DataGridView1.Rows.Count Then
DataGridView1.CurrentCell = DataGridView1.Rows(rec - 1).Cells("序號").value
DataGridView1.Rows(rec - 1).Selected = True
Else
DataGridView1.CurrentCell = DataGridView1.Rows(rec).Cells("序號").value
DataGridView1.Rows(rec).Selected = True
End If
Case Else
DataGridView1.CurrentCell = DataGridView1.Rows(rec).Cells("序號").value
End Select
End If
flag = ""
check = False
lockcontrol()
IfLogin.usertype = "系統管理員" Then
Button1.Enabled = True
Button2.Enabled = True
Else
Button1.Enabled = False
Button2.Enabled = False
Button4.Enabled = False
End If
bd = True
End Sub
以上代碼為果品編碼模塊的Load事件過程,主要進行模塊的初始化操作,包括在DataGridView控件中加載果品編碼信息,初始化界面中的所有數據綁定控件和按鈕控件等。
Private SubButton1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
n =InputBox("請輸入要進行果品編碼的數量:", "編碼數量", 1)
If Int(n) > 0 Then
If DataGridView1.Rows.Count > 0 Then
rec = DataGridView1.CurrentCell.RowIndex
End If
flag = "add"
Button1.Enabled = False
Button2.Enabled = False
lockcontrol()
TextBox1.clear()
ComboBox1.clear()
ComboBox2.clear()
ComboBox3.clear()
ComboBox4.clear()
TextBox2.clear()
TextBox3.Text = "0"
ComboBox5.clear()
TextBox4.clear()
ComboBox1.Focus()
Me.AcceptButton = Button3
Me.CancelButton = Button5
End If
End Sub
以上代碼為果品編碼模塊上Button1的Click事件過程,主要完成用戶單擊事件發生時,添加記錄的初始化操作。
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
If DataGridView1.RowCount > 0 Then
rec = DataGridView1.CurrentRow.Index
flag = "modify"
Button1.Enabled = False
Button2.Enabled = False
lockcontrol()
ComboBox1.Focus()
Me.AcceptButton = Button3
Me.CancelButton = Button5
Else
MsgBox("表中無數據!", , "信息提示")
End If
End Sub
以上代碼為果品編碼模塊上Button2的Click事件過程,主要完成用戶單擊事件發生時,修改記錄的初始化操作。
Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
checkdata()
If check = True Then
Select Case flag
Case "add"
Try
DimMyRnd As New Random
Dim a, b, c As Integer
Ifsqlconn.State = ConnectionState.Closed Then
sqlconn.Open()
End If
Fori As Integer = 1 To n
a =MyRnd.Next
b =MyRnd.Next
c =MyRnd.Next
Dim cod As String = TextBox1.Text & Format(Now, "YYYYMMDDHHmmssSSS") & a & b & c
sqladapter.InsertCommand = New SqlCommand("insert into [guopinbianma](果園標識,果品類別,果品名稱,果品等級,果品編碼,查詢次數,果品狀態,備注) values('" & ComboBox1.Text & "','" & ComboBox2.Text & "','" & ComboBox3.Text & "','" & ComboBox4.Text & "','" & cod & "','" & TextBox3.Text & "','" & ComboBox5.Text & "','" & TextBox4.Text & "')", sqlconn)
sqladapter.InsertCommand.ExecuteNonQuery()
Next
MsgBox("果品編碼添加成功!", MsgBoxStyle.Information, "成功提示")
CatchsqlExceptionErr As SqlClient.SqlException
MessageBox.Show(sqlExceptionErr.Message)
Finally
sqlconn.Close()
End Try
guopinbianma_Load(Sender, e)
guopinbianma_Activated(Sender, e)
Case "modify"
Try
sqladapter.UpdateCommand = New SqlCommand("update [guopinbianma] set 果園標識='" & ComboBox1.Text & "',果品類別='" & ComboBox2.Text & "',果品名稱='" & ComboBox3.Text & "',果品等級='" & ComboBox4.Text & "',果品編碼='" & TextBox2.Text & "',查詢次數='" & TextBox3.Text & "',果品狀態='" & ComboBox5.Text & "',備注='" & TextBox4.Text & "' where ID='" & DataGridView1.CurrentRow.Cells("ID").Value & "'", sqlconn)
Ifsqlconn.State = ConnectionState.Closed Then
sqlconn.Open()
End If
sqladapter.UpdateCommand.ExecuteNonQuery()
MsgBox("果品編碼信息修改成功!", MsgBoxStyle.Information, "成功提示")
Catch ex As Exception
Finally
sqlconn.Close()
End Try
guopinbianma_Load(Sender, e)
End Select
End If
End Sub
以上代碼為果品編碼模塊上Button3的Click事件過程,主要是對用戶添加或修改的果品編碼信息進行保存,并初始化界面上各控件的狀態。
Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
Ifds.Tables("guopinbianmatmp1").Rows.Count > 0 Then
rec = DataGridView1.CurrentCell.RowIndex
IfMsgBox("確定刪除記錄嗎?", vbInformation + vbOKCancel + vbDefaultButton2, "刪除提示") = vbOK Then
Try
Dimdeletestr As String = "delete from [guopinbianma] where ID='" & DataGridView1.CurrentRow.Cells("ID").Value & "'"
sqladapter.DeleteCommand = New SqlCommand(deletestr, sqlconn)
Ifsqlconn.State = ConnectionState.Closed Then
sqlconn.Open()
End If
sqladapter.DeleteCommand.ExecuteNonQuery()
MsgBox("果品編號刪除成功!", vbInformation, "成功提示")
flag = "delete"
guopinbianma_Load(Sender, e)
guopinbianma_Activated(sender, e)
Catch ex As Exception
Finally
sqlconn.Close()
End Try
End If
End If
End Sub
以上代碼為果品編碼模塊上Button4的Click事件過程,主要完成選定記錄的刪除操作,并初始化界面上控件的狀態。
系統測試,是檢驗各模塊按照功能設計要求正常運行,保證系統安全、可靠、持續運行的必要手段,主要包括單元測試、集成測試、系統測試、驗收測試和回歸測試等。采用各種測試方法和技術對系統進行測繪的過程中,對發現的各種錯誤和Bug即時進行修改、完善,最終使各溯源模塊能夠穩定運行,實現其溯源管理功能,如圖2所示。
圖2 系統測試流程圖
系統界面包括登錄界面、主操作平臺界面,以及各溯源管理模塊界面等。其中,用戶登錄界面和主操作平臺界面如下圖所示。
(1)用戶登錄界面
甘肅特色果品溯源智慧云平臺為分布式管理系統,允許不同空間的用戶實時進行數據采集、處理。為保證溯源環節信息的安全和責任可究,平臺的用戶登錄界面可對用戶身份的合法性進行驗證,阻止非法用戶登錄,允許合法用戶進入并使用相應的溯源模塊管理信息。系統的用戶類型包括栽培企業、加工部門、質檢機構、收購企業、零售企業、消費者、系統管理員等,用戶通過手機號碼作為用戶名登錄,界面如圖3。
圖3 用戶登錄界面
(2)主操作平臺界面
22個質量環節和管理信息模塊通過主操作平臺界面進行整合,并提供統一的操作入口,實現效果如圖4。
圖4 溯源平臺主界面
針對甘肅特色果品質量安全控制技術薄弱、生產規范程度較差、溯源體系不健全等現狀,按照“市場需要、政府支持、產教融合”的行業發展需要,基于區塊鏈數據難以篡改和去中心化的優點,通過溯源數據上鏈以實現甘肅特色果品生產過程、加工過程、質量檢測、流程銷售等質量安全關鍵環節全程可追溯,并打造甘肅特色果品的質量安全品牌形象。