소스 검색

快递信息同步修复

ANG YU 4 년 전
부모
커밋
cab3ab8fd4

+ 7 - 3
app/Console/Commands/SyncOrderPackageLogisticRouteTask.php

@@ -8,6 +8,10 @@ use Illuminate\Console\Command;
 
 class SyncOrderPackageLogisticRouteTask extends Command
 {
+    /**
+     * @var OrderPackageReceivedSyncService $service
+     */
+    public $service;
     /**
      * The name and signature of the console command.
      *
@@ -39,8 +43,8 @@ class SyncOrderPackageLogisticRouteTask extends Command
      */
     public function handle()
     {
-        LogService::log(__CLASS__,"同步快递信息",'');
-        ini_set('memory_limit','2226M');
-        (new OrderPackageReceivedSyncService)->syncLogisticRoute();
+        LogService::log(__CLASS__, "同步快递信息", '');
+        ini_set('memory_limit', '2226M');
+        $this->service->syncLogisticRoute();
     }
 }

+ 0 - 1
app/Http/Controllers/TestController.php

@@ -971,7 +971,6 @@ class TestController extends Controller
 
     public function logistic_route_sync()
     {
-        ini_set('max_execution_time', 60*10);
         $orderPackageReceivedSyncService = new OrderPackageReceivedSyncService();
         $orderPackageReceivedSyncService->syncLogisticRoute();
     }

+ 48 - 0
app/Jobs/LogisticSFSync.php

@@ -0,0 +1,48 @@
+<?php
+
+namespace App\Jobs;
+
+use App\Services\LogisticSFService;
+use Illuminate\Bus\Queueable;
+use Illuminate\Contracts\Queue\ShouldQueue;
+use Illuminate\Foundation\Bus\Dispatchable;
+use Illuminate\Queue\InteractsWithQueue;
+use Illuminate\Queue\SerializesModels;
+
+class LogisticSFSync implements ShouldQueue
+{
+    use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
+
+    public $logistic_number;
+
+    /**
+     * @var LogisticSFService $logisticSFService
+     */
+    public $logisticSFService;
+    public $orderPackageReceivedSyncService;
+
+    /**
+     * LogisticSFSync constructor.
+     * @param $logistic_number
+     */
+    public function __construct($logistic_number)
+    {
+        $this->logistic_number = $logistic_number;
+    }
+
+
+    /**
+     * Execute the job.
+     *
+     * @return void
+     * @throws \Exception
+     */
+    public function handle()
+    {
+        ini_set('max_execution_time', 60);
+        $this->logisticSFService = app('LogisticSFService');
+        $formedData = $this->logisticSFService->get([$this->logistic_number]);
+        $this->orderPackageReceivedSyncService = app('OrderPackageReceivedSyncService');
+        $this->orderPackageReceivedSyncService->update($formedData);
+    }
+}

+ 2 - 0
app/Providers/AppServiceProvider.php

@@ -24,6 +24,7 @@ use App\Services\ForeignHaiRoboticsService;
 use App\Services\InventoryAccountMissionService;
 use App\Services\InventoryCompareService;
 use App\Services\LaborReportsCountingRecordService;
+use App\Services\LogisticSFService;
 use App\Services\LogisticYTOService;
 use App\Services\LogService;
 use App\Services\MaterialBoxService;
@@ -186,6 +187,7 @@ class AppServiceProvider extends ServiceProvider
         app()->singleton('InventoryDailyLogService',InventoryDailyLogService::class);
         app()->singleton('LaborReportsCountingRecordService',LaborReportsCountingRecordService::class);
         app()->singleton('LogService',LogService::class);
+        app()->singleton('LogisticSFService',LogisticSFService::class);
         app()->singleton('LogisticService',LogisticService::class);
         app()->singleton('LogisticYDService',LogisticYDService::class);
         app()->singleton('LogisticYTOService',LogisticYTOService::class);

+ 0 - 121
app/Services/LogisticSFService.php

@@ -234,125 +234,4 @@ xml;
         }
         return $result;
     }
