Browse Source

fix:新增一个当前折扣信息活动,不查询所有活动商品,后台限制当前时间有且只有一个

super-yimizi 1 month ago
parent
commit
52e75a2831

+ 10 - 5
application/api/controller/Discount.php

@@ -31,7 +31,6 @@ class Discount extends Api
         $pageSize = $params['pageSize'] ?? 10;
         $pageSize = $params['pageSize'] ?? 10;
         $arrGoodsIds = $params['goods_ids'] ?? [];
         $arrGoodsIds = $params['goods_ids'] ?? [];
         
         
-        // try {
             // 获取折扣商品
             // 获取折扣商品
             $discountGoods = DiscountService::getCurrentDiscountGoods($arrGoodsIds);
             $discountGoods = DiscountService::getCurrentDiscountGoods($arrGoodsIds);
             
             
@@ -63,10 +62,16 @@ class Discount extends Api
                 'pageSize' => $pageSize,
                 'pageSize' => $pageSize,
                 'totalPages' => ceil($total / $pageSize)
                 'totalPages' => ceil($total / $pageSize)
             ]);
             ]);
-            
-        // } catch (\Exception $e) {
-        //     $this->error('查询失败:' . $e->getMessage());
-        // }
+    }
+
+    // 增加一个 当前时间段的折扣活动
+    public function getCurrentActivity()
+    {
+        $activity = DiscountService::getCurrentActivity();
+        //活动过期时间
+         // 添加活动剩余时间
+        $activity['remaining_time'] = DiscountService::formatRemainingTime($activity['end_time']);
+        $this->success('获取成功', $activity);
     }
     }
     
     
     /**
     /**

+ 116 - 4
application/common/Service/DiscountService.php

@@ -9,6 +9,17 @@ use app\common\Enum\ActivityEnum;
 use app\common\Enum\GoodsEnum;
 use app\common\Enum\GoodsEnum;
 /**
 /**
  * 折扣活动服务类
  * 折扣活动服务类
+ * 
+ * 外层折扣字段说明:
+ * - has_discount: 是否有折扣活动
+ * - discount_price: 折扣价格(单规格商品直接使用,多规格商品显示最低价)
+ * - discount: 折扣率(单规格商品直接使用,多规格商品显示最低折扣)
+ * - discount_amount: 折扣金额(原价-折扣价,仅单规格商品)
+ * - min_discount_price: 最低折扣价格
+ * - max_discount_price: 最高折扣价格
+ * - min_discount: 最低折扣率
+ * - max_discount: 最高折扣率
+ * - price_range: 价格区间文本(如:¥99 或 ¥99-199)
  */
  */
 class DiscountService
 class DiscountService
 {
 {
@@ -103,7 +114,17 @@ class DiscountService
                     'activity_name' => $item['activity_name'],
                     'activity_name' => $item['activity_name'],
                     'start_time' => $item['start_time'],
                     'start_time' => $item['start_time'],
                     'end_time' => $item['end_time'],
                     'end_time' => $item['end_time'],
-                    'discount_info' => []
+                    'has_discount' => true,
+                    'discount_info' => [],
+                    // 初始化折扣价格相关字段
+                    'min_discount_price' => null,
+                    'max_discount_price' => null,
+                    'min_discount' => null,
+                    'max_discount' => null,
+                    'discount_price' => null,
+                    'discount' => null,
+                    'discount_amount' => null,
+                    'price_range' => ''
                 ];
                 ];
             }
             }
             
             
@@ -127,6 +148,47 @@ class DiscountService
             $processedData[$goodsId]['discount_info'][] = $discountInfo;
             $processedData[$goodsId]['discount_info'][] = $discountInfo;
         }
         }
         
         
+        // 处理外层折扣价格字段
+        foreach ($processedData as &$goods) {
+            $discountPrices = [];
+            $discounts = [];
+            
+            // 收集所有折扣价格和折扣率
+            foreach ($goods['discount_info'] as $discount) {
+                $discountPrices[] = $discount['discount_price'];
+                $discounts[] = $discount['discount'];
+            }
+            
+            if (!empty($discountPrices)) {
+                // 计算最低和最高折扣价格
+                $goods['min_discount_price'] = min($discountPrices);
+                $goods['max_discount_price'] = max($discountPrices);
+                $goods['min_discount'] = min($discounts);
+                $goods['max_discount'] = max($discounts);
+                
+                // 单规格商品处理
+                if ($goods['spec_type'] == 0) {
+                    $goods['discount_price'] = $goods['min_discount_price'];
+                    $goods['discount'] = $goods['min_discount'];
+                    $goods['discount_amount'] = round($goods['original_price'] - $goods['discount_price'], 2);
+                    $goods['price_range'] = '¥' . $goods['discount_price'];
+                } 
+                // 多规格商品处理
+                else {
+                    // 设置价格区间
+                    if ($goods['min_discount_price'] == $goods['max_discount_price']) {
+                        $goods['price_range'] = '¥' . $goods['min_discount_price'];
+                    } else {
+                        $goods['price_range'] = '¥' . $goods['min_discount_price'] . '-' . $goods['max_discount_price'];
+                    }
+                    
+                    // 对于多规格,使用最低价格作为主要显示价格
+                    $goods['discount_price'] = $goods['min_discount_price'];
+                    $goods['discount'] = $goods['min_discount'];
+                }
+            }
+        }
+        
         return array_values($processedData);
         return array_values($processedData);
     }
     }
 
 
