123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301 |
- <?php
- namespace app\common\Service;
- use think\Db;
- use think\Model;
- use app\common\Enum\StatusEnum;
- use app\common\Enum\ActivityEnum;
- use app\common\Enum\GoodsEnum;
- /**
- * 折扣活动服务类
- */
- class DiscountService
- {
- /**
- * 查询当前时间段内的商品折扣信息
- * @param array $goodsIds 商品ID数组,为空则查询所有参与折扣的商品
- * @param int $limit 限制数量
- * @return array
- */
- public static function getCurrentDiscountGoods($goodsIds = [], $limit = 0)
- {
- $currentTime = time();
-
- // 第一步:查询当前时间有效的活动ID
- $activeActivityIds = Db::table('shop_activity')
- ->where('start_time', '<=', $currentTime)
- ->where('end_time', '>=', $currentTime)
- ->where('activity_status', ActivityEnum::ACTIVITY_STATUS_ONGOING)
- ->where('status', StatusEnum::ENABLED)
- ->column('id');
- if (empty($activeActivityIds)) {
- return [];
- }
-
- // 第二步:查询这些活动的SKU数据
- $query = 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.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'
- ]);
-
- // 如果指定了商品ID,则添加条件
- if (!empty($goodsIds)) {
- $query->where('sku.goods_id', 'in', $goodsIds);
- }
-
- // 如果设置了限制数量
- if ($limit > 0) {
- $query->limit($limit);
- }
-
- // 按折扣力度排序,折扣越大越靠前
- $query->order('sku.discount', 'asc');
-
- $result = $query->select();
- // 处理返回数据
- $processedData = [];
- foreach ($result as $item) {
- $goodsId = $item['goods_id'];
- $skuId = $item['sku_id'];
-
- // 如果商品还没有在结果中,初始化
- if (!isset($processedData[$goodsId])) {
- $processedData[$goodsId] = [
- 'goods_id' => $goodsId,
- 'title' => $item['title'],
- 'sub_title' => $item['sub_title'],
- 'image' => $item['image'],
- 'original_price' => $item['original_price'],
- 'market_price' => $item['market_price'],
- 'goods_stocks' => $item['goods_stocks'],
- 'sales' => $item['sales'],
- 'spec_type' => $item['spec_type'],
- 'activity_id' => $item['activity_id'],
- 'activity_name' => $item['activity_name'],
- 'start_time' => $item['start_time'],
- 'end_time' => $item['end_time'],
- 'discount_info' => []
- ];
- }
-
- // 添加折扣信息
- $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'];
- }
-
- $processedData[$goodsId]['discount_info'][] = $discountInfo;
- }
-
- return array_values($processedData);
- }
-
- /**
- * 根据商品ID和SKU ID获取折扣信息
- * @param int $goodsId 商品ID
- * @param int $skuId SKU ID,单规格商品传0
- * @return array|null
- */
- public static function getGoodsDiscountInfo($goodsId, $skuId = 0)
- {
- $currentTime = time();
-
- // 第一步:查询当前时间有效的活动ID
- $activeActivityIds = Db::table('shop_activity')
- ->where('start_time', '<=', $currentTime)
- ->where('end_time', '>=', $currentTime)
- ->where('activity_status', ActivityEnum::ACTIVITY_STATUS_ONGOING)
- ->where('status', 'normal')
- ->column('id');
-
- if (empty($activeActivityIds)) {
- return null;
- }
-
- // 第二步:查询指定商品的折扣信息
- $discountInfo = Db::table('shop_activity_sku_prize')
- ->alias('sku')
- ->join('shop_activity activity', 'sku.activity_id = activity.id')
- ->where('sku.goods_id', $goodsId)
- ->where('sku.sku_id', $skuId)
- ->where('sku.activity_id', 'in', $activeActivityIds)
- ->where('sku.status', 1)
- ->where('sku.stocks', '>', 0)
- ->field([
- 'sku.id as activity_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'
- ])
- ->find();
-
- return $discountInfo ?: null;
- }
-
- /**
- * 批量获取商品的折扣信息
- * @param array $goodsIds 商品ID数组
- * @return array 键为商品ID,值为折扣信息数组
- */
- public static function getBatchGoodsDiscountInfo($goodsIds)
- {
- if (empty($goodsIds)) {
- return [];
- }
-
- $currentTime = time();
-
- // 第一步:查询当前时间有效的活动ID
- $activeActivityIds = Db::table('shop_activity')
- ->where('start_time', '<=', $currentTime)
- ->where('end_time', '>=', $currentTime)
- ->where('activity_status', ActivityEnum::ACTIVITY_STATUS_ONGOING)
- ->where('status', 'normal')
- ->column('id');
-
- if (empty($activeActivityIds)) {
- return [];
- }
-
- // 第二步:查询指定商品的折扣信息
- $result = Db::table('shop_activity_sku_prize')
- ->alias('sku')
- ->join('shop_activity activity', 'sku.activity_id = activity.id')
- ->where('sku.goods_id', 'in', $goodsIds)
- ->where('sku.activity_id', 'in', $activeActivityIds)
- ->where('sku.status', 1)
- ->where('sku.stocks', '>', 0)
- ->field([
- 'sku.goods_id',
- 'sku.sku_id',
- 'sku.id as activity_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'
- ])
- ->order('sku.discount', 'asc')
- ->select();
-
- // 按商品ID分组
- $discountData = [];
- foreach ($result as $item) {
- $goodsId = $item['goods_id'];
- if (!isset($discountData[$goodsId])) {
- $discountData[$goodsId] = [];
- }
- $discountData[$goodsId][] = $item;
- }
-
- return $discountData;
- }
-
- /**
- * 检查商品是否参与当前折扣活动
- * @param int $goodsId 商品ID
- * @param int $skuId SKU ID
- * @return bool
- */
- public static function hasActiveDiscount($goodsId, $skuId = 0)
- {
- $discountInfo = self::getGoodsDiscountInfo($goodsId, $skuId);
- return !empty($discountInfo);
- }
-
- /**
- * 计算折扣后的价格
- * @param float $originalPrice 原价
- * @param float $discount 折扣(如9.0表示9折)
- * @return float
- */
- public static function calculateDiscountPrice($originalPrice, $discount)
- {
- return round($originalPrice * $discount / 10, 2);
- }
-
- /**
- * 获取活动剩余时间(秒)
- * @param int $endTime 活动结束时间戳
- * @return int
- */
- public static function getActivityRemainingTime($endTime)
- {
- return max(0, $endTime - time());
- }
-
- /**
- * 格式化活动剩余时间
- * @param int $endTime 活动结束时间戳
- * @return array
- */
- public static function formatRemainingTime($endTime)
- {
- $remaining = self::getActivityRemainingTime($endTime);
-
- if ($remaining <= 0) {
- return ['days' => 0, 'hours' => 0, 'minutes' => 0, 'seconds' => 0];
- }
-
- $days = floor($remaining / 86400);
- $hours = floor(($remaining % 86400) / 3600);
- $minutes = floor(($remaining % 3600) / 60);
- $seconds = $remaining % 60;
-
- return [
- 'days' => $days,
- 'hours' => $hours,
- 'minutes' => $minutes,
- 'seconds' => $seconds
- ];
- }
-
-
- }
|