Эх сурвалжийг харах

新增:后台优惠券适用范围添加排除商品

萤火科技 1 жил өмнө
parent
commit
f089cd8f6e

+ 47 - 7
app/api/model/Coupon.php

@@ -14,6 +14,7 @@ namespace app\api\model;
 
 use app\api\service\User as UserService;
 use app\common\model\Coupon as CouponModel;
+use app\common\enum\coupon\ApplyRange as ApplyRangeEnum;
 use cores\exception\BaseException;
 
 /**
@@ -40,14 +41,16 @@ class Coupon extends CouponModel
     /**
      * 获取优惠券列表
      * @param int|null $limit 获取的数量
-     * @param bool $onlyReceive 只显示可领取
+     * @param bool $onlyReceive 只获取可领取的优惠券
+     * @param int|null $goodsId 只获取指定商品ID可用的优惠券
+     * @param array|null $couponIds 指定优惠券ID集
      * @return array|\think\Collection
      * @throws BaseException
      * @throws \think\db\exception\DataNotFoundException
      * @throws \think\db\exception\DbException
      * @throws \think\db\exception\ModelNotFoundException
      */
-    public function getList(int $limit = null, bool $onlyReceive = false)
+    public function getList(int $limit = null, bool $onlyReceive = false, ?int $goodsId = null, ?array $couponIds = [])
     {
         // 查询构造器
         $query = $this->getNewQuery();
@@ -56,15 +59,52 @@ class Coupon extends CouponModel
             $query->where('IF ( `total_num` > - 1, `receive_num` < `total_num`, 1 = 1 )')
                 ->where('IF ( `expire_type` = 20, (`end_time` + 86400) >= ' . time() . ', 1 = 1 )');
         }
-        // 查询数量
-        $limit > 0 && $query->limit($limit);
+        // 指定优惠券ID集
+        if (!empty($couponIds)) {
+            $query->where('coupon_id', 'in', $couponIds);
+            $query->orderRaw('field(coupon_id, ' . implode(',', $couponIds) . ')');
+        } else {
+            $limit > 0 && $query->limit($limit);
+            $query->order(['sort', 'create_time' => 'desc']);
+        }
         // 优惠券列表
-        $couponList = $query->where('status', '=', 1)
+        $list = $query->where('status', '=', 1)
             ->where('is_delete', '=', 0)
-            ->order(['sort', 'create_time' => 'desc'])
             ->select();
         // 获取用户已领取的优惠券
