123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181 |
- package logic
- import (
- "app.yhyue.com/moapp/jybase/common"
- "app.yhyue.com/moapp/jybase/date"
- IC "bp.jydev.jianyu360.cn/BaseService/jyMicroservices/jyBXSubscribe/rpc/init"
- "bp.jydev.jianyu360.cn/BaseService/jyMicroservices/jyBXSubscribe/rpc/internal/svc"
- "bp.jydev.jianyu360.cn/BaseService/jyMicroservices/jyBXSubscribe/rpc/type/bxsubscribe"
- "context"
- "encoding/json"
- "errors"
- "fmt"
- "regexp"
- "strconv"
- "strings"
- "time"
- "github.com/zeromicro/go-zero/core/logx"
- )
- type SaveTSGuideLogic struct {
- ctx context.Context
- svcCtx *svc.ServiceContext
- logx.Logger
- }
- func NewSaveTSGuideLogic(ctx context.Context, svcCtx *svc.ServiceContext) *SaveTSGuideLogic {
- return &SaveTSGuideLogic{
- ctx: ctx,
- svcCtx: svcCtx,
- Logger: logx.WithContext(ctx),
- }
- }
- // 保存订阅向导设置
- func (l *SaveTSGuideLogic) SaveTSGuide(in *bxsubscribe.SaveTSGuideReq) (*bxsubscribe.StatusResp, error) {
- // 校验用户身份
- baseUserId, _ := strconv.ParseInt(in.NewUserId, 10, 64)
- accountId, _ := strconv.ParseInt(in.AccountId, 10, 64)
- positionType, _ := strconv.ParseInt(in.PositionType, 10, 64)
- positionId, _ := strconv.ParseInt(in.PositionId, 10, 64)
- entId, _ := strconv.ParseInt(in.EntId, 10, 64)
- // 校验是否已经完成过订阅向导
- // 查询订阅信息中的关键词和向导查询
- data := IC.Compatible.Select(in.UserId, `{"i_ts_guide":1,"l_registedate":1,"o_member_jy":1}`)
- if data != nil && len(*data) > 0 {
- registedate := common.Int64All((*data)["l_registedate"])
- if (in.UserType != SubFreeFlag && registedate < IC.C.GuideRegistedate) || common.Int64All((*data)["i_ts_guide"]) == tsGuidefinished {
- // 已经设置过
- return nil, errors.New("重复设置向导")
- }
- }
- user := IC.Compatible.Middleground.PowerCheckCenter.Check(in.AppId, in.UserId, baseUserId, accountId, entId, positionType, positionId)
- if user == nil {
- return nil, errors.New("身份异常")
- }
- var field string
- countLimit := 0
- areaCountLimit := 0
- switch in.UserType {
- case SubFreeFlag:
- if !user.Free.IsFree {
- return nil, errors.New("身份异常")
- }
- countLimit = 10
- field = "o_jy.%s"
- areaCountLimit = 1 // 免费用户
- case SubVipFlag:
- if user.Vip.Status <= 0 {
- return nil, errors.New("身份异常")
- }
- field = "o_vipjy.%s"
- areaCountLimit = int(user.Vip.Areacount)
- countLimit = 300
- case MemberFlag:
- if user.Member.Status <= 0 {
- return nil, errors.New("身份异常")
- }
- field = "o_member_jy.%s"
- countLimit = 300
- areaCountLimit = -1
- // 判断单省版
- o_member_jy, o_member_jyb := (*data)["o_member_jy"].(map[string]interface{})
- if o_member_jy != nil && o_member_jyb {
- if common.IntAll(o_member_jy["i_areacount"]) > 0 {
- areaCountLimit = common.IntAll(o_member_jy["i_areacount"])
- }
- }
- case EntnicheFlag:
- if user.Entniche.Status <= 0 {
- return nil, errors.New("身份异常")
- }
- field = "o_entniche.%s"
- countLimit = 300
- areaCountLimit = -1
- }
- updateMap := map[string]interface{}{}
- area := map[string]interface{}{}
- err := json.Unmarshal([]byte(in.Area), &area)
- // 验证地区数量 //会有没有走过订阅向导买过省份订阅包的情况吗? 暂时不考虑
- if err != nil {
- logx.Error("反序列化失败", in.Area, err)
- return nil, errors.New("地区有误")
- } else {
- updateMap[fmt.Sprintf(field, "o_area")] = area
- }
- if areaCountLimit > 0 && len(area) > areaCountLimit {
- return nil, errors.New("地区数量有误")
- }
- // 处理地区
- if in.District != "" {
- district := map[string]interface{}{}
- err = json.Unmarshal([]byte(in.District), &district)
- if err != nil {
- logx.Error("反序列化失败", in.District, err)
- return nil, errors.New("地区有误")
- } else {
- updateMap[fmt.Sprintf(field, "o_district")] = district
- }
- }
- // 处理关键词
- // 免费 处理掉空格
- arryMap := []interface{}{}
- key := []string{}
- if in.UserType == SubFreeFlag {
- for i := 0; i < len(in.Keywords); i++ {
- tmp := processKeyword(in.Keywords[i])
- if len(tmp) > 0 {
- arryMap = append(arryMap, map[string]interface{}{"matchway": 1, "key": tmp[0:1], "notkey": nil, "updatetime": time.Now().Unix()})
- }
- if len(key) >= countLimit {
- break
- }
- }
- updateMap[fmt.Sprintf(field, "a_key")] = arryMap
- } else {
- // 付费 按一组词
- for i := 0; i < len(in.Keywords); i++ {
- tmp := processKeyword(in.Keywords[i])
- if len(tmp) > 0 {
- arryMap = append(arryMap, map[string]interface{}{"matchway": 0, "key": tmp, "notkey": nil, "updatetime": time.Now().Unix()})
- }
- if len(key) >= countLimit {
- break
- }
- }
- item := map[string]interface{}{
- "a_key": arryMap,
- "s_item": "未分类",
- "updatetime": date.NowFormat(date.Date_Full_Layout),
- }
- updateMap[fmt.Sprintf(field, "a_items")] = []map[string]interface{}{item}
- }
- updateMap[fmt.Sprintf(field, "l_modifydate")] = time.Now().Unix()
- updateMap["i_ts_guide"] = tsGuidefinished
- if !IC.Compatible.Update(in.UserId, map[string]interface{}{
- "$set": updateMap,
- }) {
- logx.Error("设置订阅向导更新失败", in.UserId, updateMap)
- return nil, errors.New("设置订阅向导更新失败")
- }
- return &bxsubscribe.StatusResp{}, nil
- }
- // 保存入库之前,处理订阅的关键词
- func processKeyword(keyword string) []string {
- keywordReg := regexp.MustCompile("([\\s\u3000\u2003\u00a0+,,])+")
- spaceReg := regexp.MustCompile("\\s+")
- keyword = keywordReg.ReplaceAllString(keyword, " ")
- keyword = spaceReg.ReplaceAllString(keyword, " ")
- keyword = strings.Trim(keyword, " ")
- if keyword == "" {
- return nil
- }
- return strings.Split(keyword, " ")
- }
|