savelistmodelogic.go 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. package logic
  2. import (
  3. "context"
  4. "errors"
  5. IC "jyBXBase/rpc/init"
  6. "jyBXBase/rpc/internal/svc"
  7. "jyBXBase/rpc/type/bxbase"
  8. "github.com/zeromicro/go-zero/core/logx"
  9. )
  10. type SaveListModeLogic struct {
  11. ctx context.Context
  12. svcCtx *svc.ServiceContext
  13. logx.Logger
  14. }
  15. func NewSaveListModeLogic(ctx context.Context, svcCtx *svc.ServiceContext) *SaveListModeLogic {
  16. return &SaveListModeLogic{
  17. ctx: ctx,
  18. svcCtx: svcCtx,
  19. Logger: logx.WithContext(ctx),
  20. }
  21. }
  22. // SaveListMode 搜索/订阅 列表模式保存
  23. func (l *SaveListModeLogic) SaveListMode(in *bxbase.SaveListModeReq) (res *bxbase.CommonRes, err error) {
  24. // todo 待测试
  25. res = new(bxbase.CommonRes)
  26. query := map[string]interface{}{
  27. "userId": in.UserId,
  28. "type": in.Type,
  29. }
  30. if IC.Mgo.Count("list_mode", query) > 0 {
  31. // 存在则更新
  32. b := IC.Mgo.Update("list_mode", query, map[string]interface{}{
  33. "$set": map[string]interface{}{
  34. "mode": in.Mode},
  35. }, false, false)
  36. if !b {
  37. res.ErrCode = 1
  38. err = errors.New("更新失败")
  39. }
  40. } else {
  41. // 保存
  42. saveData := map[string]interface{}{
  43. "type": in.Type,
  44. "mode": in.Mode,
  45. "userId": in.UserId,
  46. }
  47. rs := IC.Mgo.Save("list_mode", saveData)
  48. if rs == "" {
  49. res.ErrCode = 1
  50. err = errors.New("保存失败")
  51. }
  52. }
  53. return res, nil
  54. }