ANG YU пре 4 година
родитељ
комит
24b56ed43d

+ 1 - 0
app/Jobs/LogisticZopSync.php

@@ -110,6 +110,7 @@ class LogisticZopSync implements ShouldQueue
             $resultItem['exception_type'] = '无';
             $resultItem['exception'] = '否';
         }
+        $resultItem['routes_length'] = array_key_exists('transfer_status', $resultItem) ? count($resultItem['transfer_status']) : 0;
         $result[] = $resultItem;
         return $result;
     }

+ 1 - 0
app/Services/LogisticSFService.php

@@ -117,6 +117,7 @@ xml;
         } catch (Exception $e) {
             throw new WarningException("单号没有查询到快递路由信息','LogisticSFService->transformSFOneToArr->{$data['logistic_number']}");
         } finally {
+            $data['routes_length'] = array_key_exists('transfer_status', $data) ? count($data['transfer_status']) : 0;
             return $data;
         }
     }

+ 1 - 158
app/Services/LogisticYDService.php

@@ -125,168 +125,11 @@ class LogisticYDService
                 $result['exception_type'] = '无';
                 $result['exception'] = '否';
             }
+            $result['routes_length'] = array_key_exists('transfer_status', $result) ? count($result['transfer_status']) : 0;
             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 = (function ($province) {
-            switch ($province) {
-                case '浙江省':
-                case '江苏省':
-                case '上海':
-                case '安徽省':
-                    return 24;
-                case '北京':
-                case '天津':
-                case '江西省':
-                case '湖北省':
-                case '湖南省':
-                case '广东省':
-                case '福建省':
-                case '山东省':
-                case '河北省':
-                case '河南省':
-                case '山西省':
-                case '四川省':
-                case '陕西省':
-                case '重庆':
-                case '广西壮族自治区':
-                case '贵州省':
-                case '云南省':
-                case '海南省':
-                case '吉林省':
-                case '黑龙江省':
-                case '辽宁省':
-                    return 72;
-                case '青海省':
-                case '宁夏回族自治区':
-                case '甘肃省':
-                case '内蒙古自治区':
-                case '新疆维吾尔自治区':
-                case '西藏自治区':
-                    return 120;
-                default:
-                    return 24;
-            }
-        })($orderPackage->order->province);
-        $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:
-                    return 72;
-            }
-        })($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,
-        ];
-    }
-
-
     /**
      * @param $nativeData
      * @return string

+ 10 - 1
app/Services/OrderPackageReceivedSyncService.php

@@ -245,6 +245,7 @@ class OrderPackageReceivedSyncService
             }
         })($orderPackage->order->province);
         $SENDING_RESPONSE_HOURS = 48;
+        $HAVEN_SECOND_GOT_HOURS = 24;
         $IS_ROUTED = 1;               //0000 0001 有路由信息
         $IS_IN_VALID_TIME = 2;        //0000 0010 大于4小时
         $IS_WEIGHED = 4;              //0000 0100 称重过
@@ -253,10 +254,11 @@ class OrderPackageReceivedSyncService
         $IS_SHORT_NO_RESPONSE = 32;     //0010 0000 中转异常
         $IS_LONG_NO_RESPONSE = 64;     //0010 0000 疑似丢件
         $IS_SENDING_NO_RESPONSE = 128;     //0010 0000 派送异常
+        $IS_SECOND_ROUTE_HAVE = 256;     //0100 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,
+            $SHORT_RESPONSE_HOURS, $LONG_RESPONSE_HOURS, $SENDING_RESPONSE_HOURS, $HAVEN_SECOND_GOT_HOURS, $IS_SECOND_ROUTE_HAVE,
             $orderPackage
         ) {
             $conclusion = 0;
@@ -268,6 +270,7 @@ class OrderPackageReceivedSyncService
             $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;
+            $conclusion |= ($delivered_duration > $HAVEN_SECOND_GOT_HOURS && $data['routes_length'] < 2) ? $IS_SECOND_ROUTE_HAVE : 0;//超过指定时间,路由信息小于两条
             return $conclusion;
         })();
         switch ($conclusion) {
@@ -292,6 +295,12 @@ class OrderPackageReceivedSyncService
             == ($conclusion | $IS_ROUTED | $IS_IN_VALID_TIME | $IS_SENDING | $IS_SENDING_NO_RESPONSE)) {
             $data['exception_type'] = '派件异常';
         }
+        if ($conclusion
+            == ($conclusion | $IS_SECOND_ROUTE_HAVE)) {
+            $data['exception_type'] = '派件异常';
+            $data['exception'] = '是';
+
+        }
         switch ($conclusion) {
             case $IS_IN_VALID_TIME:
             case $IS_IN_VALID_TIME | $IS_WEIGHED:

+ 1 - 1
database/factories/OrderPackageFactory.php

@@ -29,7 +29,7 @@ $factory->define(OrderPackage::class, function (Faker $faker) {
         'remark' => null,
         'owner_id' => random_int(1, 100),
         'uploaded_to_wms' => '是',
-        'exception_message' => null,
+        'exception_message' => $faker->name,
         'exception_type' => $faker->randomElement($exception_types),
     ];
 });

+ 105 - 0
tests/Services/OrderPackageReceivedSyncService/SetExceptionTypeTest.php

@@ -0,0 +1,105 @@
+<?php
+
+namespace Tests\Services\OrderPackageReceivedSyncService;
+
+use App\Order;
+use App\OrderPackage;
+use App\Services\OrderPackageReceivedSyncService;
+use Tests\TestCase;
+use App\Traits\TestMockSubServices;
+
+class SetExceptionTypeTest extends TestCase
+{
+    use TestMockSubServices;
+
+    /** @var OrderPackageReceivedSyncService $service */
+    public $service;
+    private $data;
+    private $amount = 2;
+
+    function setUp(): void
+    {
+        parent::setUp();
+        $this->service = app('OrderPackageReceivedSyncService');
+        $this->data['orderPackage'] = [];
+        $this->data['order'] = [];
+    }
+
+    public function testReturned()
+    {
+        $this->assertTrue(true);
+    }
+
+    function tearDown(): void
+    {
+        OrderPackage::query()->whereIn('id', $this->data['orderPackage'])->delete();
+        Order::query()->whereIn('id', $this->data['order'])->delete();
+        parent::tearDown();
+    }
+
+    /**
+     * @test
+     */
+    public function 超过24小时未揽件()
+    {
+        $order = factory(Order::class)->create();
+        $orderPackage = factory(OrderPackage::class)->create([
+            'order_id' => $order->id,
+            'sent_at' => now()->subHours(25),
+            "logistic_number" => 4314543143889
+        ]);
+        $this->data['orderPackage'][] = $orderPackage->id;
+        $this->data['order'][] = $order->id;
+        $data = [
+            "logistic_number" => "4314543143889",
+            "status" => "在途",
+            "transfer_status" => [
+                [
+                    "accept_time" => "2021-05-17 19:34:34",
+                    "accept_address" => "【上海市】上海松江区九亭公司 -张东东(18116299116) 已揽收",
+                    "remark" => ""
+                ],
+                [
+                    "accept_time" => "2021-05-18 00:14:26",
+                    "accept_address" => "【上海市】已到达 上海分拨中心",
+                    "remark" => "",
+                ],
+                [
+                    "accept_time" => "2021-05-18 00:21:23",
+                    "accept_address" => "【上海市】已离开 上海分拨中心,发往 浙江嘉兴分拨中心",
+                    "remark" => "",
+                ],
+                [
+                    "accept_time" => "2021-05-18 05:17:43",
+                    "accept_address" => "【嘉兴市】已到达 浙江嘉兴分拨中心",
+                    "remark" => "",
+                ],
+                [
+                    "accept_time" => "2021-05-18 05:20:37",
+                    "accept_address" => "【嘉兴市】已离开 浙江嘉兴分拨中心,发往 浙江嘉善县公司",
+                    "remark" => "",
+                ],
+                [
+                    "accept_time" => "2021-05-18 06:58:04",
+                    "accept_address" => "【嘉兴市】已到达 浙江嘉善县公司",
+                    "remark" => "",
+                ],
+                [
+                    "accept_time" => "2021-05-18 07:47:29",
+                    "accept_address" => "【嘉兴市】浙江嘉善县公司 快递员黄建林(17816502922) 正在为您派送。快件已消毒,小哥体温正常,将佩戴口罩为您派送,您也可联系小哥将快件放置指定代收点或快递柜,祝您身体健康【95121为韵达快递员外呼专属号码,请放心接听】",
+                    "remark" => "",
+                ],
+                [
+                    "accept_time" => "2021-05-18 12:04:50",
+                    "accept_address" => "【代收点】您的快件已暂存至 金都上品游泳池旁丰巢柜3号柜 保管,地址:金都上品游泳池旁丰巢柜3号柜,请及时领取,如有疑问请电联快递员:黄建林(17816502922),投诉电话:0571-28914641",
+                    "remark" => "",
+                ],
+            ],
+            "exception_type" => null,
+            "exception" => null,
+            "routes_length" => 1,
+        ];
+        $result = $this->service->setExceptionType($data, $data['transfer_status'][count($data['transfer_status']) - 1]['accept_time']);
+        $this->assertEquals('派件异常', $result['exception_type']);
+    }
+}