GoodsPriceManager.php 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. <?php
  2. namespace app\common\modules\goods;
  3. use app\common\helpers\Serializer;
  4. use app\common\models\Goods;
  5. use app\common\modules\goods\dealPrice\BaseDealPrice;
  6. class GoodsPriceManager
  7. {
  8. /**
  9. * @var Goods $goods
  10. */
  11. private $goods;
  12. private $detailPrice;
  13. public function __construct(Goods $goods)
  14. {
  15. $this->goods = $goods;
  16. }
  17. public function getDealPrice()
  18. {
  19. if (!isset($this->detailPrice)) {
  20. $this->detailPrice = $this->_getDealPrice();
  21. }
  22. return $this->detailPrice;
  23. }
  24. private function _getDealPrice()
  25. {
  26. $dealPrices = collect(\app\common\modules\shop\ShopConfig::current()->get('shop-foundation.goods.dealPrice'))->map(function (array $dealPriceStrategy) {
  27. return call_user_func($dealPriceStrategy['class'], $this->goods);
  28. });
  29. $dealPrices = $dealPrices->sortBy(function (BaseDealPrice $dealPrice) {
  30. return $dealPrice->getWeight();
  31. });
  32. /**
  33. * @var BaseDealPrice $dealPrice
  34. */
  35. $dealPrice = $dealPrices->first(function (BaseDealPrice $dealPrice) {
  36. return $dealPrice->enable();
  37. });
  38. return $dealPrice->getDealPrice();
  39. }
  40. }