get_label_select_list_logic.go 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. package label
  2. import (
  3. "context"
  4. "github.com/suyuan32/simple-admin-common/msg/errormsg"
  5. "wechat-api/ent/label"
  6. "wechat-api/ent/predicate"
  7. "wechat-api/internal/utils/dberrorhandler"
  8. "wechat-api/internal/svc"
  9. "wechat-api/internal/types"
  10. "github.com/zeromicro/go-zero/core/logx"
  11. )
  12. type GetLabelSelectListLogic struct {
  13. logx.Logger
  14. ctx context.Context
  15. svcCtx *svc.ServiceContext
  16. }
  17. func NewGetLabelSelectListLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GetLabelSelectListLogic {
  18. return &GetLabelSelectListLogic{
  19. Logger: logx.WithContext(ctx),
  20. ctx: ctx,
  21. svcCtx: svcCtx}
  22. }
  23. func (l *GetLabelSelectListLogic) GetLabelSelectList(req *types.LabelListReq) (*types.LabelSelectListResp, error) {
  24. var predicates []predicate.Label
  25. if req.Type != nil {
  26. predicates = append(predicates, label.TypeEQ(*req.Type))
  27. }
  28. if req.Name != nil {
  29. predicates = append(predicates, label.NameContains(*req.Name))
  30. }
  31. if req.From != nil {
  32. predicates = append(predicates, label.FromEQ(*req.From))
  33. }
  34. if req.Mode != nil {
  35. predicates = append(predicates, label.ModeEQ(*req.Mode))
  36. }
  37. data, err := l.svcCtx.DB.Label.Query().Where(predicates...).Page(l.ctx, req.Page, req.PageSize)
  38. if err != nil {
  39. return nil, dberrorhandler.DefaultEntError(l.Logger, err, req)
  40. }
  41. resp := &types.LabelSelectListResp{}
  42. resp.Msg = errormsg.Success
  43. for _, v := range data.List {
  44. resp.Data = append(resp.Data,
  45. types.LabelSelectListInfo{
  46. Value: &v.ID,
  47. Label: &v.Name,
  48. })
  49. }
  50. return resp, nil
  51. }