BaseCartDiscount.php 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * Name: 芸众商城系统
  5. * Author: 广州市芸众信息科技有限公司
  6. * Profile: 广州市芸众信息科技有限公司位于国际商贸中心的广州,专注于移动电子商务生态系统打造,拥有芸众社交电商系统、区块链数字资产管理系统、供应链管理系统、电子合同等产品/服务。官网 :www.yunzmall.com www.yunzshop.com
  7. * Date: 2021/4/29
  8. * Time: 13:47
  9. */
  10. namespace app\frontend\modules\cart\discount;
  11. use app\frontend\modules\cart\discount\models\PreCartGoodsDiscount;
  12. use app\frontend\modules\cart\models\CartGoods;
  13. abstract class BaseCartDiscount
  14. {
  15. /**
  16. * @var CartGoods
  17. */
  18. protected $cartGoods;
  19. /**
  20. * 优惠名
  21. * @var string
  22. */
  23. protected $name;
  24. /**
  25. * 优惠码
  26. * @var
  27. */
  28. protected $code;
  29. /**
  30. * @var float
  31. */
  32. private $amount;
  33. protected $weight = 0;
  34. public function __construct(CartGoods $cartGoods)
  35. {
  36. $this->cartGoods = $cartGoods;
  37. }
  38. public function setWeight($weight)
  39. {
  40. $this->weight = $weight;
  41. }
  42. public function getWeight()
  43. {
  44. return $this->weight;
  45. }
  46. public function getCode()
  47. {
  48. return $this->code;
  49. }
  50. /**
  51. * 获取总金额
  52. * @return float|int
  53. */
  54. public function getAmount()
  55. {
  56. if (isset($this->amount)) {
  57. return $this->amount;
  58. }
  59. $this->amount = $this->_getAmount();
  60. if ($this->amount) {
  61. //bccomp($this->amount, 0,2) === 1
  62. // 将抵扣总金额保存在订单优惠信息表中
  63. $preCartGoodsDiscount = new PreCartGoodsDiscount([
  64. 'code' => $this->code,
  65. 'amount' => $this->amount ?: 0,
  66. 'name' => $this->name,
  67. ]);
  68. $preCartGoodsDiscount->setCartGoods($this->cartGoods);
  69. }
  70. return $this->amount ?: 0;
  71. }
  72. /**
  73. * @return float
  74. */
  75. abstract protected function _getAmount();
  76. }