Product.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. package NatsServer
  2. import (
  3. "Yunlot/lib"
  4. "Yunlot/logs"
  5. "Yunlot/models/Product"
  6. "github.com/nats-io/nats.go"
  7. "github.com/vmihailenco/msgpack/v5"
  8. )
  9. func NatsServer_Product() {
  10. // 产品类型 - 获取
  11. _, _ = lib.Nats.Subscribe("/Product/ProductType/Get", func(m *nats.Msg) {
  12. logs.Println("Nats /Product/ProductType/List: %s\n", string(m.Data))
  13. var ProductType_r Product.ProductType
  14. err := msgpack.Unmarshal(m.Data, &ProductType_r)
  15. if err != nil {
  16. logs.PrintlnError("Nats:", err)
  17. b, _ := msgpack.Marshal(&lib.JSONR{Code: lib.Error, Msg: "解析错误!"})
  18. _ = lib.Nats.Publish(m.Reply, b)
  19. return
  20. }
  21. if !ProductType_r.Read() {
  22. b, _ := msgpack.Marshal(&lib.JSONR{Code: lib.Error, Msg: "ProductID E!"})
  23. _ = lib.Nats.Publish(m.Reply, b)
  24. return
  25. }
  26. b, _ := msgpack.Marshal(&lib.JSONR{Code: lib.Success, Msg: "ok!", Data: ProductType_r})
  27. _ = lib.Nats.Publish(m.Reply, b)
  28. })
  29. // 产品类型 - 列表
  30. _, _ = lib.Nats.Subscribe("/Product/ProductType/List", func(m *nats.Msg) {
  31. logs.Println("Nats /Product/ProductType/List: %s\n", string(m.Data))
  32. var ProductType_r Product.ProductType
  33. err := msgpack.Unmarshal(m.Data, &ProductType_r)
  34. if err != nil {
  35. logs.PrintlnError("Nats:", err)
  36. b, _ := msgpack.Marshal([]Product.ProductType{})
  37. _ = lib.Nats.Publish(m.Reply, b)
  38. return
  39. }
  40. ProductType_list := ProductType_r.Lists_All()
  41. b, _ := msgpack.Marshal(ProductType_list)
  42. _ = lib.Nats.Publish(m.Reply, b)
  43. })
  44. // 网关 - 列表
  45. _, _ = lib.Nats.Subscribe("/Product/ProductModeLists/List", func(m *nats.Msg) {
  46. logs.Println("Nats /Product/ProductModeLists/List: %s\n", string(m.Data))
  47. ProductModeLists_list, _ := Product.ProductModeLists()
  48. b, _ := msgpack.Marshal(ProductModeLists_list)
  49. _ = lib.Nats.Publish(m.Reply, b)
  50. })
  51. // 产品协议 - 获取
  52. _, _ = lib.Nats.Subscribe("/Product/ProductProt/Get", func(m *nats.Msg) {
  53. logs.Println("Nats /Product/ProductProt/Get: %s\n", string(m.Data))
  54. var ProductProt Product.ProductProt
  55. err := msgpack.Unmarshal(m.Data, &ProductProt)
  56. if err != nil {
  57. logs.PrintlnError("Nats:", err)
  58. b, _ := msgpack.Marshal(&lib.JSONR{Code: lib.Error, Msg: "解析错误!"})
  59. _ = lib.Nats.Publish(m.Reply, b)
  60. return
  61. }
  62. if !ProductProt.Read() {
  63. b, _ := msgpack.Marshal(&lib.JSONR{Code: lib.Error, Msg: "ID E!"})
  64. _ = lib.Nats.Publish(m.Reply, b)
  65. return
  66. }
  67. b, _ := msgpack.Marshal(&lib.JSONR{Code: lib.Success, Msg: "ok!", Data: ProductProt})
  68. _ = lib.Nats.Publish(m.Reply, b)
  69. })
  70. // 产品协议 - 列表
  71. _, _ = lib.Nats.Subscribe("/Product/ProductProt/List", func(m *nats.Msg) {
  72. logs.Println("Nats /Product/ProductProt/List: %s\n", string(m.Data))
  73. var ProductProt Product.ProductProt
  74. err := msgpack.Unmarshal(m.Data, &ProductProt)
  75. if err != nil {
  76. logs.PrintlnError("Nats:", err)
  77. b, _ := msgpack.Marshal([]Product.ProductProt{})
  78. _ = lib.Nats.Publish(m.Reply, b)
  79. return
  80. }
  81. ProductProt_list := ProductProt.Lists_All()
  82. b, _ := msgpack.Marshal(ProductProt_list)
  83. _ = lib.Nats.Publish(m.Reply, b)
  84. })
  85. }