subscribe.go 5.6 KB

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