ANG YU 5 лет назад
Родитель
Сommit
4f59a801b5

+ 66 - 55
app/Jobs/LogisticZopSync.php

@@ -97,7 +97,8 @@ class LogisticZopSync implements ShouldQueue
      */
     private function logisticHasNotReserve(OrderPackage $orderPackage, array $resultItem, array $result): array
     {
-        if (now()->diffInHours(Carbon::parse($orderPackage->sent_at)) > 4) {//距离出库时间已经超过了4个小时。还没有快递信息
+        $diffInHours = now()->diffInHours(Carbon::parse($orderPackage->sent_at));
+        if ($diffInHours > 4) {//距离出库时间已经超过了4个小时。还没有快递信息
             if ($orderPackage->weighed_at == null) {//没有称重
                 $resultItem['exception_type'] = '疑似库内丢件';
             } else if ($orderPackage->weighed_at) {//称重过
@@ -127,7 +128,10 @@ class LogisticZopSync implements ShouldQueue
             if ($lastRoute->scanType != 'ARRIVAL' && $lastRoute->scanType != '派件') {//快递状态不是派件
                 $resultItem = $this->notArrivalException($diffInHours, $resultItem, $orderPackage);
             } else {//派件异常
-                if ($diffInHours > 48) $resultItem['exception_type'] = '派件异常';
+                if ($diffInHours > 48) {
+                    $resultItem['exception_type'] = '派件异常';
+                    $resultItem['exception'] = '是';
+                }
             }
         }
 
@@ -145,60 +149,67 @@ class LogisticZopSync implements ShouldQueue
      */
     private function notArrivalException(int $diffInHours, array $resultItem, OrderPackage $orderPackage): array
     {
-        if ($diffInHours > 24 && $diffInHours <= 72) {//超过24小时物流没有变化
-            $resultItem['exception_type'] = '中转异常';
-            $resultItem['exception'] = '是';
-        } else {
-            switch ($orderPackage->order->province) {
-                case '浙江省':
-                case '江苏省':
-                case '上海':
-                case '安徽省':
-                    if ($diffInHours > 72) {
-                        $resultItem['exception_type'] = '疑似丢件';
-                        $resultItem['exception'] = '是';
-                        break;
-                    }
-                case '北京':
-                case '天津':
-                case '江西省':
-                case '湖北省':
-                case '湖南省':
-                case '广东省':
-                case '福建省':
-                case '山东省':
-                case '河北省':
-                case '河南省':
-                case '山西省':
-                case '四川省':
-                case '陕西省':
-                case '重庆':
-                case '广西壮族自治区':
-                case '贵州省':
-                case '云南省':
-                case '海南省':
-                case '吉林省':
-                case '黑龙江省':
-                case '辽宁省':
-                    if ($diffInHours > 120) {
-                        $resultItem['exception_type'] = '疑似丢件';
-                        $resultItem['exception'] = '是';
-                        break;
-                    }
-                case '青海省':
-                case '宁夏回族自治区':
-                case '甘肃省':
-                case '内蒙古自治区':
-                case '新疆维吾尔自治区':
-                case '西藏自治区':
-                    if ($diffInHours > 168) {
-                        $resultItem['exception_type'] = '疑似丢件';
-                        $resultItem['exception'] = '是';
-                        break;
-                    }
-                default:
+        switch ($orderPackage->order->province) {
+            case '浙江省':
+            case '江苏省':
+            case '上海':
+            case '安徽省':
+                if ($diffInHours > 72) {
+                    $resultItem['exception_type'] = '疑似丢件';
+                    $resultItem['exception'] = '是';
                     break;
-            }
+                }
+                if ($diffInHours > 24 && $diffInHours <= 72) {
+                    $resultItem['exception_type'] = '中转异常';
+                    $resultItem['exception'] = '是';
+                }
+            case '北京':
+            case '天津':
+            case '江西省':
+            case '湖北省':
+            case '湖南省':
+            case '广东省':
+            case '福建省':
+            case '山东省':
+            case '河北省':
+            case '河南省':
+            case '山西省':
+            case '四川省':
+            case '陕西省':
+            case '重庆':
+            case '广西壮族自治区':
+            case '贵州省':
+            case '云南省':
+            case '海南省':
+            case '吉林省':
+            case '黑龙江省':
+            case '辽宁省':
+                if ($diffInHours > 120) {
+                    $resultItem['exception_type'] = '疑似丢件';
+                    $resultItem['exception'] = '是';
+                    break;
+                }
+                if ($diffInHours > 24 && $diffInHours <= 120) {
+                    $resultItem['exception_type'] = '中转异常';
+                    $resultItem['exception'] = '是';
+                }
+            case '青海省':
+            case '宁夏回族自治区':
+            case '甘肃省':
+            case '内蒙古自治区':
+            case '新疆维吾尔自治区':
+            case '西藏自治区':
+                if ($diffInHours > 168) {
+                    $resultItem['exception_type'] = '疑似丢件';
+                    $resultItem['exception'] = '是';
+                    break;
+                }
+                if ($diffInHours > 24 && $diffInHours <= 168) {
+                    $resultItem['exception_type'] = '中转异常';
+                    $resultItem['exception'] = '是';
+                }
+            default:
+                break;
         }
         return $resultItem;
     }

+ 117 - 2
app/Services/LogisticSFService.php

@@ -5,8 +5,10 @@ namespace App\Services;
 
 
 use App\Exceptions\WarningException;
+use App\OrderPackage;
 use Exception;
 use Illuminate\Http\Client\Response;
+use Illuminate\Support\Carbon;
 use Illuminate\Support\Facades\Http;
 
 class LogisticSFService
@@ -92,12 +94,13 @@ xml;
      */
     public function transformSFOneToArr(array $routeResponse, array $data): array
     {
+
         $data['logistic_number'] = $routeResponse['@attributes'][$this->protected_switch['logistic_number']];
         try {
             $lastRoute = get_object_vars($routeResponse['Route'][count($routeResponse['Route']) - 1])['@attributes'];//获取最新的路由信息
             $data = $this->switchOpCodeToStatus($lastRoute, $data);
             $data['transfer_status'] = $this->transformRoutes($routeResponse['Route']);
-
+            $data = $this->setExceptionType($data, $lastRoute);
         } catch (Exception $e) {
             throw new WarningException("单号没有查询到快递路由信息','LogisticSFService->transformSFOneToArr->{$data['logistic_number']}");
         } finally {
@@ -169,7 +172,7 @@ xml;
             }
         } catch (WarningException $e) {
             $data['status'] = '其他异常';
-        }finally {
+        } finally {
             return $data;
         }
     }
@@ -209,4 +212,116 @@ xml;
         }
         return $result;
     }
+
+
+    /**
+     * @param array $data
+     * @param $lastRoute
+     * @return array
+     */
+    private function setExceptionType(array $data, $lastRoute): 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['send_at']));
+        $last_routed_duration = now()->diffInHours(Carbon::parse($lastRoute['accept_time']));
+        $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
+        $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_SHORT_NO_RESPONSE:
+                $data['exception_type'] = '中转异常';
+                break;
+            case $IS_ROUTED | $IS_LONG_NO_RESPONSE:
+                $data['exception_type'] = '疑似丢件';
+                break;
+            default:
+                break;
+        }
+        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 $data;
+    }
 }

