vd/app/Domains/Real/Commands/Sync/ActivatedSync.php
2019-03-22 11:00:05 +08:00

82 lines
2.5 KiB
PHP

<?php
namespace App\Domains\Real\Commands\Sync;
use Carbon\Carbon;
use App\Models\Card\Card;
use App\Models\Mongo\TblCard;
use MongoDB\BSON\UTCDateTime;
use Illuminate\Support\Facades\DB;
use App\Domains\Card\Repositories\CardRepository;
use App\Domains\Virtual\Repositories\OrderCardPartitionRepository;
class ActivatedSync extends Command
{
protected $name = 'real:sync-activated';
protected $description = '同步激活数据(每天同步昨天的激活数据)';
protected $limit = 1000;
public function handle()
{
$day = $this->getDay();
$startMicrotime = new UTCDateTime($day->startOfDay());
$endMicrotime = new UTCDateTime($day->endOfDay());
$query = TblCard::where('pNo', 'No00000000768')->where('isDel', 0)
->where('saDate', 'exists', true)
->where('bNo', 'exists', true)
->where('saDate', '>=', $startMicrotime)
->where('saDate', '<=', $endMicrotime);
$total = $query->count();
$this->line('待同步条数:'.$total);
$page = 1;
while ($total) {
if (($page - 1) * $this->limit >= $total) {
break;
}
$res = $query->select(['cNo', 'saDate'])
->forPage($page, $this->limit)->get();
$array = $res->map(function ($item) {
return [
'sim' => $item['cNo'],
'activated_at' => $item['saDate'] ? $item['saDate']->toDateTime()->format('Y-m-d H:i:s') : null,
];
})->all();
$table = app(Card::class)->getTable();
$as = 'as_table';
$reference = 'sim';
$updates = "activated_at={$as}.activated_at::timestamp,
virtual_activated_at=COALESCE({$table}.activated_at, {$as}.activated_at::timestamp)::timestamp";
$parameters = collect($array)->map(function ($item) {
return "({$item['sim']}, '{$item['activated_at']}')";
})->implode(', ');
$from = "FROM (VALUES $parameters) AS {$as}(sim, activated_at)";
$where = "WHERE {$table}.{$reference} = {$as}.{$reference}::int8";
$sql = trim("UPDATE {$table} SET {$updates} {$from} {$where}");
$simArray = implode(',', array_pluck($array, 'sim'));
DB::statement($sql);
DB::statement("select fix_timelines('{{$simArray}}'::INT8[]);");
$page++;
}
app(CardRepository::class)->forgetCached();
app(OrderCardPartitionRepository::class)->forgetCached();
}
}