Преглед изворни кода

客户端-京东服务商从京推推换成官方

sunxbiao пре 11 месеци
родитељ
комит
3265e05882
1 измењених фајлова са 66 додато и 36 уклоњено
  1. 66 36
      app/controller/api/store/product/Jd.php

+ 66 - 36
app/controller/api/store/product/Jd.php

@@ -7,6 +7,7 @@ use app\common\repositories\user\UserThirdRepository;
7
 use app\model\common\JdOrderModel;
7
 use app\model\common\JdOrderModel;
8
 use think\App;
8
 use think\App;
9
 use crmeb\basic\BaseController;
9
 use crmeb\basic\BaseController;
10
+use think\db\exception\DbException;
10
 use think\Exception;
11
 use think\Exception;
11
 use think\exception\ValidateException;
12
 use think\exception\ValidateException;
12
 use think\facade\Db;
13
 use think\facade\Db;
@@ -483,28 +484,37 @@ class Jd extends BaseController
483
             $page = 1;
484
             $page = 1;
484
             $maxPage = 1; // 默认值
485
             $maxPage = 1; // 默认值
485
 
486
 
486
-            do {
487
-                try {
488
-                    $result = $this->jd_query($eliteId, $page);
489
-                    // 安全退出条件
490
-                    if (empty($result) || !is_array($result)) {
487
+            Db::startTrans();
488
+            try {
489
+                // 清除数据
490
+                Db::name("third_party_goods")->where('cid', '=', $eliteId)->delete();
491
+                do {
492
+                    try {
493
+                        $result = $this->jd_query($eliteId, $page);
494
+                        // 安全退出条件
495
+                        if (empty($result) || !is_array($result)) {
496
+                            break;
497
+                        }
498
+                        // 首次请求获取总页数
499
+                        if ($page === 1 && isset($result['totalCount'])) {
500
+                            // $maxPage = max(1, (int)ceil($result['totalCount'] / 50));
501
+                        }
502
+                        // 处理数据
503
+                        if (!empty($result['data'])) {
504
+                            // 写入数据
505
+                            $this->formatGoodsList($result['data'], $eliteId);
506
+                        }
507
+                        $page++;
508
+                    } catch (\Exception $e) {
509
+                        // 记录错误并继续下一个分类
510
+                        log::info("JD API error (eliteId:{$eliteId}, page:{$page}): " . $e->getMessage());
491
                         break;
511
                         break;
492
                     }
512
                     }
493
-                    // 首次请求获取总页数
494
-                    if ($page === 1 && isset($result['totalCount'])) {
495
-                        // $maxPage = max(1, (int)ceil($result['totalCount'] / 50));
496
-                    }
497
-                    // 处理数据
498
-                    if (!empty($result['data'])) {
499
-                        $this->formatGoodsList($result['data'], $eliteId);
500
-                    }
501
-                    $page++;
502
-                } catch (\Exception $e) {
503
-                    // 记录错误并继续下一个分类
504
-                    log::info("JD API error (eliteId:{$eliteId}, page:{$page}): " . $e->getMessage());
505
-                    break;
506
-                }
507
-            } while ($page <= $maxPage);
513
+                } while ($page <= $maxPage);
514
+                Db::commit();
515
+            } catch (DbException $e) {
516
+                Db::startTrans();
517
+            }
508
         }
518
         }
509
 
519
 
510
         return 'ok';
520
         return 'ok';
