productListLogic.go 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. package product
  2. import (
  3. "context"
  4. "perms-system-server/internal/middleware"
  5. "perms-system-server/internal/svc"
  6. "perms-system-server/internal/types"
  7. "perms-system-server/internal/util"
  8. "github.com/zeromicro/go-zero/core/logx"
  9. )
  10. type ProductListLogic struct {
  11. logx.Logger
  12. ctx context.Context
  13. svcCtx *svc.ServiceContext
  14. }
  15. func NewProductListLogic(ctx context.Context, svcCtx *svc.ServiceContext) *ProductListLogic {
  16. return &ProductListLogic{
  17. Logger: logx.WithContext(ctx),
  18. ctx: ctx,
  19. svcCtx: svcCtx,
  20. }
  21. }
  22. func (l *ProductListLogic) ProductList(req *types.ProductListReq) (resp *types.PageResp, err error) {
  23. page, pageSize := util.NormalizePage(req.Page, req.PageSize)
  24. list, total, err := l.svcCtx.SysProductModel.FindList(l.ctx, page, pageSize)
  25. if err != nil {
  26. return nil, err
  27. }
  28. caller := middleware.GetUserDetails(l.ctx)
  29. items := make([]types.ProductItem, 0, len(list))
  30. for _, p := range list {
  31. item := types.ProductItem{
  32. Id: p.Id,
  33. Code: p.Code,
  34. Name: p.Name,
  35. Remark: p.Remark,
  36. Status: p.Status,
  37. CreateTime: p.CreateTime,
  38. }
  39. if caller != nil && caller.IsSuperAdmin {
  40. item.AppKey = p.AppKey
  41. }
  42. items = append(items, item)
  43. }
  44. return &types.PageResp{
  45. Total: total,
  46. List: items,
  47. }, nil
  48. }