优化同步
This commit is contained in:
parent
d4727afda4
commit
266f875c72
@ -5,6 +5,7 @@ namespace App\Domains\Virtual\Commands\Sync;
|
|||||||
use Carbon\Carbon;
|
use Carbon\Carbon;
|
||||||
use App\Models\Card\Card;
|
use App\Models\Card\Card;
|
||||||
use Illuminate\Support\Arr;
|
use Illuminate\Support\Arr;
|
||||||
|
use Illuminate\Support\Collection;
|
||||||
use Illuminate\Support\Facades\DB;
|
use Illuminate\Support\Facades\DB;
|
||||||
use Illuminate\Support\Facades\Artisan;
|
use Illuminate\Support\Facades\Artisan;
|
||||||
use App\Domains\Virtual\Jobs\CardSyncJob;
|
use App\Domains\Virtual\Jobs\CardSyncJob;
|
||||||
@ -40,16 +41,20 @@ class CardSync extends Command
|
|||||||
|
|
||||||
$page = 1;
|
$page = 1;
|
||||||
|
|
||||||
|
$jobs = new Collection();
|
||||||
|
|
||||||
while ($total) {
|
while ($total) {
|
||||||
dispatch(new CardSyncJob($page, $this->limit, $maxId));
|
$page++;
|
||||||
|
|
||||||
if ($page * $this->limit >= $total) {
|
if ($page * $this->limit >= $total) {
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
$page++;
|
$jobs->push(new CardSyncJob($page, $this->limit, $maxId));
|
||||||
}
|
}
|
||||||
|
|
||||||
app(ConfigService::class)->set(self::CURSOR_KEY, $nextId);
|
app(ConfigService::class)->set(self::CURSOR_KEY, $nextId);
|
||||||
|
|
||||||
|
CardSyncJob::withChain($jobs->toArray())->dispatch(1, $this->limit, $maxId);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -46,16 +46,20 @@ class LogSync extends Command
|
|||||||
|
|
||||||
$page = 1;
|
$page = 1;
|
||||||
|
|
||||||
|
$jobs = new Collection();
|
||||||
|
|
||||||
while ($total) {
|
while ($total) {
|
||||||
dispatch(new LogSyncJob($page, $this->limit, $maxId));
|
$page++;
|
||||||
|
|
||||||
if ($page * $this->limit >= $total) {
|
if ($page * $this->limit >= $total) {
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
$page++;
|
$jobs->push(new LogSyncJob($page, $this->limit, $maxId));
|
||||||
}
|
}
|
||||||
|
|
||||||
app(ConfigService::class)->set(self::CURSOR_KEY, $nextId);
|
app(ConfigService::class)->set(self::CURSOR_KEY, $nextId);
|
||||||
|
|
||||||
|
LogSyncJob::withChain($jobs->toArray())->dispatch(1, $this->limit, $maxId);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -7,12 +7,14 @@ use Illuminate\Support\Arr;
|
|||||||
use Illuminate\Bus\Queueable;
|
use Illuminate\Bus\Queueable;
|
||||||
use Illuminate\Support\Carbon;
|
use Illuminate\Support\Carbon;
|
||||||
use Illuminate\Support\Facades\DB;
|
use Illuminate\Support\Facades\DB;
|
||||||
|
use Illuminate\Support\Facades\Log;
|
||||||
use Dipper\Foundation\Bus\Dispatchable;
|
use Dipper\Foundation\Bus\Dispatchable;
|
||||||
use Illuminate\Support\Facades\Artisan;
|
use Illuminate\Support\Facades\Artisan;
|
||||||
use Illuminate\Contracts\Queue\ShouldQueue;
|
use Illuminate\Contracts\Queue\ShouldQueue;
|
||||||
|
use App\Domains\Config\Services\ConfigService;
|
||||||
|
use App\Domains\Virtual\Commands\Sync\CardSync;
|
||||||
use App\Domains\Card\Repositories\BlocRepository;
|
use App\Domains\Card\Repositories\BlocRepository;
|
||||||
use App\Domains\Card\Repositories\CardRepository;
|
use App\Domains\Card\Repositories\CardRepository;
|
||||||
use App\Domains\Config\Services\ConfigService;
|
|
||||||
|
|
||||||
class CardSyncJob implements ShouldQueue
|
class CardSyncJob implements ShouldQueue
|
||||||
{
|
{
|
||||||
@ -48,12 +50,17 @@ class CardSyncJob implements ShouldQueue
|
|||||||
$blocs = app(BlocRepository::class)->get()->pluck('id', 'shorthand')->toArray();
|
$blocs = app(BlocRepository::class)->get()->pluck('id', 'shorthand')->toArray();
|
||||||
|
|
||||||
$query = DB::connection('vd_old')->table('ckb_custom')
|
$query = DB::connection('vd_old')->table('ckb_custom')
|
||||||
->select(['id', 'custom_no', 'imsi', 'carrieroperator', 'iccid', 'card_number', 'card_from', 'iccid', 'company', 'custom_state', 'create_time' ,'update_time', 'card_cycle_start'])
|
->select(['id', 'custom_no', 'imsi', 'carrieroperator', 'iccid', 'card_number', 'card_from', 'iccid', 'company', 'custom_state', 'create_time' ,'update_time', 'card_cycle_start'])
|
||||||
->where('id', '>', $this->maxId)
|
->where('id', '>', $this->maxId)
|
||||||
->orderBy('id');
|
->orderBy('id');
|
||||||
|
|
||||||
$res = $query->forPage($this->page, $this->limit)->get();
|
$res = $query->forPage($this->page, $this->limit)->get();
|
||||||
|
|
||||||
|
if (empty($res)) {
|
||||||
|
Log::notice('CardSyncJob not result!');
|
||||||
|
return ;
|
||||||
|
}
|
||||||
|
|
||||||
$array = [];
|
$array = [];
|
||||||
|
|
||||||
foreach ($res as $key => $value) {
|
foreach ($res as $key => $value) {
|
||||||
@ -66,17 +73,17 @@ class CardSyncJob implements ShouldQueue
|
|||||||
}
|
}
|
||||||
|
|
||||||
$array[] = [
|
$array[] = [
|
||||||
'sim' => $value['card_number'],
|
'sim' => $value['card_number'],
|
||||||
'imsi' => $value['imsi'],
|
'imsi' => $value['imsi'],
|
||||||
'iccid' => $value['iccid'],
|
'iccid' => $value['iccid'],
|
||||||
'bloc_id' => $this->blocs[$value['card_from']] ?? 0,
|
'bloc_id' => $this->blocs[$value['card_from']] ?? 0,
|
||||||
'carrier_operator' => self::$carrierOperators[$value['carrieroperator']] ?? 255,
|
'carrier_operator' => self::$carrierOperators[$value['carrieroperator']] ?? 255,
|
||||||
'type' => ($value['card_number'][3] >= 5) ? 1 : 0,
|
'type' => ($value['card_number'][3] >= 5) ? 1 : 0,
|
||||||
'activated_at' => $virtual_activated_at,
|
'activated_at' => $virtual_activated_at,
|
||||||
'virtual_activated_at' => $virtual_activated_at,
|
'virtual_activated_at' => $virtual_activated_at,
|
||||||
'cancelled_at' => ($value['custom_state'] === 13) ? date('Y-m-d H:i:s', $value['update_time']) : null,
|
'cancelled_at' => ($value['custom_state'] === 13) ? date('Y-m-d H:i:s', $value['update_time']) : null,
|
||||||
'created_at' => date('Y-m-d H:i:s', $value['create_time']),
|
'created_at' => date('Y-m-d H:i:s', $value['create_time']),
|
||||||
'updated_at' => date('Y-m-d H:i:s', $value['update_time']),
|
'updated_at' => date('Y-m-d H:i:s', $value['update_time']),
|
||||||
];
|
];
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -85,12 +92,16 @@ class CardSyncJob implements ShouldQueue
|
|||||||
$sql = $builder->getGrammar()->compileInsert($builder, $array);
|
$sql = $builder->getGrammar()->compileInsert($builder, $array);
|
||||||
|
|
||||||
$sql .= 'on conflict (sim) do update set
|
$sql .= 'on conflict (sim) do update set
|
||||||
activated_at=COALESCE(cards.activated_at, excluded.virtual_activated_at),
|
activated_at=COALESCE(cards.activated_at, excluded.virtual_activated_at),
|
||||||
virtual_activated_at=excluded.virtual_activated_at,
|
virtual_activated_at=excluded.virtual_activated_at,
|
||||||
cancelled_at=excluded.cancelled_at';
|
cancelled_at=excluded.cancelled_at';
|
||||||
|
|
||||||
$builder->connection->insert($sql, Arr::flatten($array, 1));
|
try {
|
||||||
|
$builder->connection->insert($sql, Arr::flatten($array, 1));
|
||||||
app(CardRepository::class)->forgetCached();
|
app(CardRepository::class)->forgetCached();
|
||||||
|
} catch (\Exception $e) {
|
||||||
|
$nextId = $res->first()->id - 1;
|
||||||
|
app(ConfigService::class)->set(CardSync::CURSOR_KEY, $nextId);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -8,10 +8,12 @@ use App\Models\Virtual\Order;
|
|||||||
use Illuminate\Bus\Queueable;
|
use Illuminate\Bus\Queueable;
|
||||||
use Illuminate\Support\Carbon;
|
use Illuminate\Support\Carbon;
|
||||||
use Illuminate\Support\Facades\DB;
|
use Illuminate\Support\Facades\DB;
|
||||||
|
use Illuminate\Support\Facades\Log;
|
||||||
use Dipper\Foundation\Bus\Dispatchable;
|
use Dipper\Foundation\Bus\Dispatchable;
|
||||||
use Illuminate\Support\Facades\Artisan;
|
use Illuminate\Support\Facades\Artisan;
|
||||||
use Illuminate\Contracts\Queue\ShouldQueue;
|
use Illuminate\Contracts\Queue\ShouldQueue;
|
||||||
use App\Domains\Config\Services\ConfigService;
|
use App\Domains\Config\Services\ConfigService;
|
||||||
|
use App\Domains\Virtual\Commands\Sync\LogSync;
|
||||||
use App\Domains\Virtual\Services\ProductService;
|
use App\Domains\Virtual\Services\ProductService;
|
||||||
use App\Domains\Card\Repositories\BlocRepository;
|
use App\Domains\Card\Repositories\BlocRepository;
|
||||||
use App\Domains\Card\Repositories\CardRepository;
|
use App\Domains\Card\Repositories\CardRepository;
|
||||||
@ -73,6 +75,11 @@ class LogSyncJob implements ShouldQueue
|
|||||||
|
|
||||||
$res = $query->forPage($this->page, $this->limit)->get();
|
$res = $query->forPage($this->page, $this->limit)->get();
|
||||||
|
|
||||||
|
if (empty($res)) {
|
||||||
|
Log::notice('LogSyncJob not result!');
|
||||||
|
return ;
|
||||||
|
}
|
||||||
|
|
||||||
DB::beginTransaction();
|
DB::beginTransaction();
|
||||||
|
|
||||||
foreach ($res as $key => $value) {
|
foreach ($res as $key => $value) {
|
||||||
@ -143,6 +150,8 @@ class LogSyncJob implements ShouldQueue
|
|||||||
}
|
}
|
||||||
} catch (\Exception $e) {
|
} catch (\Exception $e) {
|
||||||
DB::rollback();
|
DB::rollback();
|
||||||
|
$nextId = $res->first()->id - 1;
|
||||||
|
app(ConfigService::class)->set(LogSync::CURSOR_KEY, $nextId);
|
||||||
throw $e;
|
throw $e;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user