Nats.go 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. package Nats
  2. import (
  3. "ERP_ams/conf"
  4. "ERP_ams/models/Account"
  5. "fmt"
  6. "github.com/astaxie/beego/logs"
  7. "github.com/beego/beego/v2/adapter/orm"
  8. "github.com/nats-io/nats.go"
  9. "github.com/vmihailenco/msgpack/v5"
  10. menulibs "gogs.baozhida.cn/zoie/ERP_libs/Menu"
  11. powerlibs "gogs.baozhida.cn/zoie/ERP_libs/Power"
  12. "gogs.baozhida.cn/zoie/ERP_libs/lib"
  13. )
  14. var Nats *nats.Conn
  15. func init() {
  16. fmt.Println("============Nats init============")
  17. var err error
  18. // 连接Nats服务器
  19. Nats, err = nats.Connect("nats://" + conf.NatsServer_Url)
  20. if err != nil {
  21. fmt.Println("nats 连接失败!")
  22. panic(err)
  23. }
  24. fmt.Println("nats OK!")
  25. go NatsInit()
  26. }
  27. func NatsInit() {
  28. // 发布-订阅 模式,异步订阅 test1
  29. _, _ = Nats.Subscribe(conf.NatsSubj_Prefix+conf.Sys_Name+"_Read_Menu_List", func(m *nats.Msg) {
  30. var t_R lib.JSONS
  31. o := orm.NewOrm()
  32. MenuDao := menulibs.NewMenu(o)
  33. menu, err := MenuDao.Read_Menu_List()
  34. if err != nil {
  35. logs.Error("Mats", lib.FuncName(), err)
  36. t_R.Code = 202
  37. t_R.Msg = "获取失败!"
  38. b, _ := msgpack.Marshal(&t_R)
  39. _ = Nats.Publish(m.Reply, b)
  40. return
  41. }
  42. t_R.Code = 200
  43. t_R.Msg = "ok"
  44. t_R.Data = menu
  45. b, _ := msgpack.Marshal(&t_R)
  46. _ = Nats.Publish(m.Reply, b)
  47. })
  48. _, _ = Nats.Subscribe(conf.NatsSubj_Prefix+conf.Sys_Name+"_Read_User_Bind_Menu_List", func(m *nats.Msg) {
  49. var t_R lib.JSONS
  50. o := orm.NewOrm()
  51. powerDao := powerlibs.NewPower(o)
  52. power, err := powerDao.Read_Power_ByT_id(string(m.Data))
  53. if err != nil {
  54. logs.Error("Mats", lib.FuncName(), err)
  55. t_R.Code = 202
  56. t_R.Msg = err.Error()
  57. b, _ := msgpack.Marshal(&t_R)
  58. _ = Nats.Publish(m.Reply, b)
  59. return
  60. }
  61. MenuDao := menulibs.NewMenu(o)
  62. menu, err := MenuDao.Read_Menu_List_ByPower_T_Menu(power.T_menu)
  63. if err != nil {
  64. logs.Error("Mats", lib.FuncName(), err)
  65. t_R.Code = 202
  66. t_R.Msg = err.Error()
  67. b, _ := msgpack.Marshal(&t_R)
  68. _ = Nats.Publish(m.Reply, b)
  69. return
  70. }
  71. t_R.Code = 200
  72. t_R.Msg = "ok"
  73. t_R.Data = menu
  74. b, _ := msgpack.Marshal(&t_R)
  75. _ = Nats.Publish(m.Reply, b)
  76. })
  77. _, _ = Nats.Subscribe(conf.NatsSubj_Prefix+conf.Sys_Name+"_Add_Power", func(m *nats.Msg) {
  78. var t_Req powerlibs.Power
  79. var t_R lib.JSONS
  80. err := msgpack.Unmarshal(m.Data, &t_Req)
  81. if err != nil {
  82. logs.Error("Mats", lib.FuncName(), err)
  83. t_R.Code = 202
  84. t_R.Msg = err.Error()
  85. b, _ := msgpack.Marshal(&t_R)
  86. _ = Nats.Publish(m.Reply, b)
  87. return
  88. }
  89. fmt.Printf("ERP_AMS_Add_Power message: %+v\n", t_Req)
  90. o := orm.NewOrm()
  91. powerDao := powerlibs.NewPower(o)
  92. id, err := powerDao.Add_Power(t_Req)
  93. if err != nil {
  94. logs.Error("Mats", lib.FuncName(), err)
  95. t_R.Code = 202
  96. t_R.Msg = err.Error()
  97. b, _ := msgpack.Marshal(&t_R)
  98. _ = Nats.Publish(m.Reply, b)
  99. return
  100. }
  101. t_R.Code = 200
  102. t_R.Msg = "ok"
  103. t_R.Data = id
  104. b, _ := msgpack.Marshal(&t_R)
  105. _ = Nats.Publish(m.Reply, b)
  106. })
  107. _, _ = Nats.Subscribe(conf.NatsSubj_Prefix+conf.Sys_Name+"_Read_Power_ByT_id", func(m *nats.Msg) {
  108. fmt.Printf("ERP_AMS_Read_Power_ByT_id message: %+v\n", string(m.Data))
  109. var t_R lib.JSONS
  110. o := orm.NewOrm()
  111. powerDao := powerlibs.NewPower(o)
  112. power, err := powerDao.Read_Power_ByT_id(string(m.Data))
  113. if err != nil {
  114. logs.Error("Mats", lib.FuncName(), err)
  115. t_R.Code = 202
  116. t_R.Msg = err.Error()
  117. b, _ := msgpack.Marshal(&t_R)
  118. _ = Nats.Publish(m.Reply, b)
  119. return
  120. }
  121. t_R.Code = 200
  122. t_R.Msg = "ok"
  123. t_R.Data = power
  124. b, _ := msgpack.Marshal(&t_R)
  125. _ = Nats.Publish(m.Reply, b)
  126. })
  127. _, _ = Nats.Subscribe(conf.NatsSubj_Prefix+conf.Sys_Name+"_Update_Power", func(m *nats.Msg) {
  128. var t_Req powerlibs.Power
  129. var t_R lib.JSONS
  130. err := msgpack.Unmarshal(m.Data, &t_Req)
  131. if err != nil {
  132. logs.Error("Mats", lib.FuncName(), err)
  133. t_R.Code = 202
  134. t_R.Msg = err.Error()
  135. b, _ := msgpack.Marshal(&t_R)
  136. _ = Nats.Publish(m.Reply, b)
  137. return
  138. }
  139. fmt.Printf("ERP_AMS_Update_Power message: %+v\n", t_Req)
  140. o := orm.NewOrm()
  141. powerDao := powerlibs.NewPower(o)
  142. id, err := powerDao.Update_Power(t_Req)
  143. if err != nil {
  144. logs.Error("Mats", lib.FuncName(), err)
  145. t_R.Code = 202
  146. t_R.Msg = err.Error()
  147. b, _ := msgpack.Marshal(&t_R)
  148. _ = Nats.Publish(m.Reply, b)
  149. return
  150. }
  151. APIDao := menulibs.NewAPI(o, Account.RedisCache_API)
  152. APIDao.Redis_API_DelK(t_Req.T_id)
  153. t_R.Code = 200
  154. t_R.Msg = "ok"
  155. t_R.Data = id
  156. b, _ := msgpack.Marshal(&t_R)
  157. _ = Nats.Publish(m.Reply, b)
  158. })
  159. _, _ = Nats.Subscribe(conf.NatsSubj_Prefix+conf.Sys_Name+"_Delete_Power", func(m *nats.Msg) {
  160. var t_Req powerlibs.Power
  161. var t_R lib.JSONS
  162. err := msgpack.Unmarshal(m.Data, &t_Req)
  163. if err != nil {
  164. logs.Error("Mats", lib.FuncName(), err)
  165. t_R.Code = 202
  166. t_R.Msg = err.Error()
  167. b, _ := msgpack.Marshal(&t_R)
  168. _ = Nats.Publish(m.Reply, b)
  169. return
  170. }
  171. fmt.Printf("ERP_AMS_Delete_Power message: %+v\n", t_Req)
  172. o := orm.NewOrm()
  173. powerDao := powerlibs.NewPower(o)
  174. id, err := powerDao.Delete_Power(t_Req)
  175. if err != nil {
  176. logs.Error("Mats", lib.FuncName(), err)
  177. t_R.Code = 202
  178. t_R.Msg = err.Error()
  179. b, _ := msgpack.Marshal(&t_R)
  180. _ = Nats.Publish(m.Reply, b)
  181. return
  182. }
  183. APIDao := menulibs.NewAPI(o, Account.RedisCache_API)
  184. APIDao.Redis_API_DelK(t_Req.T_id)
  185. t_R.Code = 200
  186. t_R.Msg = "ok"
  187. t_R.Data = id
  188. b, _ := msgpack.Marshal(&t_R)
  189. _ = Nats.Publish(m.Reply, b)
  190. })
  191. }