subscribe.go 5.3 KB

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