get_label_select_list_logic.go 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  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. organizationId := l.ctx.Value("organizationId").(uint64)
  25. var predicates []predicate.Label
  26. predicates = append(predicates, label.OrganizationIDEQ(organizationId))
  27. predicates = append(predicates, label.Ctype(1))
  28. if req.Type != nil {
  29. predicates = append(predicates, label.TypeEQ(*req.Type))
  30. }
  31. if req.Name != nil {
  32. predicates = append(predicates, label.NameContains(*req.Name))
  33. }
  34. if req.From != nil {
  35. predicates = append(predicates, label.FromEQ(*req.From))
  36. }
  37. if req.Mode != nil {
  38. predicates = append(predicates, label.ModeEQ(*req.Mode))
  39. }
  40. data, err := l.svcCtx.DB.Label.Query().Where(predicates...).Page(l.ctx, req.Page, req.PageSize)
  41. if err != nil {
  42. return nil, dberrorhandler.DefaultEntError(l.Logger, err, req)
  43. }
  44. resp := &types.LabelSelectListResp{}
  45. resp.Msg = errormsg.Success
  46. for _, v := range data.List {
  47. resp.Data = append(resp.Data,
  48. types.LabelSelectListInfo{
  49. Value: &v.ID,
  50. Label: &v.Name,
  51. })
  52. }
  53. return resp, nil
  54. }