98 lines
3.2 KiB
PHP
98 lines
3.2 KiB
PHP
<?php
|
|
|
|
namespace App\Domains\Virtual\Commands\Sync;
|
|
|
|
use Carbon\Carbon;
|
|
use App\Models\Card\Card;
|
|
use Illuminate\Support\Arr;
|
|
use Illuminate\Support\Facades\DB;
|
|
use App\Domains\Config\Services\ConfigService;
|
|
use App\Domains\Card\Repositories\BlocRepository;
|
|
use App\Domains\Card\Repositories\CardRepository;
|
|
|
|
class CardSync extends Command
|
|
{
|
|
protected $name = 'virtual:sync-card';
|
|
|
|
protected $description = '同步VD卡信息数据';
|
|
|
|
const CURSOR_KEY = 'sync_card_cursor';
|
|
|
|
protected static $carrierOperators = [10 => 0, 11 => 1, 12 => 2];
|
|
|
|
protected $blocs;
|
|
|
|
protected $limit = 1000;
|
|
|
|
public function handle()
|
|
{
|
|
$nextId = $maxId = app(ConfigService::class)->get(self::CURSOR_KEY) ?: 0;
|
|
|
|
$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', '>', $maxId);
|
|
|
|
$total = $query->count();
|
|
|
|
$this->line('待同步条数:' . $total);
|
|
|
|
if ($total) {
|
|
$this->blocs = app(BlocRepository::class)->get()->pluck('id', 'shorthand')->toArray();
|
|
}
|
|
|
|
$page = 1;
|
|
|
|
while ($total) {
|
|
echo 'nextId #: ' . $nextId . PHP_EOL;
|
|
$res = $query->offset(($page - 1) * $this->limit)->limit($this->limit)->get();
|
|
|
|
if (empty($res)) {
|
|
break;
|
|
}
|
|
|
|
$array = [];
|
|
|
|
foreach ($res as $key => $value) {
|
|
$value = (array)$value;
|
|
|
|
$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' => date('Y-m-d H:i:s', $value['card_cycle_start']),
|
|
'virtual_activated_at' => date('Y-m-d H:i:s', $value['card_cycle_start']),
|
|
'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']),
|
|
];
|
|
|
|
$nextId = $value['id'];
|
|
}
|
|
|
|
$builder = Card::query()->toBase();
|
|
|
|
$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';
|
|
|
|
$builder->connection->insert($sql, Arr::flatten($array, 1));
|
|
|
|
app(ConfigService::class)->set(self::CURSOR_KEY, $nextId);
|
|
|
|
if ($page * $this->limit >= $total) {
|
|
break;
|
|
}
|
|
|
|
$page++;
|
|
}
|
|
|
|
app(CardRepository::class)->forgetCached();
|
|
}
|
|
}
|