82 lines
2.6 KiB
PHP
82 lines
2.6 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()->toArray();
|
|
|
|
$list = [];
|
|
$total = [];
|
|
|
|
$this->line('拼装更新数据:'.count($res));
|
|
$chucks = array_chunk($res, 10000);
|
|
foreach ($chucks as $chuck) {
|
|
echo '.';
|
|
$cards = Card::select(['sim', 'real_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 '未找到卡' . $value['sim'] . PHP_EOL;
|
|
continue;
|
|
}
|
|
|
|
$activate_at = $card['saDate']->toDateTime()->format('Y-m-d H:i:s');
|
|
|
|
$month = $packages[$value['real_package_id']];
|
|
|
|
if (!$month) {
|
|
throw new \Exception('未找到套餐数据:' . $value['real_package_id']);
|
|
}
|
|
|
|
$list[] = [
|
|
'sim' => $value['sim'],
|
|
'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'),
|
|
];
|
|
}
|
|
}
|
|
|
|
$this->line('更新卡表数据,总计更新条数:' . count($list));
|
|
|
|
$except = count($list) - $total;
|
|
|
|
$this->line("其中有{$except}张卡不在表内");
|
|
|
|
foreach (array_chunk($list, 3000) as $data) {
|
|
echo '.';
|
|
Card::updateBatch($data, 'sim');
|
|
}
|
|
|
|
$this->line('更新数据成功');
|
|
}
|
|
}
|