subscribe.go 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  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. err := 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. if err != nil {
  117. log.Println("nsq队列写入失败-->", err, "task", this.UserId)
  118. }
  119. //首次设置关键词 赠送一次抽奖机会
  120. err = jy.Publish(this.MgoLog, IC.C.Nsq, IC.C.NsqTopic, "lottery_draw_task", this.UserId, jy.Jyweb_node2, map[string]interface{}{
  121. "phone": this.UserId,
  122. "userId": this.UserId,
  123. "mgoUserId": this.MgoUserId,
  124. "positionId": this.PositionId,
  125. "activeId": 16,
  126. "taskInfoId": 2,
  127. "nickName": this.UserId,
  128. })
  129. if err != nil {
  130. log.Println("nsq队列写入失败-->", err, "lottery_draw_task", this.UserId)
  131. }
  132. }
  133. }
  134. //大会员
  135. setMap[KeyHandle(key, "a_items")] = this.Items
  136. }
  137. }
  138. if this.Infotype != nil {
  139. setMap[KeyHandle(key, "a_infotype")] = this.Infotype
  140. }
  141. if this.Matchmode != "" {
  142. setMap[KeyHandle(key, "i_matchmode")] = strings.Split(this.Matchmode, ",")
  143. }
  144. if this.Matchway != "" {
  145. i_matchway, _ := strconv.Atoi(this.Matchway)
  146. setMap[KeyHandle(key, "i_matchway")] = i_matchway
  147. }
  148. if this.Projectmatch != "" {
  149. i_projectmatch, _ := strconv.Atoi(this.Projectmatch)
  150. setMap[KeyHandle(key, "i_projectmatch")] = i_projectmatch
  151. }
  152. if this.Ratemode != "" {
  153. i_ratemode, _ := strconv.Atoi(this.Ratemode)
  154. setMap[KeyHandle(key, "i_ratemode")] = i_ratemode
  155. }
  156. if this.Apppush != "" {
  157. i_apppush, _ := strconv.Atoi(this.Apppush)
  158. setMap[KeyHandle(key, "i_apppush")] = i_apppush
  159. }
  160. if this.Mailpush != "" {
  161. i_mailpush, _ := strconv.Atoi(this.Mailpush)
  162. setMap[KeyHandle(key, "i_mailpush")] = i_mailpush
  163. }
  164. if this.Mail != "" {
  165. setMap[KeyHandle(key, "s_email")] = this.Mail
  166. }
  167. if this.Otherbuyerclass != "" {
  168. i_otherbuyerclass, _ := strconv.Atoi(this.Otherbuyerclass)
  169. setMap[KeyHandle(key, "i_matchbuyerclass_other")] = i_otherbuyerclass
  170. }
  171. if ok := IC.Compatible.Update(this.UserId, map[string]interface{}{
  172. "$set": setMap,
  173. }); ok {
  174. data := IC.Compatible.Select(this.UserId, `{"o_vipjy":1,"o_member_jy":1,"o_jy":"1"}`)
  175. if data != nil && len(*data) > 0 {
  176. (*data)["userid"] = this.UserId
  177. (*data)["type"] = key
  178. (*data)["createtime"] = time.Now().Unix()
  179. IC.MgoLog.Save("ovipjy_log", data)
  180. }
  181. return 1, nil
  182. }
  183. //日志保存
  184. return -1, errors.New(entity.UPDATE_ERROR_MSG)
  185. }
  186. func KeyHandle(k, v string) string {
  187. return fmt.Sprintf("%s.%s", k, v)
  188. }
  189. func GetKeyWordLen(a_items []map[string]interface{}) (count int) {
  190. if len(a_items) == 0 {
  191. return
  192. }
  193. for _, v := range a_items {
  194. a_key, _ := v["a_key"].([]interface{})
  195. count += len(a_key)
  196. }
  197. return
  198. }
  199. // 更新订阅向导标识
  200. func (s *SubseribeService) UpdateGuide() {
  201. if s.UserId != "" {
  202. ok := IC.Compatible.Update(s.UserId, map[string]interface{}{"$set": map[string]interface{}{"i_ts_guide": 1}})
  203. if !ok {
  204. log.Println("update guide false", s.UserId)
  205. }
  206. }
  207. }