@@ -136,7 +198,7 @@ class DiscountService
      * @param mixed $activityId
      * @param mixed $activityId
      * @return array|bool|Model|string|\PDOStatement|null
      * @return array|bool|Model|string|\PDOStatement|null
      */
      */
-    public static function getActivityWithGoods()
+    public static function getCurrentActivity()
     {
     {
         $currentTime = time();
         $currentTime = time();
         
         
@@ -158,7 +220,6 @@ class DiscountService
             ->alias('sku')
             ->alias('sku')
             ->join('shop_goods goods', 'sku.goods_id = goods.id')
             ->join('shop_goods goods', 'sku.goods_id = goods.id')
             ->join('shop_goods_sku spec', 'sku.sku_id = spec.id', 'left')
             ->join('shop_goods_sku spec', 'sku.sku_id = spec.id', 'left')
-            ->where('sku.activity_id', $activity->id)
             ->where('goods.status', GoodsEnum::STATUS_ON_SALE)
             ->where('goods.status', GoodsEnum::STATUS_ON_SALE)
             ->where('sku.stocks', '>', 0)
             ->where('sku.stocks', '>', 0)
             ->field([
             ->field([
@@ -199,7 +260,17 @@ class DiscountService
                     'goods_stocks' => $item['goods_stocks'],
                     'goods_stocks' => $item['goods_stocks'],
                     'sales' => $item['sales'],
                     'sales' => $item['sales'],
                     'spec_type' => $item['spec_type'],
                     'spec_type' => $item['spec_type'],
-                    'discount_info' => []
+                    'has_discount' => true,
+                    'discount_info' => [],
+                    // 初始化折扣价格相关字段
+                    'min_discount_price' => null,
+                    'max_discount_price' => null,
+                    'min_discount' => null,
+                    'max_discount' => null,
+                    'discount_price' => null,
+                    'discount' => null,
+                    'discount_amount' => null,
+                    'price_range' => ''
                 ];
                 ];
             }
             }
             
             
@@ -220,6 +291,47 @@ class DiscountService
             $processedGoods[$goodsId]['discount_info'][] = $discountInfo;
             $processedGoods[$goodsId]['discount_info'][] = $discountInfo;
         }
         }
         
         
+        // 处理外层折扣价格字段
+        foreach ($processedGoods as &$goodsItem) {
+            $discountPrices = [];
+            $discounts = [];
+            
+            // 收集所有折扣价格和折扣率
+            foreach ($goodsItem['discount_info'] as $discount) {
+                $discountPrices[] = $discount['discount_price'];
+                $discounts[] = $discount['discount'];
+            }
+            
+            if (!empty($discountPrices)) {
+                // 计算最低和最高折扣价格
+                $goodsItem['min_discount_price'] = min($discountPrices);
+                $goodsItem['max_discount_price'] = max($discountPrices);
+                $goodsItem['min_discount'] = min($discounts);
+                $goodsItem['max_discount'] = max($discounts);
+                
+                // 单规格商品处理
+                if ($goodsItem['spec_type'] == 0) {
+                    $goodsItem['discount_price'] = $goodsItem['min_discount_price'];
+                    $goodsItem['discount'] = $goodsItem['min_discount'];
+                    $goodsItem['discount_amount'] = round($goodsItem['original_price'] - $goodsItem['discount_price'], 2);
+                    $goodsItem['price_range'] = '¥' . $goodsItem['discount_price'];
+                } 
+                // 多规格商品处理
+                else {
+                    // 设置价格区间
+                    if ($goodsItem['min_discount_price'] == $goodsItem['max_discount_price']) {
+                        $goodsItem['price_range'] = '¥' . $goodsItem['min_discount_price'];
+                    } else {
+                        $goodsItem['price_range'] = '¥' . $goodsItem['min_discount_price'] . '-' . $goodsItem['max_discount_price'];
+                    }
+                    
+                    // 对于多规格,使用最低价格作为主要显示价格
+                    $goodsItem['discount_price'] = $goodsItem['min_discount_price'];
+                    $goodsItem['discount'] = $goodsItem['min_discount'];
+                }
+            }
+        }
+        
         // 在活动信息中添加商品数组字段
         // 在活动信息中添加商品数组字段
         $activity['goods'] = array_values($processedGoods);
         $activity['goods'] = array_values($processedGoods);