Nats.go 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423
  1. package Nats
  2. import (
  3. "ColdVerify_server/Nats/NatsServer"
  4. "ColdVerify_server/conf"
  5. "ColdVerify_server/lib"
  6. "ColdVerify_server/logs"
  7. "ColdVerify_server/models/Account"
  8. "ColdVerify_server/models/Certificate"
  9. "ColdVerify_server/models/Device"
  10. "ColdVerify_server/models/System"
  11. "ColdVerify_server/models/Task"
  12. "fmt"
  13. "github.com/nats-io/nats.go"
  14. "github.com/vmihailenco/msgpack/v5"
  15. "strings"
  16. "sync"
  17. )
  18. func init() {
  19. logs.Println("============Nats init============")
  20. var err error
  21. // 连接Nats服务器
  22. lib.Nats, err = nats.Connect("nats://" + conf.NatsServer_Url)
  23. if err != nil {
  24. fmt.Println("nats 连接失败!")
  25. panic(err)
  26. }
  27. logs.Println("nats OK!")
  28. // 本地测试,屏蔽本地nats
  29. if !conf.NatsForbidden {
  30. go NatsInit()
  31. }
  32. }
  33. type Extract_TaskData_Back struct {
  34. T_uuid string `xml:"T_uuid"` // 任务主键id
  35. Time_start string `xml:"Time_start"`
  36. Time_end string `xml:"Time_end"`
  37. DeviceClassList []Device.DeviceClassList `xml:"DeviceClassList"` // 泛型
  38. Task Task.Task `xml:"Task"` // 泛型
  39. }
  40. func NatsInit() {
  41. // 发布-订阅 模式,打包数据
  42. _, _ = lib.Nats.QueueSubscribe("ColdVerify_Server_Extract_TaskData_Back", "Extract_TaskData", func(m *nats.Msg) {
  43. logs.Debug("Extract_TaskData_Back message: \n", string(m.Data))
  44. var resp Extract_TaskData_Back
  45. err := msgpack.Unmarshal(m.Data, &resp)
  46. if err != nil {
  47. System.Add_Logs("Nats", "msgpack Unmarshal err", string(m.Data))
  48. return
  49. }
  50. Task_r := resp.Task
  51. // 清空表
  52. Task.Truncate_TaskData(Task_r.T_task_id)
  53. //失败重试5次
  54. DeviceClassList := new(sync.Map)
  55. var count int
  56. for _, v := range resp.DeviceClassList {
  57. if strings.Contains(v.T_sn, "-") || len(v.T_sn) == 0 {
  58. // 从3.0平台导入
  59. continue
  60. }
  61. DeviceClassList.Store(fmt.Sprintf("%s|%s", v.T_sn, v.T_id), 5)
  62. //err = Task.Import_TaskData_Back(v.T_sn, v.T_id, resp.Task.T_task_id, resp.Time_start, resp.Time_end)
  63. //count++
  64. //time.Sleep(5 * time.Second)
  65. }
  66. DeviceClassList.Range(func(k, v interface{}) bool {
  67. count++
  68. return true
  69. })
  70. for count > 0 {
  71. DeviceClassList.Range(func(k, v any) bool {
  72. T_snid := strings.Split(k.(string), "|")
  73. T_sn := T_snid[0]
  74. T_id := T_snid[1]
  75. temp := v.(int)
  76. temp--
  77. DeviceClassList.Store(k, temp)
  78. err = Task.Import_TaskData_Back(T_sn, T_id, resp.Task.T_task_id, resp.Time_start, resp.Time_end)
  79. if err == nil || strings.Contains(err.Error(), "doesn't exist") {
  80. DeviceClassList.Delete(k)
  81. count--
  82. }
  83. return true
  84. })
  85. }
  86. TaskData_Num := Task.Read_TaskData_Count(Task_r.T_task_id)
  87. //if TaskData_Num == 0 {
  88. // Task_r.T_collection_num = 0
  89. // if !Task.Update_Task(Task_r, "T_collection_state") {
  90. // logs.Error(lib.FuncName(), "后台执行修改任务数据失败")
  91. // }
  92. // return
  93. //}
  94. // 导入到本地数据
  95. NatsServer.Import_TaskData(resp.T_uuid, Task_r.T_task_id, TaskData_Num)
  96. System.Add_UserLogs_T(resp.T_uuid, "任务", "修改", Task_r)
  97. System.Add_UserLogs(resp.T_uuid, "提取数据", "提取数据"+Task_r.T_name, Task_r.T_task_id+"|"+resp.Time_start+"|"+resp.Time_end)
  98. })
  99. _, _ = lib.Nats.QueueSubscribe("ColdVerify_Server_Update_Task", "Update_Task", func(m *nats.Msg) {
  100. var t_Req Task.Task
  101. var t_R lib.JSONS
  102. err := msgpack.Unmarshal(m.Data, &t_Req)
  103. if err != nil {
  104. logs.Error("Mats", lib.FuncName(), err)
  105. t_R.Code = 202
  106. t_R.Msg = err.Error()
  107. b, _ := msgpack.Marshal(&t_R)
  108. _ = lib.Nats.Publish(m.Reply, b)
  109. return
  110. }
  111. fmt.Printf("ColdVerify_Server_Update_Task message: %+v\n", t_Req)
  112. col := []string{}
  113. if t_Req.T_delivery_state > 0 {
  114. col = append(col, "T_delivery_state")
  115. }
  116. if !Task.Update_Task(t_Req, col...) {
  117. logs.Error("Mats", lib.FuncName(), err)
  118. t_R.Code = 202
  119. t_R.Msg = err.Error()
  120. b, _ := msgpack.Marshal(&t_R)
  121. _ = lib.Nats.Publish(m.Reply, b)
  122. return
  123. }
  124. t_R.Code = 200
  125. t_R.Msg = "ok"
  126. b, _ := msgpack.Marshal(&t_R)
  127. _ = lib.Nats.Publish(m.Reply, b)
  128. Task.Add_TaskLogs_T("nats", t_Req.T_task_id, "任务管理", "修改", t_Req)
  129. System.Add_UserLogs_T("nats", "任务管理", "修改", t_Req)
  130. })
  131. _, _ = lib.Nats.QueueSubscribe("ColdVerify_Server_Read_Task", "Read_Task", func(m *nats.Msg) {
  132. logs.Println("ColdVerify_Server_Read_Task message: %+v\n", string(m.Data))
  133. var t_R lib.JSONS
  134. task, is := Task.Read_Task(string(m.Data))
  135. if !is {
  136. logs.Error("Mats", lib.FuncName())
  137. t_R.Code = 202
  138. t_R.Msg = "查询失败"
  139. b, _ := msgpack.Marshal(&t_R)
  140. _ = lib.Nats.Publish(m.Reply, b)
  141. return
  142. }
  143. t_R.Code = 200
  144. t_R.Msg = "ok"
  145. t_R.Data = task
  146. b, _ := msgpack.Marshal(&t_R)
  147. _ = lib.Nats.Publish(m.Reply, b)
  148. })
  149. _, _ = lib.Nats.QueueSubscribe("ColdVerify_Server_Device_Class_List", "Device_Class_List", func(m *nats.Msg) {
  150. logs.Println("ColdVerify_Server_Device_Class_List message: %+v\n", string(m.Data))
  151. var t_R lib.JSONS
  152. task, is := Task.Read_Task(string(m.Data))
  153. if !is {
  154. logs.Error("Mats", lib.FuncName())
  155. t_R.Code = 202
  156. t_R.Msg = "查询失败"
  157. b, _ := msgpack.Marshal(&t_R)
  158. _ = lib.Nats.Publish(m.Reply, b)
  159. return
  160. }
  161. List, _ := Device.Read_DeviceClassList_OrderList(task.T_class, "", "", 0, 9999)
  162. t_R.Code = 200
  163. t_R.Msg = "ok"
  164. t_R.Data = List
  165. b, _ := msgpack.Marshal(&t_R)
  166. _ = lib.Nats.Publish(m.Reply, b)
  167. })
  168. _, _ = lib.Nats.QueueSubscribe("ColdVerify_Server_Read_User", "Read_User", func(m *nats.Msg) {
  169. logs.Println("ColdVerify_Server_Read_User message: %+v\n", string(m.Data))
  170. var t_R lib.JSONS
  171. err, user := Account.Read_User_ByT_uuid(string(m.Data))
  172. if err != nil {
  173. logs.Error("Mats", lib.FuncName())
  174. t_R.Code = 202
  175. t_R.Msg = "查询失败"
  176. b, _ := msgpack.Marshal(&t_R)
  177. _ = lib.Nats.Publish(m.Reply, b)
  178. return
  179. }
  180. t_R.Code = 200
  181. t_R.Msg = "ok"
  182. t_R.Data = user
  183. b, _ := msgpack.Marshal(&t_R)
  184. _ = lib.Nats.Publish(m.Reply, b)
  185. })
  186. _, _ = lib.Nats.QueueSubscribe("ColdVerify_Server_Add_DeviceClassList", "Add_DeviceClassList", func(m *nats.Msg) {
  187. type T_Req struct {
  188. T_task_id string `xml:"T_task_id"` // 任务主键id
  189. T_sn string `xml:"T_sn"`
  190. T_id string `xml:"T_id"`
  191. }
  192. var t_Req T_Req
  193. var t_R lib.JSONS
  194. err := msgpack.Unmarshal(m.Data, &t_Req)
  195. if err != nil {
  196. logs.Error("Mats", lib.FuncName(), err)
  197. t_R.Code = 202
  198. t_R.Msg = err.Error()
  199. b, _ := msgpack.Marshal(&t_R)
  200. _ = lib.Nats.Publish(m.Reply, b)
  201. return
  202. }
  203. fmt.Printf("ColdVerify_Server_Add_DeviceClassList message: %+v\n", t_Req)
  204. Task_r, is := Task.Read_Task(t_Req.T_task_id)
  205. if !is {
  206. logs.Error("Mats", lib.FuncName())
  207. t_R.Code = 202
  208. t_R.Msg = "T_task_id 错误!"
  209. b, _ := msgpack.Marshal(&t_R)
  210. _ = lib.Nats.Publish(m.Reply, b)
  211. return
  212. }
  213. // 判断是否已存在sn
  214. dc, is := Device.Read_DeviceClassList_T_class_T_sn(Task_r.T_class, t_Req.T_sn)
  215. // 添加的id和数据库已存在id相同
  216. if is && dc.T_id == t_Req.T_id {
  217. t_R.Code = 200
  218. t_R.Msg = "ok"
  219. b, _ := msgpack.Marshal(&t_R)
  220. _ = lib.Nats.Publish(m.Reply, b)
  221. return
  222. }
  223. var pdf Certificate.CertificatePdf
  224. //pdfList, _ := Certificate.Read_CertificatePdf_Newest(T_sn)
  225. pdfList, _ := Certificate.Read_CertificatePdf_T_layout_no(t_Req.T_id, "")
  226. if len(pdfList) > 0 {
  227. pdf = pdfList[0]
  228. }
  229. // 相同sn 添加的id和数据库已存在id不同
  230. if is && dc.T_id != t_Req.T_id {
  231. dc2, is := Device.Read_DeviceClassList_T_class_T_id(Task_r.T_class, t_Req.T_id)
  232. if is {
  233. logs.Error("Mats", lib.FuncName())
  234. t_R.Code = 202
  235. t_R.Msg = fmt.Sprintf("编号[%s]已被[%s]关联,请重试", t_Req.T_id, dc2.T_sn)
  236. b, _ := msgpack.Marshal(&t_R)
  237. _ = lib.Nats.Publish(m.Reply, b)
  238. return
  239. }
  240. dc.T_id = t_Req.T_id
  241. dc.T_failure_time = pdf.T_failure_time
  242. dc.T_pdf = pdf.T_pdf
  243. dc.T_Certificate_sn = pdf.T_Certificate_sn
  244. if !Device.Update_DeviceClassList(dc, "T_id", "T_failure_time", "T_pdf", "T_Certificate_sn") {
  245. logs.Error("Mats", lib.FuncName())
  246. t_R.Code = 202
  247. t_R.Msg = "修改编号失败!"
  248. b, _ := msgpack.Marshal(&t_R)
  249. _ = lib.Nats.Publish(m.Reply, b)
  250. return
  251. } else {
  252. t_R.Code = 200
  253. t_R.Msg = "ok"
  254. b, _ := msgpack.Marshal(&t_R)
  255. _ = lib.Nats.Publish(m.Reply, b)
  256. Task.Add_TaskLogs_T("nats", t_Req.T_task_id, "任务管理", "修改", t_Req)
  257. System.Add_UserLogs_T("nats", "任务管理", "修改", t_Req)
  258. return
  259. }
  260. }
  261. var_ := Device.DeviceClassList{
  262. T_class: Task_r.T_class,
  263. T_id: t_Req.T_id,
  264. T_sn: t_Req.T_sn,
  265. T_failure_time: pdf.T_failure_time,
  266. T_pdf: pdf.T_pdf,
  267. T_Certificate_sn: pdf.T_Certificate_sn,
  268. T_remark: "",
  269. T_State: 1,
  270. }
  271. _, is = Device.Add_DeviceClassList(var_)
  272. if !is {
  273. logs.Error("Mats", lib.FuncName(), err)
  274. t_R.Code = 202
  275. t_R.Msg = err.Error()
  276. b, _ := msgpack.Marshal(&t_R)
  277. _ = lib.Nats.Publish(m.Reply, b)
  278. return
  279. }
  280. t_R.Code = 200
  281. t_R.Msg = "ok"
  282. b, _ := msgpack.Marshal(&t_R)
  283. _ = lib.Nats.Publish(m.Reply, b)
  284. Task.Add_TaskLogs_T("nats", t_Req.T_task_id, "任务管理", "修改", t_Req)
  285. System.Add_UserLogs_T("nats", "任务管理", "修改", t_Req)
  286. })
  287. _, _ = lib.Nats.QueueSubscribe("ColdVerify_Server_Edit_DeviceClassList", "Edit_DeviceClassList", func(m *nats.Msg) {
  288. type T_Req struct {
  289. T_task_id string `xml:"T_task_id"` // 任务主键id
  290. T_sn string `xml:"T_sn"`
  291. T_id string `xml:"T_id"`
  292. }
  293. var t_Req T_Req
  294. var t_R lib.JSONS
  295. err := msgpack.Unmarshal(m.Data, &t_Req)
  296. if err != nil {
  297. logs.Error("Mats", lib.FuncName(), err)
  298. t_R.Code = 202
  299. t_R.Msg = err.Error()
  300. b, _ := msgpack.Marshal(&t_R)
  301. _ = lib.Nats.Publish(m.Reply, b)
  302. return
  303. }
  304. fmt.Printf("ColdVerify_Server_Edit_DeviceClassList message: %+v\n", t_Req)
  305. Task_r, is := Task.Read_Task(t_Req.T_task_id)
  306. if !is {
  307. logs.Error("Mats", lib.FuncName())
  308. t_R.Code = 202
  309. t_R.Msg = "T_task_id 错误!"
  310. b, _ := msgpack.Marshal(&t_R)
  311. _ = lib.Nats.Publish(m.Reply, b)
  312. return
  313. }
  314. // 判断是否已存在sn
  315. dc, is := Device.Read_DeviceClassList_T_class_T_sn(Task_r.T_class, t_Req.T_sn)
  316. // 添加的id和数据库已存在id相同
  317. if is && dc.T_id == t_Req.T_id {
  318. t_R.Code = 200
  319. t_R.Msg = "ok"
  320. b, _ := msgpack.Marshal(&t_R)
  321. _ = lib.Nats.Publish(m.Reply, b)
  322. return
  323. }
  324. // 相同sn 添加的id和数据库已存在id不同
  325. if is && dc.T_id != t_Req.T_id {
  326. dc2, is := Device.Read_DeviceClassList_T_class_T_id(Task_r.T_class, t_Req.T_id)
  327. if is {
  328. logs.Error("Mats", lib.FuncName())
  329. t_R.Code = 202
  330. t_R.Msg = fmt.Sprintf("编号[%s]已被[%s]关联,请重试", t_Req.T_id, dc2.T_sn)
  331. b, _ := msgpack.Marshal(&t_R)
  332. _ = lib.Nats.Publish(m.Reply, b)
  333. return
  334. }
  335. var pdf Certificate.CertificatePdf
  336. pdfList, _ := Certificate.Read_CertificatePdf_T_layout_no(t_Req.T_id, "")
  337. if len(pdfList) > 0 {
  338. pdf = pdfList[0]
  339. }
  340. dc.T_id = t_Req.T_id
  341. dc.T_failure_time = pdf.T_failure_time
  342. dc.T_pdf = pdf.T_pdf
  343. dc.T_Certificate_sn = pdf.T_Certificate_sn
  344. if !Device.Update_DeviceClassList(dc, "T_id", "T_failure_time", "T_pdf", "T_Certificate_sn") {
  345. logs.Error("Mats", lib.FuncName())
  346. t_R.Code = 202
  347. t_R.Msg = "修改编号失败!"
  348. b, _ := msgpack.Marshal(&t_R)
  349. _ = lib.Nats.Publish(m.Reply, b)
  350. return
  351. }
  352. }
  353. t_R.Code = 200
  354. t_R.Msg = "ok"
  355. b, _ := msgpack.Marshal(&t_R)
  356. _ = lib.Nats.Publish(m.Reply, b)
  357. Task.Add_TaskLogs_T("nats", t_Req.T_task_id, "任务管理", "修改设备列表", t_Req)
  358. System.Add_UserLogs_T("nats", "任务管理", "修改", t_Req)
  359. })
  360. }