GoodsDeduct.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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\service\BaseService;
  14. use app\common\library\helper;
  15. /**
  16. * 订单优惠券折扣服务类
  17. * Class GoodsDeduct
  18. * @package app\api\service\coupon
  19. */
  20. class GoodsDeduct extends BaseService
  21. {
  22. private $actualReducedMoney;
  23. public function setGoodsCouponMoney($goodsList, $reducedMoney)
  24. {
  25. // 统计订单商品总金额,(单位分)
  26. $orderTotalPrice = 0;
  27. foreach ($goodsList as &$goods) {
  28. $goods['total_price'] *= 100;
  29. $orderTotalPrice += $goods['total_price'];
  30. }
  31. // 计算实际抵扣金额
  32. $this->setActualReducedMoney($reducedMoney, $orderTotalPrice);
  33. // 实际抵扣金额为0,
  34. if ($this->actualReducedMoney > 0) {
  35. // 计算商品的价格权重
  36. $goodsList = $this->getGoodsListWeight($goodsList, $orderTotalPrice);
  37. // 计算商品优惠券抵扣金额
  38. $this->setGoodsListCouponMoney($goodsList);
  39. // 总抵扣金额
  40. $totalCouponMoney = helper::getArrayColumnSum($goodsList, 'coupon_money');
  41. $this->setGoodsListCouponMoneyFill($goodsList, $totalCouponMoney);
  42. $this->setGoodsListCouponMoneyDiff($goodsList, $totalCouponMoney);
  43. }
  44. return $goodsList;
  45. }
  46. public function getActualReducedMoney()
  47. {
  48. return $this->actualReducedMoney;
  49. }
  50. private function setActualReducedMoney($reducedMoney, $orderTotalPrice)
  51. {
  52. $reducedMoney *= 100;
  53. $this->actualReducedMoney = ($reducedMoney >= $orderTotalPrice) ? $orderTotalPrice - 1 : $reducedMoney;
  54. }
  55. private function arraySortByWeight($goodsList)
  56. {
  57. return array_sort($goodsList, 'weight', true);
  58. }
  59. private function getGoodsListWeight($goodsList, $orderTotalPrice)
  60. {
  61. foreach ($goodsList as &$goods) {
  62. $goods['weight'] = $goods['total_price'] / $orderTotalPrice;
  63. }
  64. return $this->arraySortByWeight($goodsList);
  65. }
  66. private function setGoodsListCouponMoney(&$goodsList)
  67. {
  68. foreach ($goodsList as &$goods) {
  69. $goods['coupon_money'] = helper::bcmul($this->actualReducedMoney, $goods['weight'], 0);
  70. }
  71. return true;
  72. }
  73. private function setGoodsListCouponMoneyFill(&$goodsList, $totalCouponMoney)
  74. {
  75. if ($totalCouponMoney === 0) {
  76. $temReducedMoney = $this->actualReducedMoney;
  77. foreach ($goodsList as &$goods) {
  78. if ($temReducedMoney === 0) break;
  79. $goods['coupon_money'] = 1;
  80. $temReducedMoney--;
  81. }
  82. }
  83. return true;
  84. }
  85. private function setGoodsListCouponMoneyDiff(&$goodsList, $totalCouponMoney)
  86. {
  87. $tempDiff = $this->actualReducedMoney - $totalCouponMoney;
  88. foreach ($goodsList as &$goods) {
  89. if ($tempDiff < 1) break;
  90. $goods['coupon_money']++ && $tempDiff--;
  91. }
  92. return true;
  93. }
  94. }