PriceNodeTrait.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: shenyang
  5. * Date: 2019/1/23
  6. * Time: 10:03 AM
  7. */
  8. namespace app\frontend\modules\order;
  9. use app\common\exceptions\AppException;
  10. use Illuminate\Support\Collection;
  11. trait PriceNodeTrait
  12. {
  13. /**
  14. * @var Collection
  15. */
  16. public $priceCache;
  17. /**
  18. * @var Collection
  19. */
  20. private $priceNodes;
  21. /**
  22. * @return Collection
  23. */
  24. public function getPriceNodes()
  25. {
  26. if (!isset($this->priceNodes)) {
  27. $this->priceNodes = $this->_getPriceNodes();
  28. }
  29. return $this->priceNodes;
  30. }
  31. /**
  32. * 获取某个节点之后的价格
  33. * @param $key
  34. * @return mixed
  35. * @throws AppException
  36. */
  37. public function getPriceAfter($key)
  38. {
  39. if (!isset($this->priceCache[$key])) {
  40. // 找到对应的节点
  41. $priceNode = $this->getPriceNodes()->first(function (PriceNode $priceNode) use ($key) {
  42. return $priceNode->getKey() == $key;
  43. });
  44. if (!$priceNode) {
  45. throw new AppException("不存在的价格节点{$key}");
  46. }
  47. $this->priceCache[$key] = $priceNode->getPrice();
  48. }
  49. return $this->priceCache[$key];
  50. }
  51. public function getCurrentPrice()
  52. {
  53. if(!is_array($this->priceCache)){
  54. return $this->getPriceNodes()->first()->getPrice();
  55. }
  56. return array_last($this->priceCache);
  57. }
  58. /**
  59. * 获取某个节点之前的价格
  60. * @param $key
  61. * @return mixed
  62. * @throws AppException
  63. */
  64. public function getPriceBefore($key)
  65. {
  66. $nodeKey = '';
  67. $hasKey = false;
  68. foreach ($this->getPriceNodes() as $priceNode) {
  69. if ($priceNode->getKey() == $key) {
  70. $hasKey = true;
  71. break;
  72. }
  73. $nodeKey = $priceNode->getKey();
  74. }
  75. if(!$hasKey){
  76. throw new \Exception("没有key为{$key}的节点");
  77. }
  78. if (empty($nodeKey)) {
  79. throw new \Exception("没有{$key}更先计算的节点了");
  80. }
  81. return $this->getPriceAfter($nodeKey);
  82. }
  83. public function getPriceBeforeWeight($key)
  84. {
  85. $weight = 0;
  86. foreach ($this->getPriceNodes() as $priceNode) {
  87. //todo 主要是第一个抵扣会忽悠当前节点前一个节点的金额,所以这里返回当前节点排序
  88. //todo 使用大于判断会忽略与当前节点同一种排序数值的金额
  89. $weight = $priceNode->getWeight();
  90. if ($priceNode->getKey() == $key) {
  91. break;
  92. }
  93. // $weight = $priceNode->getWeight();
  94. }
  95. $nodeKey = $this->getPriceNodes()->filter(function ($priceNode) use ($weight) {
  96. return $priceNode->getWeight() < $weight;
  97. })->last()->getKey();
  98. if (empty($nodeKey)) {
  99. throw new \Exception("没有比{$weight}权重更小节点了");
  100. }
  101. return $this->getPriceAfter($nodeKey);
  102. }
  103. /**
  104. * 验证某个节点是否已存在
  105. * @param $key
  106. * @return bool true 存在 false 不存在
  107. */
  108. public function verifyPriceNodes($key)
  109. {
  110. // 找到对应的节点
  111. $priceNode = $this->getPriceNodes()->first(function (PriceNode $priceNode) use ($key) {
  112. return $priceNode->getKey() == $key;
  113. });
  114. if (!$priceNode) {
  115. return false;
  116. }
  117. return true;
  118. }
  119. }