101 lines
3.6 KiB
PHP
101 lines
3.6 KiB
PHP
<?php
|
|
|
|
namespace App\Domains\Virtual\Commands\Sync;
|
|
|
|
use Illuminate\Console\Command;
|
|
use App\Models\Virtual\FlowPool;
|
|
use Illuminate\Support\Facades\DB;
|
|
use App\Exceptions\NotExistException;
|
|
use App\Models\Virtual\FlowPoolMonth;
|
|
use App\Models\Real\FlowPool as RealFlowPool;
|
|
use App\Domains\Virtual\Repositories\PackageRepository;
|
|
use App\Domains\Virtual\Services\FlowPoolService;
|
|
|
|
class FlowPoolMonthSync extends Command
|
|
{
|
|
protected $name = 'virtual:sync-flow-pool-month';
|
|
|
|
protected $description = '同步VD流量池每月数据';
|
|
|
|
public function handle()
|
|
{
|
|
$this->line($this->description);
|
|
$realFlowPools = RealFlowPool::select(['id', 'sn'])->get()->keyBy('sn');
|
|
$flowPools = FlowPool::withTrashed()->select(['id', 'real_pool_ids', 'name'])->get();
|
|
$flowPoolsKeyByName = $flowPools->keyBy('name');
|
|
$packages = app(PackageRepository::class)->withTrashed()->get()->keyBy('sn');
|
|
|
|
$old_stats = DB::connection('vd_old')->table('ckb_data_pool_statis')->orderBy('dpmb_id')->get()->collect()->toArray();
|
|
|
|
$array = [];
|
|
|
|
foreach ($old_stats as $stat) {
|
|
$this->line($stat['dpmb_id'] . '-' . $stat['year_month'] . '-' . $stat['td_pool_sn']);
|
|
|
|
$pool_id = 0;
|
|
|
|
if (strpos($stat['td_pool_sn'], 'vd-') === 0) {
|
|
$pool_id = $flowPoolsKeyByName[str_replace('vd-', '', $stat['td_pool_sn'])]['id'];
|
|
} else {
|
|
$real_pool_id = $realFlowPools[$stat['td_pool_sn']]['id'];
|
|
|
|
if (!$real_pool_id) {
|
|
throw new NotExistException('未找到RD流量池 #:' . $stat['td_pool_sn']);
|
|
}
|
|
|
|
foreach ($flowPools as $key => $value) {
|
|
if (in_array($real_pool_id, $value['real_pool_ids'])) {
|
|
$pool_id = $value['id'];
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
|
|
if (!$pool_id) {
|
|
throw new NotExistException('未找到对应VD流量池 #:' . $stat['td_pool_sn']);
|
|
}
|
|
|
|
$items = DB::connection('vd_old')->table('ckb_data_pool_statis_item')
|
|
->select(['sim', 'vd_package_sn', 'flows_used'])
|
|
->where('pool_statis_id', $stat['dpmb_id'])->get()->collect()->toArray();
|
|
|
|
$items = array_map(function ($item) use ($packages, $stat, $pool_id) {
|
|
return [
|
|
'month' => $stat['year_month'],
|
|
'sim' => intval($item['sim']),
|
|
'package_id' => $packages[$item['vd_package_sn']]['id'],
|
|
'pool_id' => $pool_id,
|
|
'mebibyte' => floatval($item['flows_used']),
|
|
];
|
|
}, $items);
|
|
|
|
$items = array_groupBy($items, 'sim');
|
|
|
|
foreach ($items as $sim => $group) {
|
|
if (count($group) > 1) {
|
|
$temp = $group[0];
|
|
$temp['mebibyte'] = array_sum(array_pluck($group, 'mebibyte'));
|
|
$items[$sim] = [$temp];
|
|
}
|
|
}
|
|
|
|
$array = array_merge($array, array_collapse($items));
|
|
}
|
|
|
|
DB::transaction(function () use ($array) {
|
|
$this->line('插入数据,条数 #:' . count($array));
|
|
|
|
foreach (array_groupBy($array, 'month') as $month => $group) {
|
|
$table = FlowPoolService::checkTable($month);
|
|
|
|
foreach (array_chunk($group, 1000) as $values) {
|
|
echo '.';
|
|
app(FlowPoolMonth::class)->setTable($table)->upsert($values, ['sim', 'month']);
|
|
}
|
|
}
|
|
});
|
|
|
|
$this->line($this->description . ' End');
|
|
}
|
|
}
|