Browse Source

细节优化

萤火科技 1 year ago
parent
commit
4cc373e6e0

+ 23 - 5
app/api/model/UserCoupon.php

@@ -38,7 +38,7 @@ class UserCoupon extends UserCouponModel
     ];
 
     /**
-     * 获取用户优惠券列表
+     * 获取用户优惠券列表 (分页)
      * @param int $userId
      * @param array $param
      * @return \think\Paginator
@@ -52,6 +52,24 @@ class UserCoupon extends UserCouponModel
             ->paginate();
     }
 
+    /**
+     * 获取用户优惠券列表 (全部)
+     * @param int $userId
+     * @param array $param
+     * @return UserCoupon[]|array|\think\Collection
+     * @throws \think\db\exception\DataNotFoundException
+     * @throws \think\db\exception\DbException
+     * @throws \think\db\exception\ModelNotFoundException
+     */
+    public static function getAllList(int $userId, array $param)
+    {
+        $model = new static;
+        $filter = $model->getFilter($param);
+        return $model->where($filter)
+            ->where('user_id', '=', $userId)
+            ->select();
+    }
+
     /**
      * 检索查询条件
      * @param array $param
@@ -163,21 +181,21 @@ class UserCoupon extends UserCouponModel
     }
 
     /**
-     * 订单结算优惠券列表
+     * 根据订单获取可用的优惠券列表 (用于订单结算台)
      * @param int $userId 用户id
      * @param float $orderPayPrice 订单商品总金额
      * @return array
      * @throws \think\db\exception\DbException
      */
-    public static function getUserCouponList(int $userId, float $orderPayPrice): array
+    public static function getListByOrder(int $userId, float $orderPayPrice): array
     {
         // 判断订单商品总金额不能为1分
         if ($orderPayPrice <= 0.01) {
             return [];
         }
         // 获取用户可用的优惠券列表
-        $list = (new static)->getList($userId, ['dataType' => 'isUsable', 'amount' => $orderPayPrice]);
-        $data = $list->isEmpty() ? [] : $list->toArray()['data'];
+        $list = static::getAllList($userId, ['dataType' => 'isUsable', 'amount' => $orderPayPrice]);
+        $data = $list->isEmpty() ? [] : $list->toArray();
         foreach ($data as &$item) {
             // 计算最大能折扣的金额
             if ($item['coupon_type'] == CouponTypeEnum::DISCOUNT) {

+ 14 - 15
app/api/service/coupon/GoodsDeduct.php

@@ -85,6 +85,15 @@ class GoodsDeduct extends BaseService
         return $this->rangeGoodsList;
     }
 
+    /**
+     * 获取实际抵扣的金额
+     * @return int
+     */
+    public function getActualReducedMoney(): int
+    {
+        return $this->actualReducedMoney;
+    }
+
     /**
      * 设置实际参与优惠券折扣的商品记录
      * @return void
@@ -100,12 +109,12 @@ class GoodsDeduct extends BaseService
                     $this->rangeGoodsList[$goodsKey] = $goods;
                     break;
                 case ApplyRangeEnum::SOME:
-                    if (in_array($goods['goods_id'], $this->couponInfo['apply_range_config']['applyGoodsIds'])) {
+                    if (\in_array($goods['goods_id'], $this->couponInfo['apply_range_config']['applyGoodsIds'])) {
                         $this->rangeGoodsList[$goodsKey] = $goods;
                     }
                     break;
                 case ApplyRangeEnum::EXCLUDE:
-                    if (!in_array($goods['goods_id'], $this->couponInfo['apply_range_config']['excludedGoodsIds'])) {
+                    if (!\in_array($goods['goods_id'], $this->couponInfo['apply_range_config']['excludedGoodsIds'])) {
                         $this->rangeGoodsList[$goodsKey] = $goods;
                     }
                     break;
@@ -126,11 +135,6 @@ class GoodsDeduct extends BaseService
         }
     }
 
-    public function getActualReducedMoney(): int
-    {
-        return $this->actualReducedMoney;
-    }
-
     /**
      * 计算实际抵扣的金额
      */
@@ -138,14 +142,9 @@ class GoodsDeduct extends BaseService
     {
         // 获取当前订单商品总额
         $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));
+            $reducePrice = $orderTotalPrice - ($orderTotalPrice * ($this->couponInfo['discount'] / 10));
         } else {
             $reducePrice = $this->couponInfo['reduce_price'] * 100;
         }