@@ -533,22 +543,43 @@ class Jd extends BaseController
533
     {
543
     {
534
         $install = [];
544
         $install = [];
535
         foreach ($goodsList as $goods) {
545
         foreach ($goodsList as $goods) {
546
+            // 获取最优优惠券信息
536
             // 优惠券信息
547
             // 优惠券信息
537
-            if (empty($goods['couponInfo']['couponList'])) continue;
538
             $maxCoupon = [];
548
             $maxCoupon = [];
539
-            foreach ($goods['couponInfo']['couponList'] as $coupon) {
540
-                if ($maxCoupon === [] || $coupon['discount'] > $maxCoupon['discount']) {
541
-                    $maxCoupon = $coupon;
549
+            if (!empty($goods['couponInfo']['couponList'])) {
550
+                foreach ($goods['couponInfo']['couponList'] as $coupon) {
551
+                    // isBest 最优 优惠券
552
+                    if ($coupon['isBest'] == 1) {
553
+                        $maxCoupon = [
554
+                            'discount' => $coupon['discount'],//优惠券金额
555
+                            'getStartTime' => date('Y-m-d', intval($coupon['getStartTime'] / 1000)),
556
+                            'getEndTime' => date('Y-m-d', intval($coupon['getEndTime'] / 1000)),
557
+                            'link' => $coupon['link'],
558
+                            'useStartTime' => date('Y-m-d', intval($coupon['useStartTime'] / 1000)),
559
+                            'useEndTime' => date('Y-m-d', intval($coupon['useEndTime'] / 1000)),
560
+                        ];
561
+                        break;
562
+                    }
542
                 }
563
                 }
543
             }
564
             }
544
-            $couponMoney = [
545
-                [
546
-                    'discount' => $maxCoupon['discount'],//优惠券金额
547
-                    'getStartTime' => date('Y-m-d', intval($maxCoupon['getStartTime'] / 1000)),
548
-                    'getEndTime' => date('Y-m-d', intval($maxCoupon['getEndTime'] / 1000)),
549
-                    'link' => $maxCoupon['link'],
550
-                ]
551
-            ];
565
+
566
+            // 最低到手价
567
+            $purchasePrice = 0.00;
568
+
569
+            // 售价
570
+            if (!isset($goods['priceInfo']['price'])) {
571
+                continue;
572
+            }
573
+            $purchasePrice = $goods['priceInfo']['price'];
574
+            // 券后价
575
+            if (!empty($maxCoupon['discount'])) {
576
+                // 拥有优惠券
577
+                $purchasePrice = bcsub($goods['priceInfo']['price'], $maxCoupon['discount'], 2);
578
+            }
579
+            // 到手价
580
+            if (!empty($goods['purchasePriceInfo']['purchasePrice']) && $goods['purchasePriceInfo']['purchasePrice'] < $purchasePrice) {
581
+                $purchasePrice = $goods['purchasePriceInfo']['purchasePrice'];
582
+            }
552
 
583
 
553
             // 券后佣金,(促销价-优惠券面额)*佣金比例
584
             // 券后佣金,(促销价-优惠券面额)*佣金比例
554
             $commissionMoney = $goods['commissionInfo']['couponCommission'] ?? 0;
585
             $commissionMoney = $goods['commissionInfo']['couponCommission'] ?? 0;
@@ -571,10 +602,9 @@ class Jd extends BaseController
571
 
602
 
572
                 'commission' => $commissionMoney,//佣金
603
                 'commission' => $commissionMoney,//佣金
573
                 'commission_rate' => $goods['commissionInfo']['commissionShare'] ?? '',//佣金比例
604
                 'commission_rate' => $goods['commissionInfo']['commissionShare'] ?? '',//佣金比例
574
-                'coupon_money' => $couponMoney,
575
-                'coupon_link' => $maxCoupon['link'],
576
-                'lowestCouponPrice' => decimal2($goods['priceInfo']['lowestCouponPrice']?? $goods['priceInfo']['price']), //券后价lowestCouponPrice
577
-                'purchasePrice' => decimal2($goods['purchasePriceInfo']['purchasePrice']?? 0.00), //到手价purchasePrice
605
+                'coupon_money' => empty($maxCoupon) ? [] : [$maxCoupon],
606
+                'coupon_link' => $maxCoupon['link'] ?? '',
607
+                'lowestCouponPrice' => $purchasePrice,
578
                 'price' => $goods['priceInfo']['price'], //价格
608
                 'price' => $goods['priceInfo']['price'], //价格
579
                 'imageList' => array_column($goods['imageInfo']['imageList'], 'url'), //图片合集
609
                 'imageList' => array_column($goods['imageInfo']['imageList'], 'url'), //图片合集
580
                 'materialUrl' => $goods['materialUrl']
610
                 'materialUrl' => $goods['materialUrl']