|
|
@@ -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;
|
|
|
+ }
|
|
|
}
|