GoodsDeduct.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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. $goods['total_price'] = helper::bcmul($goods['total_price'], 100, 0);
  30. $orderTotalPrice += $goods['total_price'];
  31. }
  32. // 计算实际抵扣金额
  33. $this->setActualReducedMoney($reducedMoney, $orderTotalPrice);
  34. // 实际抵扣金额为0,
  35. if ($this->actualReducedMoney > 0) {
  36. // 计算商品的价格权重
  37. $goodsList = $this->getGoodsListWeight($goodsList, $orderTotalPrice);
  38. // 计算商品优惠券抵扣金额
  39. $this->setGoodsListCouponMoney($goodsList);
  40. // 总抵扣金额
  41. $totalCouponMoney = helper::getArrayColumnSum($goodsList, 'coupon_money');
  42. $this->setGoodsListCouponMoneyFill($goodsList, $totalCouponMoney);
  43. $this->setGoodsListCouponMoneyDiff($goodsList, $totalCouponMoney);
  44. }
  45. return $goodsList;
  46. }
  47. public function getActualReducedMoney()
  48. {
  49. return $this->actualReducedMoney;
  50. }
  51. private function setActualReducedMoney($reducedMoney, $orderTotalPrice)
  52. {
  53. $reducedMoney *= 100;
  54. // $this->actualReducedMoney = ($reducedMoney >= $orderTotalPrice) ? $orderTotalPrice - 1 : $reducedMoney;
  55. $this->actualReducedMoney = ($reducedMoney >= $orderTotalPrice) ? $orderTotalPrice : $reducedMoney;
  56. }
  57. private function arraySortByWeight($goodsList)
  58. {
  59. return array_sort($goodsList, 'weight', true);
  60. }
  61. private function getGoodsListWeight($goodsList, $orderTotalPrice)
  62. {
  63. foreach ($goodsList as &$goods) {
  64. $goods['weight'] = $goods['total_price'] / $orderTotalPrice;
  65. }
  66. return $this->arraySortByWeight($goodsList);
  67. }
  68. private function setGoodsListCouponMoney(&$goodsList)
  69. {
  70. foreach ($goodsList as &$goods) {
  71. $goods['coupon_money'] = helper::bcmul($this->actualReducedMoney, $goods['weight'], 0);
  72. }
  73. return true;
  74. }
  75. private function setGoodsListCouponMoneyFill(&$goodsList, $totalCouponMoney)
  76. {
  77. if ($totalCouponMoney === 0) {
  78. $temReducedMoney = $this->actualReducedMoney;
  79. foreach ($goodsList as &$goods) {
  80. if ($temReducedMoney === 0) break;
  81. $goods['coupon_money'] = 1;
  82. $temReducedMoney--;
  83. }
  84. }
  85. return true;
  86. }
  87. private function setGoodsListCouponMoneyDiff(&$goodsList, $totalCouponMoney)
  88. {
  89. $tempDiff = $this->actualReducedMoney - $totalCouponMoney;
  90. foreach ($goodsList as &$goods) {
  91. if ($tempDiff < 1 || (int)$goods['total_price']<=(int)$goods['coupon_money']) continue;
  92. $goods['coupon_money']++ && $tempDiff--;
  93. }
  94. return true;
  95. }
  96. }