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