savetsguidelogic.go 5.4 KB

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