NatsERP.go 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. package NatsServer
  2. import (
  3. "ERP_user/Nats"
  4. "ERP_user/conf"
  5. "ERP_user/logs"
  6. "ERP_user/models/Account"
  7. "errors"
  8. "fmt"
  9. menulibs "git.baozhida.cn/ERP_libs/Menu"
  10. powerlibs "git.baozhida.cn/ERP_libs/Power"
  11. "git.baozhida.cn/ERP_libs/lib"
  12. "github.com/vmihailenco/msgpack/v5"
  13. "time"
  14. )
  15. func ERP_Read_Menu_List(prefix string) ([]menulibs.Menu, error) {
  16. sysName := Account.Get_Sys_Name(prefix)
  17. subj := conf.NatsSubj_Prefix + fmt.Sprintf("%s_%s", prefix, "Read_Menu_List")
  18. msg, err := Nats.Nats.Request(subj, []byte(""), 3*time.Second)
  19. if err != nil {
  20. logs.Error(lib.FuncName(), sysName, err)
  21. return nil, err
  22. }
  23. type T_R struct {
  24. Code int16 `xml:"Code"`
  25. Msg string `xml:"Msg"`
  26. Data []menulibs.Menu `xml:"Data"` // 泛型
  27. }
  28. var t_R T_R
  29. err = msgpack.Unmarshal(msg.Data, &t_R)
  30. if err != nil {
  31. logs.Error(lib.FuncName(), sysName, err)
  32. return nil, err
  33. }
  34. fmt.Printf("%s: %+v\n", subj, t_R)
  35. return t_R.Data, nil
  36. }
  37. func ERP_Read_User_Bind_Menu_List(prefix string, T_power string) ([]menulibs.Menu, error) {
  38. sysName := Account.Get_Sys_Name(prefix)
  39. subj := conf.NatsSubj_Prefix + fmt.Sprintf("%s_%s", prefix, "Read_User_Bind_Menu_List")
  40. msg, err := Nats.Nats.Request(subj, []byte(T_power), 3*time.Second)
  41. if err != nil {
  42. logs.Error(lib.FuncName(), sysName, err)
  43. return nil, err
  44. }
  45. type T_R struct {
  46. Code int16 `xml:"Code"`
  47. Msg string `xml:"Msg"`
  48. Data []menulibs.Menu `xml:"Data"` // 泛型
  49. }
  50. var t_R T_R
  51. err = msgpack.Unmarshal(msg.Data, &t_R)
  52. if err != nil {
  53. logs.Error(lib.FuncName(), sysName, err)
  54. return nil, err
  55. }
  56. fmt.Printf("%s: %+v\n", subj, t_R)
  57. return t_R.Data, nil
  58. }
  59. func ERP_Add_Power(prefix string, power powerlibs.Power) (id int, err error) {
  60. sysName := Account.Get_Sys_Name(prefix)
  61. subj := conf.NatsSubj_Prefix + fmt.Sprintf("%s_%s", prefix, "Add_Power")
  62. b, _ := msgpack.Marshal(&power)
  63. msg, err := Nats.Nats.Request(subj, b, 3*time.Second)
  64. if err != nil {
  65. logs.Error(lib.FuncName(), sysName, err)
  66. return id, err
  67. }
  68. type T_R struct {
  69. Code int16 `xml:"Code"`
  70. Msg string `xml:"Msg"`
  71. Data int64 `xml:"Data"` // 泛型
  72. }
  73. var t_R T_R
  74. err = msgpack.Unmarshal(msg.Data, &t_R)
  75. if err != nil {
  76. logs.Error(lib.FuncName(), sysName, err)
  77. return
  78. }
  79. fmt.Printf("%s: %+v\n", subj, t_R)
  80. if t_R.Code != 200 {
  81. err = errors.New(t_R.Msg)
  82. logs.Error(lib.FuncName(), sysName, err)
  83. return id, err
  84. }
  85. return int(t_R.Data), nil
  86. }
  87. func ERP_Read_Power_ByT_id(prefix string, T_id string) (r powerlibs.Power, err error) {
  88. sysName := Account.Get_Sys_Name(prefix)
  89. subj := conf.NatsSubj_Prefix + fmt.Sprintf("%s_%s", prefix, "Read_Power_ByT_id")
  90. msg, err := Nats.Nats.Request(subj, []byte(T_id), 3*time.Second)
  91. if err != nil {
  92. logs.Error(lib.FuncName(), sysName, err)
  93. return
  94. }
  95. type T_R struct {
  96. Code int16 `xml:"Code"`
  97. Msg string `xml:"Msg"`
  98. Data powerlibs.Power `xml:"Data"` // 泛型
  99. }
  100. var t_R T_R
  101. err = msgpack.Unmarshal(msg.Data, &t_R)
  102. if err != nil {
  103. logs.Error(lib.FuncName(), sysName, err)
  104. return
  105. }
  106. fmt.Printf("%s: %+v\n", subj, t_R)
  107. if t_R.Code != 200 {
  108. err = errors.New(t_R.Msg)
  109. logs.Error(lib.FuncName(), sysName, err)
  110. return r, err
  111. }
  112. return t_R.Data, nil
  113. }
  114. func ERP_Update_Power(prefix string, power powerlibs.Power) (id int, err error) {
  115. sysName := Account.Get_Sys_Name(prefix)
  116. subj := conf.NatsSubj_Prefix + fmt.Sprintf("%s_%s", prefix, "Update_Power")
  117. b, _ := msgpack.Marshal(&power)
  118. msg, err := Nats.Nats.Request(subj, b, 3*time.Second)
  119. if err != nil {
  120. logs.Error(lib.FuncName(), sysName, err)
  121. return id, err
  122. }
  123. type T_R struct {
  124. Code int16 `xml:"Code"`
  125. Msg string `xml:"Msg"`
  126. Data int `xml:"Data"` // 泛型
  127. }
  128. var t_R T_R
  129. err = msgpack.Unmarshal(msg.Data, &t_R)
  130. if err != nil {
  131. logs.Error(lib.FuncName(), sysName, err)
  132. return
  133. }
  134. fmt.Printf("%s: %+v\n", subj, t_R)
  135. if t_R.Code != 200 {
  136. err = errors.New(t_R.Msg)
  137. logs.Error(lib.FuncName(), sysName, err)
  138. return id, err
  139. }
  140. return t_R.Data, nil
  141. }
  142. func ERP_Delete_Power(prefix string, power powerlibs.Power) (id int64, err error) {
  143. sysName := Account.Get_Sys_Name(prefix)
  144. subj := conf.NatsSubj_Prefix + fmt.Sprintf("%s_%s", prefix, "Delete_Power")
  145. b, _ := msgpack.Marshal(&power)
  146. msg, err := Nats.Nats.Request(subj, b, 3*time.Second)
  147. if err != nil {
  148. logs.Error(lib.FuncName(), sysName, err)
  149. return id, err
  150. }
  151. type T_R struct {
  152. Code int16 `xml:"Code"`
  153. Msg string `xml:"Msg"`
  154. Data int64 `xml:"Data"` // 泛型
  155. }
  156. var t_R T_R
  157. err = msgpack.Unmarshal(msg.Data, &t_R)
  158. if err != nil {
  159. logs.Error(lib.FuncName(), sysName, err)
  160. return
  161. }
  162. fmt.Printf("%s: %+v\n", subj, t_R)
  163. if t_R.Code != 200 {
  164. err = errors.New(t_R.Msg)
  165. logs.Error(lib.FuncName(), sysName, err)
  166. return id, err
  167. }
  168. return t_R.Data, nil
  169. }