+ 3 - 6
public/t.php

@@ -1,9 +1,6 @@
 <?php
 
-$as = 'asdfsdf-234_1_2x';
-
-preg_match('/_(\d)$/',$as,$arr);
-var_dump(isset($arr[1]));
-var_dump(isset($arr[2]));
-var_dump($arr);
+$a=128;
+$a|=256;
+var_dump($a);
 

+ 9 - 1
tests/Feature/LogisticZopSyncTest.php

@@ -12,6 +12,7 @@ use Tests\TestCase;
 
 class LogisticZopSyncTest extends TestCase
 {
+    protected $order;
     /**
      * A basic feature test example.
      *
@@ -32,6 +33,7 @@ class LogisticZopSyncTest extends TestCase
     protected function tearDown(): void
     {
         OrderPackage::query()->where('logistic_number', '000000111')->delete();
+        $this->order && $this->order->delete();
         parent::tearDown(); // TODO: Change the autogenerated stub
     }
 
@@ -57,7 +59,9 @@ class LogisticZopSyncTest extends TestCase
      */
     public function transformRoutes中转异常Test()
     {
-        factory(OrderPackage::class)->create(['logistic_number' => '000000111', 'sent_at' => now()->subHours(5)]);
+        $order = factory(Order::class)->create(['province' => '上海']);
+        $this->order = $order;
+        factory(OrderPackage::class)->create(['logistic_number' => '000000111', 'sent_at' => now()->subHours(5), 'order_id' => $order->id]);
 
         $logisticZopSync = new LogisticZopSync('000000111');
         $data[] = [
@@ -76,6 +80,8 @@ class LogisticZopSyncTest extends TestCase
     public function transformRoutes疑似丢件Test()
     {
         $order = factory(Order::class)->create(['province' => '上海']);
+        $this->order = $order;
+
         factory(OrderPackage::class)->create(['logistic_number' => '000000111', 'sent_at' => now()->subHours(5), 'order_id' => $order->id]);
         $logisticZopSync = new LogisticZopSync('000000111');
         $data[] = [
@@ -94,6 +100,8 @@ class LogisticZopSyncTest extends TestCase
     public function transformRoutes派送异常Test()
     {
         $order = factory(Order::class)->create(['province' => '上海']);
+        $this->order = $order;
+
         factory(OrderPackage::class)->create(['logistic_number' => '000000111', 'sent_at' => now()->subHours(5), 'order_id' => $order->id]);
         $logisticZopSync = new LogisticZopSync('000000111');
         $data[] = [

+ 6 - 1
tests/Services/LogisticQiaoSFService/LogisticQiaoSFServiceTest.php

@@ -13,6 +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();
     }
 
 
@@ -21,7 +22,11 @@ class LogisticQiaoSFServiceTest extends TestCase
      */
     public function testGet()
     {
-        $response = $this->logisticQiaoSFService->get(['SF1038651915891','SF1038651413847','SF1038611050071']);
+        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(['SF1335376515904', 'SF2050377278135', 'SF2060390000049']);
+        dd($response);
         $this->assertCount(3, $response);
     }
 }