|
@@ -122,6 +122,130 @@ class DiscountService
|
|
return array_values($processedData);
|
|
return array_values($processedData);
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+ /**
|
|
|
|
+ * 查询单个商品的折扣信息和对应的SKU折扣信息
|
|
|
|
+ * @param int $goodsId 商品ID
|
|
|
|
+ * @return array|null 商品折扣信息,包含所有SKU的折扣详情,如果没有折扣则返回null
|
|
|
|
+ */
|
|
|
|
+ public static function getGoodsDiscountDetail($goodsId)
|
|
|
|
+ {
|
|
|
|
+ // 第一步:查询当前时间有效的活动ID
|
|
|
|
+ $activeActivityIds = self::getActiveActivityIds();
|
|
|
|
+ if (empty($activeActivityIds)) {
|
|
|
|
+ return null;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ // 第二步:查询该商品的SKU折扣数据
|
|
|
|
+ $result = Db::table('shop_activity_sku')
|
|
|
|
+ ->alias('sku')
|
|
|
|
+ ->join('shop_goods goods', 'sku.goods_id = goods.id')
|
|
|
|
+ ->join('shop_goods_sku spec', 'sku.sku_id = spec.id', 'left')
|
|
|
|
+ ->join(['shop_activity'=> 'activity'], 'sku.activity_id = activity.id')
|
|
|
|
+ ->where('sku.goods_id', $goodsId)
|
|
|
|
+ ->where('sku.activity_id', 'in', $activeActivityIds)
|
|
|
|
+ ->where('goods.status', GoodsEnum::STATUS_ON_SALE) // 已上架的商品
|
|
|
|
+ ->where('sku.stocks', '>', 0) // 有折扣库存的商品
|
|
|
|
+ ->field([
|
|
|
|
+ 'goods.id as goods_id',
|
|
|
|
+ 'goods.title',
|
|
|
|
+ 'goods.sub_title',
|
|
|
|
+ 'goods.image',
|
|
|
|
+ 'goods.price as original_price',
|
|
|
|
+ 'goods.market_price',
|
|
|
|
+ 'goods.stocks as goods_stocks',
|
|
|
|
+ 'goods.sales',
|
|
|
|
+ 'goods.spec_type',
|
|
|
|
+ 'sku.id as activity_sku_id',
|
|
|
|
+ 'sku.sku_id',
|
|
|
|
+ 'sku.discount',
|
|
|
|
+ 'sku.discount_price',
|
|
|
|
+ 'sku.stocks as discount_stocks',
|
|
|
|
+ 'sku.sale_quantity',
|
|
|
|
+ 'activity.id as activity_id',
|
|
|
|
+ 'activity.name as activity_name',
|
|
|
|
+ 'activity.start_time',
|
|
|
|
+ 'activity.end_time',
|
|
|
|
+ 'spec.price as sku_price',
|
|
|
|
+ 'spec.image as sku_image',
|
|
|
|
+ // 'spec.specs as sku_specs'
|
|
|
|
+ ])
|
|
|
|
+ ->order('sku.discount', 'asc') // 按折扣力度排序,折扣越大越靠前
|
|
|
|
+ ->select();
|
|
|
|
+
|
|
|
|
+ if (empty($result)) {
|
|
|
|
+ return null;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ // 初始化商品折扣结构
|
|
|
|
+ $goodsDiscountData = self::initGoodsDiscountStructure($result[0]);
|
|
|
|
+
|
|
|
|
+ // 处理所有SKU的折扣信息
|
|
|
|
+ foreach ($result as $item) {
|
|
|
|
+ $skuId = $item['sku_id'];
|
|
|
|
+
|
|
|
|
+ // 添加折扣信息
|
|
|
|
+ $discountInfo = [
|
|
|
|
+ 'activity_sku_id' => $item['activity_sku_id'],
|
|
|
|
+ 'sku_id' => $skuId,
|
|
|
|
+ 'discount' => $item['discount'],
|
|
|
|
+ 'discount_price' => $item['discount_price'],
|
|
|
|
+ 'discount_stocks' => $item['discount_stocks'],
|
|
|
|
+ 'sale_quantity' => $item['sale_quantity']
|
|
|
|
+ ];
|
|
|
|
+
|
|
|
|
+ // 如果是多规格商品,添加规格信息
|
|
|
|
+ if ($skuId > 0) {
|
|
|
|
+ $discountInfo['sku_price'] = $item['sku_price'];
|
|
|
|
+ $discountInfo['sku_image'] = $item['sku_image'];
|
|
|
|
+ // $discountInfo['sku_specs'] = $item['sku_specs'];
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ $goodsDiscountData['discount_info'][] = $discountInfo;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ // 处理外层折扣价格字段
|
|
|
|
+ $discountPrices = [];
|
|
|
|
+ $discounts = [];
|
|
|
|
+
|
|
|
|
+ // 收集所有折扣价格和折扣率
|
|
|
|
+ foreach ($goodsDiscountData['discount_info'] as $discount) {
|
|
|
|
+ $discountPrices[] = $discount['discount_price'];
|
|
|
|
+ $discounts[] = $discount['discount'];
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ if (!empty($discountPrices)) {
|
|
|
|
+ // 计算最低和最高折扣价格
|
|
|
|
+ $goodsDiscountData['min_discount_price'] = min($discountPrices);
|
|
|
|
+ $goodsDiscountData['max_discount_price'] = max($discountPrices);
|
|
|
|
+ $goodsDiscountData['min_discount'] = min($discounts);
|
|
|
|
+ $goodsDiscountData['max_discount'] = max($discounts);
|
|
|
|
+
|
|
|
|
+ // 单规格商品处理
|
|
|
|
+ if ($goodsDiscountData['spec_type'] == 0) {
|
|
|
|
+ $goodsDiscountData['discount_price'] = $goodsDiscountData['min_discount_price'];
|
|
|
|
+ $goodsDiscountData['discount'] = $goodsDiscountData['min_discount'];
|
|
|
|
+ $goodsDiscountData['discount_amount'] = round($goodsDiscountData['original_price'] - $goodsDiscountData['discount_price'], 2);
|
|
|
|
+ $goodsDiscountData['price_range'] = '¥' . $goodsDiscountData['discount_price'];
|
|
|
|
+ }
|
|
|
|
+ // 多规格商品处理
|
|
|
|
+ else {
|
|
|
|
+ // 设置价格区间
|
|
|
|
+ if ($goodsDiscountData['min_discount_price'] == $goodsDiscountData['max_discount_price']) {
|
|
|
|
+ $goodsDiscountData['price_range'] = '¥' . $goodsDiscountData['min_discount_price'];
|
|
|
|
+ } else {
|
|
|
|
+ $goodsDiscountData['price_range'] = '¥' . $goodsDiscountData['min_discount_price'] . '-' . $goodsDiscountData['max_discount_price'];
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ // 对于多规格,使用最低价格作为主要显示价格
|
|
|
|
+ $goodsDiscountData['discount_price'] = $goodsDiscountData['min_discount_price'];
|
|
|
|
+ $goodsDiscountData['discount'] = $goodsDiscountData['min_discount'];
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ $goodsDiscountData['remaining_time'] = self::formatRemainingTime($goodsDiscountData['end_time']);
|
|
|
|
+
|
|
|
|
+ return $goodsDiscountData;
|
|
|
|
+ }
|
|
|
|
|
|
/**
|
|
/**
|
|
* 限时查询一个当前活动的信息和商品信息 活动信息直接加一个商品数组字段
|
|
* 限时查询一个当前活动的信息和商品信息 活动信息直接加一个商品数组字段
|