GoodsDeduct.php 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  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 int
  80. */
  81. public function getActualReducedMoney(): int
  82. {
  83. return $this->actualReducedMoney;
  84. }
  85. /**
  86. * 设置实际参与优惠券折扣的商品记录
  87. * @return void
  88. */
  89. private function setRangeGoodsList()
  90. {
  91. $this->rangeGoodsList = [];
  92. foreach ($this->goodsList as $goods) {
  93. $goods['total_price'] *= 100;
  94. $goodsKey = "{$goods['goods_id']}-{$goods['goods_sku_id']}";
  95. switch ($this->couponInfo['apply_range']) {
  96. case ApplyRangeEnum::ALL:
  97. $this->rangeGoodsList[$goodsKey] = $goods;
  98. break;
  99. case ApplyRangeEnum::SOME:
  100. if (\in_array($goods['goods_id'], $this->couponInfo['apply_range_config']['goodsIds'])) {
  101. $this->rangeGoodsList[$goodsKey] = $goods;
  102. }
  103. break;
  104. case ApplyRangeEnum::EXCLUDE:
  105. if (!\in_array($goods['goods_id'], $this->couponInfo['apply_range_config']['goodsIds'])) {
  106. $this->rangeGoodsList[$goodsKey] = $goods;
  107. }
  108. break;
  109. default:
  110. break;
  111. }
  112. }
  113. }
  114. /**
  115. * 验证当前类属性
  116. * @throws BaseException
  117. */
  118. private function checkAttribute()
  119. {
  120. if (empty($this->goodsList) || empty($this->couponInfo)) {
  121. throwError('goodsList or couponInfo not found.');
  122. }
  123. }
  124. /**
  125. * 计算实际抵扣的金额
  126. */
  127. private function setActualReducedMoney()
  128. {
  129. // 获取当前订单商品总额
  130. $orderTotalPrice = $this->getOrderTotalPrice();
  131. // 计算打折金额
  132. if ($this->couponInfo['coupon_type'] == CouponTypeEnum::DISCOUNT) {
  133. $reducePrice = $orderTotalPrice - ($orderTotalPrice * ($this->couponInfo['discount'] / 10));
  134. } else {
  135. $reducePrice = $this->couponInfo['reduce_price'] * 100;
  136. }
  137. // 优惠券最大允许抵扣到一分钱,所以此处判断抵扣金额大于等于订单金额时,减去一分钱
  138. $this->actualReducedMoney = ($reducePrice >= $orderTotalPrice) ? $orderTotalPrice - 1 : (int)$reducePrice;
  139. }
  140. /**
  141. * 获取当前订单商品总额
  142. * @return int
  143. */
  144. private function getOrderTotalPrice(): int
  145. {
  146. $orderTotalPrice = 0;
  147. foreach ($this->goodsList as $goods) {
  148. $orderTotalPrice += (int)($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. $weight = \round($goods['total_price'] / $orderTotalPrice, 6);
  160. $goods['weight'] = helper::scToStr((string)$weight, 6);
  161. }
  162. \array_sort($this->rangeGoodsList, 'weight', true);
  163. }
  164. /**
  165. * 计算商品抵扣的金额
  166. */
  167. private function setGoodsListCouponMoney(): void
  168. {
  169. foreach ($this->rangeGoodsList as &$goods) {
  170. $goods['coupon_money'] = helper::bcmul($this->actualReducedMoney, $goods['weight'], 0);
  171. }
  172. }
  173. private function setGoodsListCouponMoneyFill(int $assignedCouponMoney): void
  174. {
  175. if ($assignedCouponMoney === 0) {
  176. $temReducedMoney = $this->actualReducedMoney;
  177. foreach ($this->rangeGoodsList as &$goods) {
  178. if ($temReducedMoney == 0) break;
  179. $goods['coupon_money'] = 1;
  180. $temReducedMoney--;
  181. }
  182. }
  183. }
  184. private function setGoodsListCouponMoneyDiff(int $assignedCouponMoney): void
  185. {
  186. // 剩余未抵扣金额 = 实际抵扣金额 - 总抵扣金额 (已分配的)
  187. $tempDiff = $this->actualReducedMoney - $assignedCouponMoney;
  188. foreach ($this->rangeGoodsList as &$goods) {
  189. if ($tempDiff < 1) break;
  190. // 当抵扣金额大于商品总价时不再累积抵扣
  191. if ($goods['coupon_money'] >= $goods['total_price']) continue;
  192. $goods['coupon_money']++;
  193. $tempDiff--;
  194. }
  195. }
  196. }