ShopPromotionDto.php 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. <?php
  2. namespace App\Module\Shop\Dtos;
  3. use App\Module\Shop\Models\ShopPromotion;
  4. use UCore\Dto\BaseDto;
  5. /**
  6. * 商店促销活动数据传输对象
  7. *
  8. * 用于在服务层和控制器层之间传递促销活动数据,避免直接暴露模型
  9. */
  10. class ShopPromotionDto extends BaseDto
  11. {
  12. /**
  13. * 促销活动ID
  14. *
  15. * @var int
  16. */
  17. public int $id;
  18. /**
  19. * 促销活动名称
  20. *
  21. * @var string
  22. */
  23. public string $name;
  24. /**
  25. * 促销活动描述
  26. *
  27. * @var string|null
  28. */
  29. public ?string $description;
  30. /**
  31. * 折扣类型(1:百分比折扣, 2:固定金额折扣)
  32. *
  33. * @var int
  34. */
  35. public int $discountType;
  36. /**
  37. * 折扣值
  38. *
  39. * @var int
  40. */
  41. public int $discountValue;
  42. /**
  43. * 是否激活
  44. *
  45. * @var bool
  46. */
  47. public bool $isActive;
  48. /**
  49. * 开始时间(时间戳)
  50. *
  51. * @var int|null
  52. */
  53. public ?int $startTime;
  54. /**
  55. * 结束时间(时间戳)
  56. *
  57. * @var int|null
  58. */
  59. public ?int $endTime;
  60. /**
  61. * 排序权重
  62. *
  63. * @var int
  64. */
  65. public int $sortOrder;
  66. /**
  67. * 创建时间
  68. *
  69. * @var string
  70. */
  71. public string $createdAt;
  72. /**
  73. * 更新时间
  74. *
  75. * @var string
  76. */
  77. public string $updatedAt;
  78. /**
  79. * 自定义折扣值(可选,来自促销商品关联表)
  80. *
  81. * @var int|null
  82. */
  83. public ?int $customDiscountValue = null;
  84. /**
  85. * 从模型创建DTO
  86. *
  87. * @param ShopPromotion $promotion 促销活动模型
  88. * @param int|null $customDiscountValue 自定义折扣值(可选)
  89. * @return self
  90. */
  91. public static function fromModel(ShopPromotion $promotion, ?int $customDiscountValue = null): self
  92. {
  93. $dto = new self();
  94. $dto->id = $promotion->id;
  95. $dto->name = $promotion->name;
  96. $dto->description = $promotion->description;
  97. $dto->discountType = $promotion->discount_type;
  98. $dto->discountValue = $promotion->discount_value;
  99. $dto->isActive = $promotion->is_active;
  100. $dto->startTime = $promotion->start_time ? $promotion->start_time->timestamp : null;
  101. $dto->endTime = $promotion->end_time ? $promotion->end_time->timestamp : null;
  102. $dto->sortOrder = $promotion->sort_order;
  103. $dto->createdAt = $promotion->created_at->toDateTimeString();
  104. $dto->updatedAt = $promotion->updated_at->toDateTimeString();
  105. $dto->customDiscountValue = $customDiscountValue;
  106. return $dto;
  107. }
  108. }