-        return $this->setIsReceive($couponList);
+        $list = $this->setIsReceive($list);
+        // 筛选指定商品ID可用的优惠券
+        return $this->screenByGoodsId($list, $goodsId);
+    }
+
+    /**
+     * 筛选指定商品ID可用的优惠券
+     * @param iterable $couponList
+     * @param int|null $goodsId
+     * @return array|iterable
+     */
+    private function screenByGoodsId(iterable $couponList, ?int $goodsId = null)
+    {
+        if (empty($goodsId)) {
+            return $couponList;
+        }
+        $list = [];
+        foreach ($couponList as $item) {
+            // 优惠券指定商品可用
+            if ($item['apply_range'] == ApplyRangeEnum::SOME
+                && $goodsId > 0
+                && !\in_array($goodsId, $item['apply_range_config']['goodsIds'])) {
+                continue;
+            }
+            // 优惠券指定商品不可用
+            if ($item['apply_range'] == ApplyRangeEnum::EXCLUDE
+                && $goodsId > 0
+                && \in_array($goodsId, $item['apply_range_config']['goodsIds'])) {
+                continue;
+            }
+            $list[] = $item;
+        }
+        return $list;
     }
 
     /**

+ 8 - 4
app/api/model/UserCoupon.php

@@ -218,20 +218,24 @@ class UserCoupon extends UserCouponModel
     public static function couponListApplyRange(array $couponList, array $orderGoodsIds): array
     {
         // 名词解释(is_apply):允许用于抵扣当前订单
-        foreach ($couponList as &$item) {
+        foreach ($couponList as $key => &$item) {
             if ($item['apply_range'] == ApplyRangeEnum::ALL) {
                 // 1. 全部商品
                 $item['is_apply'] = true;
             } elseif ($item['apply_range'] == ApplyRangeEnum::SOME) {
                 // 2. 指定商品, 判断订单商品是否存在可用
-                $applyGoodsIds = array_intersect($item['apply_range_config']['applyGoodsIds'], $orderGoodsIds);
+                $applyGoodsIds = array_intersect($item['apply_range_config']['goodsIds'], $orderGoodsIds);
                 $item['is_apply'] = !empty($applyGoodsIds);
             } elseif ($item['apply_range'] == ApplyRangeEnum::EXCLUDE) {
                 // 2. 排除商品, 判断订单商品是否全部都在排除行列
-                $excludedGoodsIds = array_intersect($item['apply_range_config']['excludedGoodsIds'], $orderGoodsIds);
+                $excludedGoodsIds = array_intersect($item['apply_range_config']['goodsIds'], $orderGoodsIds);
                 $item['is_apply'] = count($excludedGoodsIds) != count($orderGoodsIds);
             }
-            !$item['is_apply'] && $item['not_apply_info'] = '该优惠券不支持当前商品';
+            !$item['is_apply'] && $item['not_apply_info'] = '该优惠券不支持当前订单';
+            // 此处改为: 将不支持的优惠券剔除
+            if (!$item['is_apply']) {
+                unset($couponList[$key]);
+            }
         }
         return $couponList;
     }

+ 2 - 2
app/api/service/coupon/GoodsDeduct.php

@@ -109,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']['goodsIds'])) {
                         $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']['goodsIds'])) {
                         $this->rangeGoodsList[$goodsKey] = $goods;
                     }
                     break;

+ 27 - 1
app/common/model/Coupon.php

@@ -93,7 +93,23 @@ class Coupon extends BaseModel
      */
     public function getApplyRangeConfigAttr($value)
     {
-        return $value ? helper::jsonDecode($value) : [];
+        $config = $value ? helper::jsonDecode($value) : [];
+        // 兼容历史数据 v2.4.6之前
+        if (!empty($config['applyGoodsIds'])) {
+            $config['goodsIds'] = $config['applyGoodsIds'];
+            unset($config['applyGoodsIds']);
+        }
+        return $config;
+    }
+
+    /**
+     * 获取器:可叠加的优惠方式
+     * @param $value
+     * @return array
+     */
+    public function getStackingAttr($value): array
+    {
+        return !empty($value) ? helper::jsonDecode($value) : [];
     }
 
     /**
@@ -116,6 +132,16 @@ class Coupon extends BaseModel
         return helper::jsonEncode($array);
     }
 
+    /**
+     * 修改器:可叠加的优惠方式
+     * @param $value
+     * @return string
+     */
+    public function setStackingAttr($value): string
+    {
+        return helper::jsonEncode($value);
+    }
+
     /**
      * 优惠券详情
      * @param int $couponId 优惠券ID

+ 8 - 1
app/common/model/UserCoupon.php

@@ -100,7 +100,13 @@ class UserCoupon extends BaseModel
      */
     public function getApplyRangeConfigAttr($value)
     {
-        return $value ? helper::jsonDecode($value) : [];
+        $config = $value ? helper::jsonDecode($value) : [];
+        // 兼容历史数据 v2.4.6之前
+        if (!empty($config['applyGoodsIds'])) {
+            $config['goodsIds'] = $config['applyGoodsIds'];
+            unset($config['applyGoodsIds']);
+        }
+        return $config;
     }
 
     /**
@@ -141,6 +147,7 @@ class UserCoupon extends BaseModel
      */
     public static function setIsUse(int $userCouponId, bool $isUse = true): bool
     {
+        // updateOne
         return static::updateBase(['is_use' => (int)$isUse], $userCouponId);
     }