Browse Source

fix:限时折扣的

super-yimizi 1 month ago
parent
commit
36cd890741

+ 27 - 0
application/admin/controller/shop/Goods.php

@@ -13,6 +13,7 @@ use app\admin\model\shop\GoodsSkuSpec;
 use app\common\Enum\GoodsEnum;
 use app\common\Enum\GoodsEnum;
 use app\common\Service\SkuSpec as SkuSpecService;
 use app\common\Service\SkuSpec as SkuSpecService;
 use app\common\Enum\StatusEnum;
 use app\common\Enum\StatusEnum;
+use app\common\Service\DiscountService;
 /**
 /**
  * 商品管理
  * 商品管理
  *
  *
@@ -103,6 +104,28 @@ class Goods extends Backend
                 ->where($where)
                 ->where($where)
                 ->order($sort, $order)
                 ->order($sort, $order)
                 ->paginate($limit);
                 ->paginate($limit);
+            //  查询是否有折扣活动 根据ID
+            $arrGoodsId = $list->column('id');
+            
+            // 获取商品与活动的映射关系
+            $goodsActivityMapping = DiscountService::getGoodsActivityMapping($arrGoodsId);
+            
+            // 为每个商品添加活动信息
+            $list->each(function(&$goods) use ($goodsActivityMapping) {
+                $activityData = $goodsActivityMapping[$goods['id']] ?? null;
+                if ($activityData) {
+                    $goods['activity_name'] = $activityData['name'] ?? '';
+                    $goods['activity_id'] = $activityData['id'] ?? 0;
+                    $goods['activity_type'] = $activityData['activity_type'] ?? '';
+                    $goods['activity_status'] = '进行中';
+                } else {
+                    $goods['activity_name'] = '';
+                    $goods['activity_id'] = 0;
+                    $goods['activity_type'] = '';
+                    $goods['activity_status'] = '无活动';
+                }
+            });
+
 
 
             $result = array("total" => $list->total(), "rows" => $list->items());
             $result = array("total" => $list->total(), "rows" => $list->items());
 
 
@@ -622,6 +645,10 @@ class Goods extends Backend
             if ($params) {
             if ($params) {
                 $params = $this->preExcludeFields($params);
                 $params = $this->preExcludeFields($params);
                 $result = false;
                 $result = false;
+                // 查询该商品是否参与进行中的折扣活动
+                if (DiscountService::hasActivity($row->id)) {
+                    $this->error('该商品参与了进行中的折扣活动,不能编辑');
+                }
                 Db::startTrans();
                 Db::startTrans();
                 try {
                 try {
                     //是否采用模型验证
                     //是否采用模型验证

+ 1 - 1
application/admin/view/shop/goods/index.html

@@ -1,7 +1,7 @@
 <div class="panel panel-default panel-intro">
 <div class="panel panel-default panel-intro">
     
     
     <div class="panel-heading">
     <div class="panel-heading">
-        <div class="panel-lead"><em>商品管理</em>用于商品管理添加、修改</div>
+        <div class="panel-lead"><em>商品管理</em>用于商品管理添加、修改.参与折扣的商品,禁止编辑商品。</div>
         {:build_heading(null,FALSE)}
         {:build_heading(null,FALSE)}
         <ul class="nav nav-tabs" data-field="status">
         <ul class="nav nav-tabs" data-field="status">
             <li class="{:$Think.get.status === null ? 'active' : ''}"><a href="#t-all" data-value="" data-toggle="tab">{:__('All')}</a></li>
             <li class="{:$Think.get.status === null ? 'active' : ''}"><a href="#t-all" data-value="" data-toggle="tab">{:__('All')}</a></li>

+ 155 - 1
application/common/Service/DiscountService.php

@@ -156,7 +156,161 @@ class DiscountService
         
         
         return array_values($processedData);
         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折扣信息
      * 查询单个商品的折扣信息和对应的SKU折扣信息
@@ -382,7 +536,7 @@ class DiscountService
                 if ($goodsItem['spec_type'] == 0) {
                 if ($goodsItem['spec_type'] == 0) {
                     $goodsItem['discount_price'] = $goodsItem['min_discount_price'];
                     $goodsItem['discount_price'] = $goodsItem['min_discount_price'];
                     $goodsItem['discount'] = $goodsItem['min_discount'];
                     $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'];
                     $goodsItem['price_range'] = '¥' . $goodsItem['discount_price'];
                 } 
                 } 
                 // 多规格商品处理
                 // 多规格商品处理

+ 4 - 2
application/common/Service/OrderService.php

@@ -269,6 +269,8 @@ class OrderService
             // 活动折扣金额累计
             // 活动折扣金额累计
             if ($discountInfo) {
             if ($discountInfo) {
                 $discountAmount = bcsub($originalAmount, $amount, 2);
                 $discountAmount = bcsub($originalAmount, $amount, 2);
+                // 确保折扣金额不为负数,如果计算结果为负数则设为0
+                $discountAmount = bccomp($discountAmount, '0', 2) >= 0 ? $discountAmount : '0';
                 $orderInfo['activity_discount_amount'] = bcadd($orderInfo['activity_discount_amount'], $discountAmount, 2);
                 $orderInfo['activity_discount_amount'] = bcadd($orderInfo['activity_discount_amount'], $discountAmount, 2);
                 
                 
                 // 收集折扣信息到订单快照
                 // 收集折扣信息到订单快照
@@ -455,7 +457,7 @@ class OrderService
             }
             }
         }
         }
         
         
-        $orderInfo['pay_amount']          =  bcsub($orderInfo['order_amount'], $orderInfo['discount_fee'], 2);
+        $orderInfo['pay_amount']          =  max(0, bcsub($orderInfo['order_amount'], $orderInfo['discount_fee'], 2));
         $orderInfo['pay_original_amount'] =  $orderInfo['pay_amount'];
         $orderInfo['pay_original_amount'] =  $orderInfo['pay_amount'];
         $orderInfo['pay_remain_amount']   =  $orderInfo['pay_amount']; 
         $orderInfo['pay_remain_amount']   =  $orderInfo['pay_amount']; 
         // echo "<pre>";
         // echo "<pre>";
@@ -541,7 +543,7 @@ class OrderService
                     }
                     }
                 }
                 }
 
 
-                $orderInfo['pay_amount'] = bcsub($orderInfo['order_amount'], $orderInfo['discount_fee'], 2);
+                $orderInfo['pay_amount'] = max(0, bcsub($orderInfo['order_amount'], $orderInfo['discount_fee'], 2));
                 $orderInfo['pay_original_amount'] = $orderInfo['pay_amount'];
                 $orderInfo['pay_original_amount'] = $orderInfo['pay_amount'];
                 $orderInfo['pay_remain_amount'] = $orderInfo['pay_amount'];
                 $orderInfo['pay_remain_amount'] = $orderInfo['pay_amount'];
 
 

+ 4 - 2
application/common/service/OrderService.php

@@ -269,6 +269,8 @@ class OrderService
             // 活动折扣金额累计
             // 活动折扣金额累计
             if ($discountInfo) {
             if ($discountInfo) {
                 $discountAmount = bcsub($originalAmount, $amount, 2);
                 $discountAmount = bcsub($originalAmount, $amount, 2);
+                // 确保折扣金额不为负数,如果计算结果为负数则设为0
+                $discountAmount = bccomp($discountAmount, '0', 2) >= 0 ? $discountAmount : '0';
                 $orderInfo['activity_discount_amount'] = bcadd($orderInfo['activity_discount_amount'], $discountAmount, 2);
                 $orderInfo['activity_discount_amount'] = bcadd($orderInfo['activity_discount_amount'], $discountAmount, 2);
                 
                 
                 // 收集折扣信息到订单快照
                 // 收集折扣信息到订单快照
@@ -455,7 +457,7 @@ class OrderService
             }
             }
         }
         }
         
         
-        $orderInfo['pay_amount']          =  bcsub($orderInfo['order_amount'], $orderInfo['discount_fee'], 2);
+        $orderInfo['pay_amount']          =  max(0, bcsub($orderInfo['order_amount'], $orderInfo['discount_fee'], 2));
         $orderInfo['pay_original_amount'] =  $orderInfo['pay_amount'];
         $orderInfo['pay_original_amount'] =  $orderInfo['pay_amount'];
         $orderInfo['pay_remain_amount']   =  $orderInfo['pay_amount']; 
         $orderInfo['pay_remain_amount']   =  $orderInfo['pay_amount']; 
         // echo "<pre>";
         // echo "<pre>";
@@ -541,7 +543,7 @@ class OrderService
                     }
                     }
                 }
                 }
 
 
-                $orderInfo['pay_amount'] = bcsub($orderInfo['order_amount'], $orderInfo['discount_fee'], 2);
+                $orderInfo['pay_amount'] = max(0, bcsub($orderInfo['order_amount'], $orderInfo['discount_fee'], 2));
                 $orderInfo['pay_original_amount'] = $orderInfo['pay_amount'];
                 $orderInfo['pay_original_amount'] = $orderInfo['pay_amount'];
                 $orderInfo['pay_remain_amount'] = $orderInfo['pay_amount'];
                 $orderInfo['pay_remain_amount'] = $orderInfo['pay_amount'];
 
 

+ 13 - 0
public/assets/js/backend/shop/goods.js

@@ -79,6 +79,19 @@ define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefin
                                     html += '</div>';
                                     html += '</div>';
                                 }
                                 }
                                 
                                 
+                                // 显示活动信息(如果有活动)
+                                if (row.activity_name && row.activity_name.trim() !== '') {
+                                    html += '<div style="width: 100%; max-width: 100%; margin-top: 6px; overflow: hidden; box-sizing: border-box;">';
+                                    html += '<span style="display: inline-block; font-size: 10px; color: #fff; background-color: #ff6b35; padding: 2px 6px; border-radius: 12px; margin-right: 4px; word-wrap: break-word; word-break: break-all; overflow-wrap: break-word; white-space: normal;">';
+                                    html += '<i class="fa fa-fire" style="margin-right: 2px;"></i>';
+                                    html += '限时折扣活动';
+                                    html += '</span>';
+                                    html += '<span style="font-size: 11px; color: #ff6b35; font-weight: 600; word-wrap: break-word; word-break: break-all; overflow-wrap: break-word; white-space: normal;">';
+                                    html += row.activity_name;
+                                    html += '</span>';
+                                    html += '</div>';
+                                }
+                                
                                 html += '</div>';
                                 html += '</div>';
                                 return html;
                                 return html;
                             }
                             }