|
|
@@ -12,8 +12,11 @@ declare (strict_types=1);
|
|
|
|
|
|
namespace app\api\service\coupon;
|
|
|
|
|
|
+use app\common\enum\coupon\ApplyRange as ApplyRangeEnum;
|
|
|
+use app\common\enum\coupon\CouponType as CouponTypeEnum;
|
|
|
use app\common\service\BaseService;
|
|
|
use app\common\library\helper;
|
|
|
+use cores\exception\BaseException;
|
|
|
|
|
|
/**
|
|
|
* 订单优惠券折扣服务类
|
|
|
@@ -22,30 +25,104 @@ use app\common\library\helper;
|
|
|
*/
|
|
|
class GoodsDeduct extends BaseService
|
|
|
{
|
|
|
+ // 实际抵扣金额
|
|
|
private $actualReducedMoney;
|
|
|
|
|
|
- public function setGoodsCouponMoney($goodsList, $reducedMoney)
|
|
|
+ // 订单商品列表
|
|
|
+ private $goodsList = [];
|
|
|
+
|
|
|
+ // 优惠券信息
|
|
|
+ private $couponInfo = [];
|
|
|
+
|
|
|
+ // 实际参与优惠券折扣的商品记录
|
|
|
+ private $rangeGoodsList = [];
|
|
|
+
|
|
|
+ // 设置订单商品列表
|
|
|
+ public function setGoodsList(iterable $goodsList): GoodsDeduct
|
|
|
{
|
|
|
- // 统计订单商品总金额,(单位分)
|
|
|
- $orderTotalPrice = 0;
|
|
|
- foreach ($goodsList as &$goods) {
|
|
|
- $goods['total_price'] *= 100;
|
|
|
- $orderTotalPrice += $goods['total_price'];
|
|
|
- }
|
|
|
- // 计算实际抵扣金额
|
|
|
- $this->setActualReducedMoney($reducedMoney, $orderTotalPrice);
|
|
|
- // 实际抵扣金额为0,
|
|
|
+ $this->goodsList = $goodsList;
|
|
|
+ return $this;
|
|
|
+ }
|
|
|
+
|
|
|
+ // 设置优惠券信息
|
|
|
+ public function setCouponInfo(array $couponInfo): GoodsDeduct
|
|
|
+ {
|
|
|
+ $this->couponInfo = $couponInfo;
|
|
|
+ return $this;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 计算优惠券抵扣金额
|
|
|
+ * @return GoodsDeduct
|
|
|
+ * @throws BaseException
|
|
|
+ */
|
|
|
+ public function setGoodsCouponMoney(): GoodsDeduct
|
|
|
+ {
|
|
|
+ // 验证当前类属性
|
|
|
+ $this->checkAttribute();
|
|
|
+ // 设置实际参与优惠券折扣的商品记录
|
|
|
+ $this->setRangeGoodsList();
|
|
|
+ // 计算实际抵扣的金额
|
|
|
+ $this->setActualReducedMoney();
|
|
|
+ // 实际抵扣金额为0
|
|
|
if ($this->actualReducedMoney > 0) {
|
|
|
// 计算商品的价格权重
|
|
|
- $goodsList = $this->getGoodsListWeight($goodsList, $orderTotalPrice);
|
|
|
+ $this->setGoodsListWeight();
|
|
|
// 计算商品优惠券抵扣金额
|
|
|
- $this->setGoodsListCouponMoney($goodsList);
|
|
|
- // 总抵扣金额
|
|
|
- $totalCouponMoney = helper::getArrayColumnSum($goodsList, 'coupon_money');
|
|
|
- $this->setGoodsListCouponMoneyFill($goodsList, $totalCouponMoney);
|
|
|
- $this->setGoodsListCouponMoneyDiff($goodsList, $totalCouponMoney);
|
|
|
+ $this->setGoodsListCouponMoney();
|
|
|
+ // 总抵扣金额 (已分配的)
|
|
|
+ $assignedCouponMoney = helper::getArrayColumnSum($this->rangeGoodsList, 'coupon_money');
|
|
|
+ // 分配剩余的抵扣金额
|
|
|
+ $this->setGoodsListCouponMoneyFill($assignedCouponMoney);
|
|
|
+ $this->setGoodsListCouponMoneyDiff($assignedCouponMoney);
|
|
|
+ }
|
|
|
+ return $this;
|
|
|
+ }
|
|
|
+
|
|
|
+ // 获取实际参与优惠券折扣的商品记录
|
|
|
+ public function getRangeGoodsList(): array
|
|
|
+ {
|
|
|
+ return $this->rangeGoodsList;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 设置实际参与优惠券折扣的商品记录
|
|
|
+ * @return void
|
|
|
+ */
|
|
|
+ private function setRangeGoodsList()
|
|
|
+ {
|
|
|
+ $this->rangeGoodsList = [];
|
|
|
+ foreach ($this->goodsList as $goods) {
|
|
|
+ $goods['total_price'] *= 100;
|
|
|
+ switch ($this->couponInfo['apply_range']) {
|
|
|
+ case ApplyRangeEnum::ALL:
|
|
|
+ $this->rangeGoodsList[$goods['goods_id']] = $goods;
|
|
|
+ break;
|
|
|
+ case ApplyRangeEnum::SOME:
|
|
|
+ if (in_array($goods['goods_id'], $this->couponInfo['apply_range_config']['applyGoodsIds'])) {
|
|
|
+ $this->rangeGoodsList[$goods['goods_id']] = $goods;
|
|
|
+ }
|
|
|
+ break;
|
|
|
+ case ApplyRangeEnum::EXCLUDE:
|
|
|
+ if (!in_array($goods['goods_id'], $this->couponInfo['apply_range_config']['excludedGoodsIds'])) {
|
|
|
+ $this->rangeGoodsList[$goods['goods_id']] = $goods;
|
|
|
+ }
|
|
|
+ break;
|
|
|
+ default:
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 验证当前类属性
|
|
|
+ * @throws BaseException
|
|
|
+ */
|
|
|
+ private function checkAttribute()
|
|
|
+ {
|
|
|
+ if (empty($this->goodsList) || empty($this->couponInfo)) {
|
|
|
+ throwError('goodsList or couponInfo not found.');
|
|
|
}
|
|
|
- return $goodsList;
|
|
|
}
|
|
|
|
|
|
public function getActualReducedMoney()
|
|
|
@@ -53,54 +130,81 @@ class GoodsDeduct extends BaseService
|
|
|
return $this->actualReducedMoney;
|
|
|
}
|
|
|
|
|
|
- private function setActualReducedMoney($reducedMoney, $orderTotalPrice)
|
|
|
+ /**
|
|
|
+ * 计算实际抵扣的金额
|
|
|
+ */
|
|
|
+ private function setActualReducedMoney()
|
|
|
{
|
|
|
- $reducedMoney *= 100;
|
|
|
- $this->actualReducedMoney = ($reducedMoney >= $orderTotalPrice) ? $orderTotalPrice - 1 : $reducedMoney;
|
|
|
+ // 获取当前订单商品总额
|
|
|
+ $orderTotalPrice = $this->getOrderTotalPrice();
|
|
|
+ // 计算最大抵扣金额
|
|
|
+ $reducedMoney = 0;
|
|
|
+ foreach ($this->rangeGoodsList as $goods) {
|
|
|
+ $reducedMoney += $goods['total_price'];
|
|
|
+ }
|
|
|
+ // 计算打折金额
|
|
|
+ if ($this->couponInfo['coupon_type'] == CouponTypeEnum::DISCOUNT) {
|
|
|
+ $reducePrice = $reducedMoney - ($reducedMoney * ($this->couponInfo['discount'] / 10));
|
|
|
+ } else {
|
|
|
+ $reducePrice = $this->couponInfo['reduce_price'];
|
|
|
+ }
|
|
|
+ // 优惠券最大允许抵扣到一分钱,所以此处判断抵扣金额大于等于订单金额时,减去一分钱
|
|
|
+ $this->actualReducedMoney = ($reducePrice >= $orderTotalPrice) ? $orderTotalPrice - 1 : $reducePrice;
|
|
|
}
|
|
|
|
|
|
- private function arraySortByWeight($goodsList)
|
|
|
+ /**
|
|
|
+ * 获取当前订单商品总额
|
|
|
+ * @return float|int
|
|
|
+ */
|
|
|
+ private function getOrderTotalPrice()
|
|
|
{
|
|
|
- return array_sort($goodsList, 'weight', true);
|
|
|
+ $orderTotalPrice = 0;
|
|
|
+ foreach ($this->goodsList as $goods) {
|
|
|
+ $orderTotalPrice += ($goods['total_price'] * 100);
|
|
|
+ }
|
|
|
+ return $orderTotalPrice;
|
|
|
}
|
|
|
|
|
|
- private function getGoodsListWeight($goodsList, $orderTotalPrice)
|
|
|
+ /**
|
|
|
+ * 计算商品抵扣的权重(占比)
|
|
|
+ */
|
|
|
+ private function setGoodsListWeight()
|
|
|
{
|
|
|
- foreach ($goodsList as &$goods) {
|
|
|
+ $orderTotalPrice = helper::getArrayColumnSum($this->rangeGoodsList, 'total_price');
|
|
|
+ foreach ($this->rangeGoodsList as &$goods) {
|
|
|
$goods['weight'] = $goods['total_price'] / $orderTotalPrice;
|
|
|
}
|
|
|
- return $this->arraySortByWeight($goodsList);
|
|
|
+ array_sort($this->rangeGoodsList, 'weight', true);
|
|
|
}
|
|
|
|
|
|
- private function setGoodsListCouponMoney(&$goodsList)
|
|
|
+ /**
|
|
|
+ * 计算商品抵扣的金额
|
|
|
+ */
|
|
|
+ private function setGoodsListCouponMoney(): void
|
|
|
{
|
|
|
- foreach ($goodsList as &$goods) {
|
|
|
+ foreach ($this->rangeGoodsList as &$goods) {
|
|
|
$goods['coupon_money'] = helper::bcmul($this->actualReducedMoney, $goods['weight'], 0);
|
|
|
}
|
|
|
- return true;
|
|
|
}
|
|
|
|
|
|
- private function setGoodsListCouponMoneyFill(&$goodsList, $totalCouponMoney)
|
|
|
+ private function setGoodsListCouponMoneyFill($assignedCouponMoney): void
|
|
|
{
|
|
|
- if ($totalCouponMoney === 0) {
|
|
|
+ if ($assignedCouponMoney === 0) {
|
|
|
$temReducedMoney = $this->actualReducedMoney;
|
|
|
- foreach ($goodsList as &$goods) {
|
|
|
+ foreach ($this->rangeGoodsList as &$goods) {
|
|
|
if ($temReducedMoney === 0) break;
|
|
|
$goods['coupon_money'] = 1;
|
|
|
$temReducedMoney--;
|
|
|
}
|
|
|
}
|
|
|
- return true;
|
|
|
}
|
|
|
|
|
|
- private function setGoodsListCouponMoneyDiff(&$goodsList, $totalCouponMoney)
|
|
|
+ private function setGoodsListCouponMoneyDiff($assignedCouponMoney): void
|
|
|
{
|
|
|
- $tempDiff = $this->actualReducedMoney - $totalCouponMoney;
|
|
|
- foreach ($goodsList as &$goods) {
|
|
|
+ $tempDiff = $this->actualReducedMoney - $assignedCouponMoney;
|
|
|
+ foreach ($this->rangeGoodsList as &$goods) {
|
|
|
if ($tempDiff < 1) break;
|
|
|
$goods['coupon_money']++ && $tempDiff--;
|
|
|
}
|
|
|
- return true;
|
|
|
}
|
|
|
-
|
|
|
}
|