addlabellogic.go 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. package logic
  2. import (
  3. se "app.yhyue.com/moapp/jybase/encrypt"
  4. "context"
  5. "fmt"
  6. "jyBXBase/rpc/bxcollection/model"
  7. "log"
  8. "strconv"
  9. "time"
  10. "jyBXBase/rpc/bxcollection/bxcol"
  11. "jyBXBase/rpc/bxcollection/internal/svc"
  12. "github.com/zeromicro/go-zero/core/logx"
  13. )
  14. type AddlabelLogic struct {
  15. ctx context.Context
  16. svcCtx *svc.ServiceContext
  17. logx.Logger
  18. }
  19. func NewAddlabelLogic(ctx context.Context, svcCtx *svc.ServiceContext) *AddlabelLogic {
  20. return &AddlabelLogic{
  21. ctx: ctx,
  22. svcCtx: svcCtx,
  23. Logger: logx.WithContext(ctx),
  24. }
  25. }
  26. // 新增标签
  27. func (l *AddlabelLogic) Addlabel(in *bxcol.AddlabelReq) (*bxcol.AddlabelRes, error) {
  28. userid := in.UserId
  29. var resp bxcol.AddlabelRes
  30. if in.Name == "" {
  31. return &bxcol.AddlabelRes{
  32. ErrCode: -1,
  33. ErrMsg: "未传入名字",
  34. }, nil
  35. }
  36. //now := time.Now().Unix()
  37. if model.Mysql.CountBySql(fmt.Sprintf(`select count(1) from %s where userid =?`, "bdlabel"), userid) >= 50 {
  38. return &bxcol.AddlabelRes{
  39. ErrCode: -1,
  40. ErrMsg: "标签数量已达上限",
  41. }, nil
  42. }
  43. //是否有重名
  44. if labArr := *model.Mysql.SelectBySql(fmt.Sprintf("select * from %s where labelname = ? and userid = ?", "bdlabel"), in.Name, userid); len(labArr) == 1 {
  45. return &bxcol.AddlabelRes{
  46. ErrCode: -1,
  47. ErrMsg: "该标签已经存在",
  48. }, nil
  49. } else { //新增标签
  50. insertMap := map[string]interface{}{
  51. "userid": userid,
  52. "labelname": in.Name,
  53. "createdate": time.Now(),
  54. }
  55. if lid := model.Mysql.Insert("bdlabel", insertMap); lid > 0 {
  56. resp.Labid = se.SE.EncodeString(strconv.FormatInt(lid, 10))
  57. return &resp, nil
  58. } else {
  59. log.Println("新增标签失败 - name:", in.Name)
  60. resp.ErrMsg = "保存失败"
  61. resp.ErrCode = -1
  62. return &resp, nil
  63. }
  64. }
  65. }