savetsguidelogic.go 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  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}`)
  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. case SubVipFlag:
  65. if user.Vip.Status <= 0 {
  66. return nil, errors.New("身份异常")
  67. }
  68. field = "o_vipjy.%s"
  69. areaCountLimit = int(user.Vip.Areacount)
  70. countLimit = 300
  71. case MemberFlag:
  72. if user.Member.Status <= 0 {
  73. return nil, errors.New("身份异常")
  74. }
  75. field = "o_member_jy.%s"
  76. countLimit = 300
  77. areaCountLimit = -1
  78. // 判断单省版
  79. o_member_jy, o_member_jyb := (*data)["o_member_jy"].(map[string]interface{})
  80. if o_member_jy != nil && o_member_jyb {
  81. if common.IntAll(o_member_jy["i_areacount"]) > 0 {
  82. areaCountLimit = common.IntAll(o_member_jy["i_areacount"])
  83. }
  84. }
  85. case EntnicheFlag:
  86. if user.Entniche.Status <= 0 {
  87. return nil, errors.New("身份异常")
  88. }
  89. field = "o_entniche.%s"
  90. countLimit = 300
  91. areaCountLimit = -1
  92. }
  93. updateMap := map[string]interface{}{}
  94. area := map[string]interface{}{}
  95. err := json.Unmarshal([]byte(in.Area), &area)
  96. // 验证地区数量 //会有没有走过订阅向导买过省份订阅包的情况吗? 暂时不考虑
  97. if err != nil {
  98. logx.Error("反序列化失败", in.Area, err)
  99. return nil, errors.New("地区有误")
  100. } else {
  101. updateMap[fmt.Sprintf(field, "o_area")] = area
  102. }
  103. if areaCountLimit > 0 && len(area) > areaCountLimit {
  104. return nil, errors.New("地区数量有误")
  105. }
  106. // 处理地区
  107. if in.District != "" {
  108. district := map[string]interface{}{}
  109. err = json.Unmarshal([]byte(in.District), &district)
  110. if err != nil {
  111. logx.Error("反序列化失败", in.District, err)
  112. return nil, errors.New("地区有误")
  113. } else {
  114. updateMap[fmt.Sprintf(field, "o_district")] = district
  115. }
  116. }
  117. // 处理关键词
  118. // 免费 处理掉空格
  119. arryMap := []interface{}{}
  120. key := []string{}
  121. if in.UserType == SubFreeFlag {
  122. for i := 0; i < len(in.Keywords); i++ {
  123. tmp := processKeyword(in.Keywords[i])
  124. if len(tmp) > 0 {
  125. arryMap = append(arryMap, map[string]interface{}{"matchway": 1, "key": tmp[0:1], "notkey": nil, "updatetime": time.Now().Unix()})
  126. }
  127. if len(key) >= countLimit {
  128. break
  129. }
  130. }
  131. updateMap[fmt.Sprintf(field, "a_key")] = arryMap
  132. } else {
  133. // 付费 按一组词
  134. for i := 0; i < len(in.Keywords); i++ {
  135. tmp := processKeyword(in.Keywords[i])
  136. if len(tmp) > 0 {
  137. arryMap = append(arryMap, map[string]interface{}{"matchway": 0, "key": tmp, "notkey": nil, "updatetime": time.Now().Unix()})
  138. }
  139. if len(key) >= countLimit {
  140. break
  141. }
  142. }
  143. item := map[string]interface{}{
  144. "a_key": arryMap,
  145. "s_item": "未分类",
  146. "updatetime": date.NowFormat(date.Date_Full_Layout),
  147. }
  148. updateMap[fmt.Sprintf(field, "a_items")] = []map[string]interface{}{item}
  149. }
  150. updateMap[fmt.Sprintf(field, "l_modifydate")] = time.Now().Unix()
  151. updateMap["i_ts_guide"] = tsGuideFinished
  152. // 判断app提醒总开关是否打开 如果打开 我的订阅APP提醒初始值为“开启”状态。
  153. if in.AppSwitch {
  154. updateMap["o_pushset.o_subset.i_apppush"] = 1
  155. }
  156. go SetLog(in.UserId, strings.ReplaceAll(field, ".%s", ""))
  157. if !IC.Compatible.Update(in.UserId, map[string]interface{}{
  158. "$set": updateMap,
  159. }) {
  160. logx.Error("设置订阅向导更新失败", in.UserId, updateMap)
  161. return nil, errors.New("设置订阅向导更新失败")
  162. }
  163. go jy.Publish(IC.MgoLog, IC.C.Nsq, IC.C.NsqTopic, "task", in.UserId, jy.Jyweb_node2, map[string]interface{}{
  164. "code": 1015, //首次订阅
  165. "types": "subscribeKeyWords",
  166. "num": 50,
  167. "baseUserId": baseUserId,
  168. "positionId": positionId,
  169. })
  170. return &bxsubscribe.StatusResp{}, nil
  171. }
  172. // SetLog 订阅设置记录
  173. func SetLog(userid, types string) {
  174. if data := IC.Compatible.Select(userid, `{"o_vipjy":1,"o_member_jy":1,"o_jy":1}`); len(*data) > 0 && data != nil {
  175. (*data)["userid"] = userid
  176. (*data)["type"] = types
  177. (*data)["createtime"] = time.Now().Unix()
  178. IC.MgoLog.Save("ovipjy_log", data)
  179. }
  180. }
  181. // 保存入库之前,处理订阅的关键词
  182. func processKeyword(keyword string) []string {
  183. keywordReg := regexp.MustCompile("([\\s\u3000\u2003\u00a0+,,])+")
  184. spaceReg := regexp.MustCompile("\\s+")
  185. keyword = keywordReg.ReplaceAllString(keyword, " ")
  186. keyword = spaceReg.ReplaceAllString(keyword, " ")
  187. keyword = strings.Trim(keyword, " ")
  188. if keyword == "" {
  189. return nil
  190. }
  191. return strings.Split(keyword, " ")
  192. }