GoodsDeduct.php 4.0 KB

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