GoodsDeduct.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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\points;
  13. use app\common\library\helper;
  14. use app\api\model\Setting as SettingModel;
  15. use app\common\service\BaseService;
  16. /**
  17. * 订单积分抵扣服务类
  18. * Class GoodsDeduct
  19. * @package app\api\service\points
  20. */
  21. class GoodsDeduct extends BaseService
  22. {
  23. /**
  24. * 订单商品列表
  25. * @var array
  26. */
  27. private iterable $goodsList;
  28. /**
  29. * 构造方法
  30. * GoodsDeduct constructor.
  31. * @param iterable $goodsList
  32. */
  33. public function __construct(iterable $goodsList)
  34. {
  35. parent::__construct();
  36. $this->goodsList = $goodsList;
  37. }
  38. /**
  39. * 设置订单商品积分抵扣
  40. * @param $maxPointsNumCount
  41. * @param $actualPointsNum
  42. * @return bool
  43. */
  44. public function setGoodsPoints($maxPointsNumCount, $actualPointsNum): bool
  45. {
  46. // 计算实际积分抵扣数量
  47. $this->setGoodsListPointsNum($maxPointsNumCount, $actualPointsNum);
  48. // 总抵扣数量
  49. $totalPointsNum = (int)helper::getArrayColumnSum($this->goodsList, 'points_num');
  50. // 填充余数
  51. $this->setGoodsListPointsNumFill($actualPointsNum, $totalPointsNum);
  52. $this->setGoodsListPointsNumDiff($actualPointsNum, $totalPointsNum);
  53. // 计算实际积分抵扣金额
  54. $this->setGoodsListPointsMoney();
  55. return true;
  56. }
  57. /**
  58. * 计算实际积分抵扣数量
  59. * @param $maxPointsNumCount
  60. * @param $actualPointsNum
  61. */
  62. private function setGoodsListPointsNum($maxPointsNumCount, $actualPointsNum)
  63. {
  64. foreach ($this->goodsList as &$goods) {
  65. if (!$goods['is_points_discount']) continue;
  66. $goods['points_num'] = floor($goods['max_points_num'] / $maxPointsNumCount * $actualPointsNum);
  67. }
  68. }
  69. /**
  70. * 计算实际积分抵扣金额
  71. */
  72. private function setGoodsListPointsMoney()
  73. {
  74. $setting = SettingModel::getItem('points');
  75. foreach ($this->goodsList as &$goods) {
  76. if (!$goods['is_points_discount']) continue;
  77. $goods['points_money'] = helper::bcmul($goods['points_num'], $setting['discount']['discount_ratio']);
  78. }
  79. }
  80. private function setGoodsListPointsNumFill($actualPointsNum, $totalPointsNum): bool
  81. {
  82. if ($totalPointsNum === 0) {
  83. $temReducedMoney = $actualPointsNum;
  84. foreach ($this->goodsList as &$goods) {
  85. if (!$goods['is_points_discount']) continue;
  86. if ($temReducedMoney === 0) break;
  87. $goods['points_num'] = 1;
  88. $temReducedMoney--;
  89. }
  90. }
  91. return true;
  92. }
  93. private function setGoodsListPointsNumDiff($actualPointsNum, $totalPointsNum): bool
  94. {
  95. $tempDiff = $actualPointsNum - $totalPointsNum;
  96. foreach ($this->goodsList as &$goods) {
  97. if (!$goods['is_points_discount']) continue;
  98. if ($tempDiff < 1) break;
  99. $goods['points_num'] = $goods['points_num'] + 1;
  100. $tempDiff--;
  101. }
  102. return true;
  103. }
  104. }