GoodsDeduct.php 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | 萤火商城系统 [ 致力于通过产品和服务,帮助商家高效化开拓市场 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2017~2024 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 int $actualReducedMoney;
  27. // 订单商品列表
  28. private iterable $goodsList = [];
  29. // 优惠券信息
  30. private array $couponInfo = [];
  31. // 实际参与优惠券折扣的商品记录
  32. private array $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 = (int)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. $goodsKey = "{$goods['goods_id']}-{$goods['goods_sku_id']}";
  87. switch ($this->couponInfo['apply_range']) {
  88. case ApplyRangeEnum::ALL:
  89. $this->rangeGoodsList[$goodsKey] = $goods;
  90. break;
  91. case ApplyRangeEnum::SOME:
  92. if (in_array($goods['goods_id'], $this->couponInfo['apply_range_config']['applyGoodsIds'])) {
  93. $this->rangeGoodsList[$goodsKey] = $goods;
  94. }
  95. break;
  96. case ApplyRangeEnum::EXCLUDE:
  97. if (!in_array($goods['goods_id'], $this->couponInfo['apply_range_config']['excludedGoodsIds'])) {
  98. $this->rangeGoodsList[$goodsKey] = $goods;
  99. }
  100. break;
  101. default:
  102. break;
  103. }
  104. }
  105. }
  106. /**
  107. * 验证当前类属性
  108. * @throws BaseException
  109. */
  110. private function checkAttribute()
  111. {
  112. if (empty($this->goodsList) || empty($this->couponInfo)) {
  113. throwError('goodsList or couponInfo not found.');
  114. }
  115. }
  116. public function getActualReducedMoney(): int
  117. {
  118. return $this->actualReducedMoney;
  119. }
  120. /**
  121. * 计算实际抵扣的金额
  122. */
  123. private function setActualReducedMoney()
  124. {
  125. // 获取当前订单商品总额
  126. $orderTotalPrice = $this->getOrderTotalPrice();
  127. // 计算最大抵扣金额
  128. $reducedMoney = 0;
  129. foreach ($this->rangeGoodsList as $goods) {
  130. $reducedMoney += $goods['total_price'];
  131. }
  132. // 计算打折金额
  133. if ($this->couponInfo['coupon_type'] == CouponTypeEnum::DISCOUNT) {
  134. $reducePrice = $reducedMoney - ($reducedMoney * ($this->couponInfo['discount'] / 10));
  135. } else {
  136. $reducePrice = $this->couponInfo['reduce_price'] * 100;
  137. }
  138. // 优惠券最大允许抵扣到一分钱,所以此处判断抵扣金额大于等于订单金额时,减去一分钱
  139. $this->actualReducedMoney = ($reducePrice >= $orderTotalPrice) ? $orderTotalPrice - 1 : (int)$reducePrice;
  140. }
  141. /**
  142. * 获取当前订单商品总额
  143. * @return int
  144. */
  145. private function getOrderTotalPrice(): int
  146. {
  147. $orderTotalPrice = 0;
  148. foreach ($this->goodsList as $goods) {
  149. $orderTotalPrice += (int)($goods['total_price'] * 100);
  150. }
  151. return $orderTotalPrice;
  152. }
  153. /**
  154. * 计算商品抵扣的权重(占比)
  155. */
  156. private function setGoodsListWeight()
  157. {
  158. $orderTotalPrice = helper::getArrayColumnSum($this->rangeGoodsList, 'total_price');
  159. foreach ($this->rangeGoodsList as &$goods) {
  160. $weight = round($goods['total_price'] / $orderTotalPrice, 6);
  161. $goods['weight'] = helper::scToStr((string)$weight, 6);
  162. }
  163. array_sort($this->rangeGoodsList, 'weight', true);
  164. }
  165. /**
  166. * 计算商品抵扣的金额
  167. */
  168. private function setGoodsListCouponMoney(): void
  169. {
  170. foreach ($this->rangeGoodsList as &$goods) {
  171. $goods['coupon_money'] = helper::bcmul($this->actualReducedMoney, $goods['weight'], 0);
  172. }
  173. }
  174. private function setGoodsListCouponMoneyFill(int $assignedCouponMoney): void
  175. {
  176. if ($assignedCouponMoney === 0) {
  177. $temReducedMoney = $this->actualReducedMoney;
  178. foreach ($this->rangeGoodsList as &$goods) {
  179. if ($temReducedMoney == 0) break;
  180. $goods['coupon_money'] = 1;
  181. $temReducedMoney--;
  182. }
  183. }
  184. }
  185. private function setGoodsListCouponMoneyDiff(int $assignedCouponMoney): void
  186. {
  187. // 剩余未抵扣金额 = 实际抵扣金额 - 总抵扣金额 (已分配的)
  188. $tempDiff = $this->actualReducedMoney - $assignedCouponMoney;
  189. foreach ($this->rangeGoodsList as &$goods) {
  190. if ($tempDiff < 1) break;
  191. // 当抵扣金额大于商品总价时不再累积抵扣
  192. if ($goods['coupon_money'] >= $goods['total_price']) continue;
  193. $goods['coupon_money']++;
  194. $tempDiff--;
  195. }
  196. }
  197. }