@@ -173,10 +172,10 @@ class GoodsDeduct extends BaseService
     {
         $orderTotalPrice = helper::getArrayColumnSum($this->rangeGoodsList, 'total_price');
         foreach ($this->rangeGoodsList as &$goods) {
-            $weight = round($goods['total_price'] / $orderTotalPrice, 6);
+            $weight = \round($goods['total_price'] / $orderTotalPrice, 6);
             $goods['weight'] = helper::scToStr((string)$weight, 6);
         }
-        array_sort($this->rangeGoodsList, 'weight', true);
+        \array_sort($this->rangeGoodsList, 'weight', true);
     }
 
     /**

+ 13 - 12
app/api/service/order/Checkout.php

@@ -22,8 +22,8 @@ use app\api\service\user\Grade as UserGradeService;
 use app\api\service\coupon\GoodsDeduct as GoodsDeductService;
 use app\api\service\points\GoodsDeduct as PointsDeductService;
 use app\api\service\order\source\checkout\Factory as CheckoutFactory;
-use app\common\enum\order\OrderType;
 use app\common\enum\Setting as SettingEnum;
+use app\common\enum\order\OrderType as OrderTypeEnum;
 use app\common\enum\order\OrderStatus as OrderStatusEnum;
 use app\common\enum\order\OrderSource as OrderSourceEnum;
 use app\common\enum\order\DeliveryType as DeliveryTypeEnum;
@@ -178,7 +178,7 @@ class Checkout extends BaseService
         // 设置订单商品总金额(不含优惠折扣)
         $this->setOrderTotalPrice();
         // 当前用户可用的优惠券列表
-        $couponList = $this->getUserCouponList((float)$this->orderData['orderTotalPrice']);
+        $couponList = $this->getCouponListByOrder((float)$this->orderData['orderTotalPrice']);
         // 计算优惠券抵扣
         $this->setOrderCouponMoney($couponList, (int)$this->param['couponId']);
         // 计算可用积分抵扣
@@ -289,7 +289,7 @@ class Checkout extends BaseService
             // 积分抵扣比例
             $deductionRatio = helper::bcdiv($setting['discount']['max_money_ratio'], 100);
             // 最多可抵扣的金额
-            $totalPayPrice = helper::bcsub($goods['total_price'], $goods['coupon_money']);
+            $totalPayPrice = helper::bcsubs([$goods['total_price'], $goods['coupon_money']]);
             $maxPointsMoney = helper::bcmul($totalPayPrice, $deductionRatio);
             // 最多可抵扣的积分数量
             $goods['max_points_num'] = helper::bcdiv($maxPointsMoney, $setting['discount']['discount_ratio'], 0);
@@ -357,7 +357,7 @@ class Checkout extends BaseService
         // 系统默认的配送方式
         $setting = SettingModel::getItem(SettingEnum::DELIVERY)['delivery_type'];
         // 配送方式:实物订单
-        if ($this->getOrderType() == OrderType::PHYSICAL) {
+        if ($this->getOrderType() == OrderTypeEnum::PHYSICAL) {
             return $this->param['delivery'] > 0 ? $this->param['delivery'] : $setting[0];
         }
         // 配送方式:无需配送
@@ -409,11 +409,11 @@ class Checkout extends BaseService
 
     /**
      * 当前用户可用的优惠券列表
-     * @param float $orderTotalPrice 总金额
+     * @param float $orderTotalPrice 订单商品总金额
      * @return array
      * @throws \think\db\exception\DbException
      */
