69 lines
2.2 KiB
PHP
69 lines
2.2 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\PackageRepository;
|
|
|
|
class ActivateSync extends Command
|
|
{
|
|
protected $name = 'sync:activate';
|
|
|
|
protected $description = '同步RD激活数据';
|
|
|
|
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('查询套餐周期');
|
|
$packages = app(PackageRepository::class)->get()->pluck('service_cycle', 'id')->toArray();
|
|
|
|
$this->line('查询激活数据');
|
|
$res = DB::connection('mongo')->table('tblCard')->select(['cNo', 'saDate', 'sPCode'])
|
|
->where('pNo', 'No00000000768')
|
|
->where('isDel', '<>', 1)
|
|
->whereBetween('saDate', [$starttime, $endtime])->get();
|
|
|
|
$list = $res->map(function ($item) use ($packages) {
|
|
$activate_at = $item['saDate']->toDateTime()->format('Y-m-d H:i:s');
|
|
$month = $packages[$item['sPCode']];
|
|
|
|
if (!$month) {
|
|
throw new \Exception('未找到套餐数据:' . $item['sPCode']);
|
|
}
|
|
|
|
return [
|
|
'sim' => $item['cNo'],
|
|
'activate_at' => $activate_at,
|
|
'service_start_at' => $activate_at,
|
|
'service_end_at' => Carbon::parse($activate_at)->addMonths($month)->format('Y-m-d H:i:s'),
|
|
];
|
|
})->toArray();
|
|
|
|
$this->line('更新卡表数据,总计更新条数:' . count($list));
|
|
$total = 0;
|
|
$sims = array_pluck($list, 'sim');
|
|
|
|
foreach (array_chunk($sims, 30000) as $data) {
|
|
echo '.';
|
|
$total += Card::whereIn('sim', array_pluck($data, 'sim'))->count();
|
|
}
|
|
|
|
$except = count($list) - $total;
|
|
|
|
$this->line("其中有{$except}张卡不在表内");
|
|
|
|
foreach (array_chunk($list, 3000) as $data) {
|
|
echo '.';
|
|
Card::updateBatch($data, 'sim');
|
|
}
|
|
|
|
$this->line('更新数据成功');
|
|
}
|
|
}
|