2018-12-25 15:08:11 +08:00

144 lines
4.1 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\Card\Repositories\BlocRepository;
use App\Domains\Card\Repositories\CardRepository;
class CardSync extends Command
{
protected $name = 'virtual:sync-card';
protected $description = '同步VD卡信息数据';
protected static $carrierOperators = [10 => 0, 11 => 1, 12 => 2];
protected $blocs;
protected $limit = 1000;
const FILENAME = 'app/command/sync-card.json';
const INIT_ID = 0;
public function handle()
{
$contents = $this->getFile();
$maxId = $contents['maxId'];
$nextId = $contents['maxId'];
$this->saveFile(1, $maxId);
$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']],
'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));
$this->saveFile(0, $nextId);
if ($page * $this->limit >= $total) {
break;
}
$page++;
}
app(CardRepository::class)->forgetCached();
}
/**
* 获取文件内容
*
* @return void
*/
protected function getFile()
{
$file = storage_path(self::FILENAME);
if (!file_exists($file)) {
$dir = dirname($file);
if (null !== $dir && !is_dir($dir)) {
mkdir($dir, 0777, true);
}
$this->saveFile(0, self::INIT_ID);
}
$contents = file_get_contents($file);
return json_decode($contents, 256);
}
/**
* 写入文件
*
* @param integer $status 状态 1运行中 0运行结束
* @param integer $maxId 最后查询的ID
* @return void
*/
protected function saveFile(int $status, int $maxId)
{
$file = storage_path(self::FILENAME);
$contents = json_encode([
'status' => $status,
'maxId' => $maxId,
]);
file_put_contents($file, $contents);
}
}