-    private function getUserCouponList(float $orderTotalPrice): array
+    private function getCouponListByOrder(float $orderTotalPrice): array
     {
         // 是否开启优惠券折扣
         if (!$this->checkoutRule['isCoupon']) {
@@ -422,7 +422,7 @@ class Checkout extends BaseService
         // 整理当前订单所有商品ID集
         $orderGoodsIds = helper::getArrayColumn($this->goodsList, 'goods_id');
         // 当前用户可用的优惠券列表
-        $couponList = UserCouponModel::getUserCouponList($this->user['user_id'], $orderTotalPrice);
+        $couponList = UserCouponModel::getListByOrder($this->user['user_id'], $orderTotalPrice);
         // 判断当前优惠券是否满足订单使用条件 (优惠券适用范围)
         return UserCouponModel::couponListApplyRange($couponList, $orderGoodsIds);
     }
@@ -502,11 +502,12 @@ class Checkout extends BaseService
         // 商品总价 - 优惠抵扣
         foreach ($this->goodsList as &$goods) {
             // 减去优惠券抵扣金额
-            $value = helper::bcsub($goods['total_price'], $goods['coupon_money']);
+            $value = helper::bcsubs([$goods['total_price'], $goods['coupon_money']]);
             // 减去积分抵扣金额
             if ($this->orderData['isAllowPoints'] && $this->orderData['isUsePoints']) {
                 $value = helper::bcsub($value, $goods['points_money']);
             }
+            // 订单商品的实际付款金额
             $goods['total_pay_price'] = helper::number2($value);
         }
     }
@@ -532,7 +533,6 @@ class Checkout extends BaseService
             // 会员折扣的总额差
             'grade_total_money' => 0.00,
         ], true);
-
         // 是否开启会员等级折扣
         if (!$this->checkoutRule['isUserGrade']) {
             return;
@@ -594,8 +594,9 @@ class Checkout extends BaseService
         }
         // 获取优惠券信息
         $couponInfo = $this->getCouponInfo($userCouponId, $couponList);
-        // 计算订单商品优惠券抵扣金额
+        // 商品列表信息格式化
         $goodsListTemp = helper::getArrayColumns($this->goodsList, ['goods_id', 'goods_sku_id', 'total_price']);
+        // 计算订单商品优惠券抵扣金额
         $CouponMoney = new GoodsDeductService;
         $rangeGoodsList = $CouponMoney->setGoodsList($goodsListTemp)
             ->setCouponInfo($couponInfo)
@@ -712,7 +713,7 @@ class Checkout extends BaseService
         // 新增订单记录
         $this->add($order, $this->param['remark']);
         // 保存订单数据 (根据订单类型)
-        $order['orderType'] == OrderType::PHYSICAL && $this->saveOrderByPhysical($order);
+        $order['orderType'] == OrderTypeEnum::PHYSICAL && $this->saveOrderByPhysical($order);
         // 保存订单商品信息
         $this->saveOrderGoods($order);
         // 更新商品库存 (针对下单减库存的商品)
@@ -749,7 +750,7 @@ class Checkout extends BaseService
      */
     private function validateOrderForm(array $order): bool
     {
-        if ($order['orderType'] == OrderType::PHYSICAL) {
+        if ($order['orderType'] == OrderTypeEnum::PHYSICAL) {
             return $this->validateOrderFormByPhysical($order);
         }
         return true;

+ 9 - 0
app/common/library/helper.php

@@ -216,6 +216,15 @@ class helper
         return \bcsub($leftOperand, $rightOperand, $scale);
     }
 
+    public static function bcsubs(array $values, $scale = 2): string
+    {
+        $result = null;
+        foreach ($values as $value) {
+            $result = is_null($result) ? $value : self::bcsub($result, $value, $scale);
+        }
+        return $result;
+    }
+
     public static function bcadd($leftOperand, $rightOperand, $scale = 2): string
     {
         return \bcadd($leftOperand, $rightOperand, $scale);