vd/app/Domains/Real/Commands/Sync/ActivateSync.php
2018-12-05 16:24:44 +08:00

76 lines
2.3 KiB
PHP

<?php
namespace App\Domains\Real\Commands\Sync;
use Carbon\Carbon;
use App\Models\Real\Card;
use MongoDB\BSON\UTCDateTime;
use Illuminate\Support\Facades\DB;
use App\Domains\Real\Repositories\CardRepository;
use App\Domains\Real\Repositories\PackageRepository;
class ActivateSync extends Command
{
protected $name = 'real:sync-activate';
protected $description = '同步RD激活数据';
protected $chunks = 1000;
public function handle()
{
$datetime = $this->getDateTime();
$starttime = new UTCDateTime($datetime->copy()->startOfDay()->startOfMonth()->timestamp * 1000);
$endtime = new UTCDateTime($datetime->copy()->endOfDay()->endOfMonth()->timestamp * 1000);
$this->line('查询激活数据');
$res = DB::connection('mongo')->table('tblCard')->select(['cNo', 'saDate', 'sPCode'])
->where('pNo', 'No00000000768')
->where('isDel', '<>', 1)
->whereBetween('saDate', [$starttime, $endtime])->get()->toArray();
$list = [];
$total = 0;
$this->line('拼装更新数据:'.count($res));
$chucks = array_chunk($res, $this->chunks);
foreach ($chucks as $chuck) {
echo '.';
$cards = Card::select(['sim', 'package_id'])->whereIn('sim', array_pluck($chuck, 'cNo'))->get()->keyBy('sim')->toArray();
$total += count($cards);
foreach ($chuck as $card) {
$value = $cards[$card['cNo']];
if (!$value) {
echo '未找到卡' . $card['cNo'] . PHP_EOL;
continue;
}
$activate_at = $card['saDate']->toDateTime()->format('Y-m-d H:i:s');
$list[] = [
'sim' => $value['sim'],
'activate_at' => $activate_at,
];
}
}
$this->line('更新卡表数据,总计更新条数:' . count($list));
$except = count($list) - $total;
$this->line("其中有{$except}张卡不在表内");
foreach (array_chunk($list, 3000) as $data) {
echo '.';
app(CardRepository::class)->updateActivateAt($data);
}
app(CardRepository::class)->forgetCached();
$this->line('更新数据成功');
}
}