GoodsDeduct.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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\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. private $goodsList;
  24. /**
  25. * 构造方法
  26. * GoodsDeduct constructor.
  27. * @param $goodsList
  28. */
  29. public function __construct($goodsList)
  30. {
  31. parent::__construct();
  32. $this->goodsList = $goodsList;
  33. }
  34. public function setGoodsPoints($maxPointsNumCount, $actualPointsNum)
  35. {
  36. // 计算实际积分抵扣数量
  37. $this->setGoodsListPointsNum($maxPointsNumCount, $actualPointsNum);
  38. // 总抵扣数量
  39. $totalPointsNum = helper::getArrayColumnSum($this->goodsList, 'points_num');
  40. // 填充余数
  41. $this->setGoodsListPointsNumFill($actualPointsNum, $totalPointsNum);
  42. $this->setGoodsListPointsNumDiff($actualPointsNum, $totalPointsNum);
  43. // 计算实际积分抵扣金额
  44. $this->setGoodsListPointsMoney();
  45. return true;
  46. }
  47. /**
  48. * 计算实际积分抵扣数量
  49. * @param $maxPointsNumCount
  50. * @param $actualPointsNum
  51. */
  52. private function setGoodsListPointsNum($maxPointsNumCount, $actualPointsNum)
  53. {
  54. foreach ($this->goodsList as &$goods) {
  55. if (!$goods['is_points_discount']) continue;
  56. $goods['points_num'] = floor($goods['max_points_num'] / $maxPointsNumCount * $actualPointsNum);
  57. }
  58. }
  59. /**
  60. * 计算实际积分抵扣金额
  61. */
  62. private function setGoodsListPointsMoney()
  63. {
  64. $setting = SettingModel::getItem('points');
  65. foreach ($this->goodsList as &$goods) {
  66. if (!$goods['is_points_discount']) continue;
  67. $goods['points_money'] = helper::bcmul($goods['points_num'], $setting['discount']['discount_ratio']);
  68. }
  69. }
  70. private function setGoodsListPointsNumFill($actualPointsNum, $totalPointsNum)
  71. {
  72. if ($totalPointsNum === 0) {
  73. $temReducedMoney = $actualPointsNum;
  74. foreach ($this->goodsList as &$goods) {
  75. if (!$goods['is_points_discount']) continue;
  76. if ($temReducedMoney === 0) break;
  77. $goods['points_num'] = 1;
  78. $temReducedMoney--;
  79. }
  80. }
  81. return true;
  82. }
  83. private function setGoodsListPointsNumDiff($actualPointsNum, $totalPointsNum)
  84. {
  85. $tempDiff = $actualPointsNum - $totalPointsNum;
  86. foreach ($this->goodsList as &$goods) {
  87. if (!$goods['is_points_discount']) continue;
  88. if ($tempDiff < 1) break;
  89. $goods['points_num'] = $goods['points_num'] + 1;
  90. $tempDiff--;
  91. }
  92. return true;
  93. }
  94. }