utils.go 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. package main
  2. import (
  3. "app.yhyue.com/data_processing/common_utils/udp"
  4. "encoding/json"
  5. "fmt"
  6. wlog "github.com/wcc4869/common_utils/log"
  7. "go.mongodb.org/mongo-driver/bson/primitive"
  8. "net"
  9. )
  10. //GetJyKey 免费订阅:o_jy.a_key.key/appendkey
  11. func GetJyKey(data map[string]interface{}) (res []interface{}) {
  12. // 获取o_jy.a_key.key的值
  13. if ojy, ok := data["o_jy"].(map[string]interface{}); ok {
  14. if akey, ok := ojy["a_key"].([]interface{}); ok {
  15. for _, v := range akey {
  16. if m, ok := v.(map[string]interface{}); ok {
  17. if key, ok := m["key"].([]interface{}); ok {
  18. res = append(res, key...)
  19. }
  20. if key, ok := m["appendkey"].([]interface{}); ok {
  21. res = append(res, key...)
  22. }
  23. }
  24. }
  25. }
  26. }
  27. return
  28. }
  29. //GetVipKey 超级订阅:o_vipjy.a_items.a_key.key/appendkey
  30. func GetVipKey(data map[string]interface{}) (res []interface{}) {
  31. //o_vipjy.a_items.a_key.key
  32. if o_vipjy, ok := data["o_vipjy"].(map[string]interface{}); ok {
  33. if aItems, ok := o_vipjy["a_items"].([]interface{}); ok {
  34. for _, aItem := range aItems {
  35. if a_k, ok := aItem.(map[string]interface{}); ok {
  36. if aKey, ok := a_k["a_key"].([]interface{}); ok {
  37. for _, ak := range aKey {
  38. if va, ok := ak.(map[string]interface{}); ok {
  39. if key, ok := va["key"].([]interface{}); ok {
  40. res = append(res, key...)
  41. }
  42. if key, ok := va["appendkey"].([]interface{}); ok {
  43. res = append(res, key...)
  44. }
  45. }
  46. }
  47. }
  48. }
  49. }
  50. }
  51. }
  52. return
  53. }
  54. //GetMemberKey 大会员订阅:o_member_jy.a_items.a_key.key/appendkey
  55. func GetMemberKey(data map[string]interface{}) (res []interface{}) {
  56. //o_member_jy.a_items.a_key.key
  57. if o_member_jy, ok := data["o_member_jy"].(map[string]interface{}); ok {
  58. if aItems, ok := o_member_jy["a_items"].([]interface{}); ok {
  59. for _, aItem := range aItems {
  60. if tem, ok := aItem.(map[string]interface{}); ok {
  61. if aKey, ok := tem["a_key"].([]interface{}); ok {
  62. for _, ak := range aKey {
  63. if va, ok := ak.(map[string]interface{}); ok {
  64. if key, ok := va["key"].([]interface{}); ok {
  65. res = append(res, key...)
  66. }
  67. if key, ok := va["appendkey"].([]interface{}); ok {
  68. res = append(res, key...)
  69. }
  70. }
  71. }
  72. }
  73. }
  74. }
  75. }
  76. }
  77. return
  78. }
  79. //RemoveDuplicates 过滤重复数据
  80. func RemoveDuplicates(elements []interface{}) []interface{} {
  81. encountered := map[interface{}]bool{}
  82. result := []interface{}{}
  83. for _, v := range elements {
  84. if encountered[v] == true {
  85. continue
  86. } else {
  87. encountered[v] = true
  88. result = append(result, v)
  89. }
  90. }
  91. return result
  92. }
  93. //GetUserKeys 获取用户订阅关键词
  94. func GetUserKeys(data map[string]interface{}) (res []interface{}) {
  95. keys := GetJyKey(data)
  96. vkeys := GetVipKey(data)
  97. mkeys := GetMemberKey(data)
  98. res = append(res, keys...)
  99. res = append(res, vkeys...)
  100. res = append(res, mkeys...)
  101. res = RemoveDuplicates(res)
  102. return
  103. }
  104. func toString(i interface{}) string {
  105. return fmt.Sprintf("%v", i)
  106. }
  107. //getLastUser 获取最新的用户
  108. func getLastUser(coll string) (user map[string]interface{}) {
  109. rs, _ := Mgo.Find(coll, nil, map[string]interface{}{"_id": -1}, nil, true, -1, 1)
  110. for _, v := range *rs {
  111. user = v
  112. }
  113. return
  114. }
  115. //BsonIdToSId 根据bsonID转string
  116. func BsonIdToSId(uid interface{}) string {
  117. if uid == nil {
  118. return ""
  119. } else if u, ok := uid.(string); ok {
  120. return u
  121. } else if u, ok := uid.(primitive.ObjectID); ok {
  122. return u.Hex()
  123. } else {
  124. return ""
  125. }
  126. }
  127. //StringTOBsonId StringTOBsonId
  128. func StringTOBsonId(id string) (bid primitive.ObjectID) {
  129. if id != "" {
  130. bid, _ = primitive.ObjectIDFromHex(id)
  131. } else {
  132. bid = primitive.NilObjectID
  133. }
  134. return
  135. }
  136. //SendUdpMsg 通知处理企业新增数据
  137. func SendUdpMsg(data map[string]interface{}, target *net.UDPAddr) {
  138. bytes, _ := json.Marshal(data)
  139. UdpClient.WriteUdp(bytes, udp.OP_TYPE_DATA, target)
  140. wlog.Info("SendUdpMsg", wlog.Any("data", data))
  141. wlog.Info("SendUdpMsg", wlog.Any("target", target))
  142. }