subscribe.go 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. // @Description 订阅设置相关
  2. package service
  3. import (
  4. "bp.jydev.jianyu360.cn/BaseService/jyMicroservices/jyBXSubscribe/entity"
  5. IC "bp.jydev.jianyu360.cn/BaseService/jyMicroservices/jyBXSubscribe/rpc/init"
  6. "errors"
  7. "fmt"
  8. "github.com/gogf/gf/v2/util/gconv"
  9. "strconv"
  10. "strings"
  11. "app.yhyue.com/moapp/jybase/common"
  12. "app.yhyue.com/moapp/jybase/mongodb"
  13. "app.yhyue.com/moapp/jypkg/common/src/qfw/util/jy"
  14. "github.com/zeromicro/go-zero/core/logx"
  15. )
  16. type SubseribeService struct {
  17. Mgo mongodb.MongodbSim
  18. UserId string //mongodb 的用户id
  19. Types string //订阅信息产品类型 超级订阅、大会员、商机管理、免费订阅...
  20. PositionType int64
  21. *entity.SubscribeUpdate
  22. PositionId int64
  23. BaseUserId int64
  24. MgoLog mongodb.MongodbSim
  25. UserType string
  26. }
  27. // @description 订阅设置相关修改
  28. // @param SubseribeService 用户信息相关
  29. // @return int64 是否成功 1成功 -1失败
  30. // @return error 错误信息
  31. // TODO 后期需要实现 查询各个身份的订阅设置修改[目前只实现了超级订阅]
  32. func (this *SubseribeService) Update() (int64, error) {
  33. if this.UserId == "" {
  34. return -1, errors.New(entity.UPDATE_ERROR_MSG)
  35. }
  36. //查询条件
  37. //查询字段
  38. fields := ``
  39. key := ""
  40. status := ""
  41. switch this.UserType {
  42. //fType:普通用户;vType:超级订阅用户;mType:大会员用户;eType:商机管理用户
  43. case "fType":
  44. fields = `{"o_jy":1}`
  45. key = "o_jy"
  46. case "vType":
  47. key = "o_vipjy"
  48. status = "i_vip_status"
  49. fields = `{"i_vip_status":1,"o_vipjy":1}`
  50. case "mType":
  51. key = "o_member_jy"
  52. status = "i_member_status"
  53. fields = `{"i_member_status":1,"o_member_jy":1}`
  54. }
  55. //查询用户信息
  56. r := IC.Compatible.Select(this.UserId, fields)
  57. if r == nil || len(*r) == 0 {
  58. logx.Error("未找到用户")
  59. return -1, errors.New(entity.UPDATE_ERROR_MSG)
  60. }
  61. //判断用户权益
  62. vip_status := int64(1)
  63. if key != "o_jy" {
  64. vip_status = common.Int64All((*r)[status])
  65. }
  66. if vip_status <= 0 {
  67. logx.Error("用户暂无权益")
  68. return -1, errors.New(entity.UPDATE_ERROR_MSG)
  69. }
  70. o_vipjy := common.ObjToMap((*r)[key])
  71. //权益通过,获取要修改的参数
  72. setMap := map[string]interface{}{}
  73. if this.Area != nil {
  74. setMap[KeyHandle(key, "o_area")] = this.Area
  75. }
  76. //区县
  77. if this.District != nil {
  78. setMap[KeyHandle(key, "o_district")] = this.District
  79. }
  80. if this.Buyerclass != nil {
  81. setMap[KeyHandle(key, "a_buyerclass")] = this.Buyerclass
  82. }
  83. if this.Amount != "" {
  84. setMap[KeyHandle(key, "amount")] = this.Amount
  85. }
  86. if this.UserType != "vType" {
  87. this.ISwitch = 1
  88. }
  89. setMap[KeyHandle(key, "i_switch")] = this.ISwitch
  90. if this.Items != nil {
  91. if this.UserType == "fType" {
  92. if len(this.Items) > 0 {
  93. setMap[KeyHandle(key, "a_key")] = gconv.Maps(this.Items[0]["a_key"])
  94. }
  95. } else {
  96. if this.UserType == "vType" {
  97. //判断是否新增
  98. keyLen := 0
  99. if (*o_vipjy)["a_items"] != nil {
  100. aitems, _ := (*o_vipjy)["a_items"].([]interface{})
  101. a_items := common.ObjArrToMapArr(aitems)
  102. keyLen = GetKeyWordLen(a_items)
  103. }
  104. newKeyLen := GetKeyWordLen(this.Items)
  105. if newKeyLen > keyLen {
  106. jy.Publish(this.MgoLog, IC.C.Nsq, IC.C.NsqTopic, "task", this.UserId, jy.Jyweb_node2, map[string]interface{}{
  107. "code": 1015, //首次订阅
  108. "types": "subscribeKeyWords",
  109. "num": 50,
  110. "baseUserId": this.BaseUserId,
  111. "positionId": this.PositionId,
  112. })
  113. }
  114. }
  115. //大会员
  116. setMap[KeyHandle(key, "a_items")] = this.Items
  117. }
  118. }
  119. if this.Infotype != nil {
  120. setMap[KeyHandle(key, "a_infotype")] = this.Infotype
  121. }
  122. if this.Matchmode != "" {
  123. setMap[KeyHandle(key, "i_matchmode")] = strings.Split(this.Matchmode, ",")
  124. }
  125. if this.Matchway != "" {
  126. i_matchway, _ := strconv.Atoi(this.Matchway)
  127. setMap[KeyHandle(key, "i_matchway")] = i_matchway
  128. }
  129. if this.Projectmatch != "" {
  130. i_projectmatch, _ := strconv.Atoi(this.Projectmatch)
  131. setMap[KeyHandle(key, "i_projectmatch")] = i_projectmatch
  132. }
  133. if this.Ratemode != "" {
  134. i_ratemode, _ := strconv.Atoi(this.Ratemode)
  135. setMap[KeyHandle(key, "i_ratemode")] = i_ratemode
  136. }
  137. if this.Apppush != "" {
  138. i_apppush, _ := strconv.Atoi(this.Apppush)
  139. setMap[KeyHandle(key, "i_apppush")] = i_apppush
  140. }
  141. if this.Mailpush != "" {
  142. i_mailpush, _ := strconv.Atoi(this.Mailpush)
  143. setMap[KeyHandle(key, "i_mailpush")] = i_mailpush
  144. }
  145. if this.Mail != "" {
  146. setMap[KeyHandle(key, "s_email")] = this.Mail
  147. }
  148. if this.Otherbuyerclass != "" {
  149. i_otherbuyerclass, _ := strconv.Atoi(this.Otherbuyerclass)
  150. setMap[KeyHandle(key, "i_matchbuyerclass_other")] = i_otherbuyerclass
  151. }
  152. if ok := IC.Compatible.Update(this.UserId, map[string]interface{}{
  153. "$set": setMap,
  154. }); ok {
  155. return 1, nil
  156. }
  157. return -1, errors.New(entity.UPDATE_ERROR_MSG)
  158. }
  159. func KeyHandle(k, v string) string {
  160. return fmt.Sprintf("%s.%s", k, v)
  161. }
  162. func GetKeyWordLen(a_items []map[string]interface{}) (count int) {
  163. if len(a_items) == 0 {
  164. return
  165. }
  166. for _, v := range a_items {
  167. a_key, _ := v["a_key"].([]interface{})
  168. count += len(a_key)
  169. }
  170. return
  171. }