|  | @@ -9,6 +9,17 @@ use app\common\Enum\ActivityEnum;
 | 
	
		
			
				|  |  |  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
 | 
	
		
			
				|  |  |  {
 | 
	
	
		
			
				|  | @@ -103,7 +114,17 @@ class DiscountService
 | 
	
		
			
				|  |  |                      'activity_name' => $item['activity_name'],
 | 
	
		
			
				|  |  |                      'start_time' => $item['start_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;
 | 
	
		
			
				|  |  |          }
 | 
	
		
			
				|  |  |          
 | 
	
		
			
				|  |  | +        // 处理外层折扣价格字段
 | 
	
		
			
				|  |  | +        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);
 | 
	
		
			
				|  |  |      }
 | 
	
		
			
				|  |  |  
 | 
	
	
		
			
				|  | @@ -136,7 +198,7 @@ class DiscountService
 | 
	
		
			
				|  |  |       * @param mixed $activityId
 | 
	
		
			
				|  |  |       * @return array|bool|Model|string|\PDOStatement|null
 | 
	
		
			
				|  |  |       */
 | 
	
		
			
				|  |  | -    public static function getActivityWithGoods()
 | 
	
		
			
				|  |  | +    public static function getCurrentActivity()
 | 
	
		
			
				|  |  |      {
 | 
	
		
			
				|  |  |          $currentTime = time();
 | 
	
		
			
				|  |  |          
 | 
	
	
		
			
				|  | @@ -158,7 +220,6 @@ class DiscountService
 | 
	
		
			
				|  |  |              ->alias('sku')
 | 
	
		
			
				|  |  |              ->join('shop_goods goods', 'sku.goods_id = goods.id')
 | 
	
		
			
				|  |  |              ->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('sku.stocks', '>', 0)
 | 
	
		
			
				|  |  |              ->field([
 | 
	
	
		
			
				|  | @@ -199,7 +260,17 @@ class DiscountService
 | 
	
		
			
				|  |  |                      'goods_stocks' => $item['goods_stocks'],
 | 
	
		
			
				|  |  |                      'sales' => $item['sales'],
 | 
	
		
			
				|  |  |                      '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;
 | 
	
		
			
				|  |  |          }
 | 
	
		
			
				|  |  |          
 | 
	
		
			
				|  |  | +        // 处理外层折扣价格字段
 | 
	
		
			
				|  |  | +        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);
 | 
	
		
			
				|  |  |          
 |