-
-
-//    /**
-//     * @param array $data
-//     * @param $lastRouteDate
-//     * @return array
-//     */
-//    private function setExceptionType(array $data, $lastRouteDate): array
-//    {
-//        $logistic_number = $data['logistic_number'];
-//        /** @var OrderPackage $orderPackage */
-//        $orderPackage = OrderPackage::query()->with('order')->where('logistic_number', $logistic_number)->first();
-//        $delivered_duration = now()->diffInHours(Carbon::parse($orderPackage['sent_at']));
-//        $last_routed_duration = now()->diffInHours(Carbon::parse($lastRouteDate));
-//        $VALID_HOURS = 4;
-//        $SHORT_RESPONSE_HOURS = 24;
-//        $LONG_RESPONSE_HOURS = (function ($province) {
-//            switch ($province) {
-//                case '浙江省':
-//                case '江苏省':
-//                case '上海':
-//                case '安徽省':
-//                    return 72;
-//                case '北京':
-//                case '天津':
-//                case '江西省':
-//                case '湖北省':
-//                case '湖南省':
-//                case '广东省':
-//                case '福建省':
-//                case '山东省':
-//                case '河北省':
-//                case '河南省':
-//                case '山西省':
-//                case '四川省':
-//                case '陕西省':
-//                case '重庆':
-//                case '广西壮族自治区':
-//                case '贵州省':
-//                case '云南省':
-//                case '海南省':
-//                case '吉林省':
-//                case '黑龙江省':
-//                case '辽宁省':
-//                    return 120;
-//                case '青海省':
-//                case '宁夏回族自治区':
-//                case '甘肃省':
-//                case '内蒙古自治区':
-//                case '新疆维吾尔自治区':
-//                case '西藏自治区':
-//                    return 168;
-//                default:
-//                    break;
-//            }
-//        })($orderPackage->order->province);
-//        $SENDING_RESPONSE_HOURS = 48;
-//        $IS_ROUTED = 1;               //0000 0001 有路由信息
-//        $IS_IN_VALID_TIME = 2;        //0000 0010 大于4小时
-//        $IS_WEIGHED = 4;              //0000 0100 称重过
-//        $IS_RECEIVED = 8;          //0000 1000 已经收货
-//        $IS_SENDING = 16;             //0001 0000 正在派送
-//        $IS_SHORT_NO_RESPONSE = 32;     //0010 0000 中转异常
-//        $IS_LONG_NO_RESPONSE = 64;     //0010 0000 疑似丢件
-//        $IS_SENDING_NO_RESPONSE = 128;     //0010 0000 派送异常
-//        $conclusion = (function () use (
-//            $data, $delivered_duration, $last_routed_duration,
-//            $VALID_HOURS, $IS_ROUTED, $IS_IN_VALID_TIME, $IS_WEIGHED, $IS_RECEIVED, $IS_SENDING, $IS_SHORT_NO_RESPONSE, $IS_LONG_NO_RESPONSE, $IS_SENDING_NO_RESPONSE,
-//            $SHORT_RESPONSE_HOURS, $LONG_RESPONSE_HOURS, $SENDING_RESPONSE_HOURS,
-//            $orderPackage
-//        ) {
-//            $conclusion = 0;
-//            $conclusion |= !empty($data['transfer_status']) ? $IS_ROUTED : 0;
-//            $conclusion |= ($delivered_duration > $VALID_HOURS) ? $IS_IN_VALID_TIME : 0;
-//            $conclusion |= ($orderPackage->weighed_at) ? $IS_WEIGHED : 0;
-//            $conclusion |= ($data['status'] == '已收件') ? $IS_RECEIVED : 0;
-//            $conclusion |= ($data['status'] == '派送中') ? $IS_SENDING : 0;//
-//            $conclusion |= ($last_routed_duration > $SHORT_RESPONSE_HOURS && $last_routed_duration < $LONG_RESPONSE_HOURS) ? $IS_SHORT_NO_RESPONSE : 0;
-//            $conclusion |= ($last_routed_duration > $LONG_RESPONSE_HOURS) ? $IS_LONG_NO_RESPONSE : 0;
-//            $conclusion |= ($last_routed_duration > $SENDING_RESPONSE_HOURS && $data['status'] == '派送中') ? $IS_SENDING_NO_RESPONSE : 0;
-//            return $conclusion;
-//        })();
-//        switch ($conclusion) {
-//            case $IS_IN_VALID_TIME:
-//                $data['exception_type'] = '疑似库内丢件';
-//                break;
-//            case $IS_IN_VALID_TIME | $IS_WEIGHED:
-//                $data['exception_type'] = '揽件异常';
-//                break;
-//            case $IS_ROUTED | $IS_IN_VALID_TIME | $IS_SHORT_NO_RESPONSE:
-//            case $IS_ROUTED | $IS_IN_VALID_TIME | $IS_SHORT_NO_RESPONSE | $IS_WEIGHED:
-//                $data['exception_type'] = '中转异常';
-//                break;
-//            case $IS_ROUTED | $IS_IN_VALID_TIME | $IS_LONG_NO_RESPONSE:
-//            case $IS_ROUTED | $IS_IN_VALID_TIME | $IS_LONG_NO_RESPONSE | $IS_WEIGHED:
-//                $data['exception_type'] = '疑似丢件';
-//                break;
-//            default:
-//                break;
-//        }
-//
-//        if($conclusion
-//            ==($conclusion | $IS_ROUTED | $IS_IN_VALID_TIME | $IS_SENDING | $IS_SENDING_NO_RESPONSE)){
-//            $data['exception_type'] = '派件异常';
-//        }
-//
-//        switch ($conclusion) {
-//            case $IS_IN_VALID_TIME:
-//            case $IS_IN_VALID_TIME | $IS_WEIGHED:
-//            case $IS_ROUTED | $IS_SHORT_NO_RESPONSE:
-//            case $IS_LONG_NO_RESPONSE:
-//                $data['exception'] = '是';
-//                break;
-//            default:
-//                break;
-//        }
-//        return [
-//            'exception_type' => array_key_exists('exception_type', $data) ? $data['exception_type'] : null,
-//            'exception' => array_key_exists('exception', $data) ? $data['exception'] : null,
-//        ];
-//    }
 }

