Без опису
Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. Imports System.Net.Http
  2. Imports System.Data.SqlClient
  3. Imports Newtonsoft.Json.Linq
  4. Imports System.Timers
  5. Imports System.Text
  6. Imports System.Data.Common
  7. Public Class Form1
  8. ' API 密钥和 Web API 的 URL
  9. Private apiToken As String = "291A17F2-C8A0-438A-BF64-0834C1B9C1D6"
  10. Private apiUrl As String = "http://hf.fjkscloud.cn/Api2/AccountReportList"
  11. Private connectionString As String = "Server=192.168.110.164;Database=rt2021;User Id=sa;Password=Lafayette11@"
  12. ' 定时器,每隔5分钟执行一次
  13. Private timer As New Timer(60000) ' 300000毫秒即5分钟
  14. ' 在 Form Load 事件中设置定时器
  15. Private Async Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
  16. ' 设置定时器触发的事件
  17. 'Dim requestBody As String = "{""Begin"":""2024-12-16"",""End"":""2024-12-16""}"
  18. 'Dim response As String = Await PostApiDataAsync(requestBody)
  19. 'If Not String.IsNullOrEmpty(response) Then
  20. ' SaveDataToDatabase(response)
  21. 'End If
  22. Using connection As New SqlConnection(connectionString)
  23. connection.Open()
  24. Dim sql As String = "select top(1) createtime from ht_cloud_workshop order by createtime desc "
  25. Using command As New SqlCommand(sql, connection)
  26. TextBox1.Text = command.ExecuteScalar()
  27. End Using
  28. End Using
  29. ' AddHandler timer.Elapsed, AddressOf TimerElapsed
  30. ' 启动定时器
  31. 'timer.Start()
  32. End Sub
  33. ' 定时器触发时的事件
  34. ' 异步调用 Web API 获取数据
  35. Private Async Function PostApiDataAsync(requestBody As String) As Task(Of String)
  36. Using client As New HttpClient()
  37. Try
  38. ' 设置 Header
  39. client.DefaultRequestHeaders.Add("KsApiToken", apiToken)
  40. ' 设置请求内容
  41. Dim content As New StringContent(requestBody, Encoding.UTF8, "application/json")
  42. ' 发送 POST 请求
  43. Dim response As HttpResponseMessage = Await client.PostAsync(apiUrl, content)
  44. response.EnsureSuccessStatusCode()
  45. Dim responseBody As String = Await response.Content.ReadAsStringAsync()
  46. Return responseBody
  47. Catch ex As Exception
  48. MessageBox.Show("请求失败: " & ex.Message)
  49. Return String.Empty
  50. End Try
  51. End Using
  52. End Function
  53. ' 保存数据到本地 SQL Server 数据库
  54. ' 保存数据到本地 SQL Server 数据库
  55. Private Sub SaveDataToDatabase(jsonResponse As String)
  56. Try
  57. ' 解析 JSON 数据
  58. ' MessageBox.Show(jsonResponse)
  59. Dim json As JObject = JObject.Parse(jsonResponse)
  60. ' 确保您知道 JSON 的具体结构,假设数据位于 "data" 字段中
  61. Dim resultArray As JArray = json("result")
  62. Using connection As New SqlConnection(connectionString)
  63. connection.Open()
  64. For Each item As JObject In resultArray
  65. ' 假设字段包括 accountName, orderNumber, productName
  66. Dim accountName As String = item("accountName").ToString()
  67. Dim workshopName As String = item("workshopName").ToString() '车间
  68. Dim orderNumber As String = item("orderNumber").ToString() '卡号
  69. Dim orderName As String = item("orderName").ToString() '品名
  70. Dim createTime As String = item("createTime").ToString() '时间
  71. Dim remark As String = item("remark").ToString() '备注
  72. Dim procedureName As String = item("procedureName").ToString() '工序
  73. ' 插入数据到数据库
  74. Dim query As String = "INSERT INTO ht_cloud_workshop (AccountName, workshopName, orderNumber,orderName,createTime,remark,procedureName) VALUES (@AccountName, @workshopName, @orderNumber,@orderName,@createTime,@remark,@procedureName)"
  75. Using command As New SqlCommand(query, connection)
  76. command.Parameters.AddWithValue("@AccountName", accountName)
  77. command.Parameters.AddWithValue("@workshopName", workshopName)
  78. command.Parameters.AddWithValue("@orderNumber", orderNumber)
  79. command.Parameters.AddWithValue("@orderName", orderName)
  80. command.Parameters.AddWithValue("@createTime", createTime)
  81. command.Parameters.AddWithValue("@remark", remark)
  82. command.Parameters.AddWithValue("@procedureName", procedureName)
  83. command.ExecuteNonQuery()
  84. End Using
  85. Next
  86. End Using
  87. MessageBox.Show("数据保存成功!")
  88. Catch ex As Exception
  89. MessageBox.Show("保存数据失败: " & ex.Message)
  90. End Try
  91. End Sub
  92. Private Async Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
  93. ' 定义日期变量
  94. Dim beginDate As String = String.Format("{0:d}", dtp1.Value)
  95. Dim endDate As String = String.Format("{0:d}", dtp2.Value)
  96. ' 创建JSON请求字符串
  97. Dim requestBody As String = $"{{""Begin"":""{beginDate}"",""End"":""{endDate}""}}"
  98. Dim requestBody1 As String = "{""Begin"":""2024-12-20"",""End"":""2024-12-21""}"
  99. ' 调用 API 并获取返回数据
  100. Dim response As String = Await PostApiDataAsync(requestBody)
  101. ' 将数据保存到本地数据库
  102. If Not String.IsNullOrEmpty(response) Then
  103. Using connection As New SqlConnection(connectionString)
  104. connection.Open()
  105. Dim sql As String = "delete from ht_cloud_workshop where cast(createtime as date ) between '" & String.Format("{0:d}", dtp1.Value) & "' and '" & String.Format("{0:d}", dtp2.Value) & "'"
  106. Using cmd As New SqlCommand
  107. cmd.Connection = connection
  108. cmd.CommandText = sql
  109. cmd.ExecuteNonQuery()
  110. End Using
  111. End Using
  112. SaveDataToDatabase(response)
  113. Using connection As New SqlConnection(connectionString)
  114. connection.Open()
  115. Dim sql As String = "select top(1) createtime from ht_cloud_workshop order by createtime desc "
  116. Using command As New SqlCommand(sql, connection)
  117. TextBox1.Text = command.ExecuteScalar()
  118. End Using
  119. End Using
  120. End If
  121. End Sub
  122. Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
  123. Using connection As New SqlConnection(connectionString)
  124. connection.Open()
  125. Dim sql As String = "select createtime as 日期,ordernumber as 卡号, ordername as 品名, procedurename as 工序,remark as 备注 from ht_cloud_workshop where createtime between '" & String.Format("{0:d}", dtp1.Value) & "' and '" & String.Format("{0:d}", dtp2.Value) & "' order by createtime "
  126. Using command As New SqlCommand(sql, connection)
  127. TextBox1.Text = command.ExecuteScalar()
  128. Dim dt As New DataTable
  129. Dim da As DataAdapter
  130. End Using
  131. End Using
  132. End Sub
  133. End Class