|
|
@@ -25,48 +25,52 @@ class OrderPackageReceivedSyncService
|
|
|
* 2 如果当前时间小于等于初始化时间,执行初始化脚本,将数据库中全部小于等于初始化时间的数据更新
|
|
|
* @throws Exception
|
|
|
*/
|
|
|
- public function syncLogisticRoute()
|
|
|
+ public function syncLogisticRoute($is_to_init=false)
|
|
|
{
|
|
|
ini_set('max_execution_time', 2 * 60 * 60);
|
|
|
+ ini_set('memory_limit', '1024M');
|
|
|
LogService::log(OrderPackageReceivedSyncService::class, "同步快递信息定时方法", '');
|
|
|
- //初始化时间 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');
|
|
|
+ return $query->select(['id','logistic_id'])->with('logistic:id,name,code');
|
|
|
}]);
|
|
|
- if (Carbon::now()->lte($initDate)) {//当前时间小于等于初始化时间
|
|
|
+ if ($is_to_init) {//当前时间小于等于初始化时间
|
|
|
+ $initDate = Carbon::parse(config('api_logistic.init_date'));
|
|
|
//初始化查询一个月的数据,exception为否
|
|
|
- $query = $query->where('sent_at', '>=', $initDate->subDays((int)config('api_logistic.days'))->toDateTimeString())
|
|
|
+ $query = $query->where('sent_at', '>=', $initDate)
|
|
|
->whereNull('received_at');
|
|
|
} else {//查询20天以内的数据
|
|
|
- $query = $query->where('sent_at', '>=', now()->subDays(20))
|
|
|
+ $query = $query->where('sent_at', '>=', now()->subDays(config('api_logistic.querying_days'))->startOfDay())
|
|
|
->whereNull('received_at');
|
|
|
}
|
|
|
- $query->chunk(2000, function ($orderPackages) {
|
|
|
+ $query->chunk(1000, function ($orderPackages) {
|
|
|
+ LogService::log(OrderPackageReceivedSyncService::class, "同步快递信息定时方法-SF", json_encode(data_get($orderPackages,'*.logistic_number')));
|
|
|
$logisticNumbers = $this->buildData($orderPackages);
|
|
|
//sf
|
|
|
if (array_key_exists('SF', $logisticNumbers)) {
|
|
|
$SFLogisticNumbers = $logisticNumbers['SF'];
|
|
|
- LogService::log(OrderPackageReceivedSyncService::class, "同步快递信息定时方法-SF", '');
|
|
|
+ LogService::log(OrderPackageReceivedSyncService::class, "同步快递信息定时方法-SF", json_encode($SFLogisticNumbers));
|
|
|
foreach ($SFLogisticNumbers as $logisticNumber) {
|
|
|
+ LogService::log(OrderPackageReceivedSyncService::class, "同步SF快递单号", $logisticNumber);
|
|
|
LogisticSFSync::dispatch($logisticNumber);
|
|
|
}
|
|
|
}
|
|
|
//更新中通
|
|
|
if (array_key_exists('ZTO', $logisticNumbers)) {
|
|
|
$ZTOLogisticNumbers = $logisticNumbers['ZTO'];
|
|
|
- LogService::log(OrderPackageReceivedSyncService::class, "同步快递信息定时方法-ZTO", '');
|
|
|
+ LogService::log(OrderPackageReceivedSyncService::class, "同步快递信息定时方法-ZTO", json_encode($ZTOLogisticNumbers));
|
|
|
foreach ($ZTOLogisticNumbers as $logisticNumber) {
|
|
|
+ LogService::log(OrderPackageReceivedSyncService::class, "同步ZTO快递单号", $logisticNumber);
|
|
|
LogisticZopSync::dispatch($logisticNumber);
|
|
|
}
|
|
|
}
|
|
|
//更新韵达
|
|
|
if (array_key_exists('YUNDA', $logisticNumbers)) {
|
|
|
$YDLogisticNumbers = $logisticNumbers['YUNDA'];
|
|
|
- LogService::log(OrderPackageReceivedSyncService::class, "同步快递信息定时方法-YUNDA", '');
|
|
|
+ LogService::log(OrderPackageReceivedSyncService::class, "同步快递信息定时方法-YUNDA", json_encode($YDLogisticNumbers));
|
|
|
foreach ($YDLogisticNumbers as $logistic_number) {
|
|
|
+ LogService::log(OrderPackageReceivedSyncService::class, "同步YUNDA快递单号", $logisticNumber);
|
|
|
LogisticYDSync::dispatch($logistic_number);
|
|
|
}
|
|
|
}
|
|
|
@@ -185,31 +189,31 @@ class OrderPackageReceivedSyncService
|
|
|
|
|
|
/**
|
|
|
* 根据快递单号更新状态
|
|
|
- * @param array $orderPackages
|
|
|
+ * @param array $logisticResponses
|
|
|
*/
|
|
|
- public function update(array $orderPackages)
|
|
|
+ public function update(array $logisticResponses)
|
|
|
{
|
|
|
- foreach ($orderPackages as $data) {
|
|
|
- if (empty($data)) continue;
|
|
|
- $orderPackage = OrderPackage::query()->where('logistic_number', $data['logistic_number'])->first();
|
|
|
+ foreach ($logisticResponses as $logisticResponse) {
|
|
|
+ if (empty($logisticResponse)) continue;
|
|
|
+ $orderPackage = OrderPackage::query()->where('logistic_number', $logisticResponse['logistic_number'])->first();
|
|
|
try {
|
|
|
if ($orderPackage->order && $orderPackage->order->issue) {
|
|
|
- unset($data['exception_type']);
|
|
|
- unset($data['exception']);
|
|
|
+ unset($logisticResponse['exception_type']);
|
|
|
+ unset($logisticResponse['exception']);
|
|
|
}
|
|
|
} catch (Exception $e) {
|
|
|
- LogService::log(OrderPackageReceivedSyncService::class, "标记问题件不需要更新异常状态失败", $data['logistic_number'] . '-' . json_encode($e));
|
|
|
+ LogService::log(OrderPackageReceivedSyncService::class, "标记问题件不需要更新异常状态失败", $logisticResponse['logistic_number'] . '-' . json_encode($e));
|
|
|
}
|
|
|
//如果已经收货,将异常更新为正常
|
|
|
- if (array_key_exists('received_at', $data)) {
|
|
|
- $data['exception_type'] = '无';
|
|
|
- $data['exception'] = '否';
|
|
|
+ if ($logisticResponse['received_at']??false) {
|
|
|
+ $logisticResponse['exception_type'] = '无';
|
|
|
+ $logisticResponse['exception'] = '否';
|
|
|
}
|
|
|
- if (isset($data['status'])) $orderPackage->status = $data['status'];
|
|
|
- if (isset($data['received_at'])) $orderPackage->received_at = $data['received_at'];
|
|
|
- if (isset($data['exception'])) $orderPackage->exception = $data['exception'];
|
|
|
- if (isset($data['transfer_status']) && !empty($data['transfer_status'])) $orderPackage->transfer_status = $data['transfer_status'];
|
|
|
- if (isset($data['exception_type'])) $orderPackage->exception_type = $data['exception_type'];
|
|
|
+ if (isset($logisticResponse['status'])) $orderPackage->status = $logisticResponse['status'];
|
|
|
+ if (isset($logisticResponse['received_at'])) $orderPackage->received_at = $logisticResponse['received_at'];
|
|
|
+ if (isset($logisticResponse['exception'])) $orderPackage->exception = $logisticResponse['exception'];
|
|
|
+ if (isset($logisticResponse['transfer_status']) && !empty($logisticResponse['transfer_status'])) $orderPackage->transfer_status = $logisticResponse['transfer_status'];
|
|
|
+ if (isset($logisticResponse['exception_type'])) $orderPackage->exception_type = $logisticResponse['exception_type'];
|
|
|
$orderPackage->save();
|
|
|
}
|
|
|
}
|
|
|
@@ -227,6 +231,7 @@ class OrderPackageReceivedSyncService
|
|
|
try {
|
|
|
$logisticCode = $orderPackage->order->logistic->code;
|
|
|
} catch (Exception $e) {
|
|
|
+ LogService::log(OrderPackageReceivedSyncService::class, "快递同步按照承运商分组异常", json_encode($orderPackage??[]));
|
|
|
continue;
|
|
|
}
|
|
|
$key = config('api_logistic.logistic.' . $logisticCode);
|
|
|
@@ -362,7 +367,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'] < 3) ? $IS_SECOND_ROUTE_HAVE : 0;//超过指定时间,路由信息小于两条
|
|
|
+ $conclusion |= ($delivered_duration > $HAVEN_SECOND_GOT_HOURS && $data['routes_length'] < 3) ? $IS_SECOND_ROUTE_HAVE : 0;//和出库时间比较 超过指定时间,路由信息小于三条
|
|
|
return $conclusion;
|
|
|
})();
|
|
|
switch ($conclusion) {
|
|
|
@@ -389,7 +394,7 @@ class OrderPackageReceivedSyncService
|
|
|
}
|
|
|
if ($conclusion
|
|
|
== ($conclusion | $IS_SECOND_ROUTE_HAVE)) {
|
|
|
- $data['exception_type'] = '派件异常';
|
|
|
+ $data['exception_type'] = '揽件异常';
|
|
|
$data['exception'] = '是';
|
|
|
|
|
|
}
|