|
@@ -156,7 +156,161 @@ class DiscountService
|
|
|
|
|
|
return array_values($processedData);
|
|
|
}
|
|
|
+ /**
|
|
|
+ * 新出一个方法 通过传入商品id 数组 查询 shop_activity 表中的 goods_ids 来查询是否有在进行中的 活动
|
|
|
+ * @param mixed $goodsIds
|
|
|
+ * @return mixed|string|null
|
|
|
+ */
|
|
|
+ public static function getActivityByGoodsIds($goodsIds = [])
|
|
|
+ {
|
|
|
+ if (empty($goodsIds)) {
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+
|
|
|
+ // 获取所有进行中的活动
|
|
|
+ $activities = Db::table('shop_activity')
|
|
|
+ ->where('start_time', '<=', time())
|
|
|
+ ->where('end_time', '>=', time())
|
|
|
+ ->where('activity_status', ActivityEnum::ACTIVITY_STATUS_ONGOING)
|
|
|
+ ->select();
|
|
|
+
|
|
|
+ // 在PHP中过滤匹配的活动
|
|
|
+ foreach ($activities as $activity) {
|
|
|
+ if (empty($activity['goods_ids'])) {
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+
|
|
|
+ // 解析JSON数组
|
|
|
+ $activityGoodsIds = json_decode($activity['goods_ids'], true);
|
|
|
+ if (!is_array($activityGoodsIds)) {
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+
|
|
|
+ // 检查是否有商品ID匹配
|
|
|
+ $intersection = array_intersect($goodsIds, $activityGoodsIds);
|
|
|
+ if (!empty($intersection)) {
|
|
|
+ return $activity;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获取商品ID与活动的映射关系
|
|
|
+ * @param array $goodsIds 商品ID数组
|
|
|
+ * @return array 返回商品ID与活动信息的映射关系
|
|
|
+ */
|
|
|
+ public static function getGoodsActivityMapping($goodsIds = [])
|
|
|
+ {
|
|
|
+ if (empty($goodsIds)) {
|
|
|
+ return [];
|
|
|
+ }
|
|
|
+
|
|
|
+ // 获取所有进行中的活动
|
|
|
+ $activities = Db::table('shop_activity')
|
|
|
+ ->where('start_time', '<=', time())
|
|
|
+ ->where('end_time', '>=', time())
|
|
|
+ ->where('activity_status', ActivityEnum::ACTIVITY_STATUS_ONGOING)
|
|
|
+ ->select();
|
|
|
+
|
|
|
+ $goodsActivityMapping = [];
|
|
|
+
|
|
|
+ // 为每个商品ID初始化空的活动信息
|
|
|
+ foreach ($goodsIds as $goodsId) {
|
|
|
+ $goodsActivityMapping[$goodsId] = null;
|
|
|
+ }
|
|
|
+
|
|
|
+ // 在PHP中匹配商品与活动
|
|
|
+ foreach ($activities as $activity) {
|
|
|
+ if (empty($activity['goods_ids'])) {
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+
|
|
|
+ // 解析JSON数组
|
|
|
+ $activityGoodsIds = json_decode($activity['goods_ids'], true);
|
|
|
+ if (!is_array($activityGoodsIds)) {
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+
|
|
|
+ // 为匹配的商品ID设置活动信息
|
|
|
+ foreach ($activityGoodsIds as $activityGoodsId) {
|
|
|
+ if (in_array($activityGoodsId, $goodsIds)) {
|
|
|
+ $goodsActivityMapping[$activityGoodsId] = $activity;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ return $goodsActivityMapping;
|
|
|
+ }
|
|
|
|
|
|
+ /**
|
|
|
+ * 查询单个商品是否参与正在进行的活动
|
|
|
+ * @param int $goodsId 商品ID
|
|
|
+ * @return array|null 返回活动信息,如果没有参与活动则返回null
|
|
|
+ *
|
|
|
+ * 使用示例:
|
|
|
+ * $goodsId = 47;
|
|
|
+ * $activity = DiscountService::getGoodsActivity($goodsId);
|
|
|
+ * if ($activity) {
|
|
|
+ * echo "商品参与活动:" . $activity['activity_name'];
|
|
|
+ * echo "活动ID:" . $activity['id'];
|
|
|
+ * echo "活动类型:" . $activity['activity_type'];
|
|
|
+ * } else {
|
|
|
+ * echo "商品未参与任何活动";
|
|
|
+ * }
|
|
|
+ */
|
|
|
+ public static function getGoodsActivity($goodsId)
|
|
|
+ {
|
|
|
+ if (empty($goodsId)) {
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+
|
|
|
+ // 获取所有进行中的活动
|
|
|
+ $activities = Db::table('shop_activity')
|
|
|
+ ->where('start_time', '<=', time())
|
|
|
+ ->where('end_time', '>=', time())
|
|
|
+ ->where('activity_status', ActivityEnum::ACTIVITY_STATUS_ONGOING)
|
|
|
+ ->select();
|
|
|
+
|
|
|
+ // 在PHP中查找匹配的活动
|
|
|
+ foreach ($activities as $activity) {
|
|
|
+ if (empty($activity['goods_ids'])) {
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+
|
|
|
+ // 解析JSON数组
|
|
|
+ $activityGoodsIds = json_decode($activity['goods_ids'], true);
|
|
|
+ if (!is_array($activityGoodsIds)) {
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+
|
|
|
+ // 检查商品ID是否在活动的商品列表中
|
|
|
+ if (in_array($goodsId, $activityGoodsIds)) {
|
|
|
+ return $activity;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 检查单个商品是否参与正在进行的活动(简化版本,只返回true/false)
|
|
|
+ * @param int $goodsId 商品ID
|
|
|
+ * @return bool 是否参与活动
|
|
|
+ *
|
|
|
+ * 使用示例:
|
|
|
+ * $goodsId = 47;
|
|
|
+ * if (DiscountService::hasActivity($goodsId)) {
|
|
|
+ * echo "商品正在参与活动";
|
|
|
+ * } else {
|
|
|
+ * echo "商品未参与活动";
|
|
|
+ * }
|
|
|
+ */
|
|
|
+ public static function hasActivity($goodsId)
|
|
|
+ {
|
|
|
+ return self::getGoodsActivity($goodsId) !== null;
|
|
|
+ }
|
|
|
|
|
|
/**
|
|
|
* 查询单个商品的折扣信息和对应的SKU折扣信息
|
|
@@ -382,7 +536,7 @@ class DiscountService
|
|
|
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['discount_amount'] = max(0, round($goodsItem['original_price'] - $goodsItem['discount_price'], 2));
|
|
|
$goodsItem['price_range'] = '¥' . $goodsItem['discount_price'];
|
|
|
}
|
|
|
// 多规格商品处理
|