梯度
This commit is contained in:
parent
07660724d4
commit
bcfcca3ec8
148
app/Domains/Virtual/Commands/Sync/FlowPoolSync.php
Normal file
148
app/Domains/Virtual/Commands/Sync/FlowPoolSync.php
Normal file
@ -0,0 +1,148 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Domains\Virtual\Commands\Sync;
|
||||||
|
|
||||||
|
use Carbon\Carbon;
|
||||||
|
use Illuminate\Console\Command;
|
||||||
|
use App\Models\Virtual\FlowPool;
|
||||||
|
use Illuminate\Support\Facades\DB;
|
||||||
|
use App\Domains\Virtual\Services\CommonService;
|
||||||
|
use App\Domains\Virtual\Repositories\CompanyRepository;
|
||||||
|
use App\Domains\Virtual\Repositories\PackageRepository;
|
||||||
|
use App\Domains\Virtual\Repositories\FlowPoolRepository;
|
||||||
|
use App\Models\Virtual\FlowPoolSetting;
|
||||||
|
use App\Domains\Virtual\Repositories\FlowPoolSettingRepository;
|
||||||
|
|
||||||
|
class FlowPoolSync extends Command
|
||||||
|
{
|
||||||
|
protected $name = 'virtual:sync-flow-pool';
|
||||||
|
|
||||||
|
protected $description = '同步VD流量池';
|
||||||
|
|
||||||
|
protected static $carrier_operator = [10 => 0, 11 => 1, 12 => 2];
|
||||||
|
protected static $shared = [10 => 2, 11 => 1];
|
||||||
|
|
||||||
|
public function handle()
|
||||||
|
{
|
||||||
|
$companies = app(CompanyRepository::class)->withTrashed()->get()->keyBy('sn');
|
||||||
|
$packages = app(PackageRepository::class)->withTrashed()->get()->keyBy('sn');
|
||||||
|
$flowPools = FlowPool::withTrashed()->select(['id', 'sn'])->get()->keyBy('sn');
|
||||||
|
|
||||||
|
$oldFlowPools = DB::connection('vd_old')->table('ckb_data_pool')->select([
|
||||||
|
'dp_sn',
|
||||||
|
'dp_name',
|
||||||
|
'dp_carrieroperator',
|
||||||
|
'dp_share_type',
|
||||||
|
'dp_vd_company_id',
|
||||||
|
'dp_vd_packages',
|
||||||
|
'dp_td_data_pool_id',
|
||||||
|
'dp_del',
|
||||||
|
'dp_create_time',
|
||||||
|
])->get()->collect()->toArray();
|
||||||
|
|
||||||
|
$oldFlowPoolSettings = DB::connection('vd_old')->table('ckb_data_pool_setting')->select([
|
||||||
|
'dp_sn',
|
||||||
|
'year_month',
|
||||||
|
'dp_billing_rules',
|
||||||
|
'dp_min_cost_rules',
|
||||||
|
])->get()->collect()->groupBy('dp_sn')->toArray();
|
||||||
|
|
||||||
|
$maxId = FlowPool::withTrashed()->max('id');
|
||||||
|
|
||||||
|
$array = [];
|
||||||
|
$settingArray = [];
|
||||||
|
|
||||||
|
foreach ($oldFlowPools as $key => $value) {
|
||||||
|
$id = ++$maxId;
|
||||||
|
$id = isset($flowPools[$value['dp_sn']]) ? $flowPools[$value['dp_sn']]['id'] : $id;
|
||||||
|
|
||||||
|
$flowPoolPackages = json_decode($value['dp_vd_packages'], true);
|
||||||
|
|
||||||
|
$flowPoolPackages = array_map(function ($item) use ($packages) {
|
||||||
|
$sn = explode('---', $item)[0];
|
||||||
|
return $packages[$sn]['id'];
|
||||||
|
}, $flowPoolPackages);
|
||||||
|
|
||||||
|
$settings = $oldFlowPoolSettings[$value['dp_sn']];
|
||||||
|
|
||||||
|
$settings = array_sort($settings, function ($item) {
|
||||||
|
return $item['year_month'];
|
||||||
|
});
|
||||||
|
|
||||||
|
foreach ($settings as $i => $item) {
|
||||||
|
$dp_billing_rules = json_decode($item['dp_billing_rules'], true);
|
||||||
|
$dp_min_cost_rules = json_decode($item['dp_min_cost_rules'], true);
|
||||||
|
|
||||||
|
if (count($settings) === 1) {
|
||||||
|
$start_at = '2000-01-01 00:00:00';
|
||||||
|
$end_at = '3000-01-01 23:59:59';
|
||||||
|
} else {
|
||||||
|
if ($i === 0) {
|
||||||
|
$start_at = '2000-01-01 00:00:00';
|
||||||
|
$end_at = Carbon::parse($item['year_month'])->endOfMonth()->format('Y-m-d H:i:s');
|
||||||
|
} elseif ($i === count($settings) - 1) {
|
||||||
|
$start_at = Carbon::parse($item['year_month'])->startOfMonth()->format('Y-m-d H:i:s');
|
||||||
|
$end_at = '3000-01-01 23:59:59';
|
||||||
|
} else {
|
||||||
|
$start_at = Carbon::parse($item['year_month'])->startOfMonth()->format('Y-m-d H:i:s');
|
||||||
|
$end_at = Carbon::parse($item['year_month'])->endOfMonth()->format('Y-m-d H:i:s');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
$settingData = [
|
||||||
|
'pool_id' => $id,
|
||||||
|
'gradient_price' => intval($dp_billing_rules['first_month_price']) * 100,
|
||||||
|
'gradient' => $dp_billing_rules['data_step_num'] * 1024,
|
||||||
|
'gradient_unit' => $dp_billing_rules['data_step_num_unit'] === 'M' ? 0 : 1,
|
||||||
|
'start_at' => $start_at,
|
||||||
|
'end_at' => $end_at,
|
||||||
|
'created_at' => Carbon::parse($item['year_month'])->startOfMonth()->format('Y-m-d H:i:s'),
|
||||||
|
'updated_at' => Carbon::parse($item['year_month'])->startOfMonth()->format('Y-m-d H:i:s'),
|
||||||
|
];
|
||||||
|
|
||||||
|
$minimum_settings = [];
|
||||||
|
|
||||||
|
foreach ($dp_min_cost_rules as $package => $rule) {
|
||||||
|
$sn = explode('---', $package)[0];
|
||||||
|
$minimum_settings[] = [
|
||||||
|
'package_id' => $packages[$sn]['id'],
|
||||||
|
'flows' => $rule['dp_min_cost_data'] * 1024,
|
||||||
|
'price' => $rule['dp_min_cost_money'] * 100,
|
||||||
|
];
|
||||||
|
}
|
||||||
|
|
||||||
|
$settingData['minimum_settings'] = json_encode($minimum_settings);
|
||||||
|
|
||||||
|
$settingArray[] = $settingData;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
$data = [
|
||||||
|
'id' => $id,
|
||||||
|
'sn' => $value['dp_sn'],
|
||||||
|
'name' => $value['dp_name'],
|
||||||
|
'company_id' => $companies[CommonService::stringifyCompanyId($value['dp_vd_company_id'])]['id'],
|
||||||
|
'carrier_operator' => self::$carrier_operator[$value['dp_carrieroperator']],
|
||||||
|
'shared' => self::$shared[$value['dp_share_type']],
|
||||||
|
'package_ids' => json_encode($flowPoolPackages),
|
||||||
|
'real_pool_ids' => json_encode(array_map('intval', json_decode($value['dp_td_data_pool_id'], true))),
|
||||||
|
'start_at' => date('Y-m-1 00:00:00', $value['dp_create_time']),
|
||||||
|
'remark' => '',
|
||||||
|
'created_at' => date('Y-m-d H:i:s', $value['dp_create_time']),
|
||||||
|
'updated_at' => date('Y-m-d H:i:s'),
|
||||||
|
'deleted_at' => $value['dp_del'] ? date('Y-m-d H:i:s') : null,
|
||||||
|
];
|
||||||
|
|
||||||
|
$array[] = $data;
|
||||||
|
}
|
||||||
|
|
||||||
|
FlowPool::upsert($array, 'id');
|
||||||
|
$maxId = FlowPoolSetting::withoutTrashed()->max('id');
|
||||||
|
FlowPoolSetting::whereIn('pool_id', array_pluck($array, 'id'))->forceDelete();
|
||||||
|
DB::statement("select setval('virtual_flow_pool_settings_id_seq', {$maxId})");
|
||||||
|
FlowPoolSetting::insert($settingArray);
|
||||||
|
|
||||||
|
app(FlowPoolRepository::class)->forgetCached();
|
||||||
|
app(FlowPoolSettingRepository::class)->forgetCached();
|
||||||
|
}
|
||||||
|
}
|
@ -190,6 +190,8 @@ class FlowPoolController extends Controller
|
|||||||
|
|
||||||
$flowPool = FlowPoolService::transformer(collect([$flowPool]), $month)->first();
|
$flowPool = FlowPoolService::transformer(collect([$flowPool]), $month)->first();
|
||||||
|
|
||||||
|
$package_ids = [];
|
||||||
|
|
||||||
if ($flowPool->setting_status) {
|
if ($flowPool->setting_status) {
|
||||||
$repository = app(OrderCardPartitionRepository::class);
|
$repository = app(OrderCardPartitionRepository::class);
|
||||||
|
|
||||||
|
@ -26,6 +26,7 @@ class VirtualServiceProvider extends ServiceProvider
|
|||||||
\App\Domains\Virtual\Commands\Sync\CompanySync::class,
|
\App\Domains\Virtual\Commands\Sync\CompanySync::class,
|
||||||
\App\Domains\Virtual\Commands\Sync\PackageSync::class,
|
\App\Domains\Virtual\Commands\Sync\PackageSync::class,
|
||||||
\App\Domains\Virtual\Commands\Sync\ProductSync::class,
|
\App\Domains\Virtual\Commands\Sync\ProductSync::class,
|
||||||
|
\App\Domains\Virtual\Commands\Sync\FlowPoolSync::class,
|
||||||
]);
|
]);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -703,7 +703,7 @@ class FlowPoolService extends Service
|
|||||||
|
|
||||||
foreach ($settings as &$setting) {
|
foreach ($settings as &$setting) {
|
||||||
$setting['gradient_price'] = sprintf('%.02f', $setting['gradient_price']/100);
|
$setting['gradient_price'] = sprintf('%.02f', $setting['gradient_price']/100);
|
||||||
$setting['gradient'] = sprintf('%.02f', $setting['gradient']/1024);
|
$setting['gradient'] = sprintf('%d', $setting['gradient']/1024);
|
||||||
$minimum_settings = $setting['minimum_settings'] ?? [];
|
$minimum_settings = $setting['minimum_settings'] ?? [];
|
||||||
|
|
||||||
foreach ($minimum_settings as &$minimum_setting) {
|
foreach ($minimum_settings as &$minimum_setting) {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user