|
|
@@ -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集合分类并摘取指定数据
|