95 lines
3.4 KiB
PHP
95 lines
3.4 KiB
PHP
<?php
|
|
|
|
namespace App\Domains\Virtual\Commands\Sync;
|
|
|
|
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');
|
|
|
|
$array = [];
|
|
|
|
$query = DB::connection('vd_old')->table('ckb_data_pool_statis_item')->select([
|
|
'td_pool_sn', 'sim', 'vd_package_sn', 'flows_used', 'year_month'
|
|
])->orderBy('dpci_id');
|
|
|
|
$total = $query->count();
|
|
|
|
$this->line('查询数据,条数 #:' . $total);
|
|
|
|
$query->chunk(10000, function ($chunk) use (&$array, $flowPoolsKeyByName, $realFlowPools, $flowPools, $packages) {
|
|
echo '.';
|
|
foreach ($chunk as $item) {
|
|
$item = (array)$item;
|
|
$pool_id = 0;
|
|
|
|
if (strpos($item['td_pool_sn'], 'vd-') === 0) {
|
|
$pool_id = $flowPoolsKeyByName[str_replace('vd-', '', $item['td_pool_sn'])]['id'];
|
|
} else {
|
|
$real_pool_id = $realFlowPools[$item['td_pool_sn']]['id'];
|
|
|
|
if (!$real_pool_id) {
|
|
throw new NotExistException('未找到RD流量池 #:' . $item['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流量池 #:' . $item['td_pool_sn']);
|
|
}
|
|
|
|
$sim = intval($item['sim']);
|
|
$i = $sim . '-' . $item['year_month'];
|
|
$mebibyte = isset($array[$i]) ? $array[$i]['mebibyte'] + floatval($item['flows_used']) : floatval($item['flows_used']);
|
|
|
|
$array[$i] = [
|
|
'month' => $item['year_month'],
|
|
'sim' => $sim,
|
|
'package_id' => $packages[$item['vd_package_sn']]['id'],
|
|
'pool_id' => $pool_id,
|
|
'mebibyte' => $mebibyte,
|
|
];
|
|
}
|
|
});
|
|
|
|
$array = array_values($array);
|
|
|
|
$this->line('插入数据,条数 #:' . count($array));
|
|
|
|
$array = array_groupBy($array, 'month');
|
|
|
|
foreach ($array as $month => $items) {
|
|
$table = FlowPoolService::checkTable($month);
|
|
foreach (array_chunk($items, 1000) as $values) {
|
|
echo '.';
|
|
app(FlowPoolMonth::class)->setTable($table)->upsert($values, ['sim', 'month']);
|
|
}
|
|
}
|
|
|
|
$this->line($this->description . ' End');
|
|
}
|
|
}
|