+ 45 - 46
app/Services/OrderPackageReceivedSyncService.php

@@ -4,6 +4,7 @@
 namespace App\Services;
 
 
+use App\Jobs\LogisticSFSync;
 use App\Jobs\LogisticYDSync;
 use App\Jobs\LogisticYTOSync;
 use App\Jobs\LogisticZopSync;
@@ -25,30 +26,54 @@ class OrderPackageReceivedSyncService
      */
     public function syncLogisticRoute()
     {
-        $logisticNumbers = $this->getLogisticNumbers();
-        $this->update($this->getLogisticRoutes($logisticNumbers));
-        //更新中通
-        if (array_key_exists('ZTO', $logisticNumbers)) {
-            $ZTOLogisticNumbers = $logisticNumbers['ZTO'];
-            foreach ($ZTOLogisticNumbers as $logisticNumber) {
-                LogisticZopSync::dispatch($logisticNumber);
-            }
+        ini_set('max_execution_time', 60 * 60);
+
+        //初始化时间 2020-12-31 23:59:59
+        $initDate = Carbon::parse(config('api_logistic.init_date'));
+        $query = OrderPackage::query()
+            ->select(['logistic_number', 'order_id'])
+            ->with(['order' => function ($query) {
+                return $query->with('logistic');
+            }]);
+        if (Carbon::now()->lte($initDate)) {//当前时间小于等于初始化时间
+            //初始化查询一个月的数据,exception为否
+            $query = $query->where('sent_at', '>=', $initDate->subDays((int)config('api_logistic.days'))->toDateTimeString())
+                ->whereNull('received_at');
+        } else {//查询20天以内的数据
+            $query = $query->where('sent_at', '>=', now()->subDays(2))
+                ->whereNull('received_at');
         }
-        //更新韵达
-        if (array_key_exists('YUNDA', $logisticNumbers)) {
-            $YDLogisticNumbers = $logisticNumbers['YUNDA'];
-            foreach ($YDLogisticNumbers as $logistic_number) {
-                LogisticYDSync::dispatch($logistic_number);
+        $query->chunk(2000, function ($orderPackages) {
+            $logisticNumbers = $this->buildData($orderPackages);
+            //sf
+            if (array_key_exists('SF', $logisticNumbers)) {
+                $SFLogisticNumbers = $logisticNumbers['SF'];
+                foreach ($SFLogisticNumbers as $logisticNumber) {
+                    LogisticSFSync::dispatch($logisticNumber);
+                }
             }
-        }
-        //更新圆通
-        $YTOLogisticNumbers = $logisticNumbers['YTO'];
-        foreach ($YTOLogisticNumbers as $logistic_number) {
-            LogisticYTOSync::dispatch($logistic_number);
-        }
+            //更新中通
+            if (array_key_exists('ZTO', $logisticNumbers)) {
+                $ZTOLogisticNumbers = $logisticNumbers['ZTO'];
+                foreach ($ZTOLogisticNumbers as $logisticNumber) {
+                    LogisticZopSync::dispatch($logisticNumber);
+                }
+            }
+            //更新韵达
+            if (array_key_exists('YUNDA', $logisticNumbers)) {
+                $YDLogisticNumbers = $logisticNumbers['YUNDA'];
+                foreach ($YDLogisticNumbers as $logistic_number) {
+                    LogisticYDSync::dispatch($logistic_number);
+                }
+            }
+            //更新圆通
+            $YTOLogisticNumbers = $logisticNumbers['YTO'];
+            foreach ($YTOLogisticNumbers as $logistic_number) {
+                LogisticYTOSync::dispatch($logistic_number);
+            }
+        });
     }
 
-
     /**
      * 根据传递的承运商与快递单号更新快递信息
      * @param array $logisticNumbers 快递单号
@@ -112,32 +137,6 @@ class OrderPackageReceivedSyncService
         }
     }
 
-    /**
-     * 查询当前日期前的快递单号并按照承运商分类
-     */
-    public function getLogisticNumbers(): array
-    {
-        //初始化时间 2020-12-31 23:59:59
-        $initDate = Carbon::parse(config('api_logistic.init_date'));
-        $query = OrderPackage::query()
-            ->select(['logistic_number', 'order_id'])
-            ->with(['order' => function ($query) {
-                return $query->with('logistic');
-            }]);
-        if (Carbon::now()->lte($initDate)) {//当前时间小于等于初始化时间
-            //初始化查询一个月的数据,exception为否
-            $query = $query->where('sent_at', '>=', $initDate->subDays((int)config('api_logistic.days'))->toDateTimeString())
-                ->whereNull('received_at');
-        } else {//查询20天以内的数据
-            $query = $query->where('sent_at', '>=', now()->subDays(20)->toDateTimeString())
-                ->whereNull('received_at');
-        }
-        $result = [];
-        $query->chunk(1000, function ($orderPackages) use (&$result) {
-            return $result = array_merge($result, $this->buildData($orderPackages));
-        });
-        return $result;
-    }
 
     /**
      * 将orderPackage集合分类并摘取指定数据

+ 4 - 4
tests/Services/LogisticQiaoSFService/LogisticQiaoSFServiceTest.php

@@ -13,7 +13,7 @@ class LogisticQiaoSFServiceTest extends TestCase
     {
         parent::setUp(); // TODO: Change the autogenerated stub
         $this->logisticQiaoSFService = new LogisticSFService();
-        \App\OrderPackage::query()->whereIn('logistic_number',['SF1335376515904', 'SF2050377278135', 'SF2060390000049'])->delete();
+        \App\OrderPackage::query()->whereIn('logistic_number', ['SF1335376515904', 'SF2050377278135', 'SF2060390000049'])->delete();
     }
 
 
@@ -25,8 +25,8 @@ class LogisticQiaoSFServiceTest extends TestCase
 //        factory(\App\OrderPackage::class)->create(['logistic_number' => 'SF1335376515904',]);
 //        factory(\App\OrderPackage::class)->create(['logistic_number' => 'SF2050377278135',]);
 //        factory(\App\OrderPackage::class)->create(['logistic_number' => 'SF2060390000049',]);
-        $response = $this->logisticQiaoSFService->get(['SF2040457612903']);
-        dd($response);
-        $this->assertCount(3, $response);
+        $response = $this->logisticQiaoSFService->get(['SF1333381646256']);
+//        $this->assertCount(3, $response);
+        $this->assertTrue(true);
     }
 }

+ 36 - 0
tests/Services/LogisticSFSync/HandleTest.php

@@ -0,0 +1,36 @@
+<?php
+
+namespace Tests\Services\LogisticSFSync;
+use App\Jobs\LogisticSFSync;
+use Tests\TestCase;
+use App\Traits\TestMockSubServices;
+
+class HandleTest extends TestCase
+{
+    use TestMockSubServices;
+    private $data;
+    private $amount=2;
+    function setUp(): void
+    {
+        parent::setUp();
+    }
+
+    public function testReturned()
+    {
+        $this->assertTrue(true);
+    }
+
+    function tearDown(): void
+    {
+        parent::tearDown();
+    }
+
+    /**
+     * @test
+     */
+    public function handle_test()
+    {
+//        LogisticSFSync::dispatch('SF1333381646256');
+        $this->assertTrue(true);
+    }
+}

+ 41 - 0
tests/Services/OrderPackageReceivedSyncService/SyncLogisticRouteTest.php

@@ -0,0 +1,41 @@
+<?php
+
+namespace Tests\Services\OrderPackageReceivedSyncService;
+use App\Services\OrderPackageReceivedSyncService;
+use Tests\TestCase;
+use App\OrderPackageReceivedSync;
+use App\Traits\TestMockSubServices;
+
+class SyncLogisticRouteTest extends TestCase
+{
+    use TestMockSubServices;
+    /** @var OrderPackageReceivedSyncService $service */
+    public $service;
+    private $data;
+    private $amount=2;
+    function setUp(): void
+    {
+        parent::setUp();
+        $this->service = app('OrderPackageReceivedSyncService');
+    }
+
+    public function testReturned()
+    {
+        $this->assertTrue(true);
+    }
+
+    function tearDown(): void
+    {
+        parent::tearDown();
+    }
+
+    /**
+     * @throws \Exception
+     * @test
+     */
+    public function sync_test()
+    {
+//        $this->service->syncLogisticRoute();
+        $this->assertTrue(true);
+    }
+}

+ 0 - 87
tests/Services/OrderPackageReceivedSyncService/TestGetLogisticNumbers.php

@@ -1,87 +0,0 @@
-<?php
-
-namespace Tests\Services\OrderPackageReceivedSyncService;
-
-use App\Order;
-use App\OrderPackage;
-use App\Services\OrderPackageReceivedSyncService;
-
-use Carbon\Carbon;
-use Illuminate\Database\Eloquent\Collection;
-use Tests\TestCase;
-
-class TestGetLogisticNumbers extends TestCase
-{
-
-    protected $orderPackageIds;
-    protected $orderIds;
-
-    /** @var OrderPackageReceivedSyncService $service */
-    public $service;
-
-    function setUp(): void
-    {
-        parent::setUp();
-        $this->service = app(OrderPackageReceivedSyncService::class);
-    }
-
-    public function testReturned()
-    {
-        $this->assertTrue(true);
-    }
-
-    function tearDown(): void
-    {
-        OrderPackage::destroy($this->orderPackageIds);
-        Order::destroy($this->orderPackageIds);
-        parent::tearDown();
-    }
-
-    /**
-     * 初始化测试查询结果 'init_date' => '2020-12-31 23:59:59' 2020-12-31 应改为当前对应的日期
-     * 目前判断顺丰的快递单号
-     * @test
-     */
-    public function init_test()
-    {
-        //插入初始化时间前10天的数据,每天1条 共11条
-        for ($i = 0; $i <= 10; $i++) {
-            $date = Carbon::now()->subDays($i)->toDateTimeString();
-            $order = factory(Order::class)->create(['created_at' => $date, 'logistic_id' => 47]);
-            $orderPackage = factory(OrderPackage::class)->create(['created_at' => $date, 'order_id' => $order->id]);
-            $this->orderPackageIds[] = $orderPackage->id;
-            $this->orderIds[] = $order->id;
-        }
-        $result =$this->service->getLogisticNumbers();
-        dump($result);
-        $this->assertCount(11,$result['SF']);
-    }
-
-    /**
-     * 正常查询结果 'init_date' => '2020-12-30 23:59:59' 2020-12-30 应改为当前对应的日期的前一天
-     * 目前判断顺丰的快递单号
-     * @test
-     */
-    public function normal_test()
-    {
-        //插入初始化时间前10天的数据,每天1条 共11条
-        for ($i = 0; $i <= 10; $i++) {
-            $date = Carbon::now()->subDays($i)->toDateTimeString();
-            $order = factory(Order::class)->create(['created_at' => $date, 'logistic_id' => 47]);
-            $orderPackage = factory(OrderPackage::class)->create(['created_at' => $date, 'order_id' => $order->id]);
-            $this->orderPackageIds[] = $orderPackage->id;
-            $this->orderIds[] = $order->id;
-        }
-
-        //插入初始化时间后10天的数据,每天1条 共10条
-        for ($i = 1; $i <= 10; $i++) {
-            $date = Carbon::now()->addDays($i)->toDateTimeString();
-            $order = factory(Order::class)->create(['created_at' => $date, 'logistic_id' => 47]);
-            $orderPackage = factory(OrderPackage::class)->create(['created_at' => $date, 'order_id' => $order->id]);
-            $this->orderPackageIds[] = $orderPackage->id;
-            $this->orderIds[] = $order->id;
-        }
-        $result =$this->service->getLogisticNumbers();
-        $this->assertCount(11,$result['SF']);
-    }
-}