GoodsOption.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * Author: 芸众商城 www.yunzshop.com
  5. * Date: 2017/4/14
  6. * Time: 下午10:57
  7. */
  8. namespace app\frontend\models;
  9. use app\common\modules\discount\GoodsMemberLevelDiscount;
  10. use app\common\modules\goodsOption\GoodsOptionPriceManager;
  11. use app\frontend\modules\orderGoods\price\adapter\GoodsOptionPriceAdapter;
  12. /**
  13. * Class GoodsOption
  14. * @package app\frontend\models
  15. * @property int id
  16. * @property int goods_id
  17. * @property string title
  18. * @property float weight
  19. * @property float product_price
  20. * @property float market_price
  21. * @property float cost_price
  22. * @property float deal_price
  23. * @property Goods goods
  24. */
  25. class GoodsOption extends \app\common\models\GoodsOption
  26. {
  27. private $dealPrice;
  28. protected $vipDiscountAmount;
  29. public $vipDiscountLog;
  30. private $priceManager;
  31. //public $appends = ['vip_price'];
  32. public function getPriceManager()
  33. {
  34. if (!isset($this->priceManager)) {
  35. $this->priceManager = new GoodsOptionPriceManager($this);
  36. }
  37. return $this->priceManager;
  38. }
  39. /**
  40. * 获取交易价(实际参与交易的商品价格)
  41. * @return float|int
  42. * @throws \app\common\exceptions\MemberNotLoginException
  43. */
  44. public function getDealPriceAttribute()
  45. {
  46. if (!isset($this->dealPrice)) {
  47. $this->dealPrice = $this->getPriceManager()->getDealPrice();
  48. }
  49. return $this->dealPrice;
  50. }
  51. /**
  52. * @var GoodsMemberLevelDiscount
  53. */
  54. private $memberLevelDiscount;
  55. /**
  56. * @return GoodsMemberLevelDiscount
  57. * @throws \app\common\exceptions\MemberNotLoginException
  58. */
  59. public function memberLevelDiscount()
  60. {
  61. if (!isset($this->memberLevelDiscount)) {
  62. $this->memberLevelDiscount = new GoodsMemberLevelDiscount($this->goods, Member::current());
  63. }
  64. return $this->memberLevelDiscount;
  65. }
  66. /**
  67. * 缓存等级折金额
  68. * todo 如何解决等级优惠种类记录的问题
  69. * @param $price
  70. * @return float
  71. * @throws \app\common\exceptions\MemberNotLoginException
  72. */
  73. public function getVipDiscountAmount($price)
  74. {
  75. if (isset($this->vipDiscountAmount)) {
  76. return $this->vipDiscountAmount;
  77. }
  78. $this->vipDiscountAmount = $this->memberLevelDiscount()->getAmount($price);
  79. $this->vipDiscountLog = $this->memberLevelDiscount()->getLog($this->vipDiscountAmount);
  80. return $this->vipDiscountAmount;
  81. }
  82. //todo blank 商品价格适配器
  83. public function getGoodsOptionPriceAdapter()
  84. {
  85. return new GoodsOptionPriceAdapter($this);
  86. }
  87. /**
  88. * 获取商品的会员价格
  89. * @return float|int|mixed
  90. * @throws \app\common\exceptions\MemberNotLoginException
  91. */
  92. public function getVipPriceAttribute()
  93. {
  94. return sprintf('%.2f', $this->deal_price - $this->getVipDiscountAmount($this->getGoodsOptionPriceAdapter()));
  95. }
  96. public function goods()
  97. {
  98. return $this->belongsTo(Goods::class);
  99. }
  100. }