savetsguidelogic.go 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  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. countLimit = 300
  70. case MemberFlag:
  71. if user.Member.Status <= 0 {
  72. return nil, errors.New("身份异常")
  73. }
  74. field = "o_member_jy.%s"
  75. countLimit = 300
  76. areaCountLimit = -1
  77. // 判断单省版
  78. o_member_jy, o_member_jyb := (*data)["o_member_jy"].(map[string]interface{})
  79. if o_member_jy != nil && o_member_jyb {
  80. if common.IntAll(o_member_jy["i_areacount"]) > 0 {
  81. areaCountLimit = common.IntAll(o_member_jy["i_areacount"])
  82. }
  83. }
  84. case EntnicheFlag:
  85. if user.Entniche.Status <= 0 {
  86. return nil, errors.New("身份异常")
  87. }
  88. field = "o_entniche.%s"
  89. countLimit = 300
  90. areaCountLimit = -1
  91. }
  92. updateMap := map[string]interface{}{}
  93. area := map[string]interface{}{}
  94. err := json.Unmarshal([]byte(in.Area), &area)
  95. // 验证地区数量 //会有没有走过订阅向导买过省份订阅包的情况吗? 暂时不考虑
  96. if err != nil {
  97. logx.Error("反序列化失败", in.Area, err)
  98. return nil, errors.New("地区有误")
  99. } else {
  100. updateMap[fmt.Sprintf(field, "o_area")] = area
  101. }
  102. if areaCountLimit > 0 && len(area) > areaCountLimit {
  103. return nil, errors.New("地区数量有误")
  104. }
  105. // 处理地区
  106. if in.District != "" {
  107. district := map[string]interface{}{}
  108. err = json.Unmarshal([]byte(in.District), &district)
  109. if err != nil {
  110. logx.Error("反序列化失败", in.District, err)
  111. return nil, errors.New("地区有误")
  112. } else {
  113. updateMap[fmt.Sprintf(field, "o_district")] = district
  114. }
  115. }
  116. // 处理关键词
  117. // 免费 处理掉空格
  118. arryMap := []interface{}{}
  119. key := []string{}
  120. if in.UserType == SubFreeFlag {
  121. for i := 0; i < len(in.Keywords); i++ {
  122. tmp := processKeyword(in.Keywords[i])
  123. if len(tmp) > 0 {
  124. arryMap = append(arryMap, map[string]interface{}{"matchway": 1, "key": tmp[0:1], "notkey": nil, "updatetime": time.Now().Unix()})
  125. }
  126. if len(key) >= countLimit {
  127. break
  128. }
  129. }
  130. updateMap[fmt.Sprintf(field, "a_key")] = arryMap
  131. } else {
  132. // 付费 按一组词
  133. for i := 0; i < len(in.Keywords); i++ {
  134. tmp := processKeyword(in.Keywords[i])
  135. if len(tmp) > 0 {
  136. arryMap = append(arryMap, map[string]interface{}{"matchway": 0, "key": tmp, "notkey": nil, "updatetime": time.Now().Unix()})
  137. }
  138. if len(key) >= countLimit {
  139. break
  140. }
  141. }
  142. item := map[string]interface{}{
  143. "a_key": arryMap,
  144. "s_item": "未分类",
  145. "updatetime": date.NowFormat(date.Date_Full_Layout),
  146. }
  147. updateMap[fmt.Sprintf(field, "a_items")] = []map[string]interface{}{item}
  148. }
  149. updateMap[fmt.Sprintf(field, "l_modifydate")] = time.Now().Unix()
  150. updateMap["i_ts_guide"] = tsGuidefinished
  151. if !IC.Compatible.Update(in.UserId, map[string]interface{}{
  152. "$set": updateMap,
  153. }) {
  154. logx.Error("设置订阅向导更新失败", in.UserId, updateMap)
  155. return nil, errors.New("设置订阅向导更新失败")
  156. }
  157. return &bxsubscribe.StatusResp{}, nil
  158. }
  159. // 保存入库之前,处理订阅的关键词
  160. func processKeyword(keyword string) []string {
  161. keywordReg := regexp.MustCompile("([\\s\u3000\u2003\u00a0+,,])+")
  162. spaceReg := regexp.MustCompile("\\s+")
  163. keyword = keywordReg.ReplaceAllString(keyword, " ")
  164. keyword = spaceReg.ReplaceAllString(keyword, " ")
  165. keyword = strings.Trim(keyword, " ")
  166. if keyword == "" {
  167. return nil
  168. }
  169. return strings.Split(keyword, " ")
  170. }