BaseDiscount.php 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: shenyang
  5. * Date: 2018/5/23
  6. * Time: 下午3:55
  7. */
  8. namespace app\frontend\modules\orderGoods\discount;
  9. use app\frontend\models\orderGoods\PreOrderGoodsDiscount;
  10. use app\frontend\modules\order\models\PreOrder;
  11. use app\common\modules\orderGoods\models\PreOrderGoods;
  12. abstract class BaseDiscount
  13. {
  14. /**
  15. * @var PreOrder
  16. */
  17. protected $orderGoods;
  18. /**
  19. * 优惠名
  20. * @var string
  21. */
  22. protected $name;
  23. /**
  24. * 优惠码
  25. * @var
  26. */
  27. protected $code;
  28. /**
  29. * @var float
  30. */
  31. private $amount;
  32. protected $weight;
  33. public function __construct(PreOrderGoods $orderGoods)
  34. {
  35. $this->orderGoods = $orderGoods;
  36. }
  37. public function setWeight($weight)
  38. {
  39. $this->weight = $weight;
  40. }
  41. public function getWeight()
  42. {
  43. return $this->weight;
  44. }
  45. public function getCode()
  46. {
  47. return $this->code;
  48. }
  49. public function getName()
  50. {
  51. return $this->name;
  52. }
  53. /**
  54. * 获取总金额
  55. * @return float|int
  56. */
  57. public function getAmount()
  58. {
  59. if (isset($this->amount)) {
  60. return $this->amount;
  61. }
  62. $this->amount = $this->_getAmount();
  63. if ($this->amount) {
  64. // 将抵扣总金额保存在订单优惠信息表中
  65. $preOrderGoodsDiscount = new PreOrderGoodsDiscount([
  66. 'discount_code' => $this->getCode(),
  67. 'amount' => $this->amount ?: 0,
  68. 'name' => $this->getName(),
  69. ]);
  70. $preOrderGoodsDiscount->setOrderGoods($this->orderGoods);
  71. }
  72. return $this->amount ?: 0;
  73. }
  74. /**
  75. * @return bool
  76. */
  77. protected function orderDiscountCalculated()
  78. {
  79. return $this->orderGoods->order->getDiscount()->getAmountByCode($this->getCode())->calculated();
  80. }
  81. /**
  82. * @return float
  83. */
  84. abstract protected function _getAmount();
  85. }