savetsguidelogic.go 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. package logic
  2. import (
  3. "app.yhyue.com/moapp/jybase/common"
  4. "app.yhyue.com/moapp/jybase/date"
  5. "app.yhyue.com/moapp/jypkg/common/src/qfw/util/jy"
  6. IC "bp.jydev.jianyu360.cn/BaseService/jyMicroservices/jyBXSubscribe/rpc/init"
  7. "bp.jydev.jianyu360.cn/BaseService/jyMicroservices/jyBXSubscribe/rpc/internal/svc"
  8. "bp.jydev.jianyu360.cn/BaseService/jyMicroservices/jyBXSubscribe/rpc/type/bxsubscribe"
  9. "context"
  10. "encoding/json"
  11. "errors"
  12. "fmt"
  13. "regexp"
  14. "strconv"
  15. "strings"
  16. "time"
  17. "github.com/zeromicro/go-zero/core/logx"
  18. )
  19. type SaveTSGuideLogic struct {
  20. ctx context.Context
  21. svcCtx *svc.ServiceContext
  22. logx.Logger
  23. }
  24. func NewSaveTSGuideLogic(ctx context.Context, svcCtx *svc.ServiceContext) *SaveTSGuideLogic {
  25. return &SaveTSGuideLogic{
  26. ctx: ctx,
  27. svcCtx: svcCtx,
  28. Logger: logx.WithContext(ctx),
  29. }
  30. }
  31. // 保存订阅向导设置
  32. func (l *SaveTSGuideLogic) SaveTSGuide(in *bxsubscribe.SaveTSGuideReq) (*bxsubscribe.StatusResp, error) {
  33. // 校验用户身份
  34. baseUserId, _ := strconv.ParseInt(in.NewUserId, 10, 64)
  35. accountId, _ := strconv.ParseInt(in.AccountId, 10, 64)
  36. positionType, _ := strconv.ParseInt(in.PositionType, 10, 64)
  37. positionId, _ := strconv.ParseInt(in.PositionId, 10, 64)
  38. entId, _ := strconv.ParseInt(in.EntId, 10, 64)
  39. // 校验是否已经完成过订阅向导
  40. // 查询订阅信息中的关键词和向导查询
  41. data := IC.Compatible.Select(in.UserId, `{"i_ts_guide":1,"l_registedate":1,"o_member_jy":1,"o_jy":1}`)
  42. if data != nil && len(*data) > 0 {
  43. registedate := common.Int64All((*data)["l_registedate"])
  44. if (in.UserType != SubFreeFlag && registedate < IC.C.GuideRegistedate) || common.Int64All((*data)["i_ts_guide"]) == tsGuideFinished {
  45. // 已经设置过
  46. return nil, errors.New("重复设置向导")
  47. }
  48. }
  49. user := IC.Compatible.Middleground.PowerCheckCenter.Check(in.AppId, in.UserId, baseUserId, accountId, entId, positionType, positionId)
  50. if user == nil {
  51. return nil, errors.New("身份异常")
  52. }
  53. var field string
  54. countLimit := 0
  55. areaCountLimit := 0
  56. switch in.UserType {
  57. case SubFreeFlag:
  58. if !user.Free.IsFree {
  59. return nil, errors.New("身份异常")
  60. }
  61. countLimit = 10
  62. field = "o_jy.%s"
  63. areaCountLimit = 1 // 免费用户
  64. // 省份订阅包
  65. o_jy, o_jyb := (*data)["o_jy"].(map[string]interface{})
  66. if o_jy != nil && o_jyb {
  67. o_buyset_p, o_buyset_p_b := o_jy["o_buyset_p"].(map[string]interface{})
  68. if o_buyset_p != nil && o_buyset_p_b {
  69. areaCountLimit += common.Int64All(o_buyset_p["areacount"])
  70. }
  71. }
  72. case SubVipFlag:
  73. if user.Vip.Status <= 0 {
  74. return nil, errors.New("身份异常")
  75. }
  76. field = "o_vipjy.%s"
  77. areaCountLimit = int(user.Vip.Areacount)
  78. countLimit = 300
  79. case MemberFlag:
  80. if user.Member.Status <= 0 {
  81. return nil, errors.New("身份异常")
  82. }
  83. field = "o_member_jy.%s"
  84. countLimit = 300
  85. areaCountLimit = -1
  86. // 判断单省版
  87. o_member_jy, o_member_jyb := (*data)["o_member_jy"].(map[string]interface{})
  88. if o_member_jy != nil && o_member_jyb {
  89. if common.IntAll(o_member_jy["i_areacount"]) > 0 {
  90. areaCountLimit = common.IntAll(o_member_jy["i_areacount"])
  91. }
  92. }
  93. case EntnicheFlag:
  94. if user.Entniche.Status <= 0 {
  95. return nil, errors.New("身份异常")
  96. }
  97. field = "o_entniche.%s"
  98. countLimit = 300
  99. areaCountLimit = -1
  100. }
  101. updateMap := map[string]interface{}{}
  102. area := map[string]interface{}{}
  103. err := json.Unmarshal([]byte(in.Area), &area)
  104. // 验证地区数量 //会有没有走过订阅向导买过省份订阅包的情况吗? 暂时不考虑
  105. if err != nil {
  106. logx.Error("反序列化失败", in.Area, err)
  107. return nil, errors.New("地区有误")
  108. } else {
  109. updateMap[fmt.Sprintf(field, "o_area")] = area
  110. }
  111. if areaCountLimit > 0 && len(area) > areaCountLimit {
  112. return nil, errors.New("地区数量有误")
  113. }
  114. // 处理地区
  115. if in.District != "" {
  116. district := map[string]interface{}{}
  117. err = json.Unmarshal([]byte(in.District), &district)
  118. if err != nil {
  119. logx.Error("反序列化失败", in.District, err)
  120. return nil, errors.New("地区有误")
  121. } else {
  122. updateMap[fmt.Sprintf(field, "o_district")] = district
  123. }
  124. }
  125. // 处理关键词
  126. // 免费 处理掉空格
  127. arryMap := []interface{}{}
  128. key := []string{}
  129. if in.UserType == SubFreeFlag {
  130. for i := 0; i < len(in.Keywords); i++ {
  131. tmp := processKeyword(in.Keywords[i])
  132. if len(tmp) > 0 {
  133. arryMap = append(arryMap, map[string]interface{}{"matchway": 1, "key": tmp[0:1], "notkey": nil, "updatetime": time.Now().Unix()})
  134. }
  135. if len(key) >= countLimit {
  136. break
  137. }
  138. }
  139. updateMap[fmt.Sprintf(field, "a_key")] = arryMap
  140. } else {
  141. // 付费 按一组词
  142. for i := 0; i < len(in.Keywords); i++ {
  143. tmp := processKeyword(in.Keywords[i])
  144. if len(tmp) > 0 {
  145. arryMap = append(arryMap, map[string]interface{}{"matchway": 0, "key": tmp, "notkey": nil, "updatetime": time.Now().Unix()})
  146. }
  147. if len(key) >= countLimit {
  148. break
  149. }
  150. }
  151. item := map[string]interface{}{
  152. "a_key": arryMap,
  153. "s_item": "未分类",
  154. "updatetime": date.NowFormat(date.Date_Full_Layout),
  155. }
  156. updateMap[fmt.Sprintf(field, "a_items")] = []map[string]interface{}{item}
  157. }
  158. updateMap[fmt.Sprintf(field, "l_modifydate")] = time.Now().Unix()
  159. updateMap["i_ts_guide"] = tsGuideFinished
  160. // 判断app提醒总开关是否打开 如果打开 我的订阅APP提醒初始值为“开启”状态。
  161. if in.AppSwitch {
  162. updateMap["o_pushset.o_subset.i_apppush"] = 1
  163. }
  164. go SetLog(in.UserId, strings.ReplaceAll(field, ".%s", ""))
  165. if !IC.Compatible.Update(in.UserId, map[string]interface{}{
  166. "$set": updateMap,
  167. }) {
  168. logx.Error("设置订阅向导更新失败", in.UserId, updateMap)
  169. return nil, errors.New("设置订阅向导更新失败")
  170. }
  171. go jy.Publish(IC.MgoLog, IC.C.Nsq, IC.C.NsqTopic, "task", in.UserId, jy.Jyweb_node2, map[string]interface{}{
  172. "code": 1015, //首次订阅
  173. "types": "subscribeKeyWords",
  174. "num": 50,
  175. "baseUserId": baseUserId,
  176. "positionId": positionId,
  177. })
  178. return &bxsubscribe.StatusResp{}, nil
  179. }
  180. // SetLog 订阅设置记录
  181. func SetLog(userid, types string) {
  182. if data := IC.Compatible.Select(userid, `{"o_vipjy":1,"o_member_jy":1,"o_jy":1}`); len(*data) > 0 && data != nil {
  183. (*data)["userid"] = userid
  184. (*data)["type"] = types
  185. (*data)["createtime"] = time.Now().Unix()
  186. IC.MgoLog.Save("ovipjy_log", data)
  187. }
  188. }
  189. // 保存入库之前,处理订阅的关键词
  190. func processKeyword(keyword string) []string {
  191. keywordReg := regexp.MustCompile("([\\s\u3000\u2003\u00a0+,,])+")
  192. spaceReg := regexp.MustCompile("\\s+")
  193. keyword = keywordReg.ReplaceAllString(keyword, " ")
  194. keyword = spaceReg.ReplaceAllString(keyword, " ")
  195. keyword = strings.Trim(keyword, " ")
  196. if keyword == "" {
  197. return nil
  198. }
  199. return strings.Split(keyword, " ")
  200. }