WIN-2308041133\Administrator 3 дней назад
Родитель
Сommit
09c36bec88
1 измененных файлов с 94 добавлено и 87 удалено
  1. 94 87
      app/services/activity/coupon/StoreCouponUserServices.php

+ 94 - 87
app/services/activity/coupon/StoreCouponUserServices.php

@@ -127,107 +127,114 @@ class StoreCouponUserServices extends BaseServices
      */
     public function getUsableCouponList(int $uid, array $cartGroup)
     {
-        $userCoupons = $this->dao->getUserAllCoupon($uid);
-        $result = [];
-        if ($userCoupons) {
-            $cartInfo = $cartGroup['valid'];
+        try {
+            $userCoupons = $this->dao->getUserAllCoupon($uid);
+            $result = [];
+            if ($userCoupons) {
+                $cartInfo = $cartGroup['valid'];
 
-            // 获取购物车中所有商品的 is_repeat、is_free 字段
-            $productIds = array_unique(array_column($cartInfo, 'product_id'));
-            $hasRepeat = false;
-            $hasFree = false;
-            if ($productIds) {
-                /** @var StoreProductServices $storeProductServices */
-                $storeProductServices = app()->make(StoreProductServices::class);
-                $products = $storeProductServices->getColumn([['id', 'in', $productIds]], 'is_repeat,is_free', 'id');
-                foreach ($products as $product) {
-                    if (!empty($product['is_repeat']) && $product['is_repeat'] == 1) {
-                        $hasRepeat = true;
-                    }
-                    if (!empty($product['is_free']) && $product['is_free'] == 1) {
-                        $hasFree = true;
+                // 获取购物车中所有商品的 is_repeat、is_free 字段
+                $productIds = array_unique(array_column($cartInfo, 'product_id'));
+                $hasRepeat = false;
+                $hasFree = false;
+                if ($productIds) {
+                    /** @var StoreProductServices $storeProductServices */
+                    $storeProductServices = app()->make(StoreProductServices::class);
+                    $products = $storeProductServices->getColumn([['id', 'in', $productIds]], 'is_repeat,is_free', 'id');
+                    foreach ($products as $product) {
+                        if (!empty($product['is_repeat']) && $product['is_repeat'] == 1) {
+                            $hasRepeat = true;
+                        }
+                        if (!empty($product['is_free']) && $product['is_free'] == 1) {
+                            $hasFree = true;
+                        }
                     }
                 }
-            }
-            @file_put_contents("quanju.txt", $hasRepeat."-是否是复购商品\r\n", 8);
-            @file_put_contents("quanju.txt", $hasFree."-是否是50元兑换商品\r\n", 8);
-            @file_put_contents("quanju.txt", json_encode($userCoupons)."-用户所有优惠券数据\r\n", 8);
+                @file_put_contents("quanju.txt", $hasRepeat."-是否是复购商品\r\n", 8);
+                @file_put_contents("quanju.txt", $hasFree."-是否是50元兑换商品\r\n", 8);
+                @file_put_contents("quanju.txt", json_encode($userCoupons)."-用户所有优惠券数据\r\n", 8);
 
-            foreach ($userCoupons as $coupon) {
-                $couponType = (int)$coupon['applicable_type'];
-                @file_put_contents("quanju.txt", "优惠券ID:".$coupon['id'].", 类型:".$couponType."-优惠卷类型\r\n", 8);
+                foreach ($userCoupons as $coupon) {
+                    $couponType = (int)$coupon['applicable_type'];
+                    @file_put_contents("quanju.txt", "优惠券ID:".$coupon['id'].", 类型:".$couponType."-优惠卷类型\r\n", 8);
 
-                // is_repeat 为 1:只显示 type=3 的优惠券
-                if ($hasRepeat && $couponType !== 3) {
-                    @file_put_contents("quanju.txt", "跳过:复购商品但类型不是3\r\n", 8);
-                    continue;
-                }
-                // is_free 为 1:只显示 type=4 的优惠券
-                if ($hasFree && $couponType !== 4) {
-                    @file_put_contents("quanju.txt", "跳过:50元兑换商品但类型不是4\r\n", 8);
-                    continue;
-                }
-                // is_repeat 和 is_free 都不为 1:隐藏 type=3 和 type=4 的优惠券
-                if (!$hasRepeat && !$hasFree && ($couponType === 3 || $couponType === 4)) {
-                    @file_put_contents("quanju.txt", "跳过:普通商品但类型是3或4\r\n", 8);
-                    continue;
-                }
+                    // is_repeat 为 1:只显示 type=3 的优惠券
+                    if ($hasRepeat && $couponType !== 3) {
+                        @file_put_contents("quanju.txt", "跳过:复购商品但类型不是3\r\n", 8);
+                        continue;
+                    }
+                    // is_free 为 1:只显示 type=4 的优惠券
+                    if ($hasFree && $couponType !== 4) {
+                        @file_put_contents("quanju.txt", "跳过:50元兑换商品但类型不是4\r\n", 8);
+                        continue;
+                    }
+                    // is_repeat 和 is_free 都不为 1:隐藏 type=3 和 type=4 的优惠券
+                    if (!$hasRepeat && !$hasFree && ($couponType === 3 || $couponType === 4)) {
+                        @file_put_contents("quanju.txt", "跳过:普通商品但类型是3或4\r\n", 8);
+                        continue;
+                    }
 
-                $price = 0;
-                $count = 0;
-                switch ($coupon['applicable_type']) {
-                    case 0:
-//                    case 3:
-//                        foreach ($cartInfo as $cart) {
-//                            $price += bcmul((string)$cart['truePrice'], (string)$cart['cart_num'], 2);
-//                            $count++;
-//                        }
-//                        break;
-//                    case 4:
-//                        foreach ($cartInfo as $cart) {
-//                            $price += bcmul((string)$cart['truePrice'], (string)$cart['cart_num'], 2);
-//                            $count++;
-//                        }
-//                        break;
-                    case 1://品类券
-                        /** @var StoreCategoryServices $storeCategoryServices */
-                        $storeCategoryServices = app()->make(StoreCategoryServices::class);
-                        $coupon_category = explode(',', (string)$coupon['category_id']);
-                        $category_ids = $storeCategoryServices->getAllById($coupon_category);
-                        if ($category_ids) {
-                            $cateIds = array_column($category_ids, 'id');
+                    $price = 0;
+                    $count = 0;
+                    switch ($coupon['applicable_type']) {
+                        case 0:
+                        case 3:
                             foreach ($cartInfo as $cart) {
-                                if (isset($cart['productInfo']['cate_id']) && array_intersect(explode(',', $cart['productInfo']['cate_id']), $cateIds)) {
-                                    $price += bcmul((string)$cart['truePrice'], (string)$cart['cart_num'], 2);
-                                    $count++;
-                                }
+                                $price += bcmul((string)$cart['truePrice'], (string)$cart['cart_num'], 2);
+                                $count++;
                             }
-                        }
-                        break;
-                    case 2:
-                        foreach ($cartInfo as $cart) {
-                            if (isset($cart['product_id']) && in_array($cart['product_id'], explode(',', $coupon['product_id']))) {
+                            break;
+                        case 4:
+                            foreach ($cartInfo as $cart) {
                                 $price += bcmul((string)$cart['truePrice'], (string)$cart['cart_num'], 2);
                                 $count++;
                             }
-                        }
-                        break;
-                }
-                if ($count && $coupon['use_min_price'] <= $price) {
-                    $coupon['start_time'] = $coupon['start_time'] ? date('Y/m/d', $coupon['start_time']) : date('Y/m/d', $coupon['add_time']);
-                    $coupon['add_time'] = date('Y/m/d', $coupon['add_time']);
-                    $coupon['end_time'] = date('Y/m/d', $coupon['end_time']);
-                    $coupon['start_use_time'] = $coupon['start_use_time'] > 0 ? date('Y/m/d', $coupon['start_use_time']) : 0;
-                    $coupon['end_use_time'] = $coupon['start_use_time'] > 0 ? date('Y/m/d', $coupon['end_use_time']) : 0;
-                    $coupon['title'] = $coupon['coupon_title'];
-                    $coupon['type'] = $coupon['applicable_type'];
-                    $coupon['use_min_price'] = floatval($coupon['use_min_price']);
-                    $coupon['coupon_price'] = floatval($coupon['coupon_price']);
-                    $result[] = $coupon;
+                            break;
+                        case 1://品类券
+                            /** @var StoreCategoryServices $storeCategoryServices */
+                            $storeCategoryServices = app()->make(StoreCategoryServices::class);
+                            $coupon_category = explode(',', (string)$coupon['category_id']);
+                            $category_ids = $storeCategoryServices->getAllById($coupon_category);
+                            if ($category_ids) {
+                                $cateIds = array_column($category_ids, 'id');
+                                foreach ($cartInfo as $cart) {
+                                    if (isset($cart['productInfo']['cate_id']) && array_intersect(explode(',', $cart['productInfo']['cate_id']), $cateIds)) {
+                                        $price += bcmul((string)$cart['truePrice'], (string)$cart['cart_num'], 2);
+                                        $count++;
+                                    }
+                                }
+                            }
+                            break;
+                        case 2:
+                            foreach ($cartInfo as $cart) {
+                                if (isset($cart['product_id']) && in_array($cart['product_id'], explode(',', $coupon['product_id']))) {
+                                    $price += bcmul((string)$cart['truePrice'], (string)$cart['cart_num'], 2);
+                                    $count++;
+                                }
+                            }
+                            break;
+                    }
+                    if ($count && $coupon['use_min_price'] <= $price) {
+                        $coupon['start_time'] = $coupon['start_time'] ? date('Y/m/d', $coupon['start_time']) : date('Y/m/d', $coupon['add_time']);
+                        $coupon['add_time'] = date('Y/m/d', $coupon['add_time']);
+                        $coupon['end_time'] = date('Y/m/d', $coupon['end_time']);
+                        $coupon['start_use_time'] = $coupon['start_use_time'] > 0 ? date('Y/m/d', $coupon['start_use_time']) : 0;
+                        $coupon['end_use_time'] = $coupon['start_use_time'] > 0 ? date('Y/m/d', $coupon['end_use_time']) : 0;
+                        $coupon['title'] = $coupon['coupon_title'];
+                        $coupon['type'] = $coupon['applicable_type'];
+                        $coupon['use_min_price'] = floatval($coupon['use_min_price']);
+                        $coupon['coupon_price'] = floatval($coupon['coupon_price']);
+                        $result[] = $coupon;
+                    }
                 }
             }
+            return $result;
+        } catch (\Exception $e) {
+            @file_put_contents("quanju.txt", json_encode($e->getMessage())."-getUsableCouponList报错内容\r\n", 8);
+            @file_put_contents("quanju.txt", json_encode($e->getLine())."-getUsableCouponList报错位置\r\n", 8);
+            @file_put_contents("quanju.txt", json_encode($e->getFile())."-getUsableCouponList报错文件\r\n", 8);
+            throw $e;
         }
-        return $result;
     }
 
     /**