GoodsDeduct.php 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | 萤火商城系统 [ 致力于通过产品和服务,帮助商家高效化开拓市场 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2017~2021 https://www.yiovo.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed 这不是一个自由软件,不允许对程序代码以任何形式任何目的的再发行
  8. // +----------------------------------------------------------------------
  9. // | Author: 萤火科技 <admin@yiovo.com>
  10. // +----------------------------------------------------------------------
  11. declare (strict_types=1);
  12. namespace app\api\service\coupon;
  13. use app\common\enum\coupon\ApplyRange as ApplyRangeEnum;
  14. use app\common\enum\coupon\CouponType as CouponTypeEnum;
  15. use app\common\service\BaseService;
  16. use app\common\library\helper;
  17. use cores\exception\BaseException;
  18. /**
  19. * 订单优惠券折扣服务类
  20. * Class GoodsDeduct
  21. * @package app\api\service\coupon
  22. */
  23. class GoodsDeduct extends BaseService
  24. {
  25. // 实际抵扣金额
  26. private $actualReducedMoney;
  27. // 订单商品列表
  28. private $goodsList = [];
  29. // 优惠券信息
  30. private $couponInfo = [];
  31. // 实际参与优惠券折扣的商品记录
  32. private $rangeGoodsList = [];
  33. // 设置订单商品列表
  34. public function setGoodsList(iterable $goodsList): GoodsDeduct
  35. {
  36. $this->goodsList = $goodsList;
  37. return $this;
  38. }
  39. // 设置优惠券信息
  40. public function setCouponInfo(array $couponInfo): GoodsDeduct
  41. {
  42. $this->couponInfo = $couponInfo;
  43. return $this;
  44. }
  45. /**
  46. * 计算优惠券抵扣金额
  47. * @return GoodsDeduct
  48. * @throws BaseException
  49. */
  50. public function setGoodsCouponMoney(): GoodsDeduct
  51. {
  52. // 验证当前类属性
  53. $this->checkAttribute();
  54. // 设置实际参与优惠券折扣的商品记录
  55. $this->setRangeGoodsList();
  56. // 计算实际抵扣的金额
  57. $this->setActualReducedMoney();
  58. // 实际抵扣金额为0
  59. if ($this->actualReducedMoney > 0) {
  60. // 计算商品的价格权重
  61. $this->setGoodsListWeight();
  62. // 计算商品优惠券抵扣金额
  63. $this->setGoodsListCouponMoney();
  64. // 总抵扣金额 (已分配的)
  65. $assignedCouponMoney = helper::getArrayColumnSum($this->rangeGoodsList, 'coupon_money');
  66. // 分配剩余的抵扣金额
  67. $this->setGoodsListCouponMoneyFill($assignedCouponMoney);
  68. $this->setGoodsListCouponMoneyDiff($assignedCouponMoney);
  69. }
  70. return $this;
  71. }
  72. // 获取实际参与优惠券折扣的商品记录
  73. public function getRangeGoodsList(): array
  74. {
  75. return $this->rangeGoodsList;
  76. }
  77. /**
  78. * 设置实际参与优惠券折扣的商品记录
  79. * @return void
  80. */
  81. private function setRangeGoodsList()
  82. {
  83. $this->rangeGoodsList = [];
  84. foreach ($this->goodsList as $goods) {
  85. $goods['total_price'] *= 100;
  86. switch ($this->couponInfo['apply_range']) {
  87. case ApplyRangeEnum::ALL:
  88. $this->rangeGoodsList[$goods['goods_id']] = $goods;
  89. break;
  90. case ApplyRangeEnum::SOME:
  91. if (in_array($goods['goods_id'], $this->couponInfo['apply_range_config']['applyGoodsIds'])) {
  92. $this->rangeGoodsList[$goods['goods_id']] = $goods;
  93. }
  94. break;
  95. case ApplyRangeEnum::EXCLUDE:
  96. if (!in_array($goods['goods_id'], $this->couponInfo['apply_range_config']['excludedGoodsIds'])) {
  97. $this->rangeGoodsList[$goods['goods_id']] = $goods;
  98. }
  99. break;
  100. default:
  101. break;
  102. }
  103. }
  104. }
  105. /**
  106. * 验证当前类属性
  107. * @throws BaseException
  108. */
  109. private function checkAttribute()
  110. {
  111. if (empty($this->goodsList) || empty($this->couponInfo)) {
  112. throwError('goodsList or couponInfo not found.');
  113. }
  114. }
  115. public function getActualReducedMoney()
  116. {
  117. return $this->actualReducedMoney;
  118. }
  119. /**
  120. * 计算实际抵扣的金额
  121. */
  122. private function setActualReducedMoney()
  123. {
  124. // 获取当前订单商品总额
  125. $orderTotalPrice = $this->getOrderTotalPrice();
  126. // 计算最大抵扣金额
  127. $reducedMoney = 0;
  128. foreach ($this->rangeGoodsList as $goods) {
  129. $reducedMoney += $goods['total_price'];
  130. }
  131. // 计算打折金额
  132. if ($this->couponInfo['coupon_type'] == CouponTypeEnum::DISCOUNT) {
  133. $reducePrice = $reducedMoney - ($reducedMoney * ($this->couponInfo['discount'] / 10));
  134. } else {
  135. $reducePrice = $this->couponInfo['reduce_price'] * 100;
  136. }
  137. // 优惠券最大允许抵扣到一分钱,所以此处判断抵扣金额大于等于订单金额时,减去一分钱
  138. $this->actualReducedMoney = ($reducePrice >= $orderTotalPrice) ? $orderTotalPrice - 1 : $reducePrice;
  139. }
  140. /**
  141. * 获取当前订单商品总额
  142. * @return float|int
  143. */
  144. private function getOrderTotalPrice()
  145. {
  146. $orderTotalPrice = 0;
  147. foreach ($this->goodsList as $goods) {
  148. $orderTotalPrice += ($goods['total_price'] * 100);
  149. }
  150. return $orderTotalPrice;
  151. }
  152. /**
  153. * 计算商品抵扣的权重(占比)
  154. */
  155. private function setGoodsListWeight()
  156. {
  157. $orderTotalPrice = helper::getArrayColumnSum($this->rangeGoodsList, 'total_price');
  158. foreach ($this->rangeGoodsList as &$goods) {
  159. $goods['weight'] = $goods['total_price'] / $orderTotalPrice;
  160. }
  161. array_sort($this->rangeGoodsList, 'weight', true);
  162. }
  163. /**
  164. * 计算商品抵扣的金额
  165. */
  166. private function setGoodsListCouponMoney(): void
  167. {
  168. foreach ($this->rangeGoodsList as &$goods) {
  169. $goods['coupon_money'] = helper::bcmul($this->actualReducedMoney, $goods['weight'], 0);
  170. }
  171. }
  172. private function setGoodsListCouponMoneyFill($assignedCouponMoney): void
  173. {
  174. if ($assignedCouponMoney === 0) {
  175. $temReducedMoney = $this->actualReducedMoney;
  176. foreach ($this->rangeGoodsList as &$goods) {
  177. if ($temReducedMoney === 0) break;
  178. $goods['coupon_money'] = 1;
  179. $temReducedMoney--;
  180. }
  181. }
  182. }
  183. private function setGoodsListCouponMoneyDiff($assignedCouponMoney): void
  184. {
  185. $tempDiff = $this->actualReducedMoney - $assignedCouponMoney;
  186. foreach ($this->rangeGoodsList as &$goods) {
  187. if ($tempDiff < 1) break;
  188. $goods['coupon_money']++ && $tempDiff--;
  189. }
  190. }
  191. }