251 lines
7.0 KiB
PHP
251 lines
7.0 KiB
PHP
<?php
|
|
namespace App\Domains\Virtual\Http\Controllers;
|
|
|
|
use App\Core\Controller;
|
|
use Illuminate\Http\Request;
|
|
use Dipper\Excel\Facades\Excel;
|
|
use Illuminate\Support\Facades\DB;
|
|
use App\Models\Virtual\FlowPoolData;
|
|
use App\Exceptions\NotExistException;
|
|
use Illuminate\Support\Facades\Validator;
|
|
use App\Models\Virtual\OrderCardPartition;
|
|
use App\Exceptions\InvalidArgumentException;
|
|
use App\Domains\Export\Services\ExportService;
|
|
use App\Domains\Virtual\Exports\FlowPoolExport;
|
|
use App\Domains\Virtual\Imports\FlowCardImport;
|
|
use App\Domains\Virtual\Services\ProductService;
|
|
use App\Domains\Virtual\Services\FlowPoolService;
|
|
use App\Domains\Virtual\Repositories\FlowPoolRepository;
|
|
use App\Domains\Virtual\Repositories\OrderCardPartitionRepository;
|
|
|
|
class FlowPoolController extends Controller
|
|
{
|
|
protected $request;
|
|
protected $flowPoolService;
|
|
|
|
/**
|
|
* 构造函数,自动注入.
|
|
*/
|
|
public function __construct(Request $request, FlowPoolService $flowPoolService)
|
|
{
|
|
$this->request = $request;
|
|
$this->flowPoolService = $flowPoolService;
|
|
}
|
|
|
|
/**
|
|
* RD流量池列表
|
|
*
|
|
* @return void
|
|
*/
|
|
public function real()
|
|
{
|
|
$list = $this->flowPoolService->real();
|
|
return res($list, 'RD流量池列表', 201);
|
|
}
|
|
|
|
/**
|
|
* 后向定价列表
|
|
*
|
|
* @return void
|
|
*/
|
|
public function products()
|
|
{
|
|
$list = $this->flowPoolService->products();
|
|
return res($list, '后向定价列表', 201);
|
|
}
|
|
|
|
/**
|
|
* 列表.
|
|
*
|
|
* @return \Illuminate\Http\Response
|
|
*/
|
|
public function index()
|
|
{
|
|
$conditions = $this->request->all();
|
|
|
|
$flowPools = $this->flowPoolService->index($conditions);
|
|
|
|
return res($flowPools, '流量池列表', 201);
|
|
}
|
|
|
|
/**
|
|
* 详情.
|
|
*
|
|
* @return \Illuminate\Http\Response
|
|
*/
|
|
public function show()
|
|
{
|
|
$conditions = $this->request->all();
|
|
$res = $this->flowPoolService->show($conditions);
|
|
return res($res, '流量池详情', 201);
|
|
}
|
|
|
|
|
|
/**
|
|
* 创建.
|
|
*
|
|
* @return \Illuminate\Http\Response
|
|
*/
|
|
public function create()
|
|
{
|
|
$attributes = $this->request->all();
|
|
|
|
$flowPool = $this->flowPoolService->store($attributes);
|
|
|
|
return res($flowPool, '创建成功');
|
|
}
|
|
|
|
/**
|
|
* 编辑.
|
|
*
|
|
* @return \Illuminate\Http\Response
|
|
*/
|
|
public function update($id)
|
|
{
|
|
$attributes = $this->request->all();
|
|
$attributes['id'] = $id;
|
|
|
|
$flowPool = $this->flowPoolService->store($attributes);
|
|
|
|
return res($flowPool, '修改成功');
|
|
}
|
|
|
|
/**
|
|
* 删除.
|
|
*
|
|
* @return \Illuminate\Http\Response
|
|
*/
|
|
public function destroy()
|
|
{
|
|
$ids = $this->request->ids();
|
|
|
|
$this->flowPoolService->destroy($ids);
|
|
|
|
return res(true, '删除成功');
|
|
}
|
|
|
|
|
|
/**
|
|
* 编辑.
|
|
*
|
|
* @return \Illuminate\Http\Response
|
|
*/
|
|
public function setting()
|
|
{
|
|
$attributes = $this->request->all();
|
|
|
|
$setting = $this->flowPoolService->setting($attributes);
|
|
|
|
return res($setting, $attributes['id'] ? '修改成功' : '添加成功');
|
|
}
|
|
|
|
/**
|
|
* 数据生成.
|
|
*
|
|
* @return \Illuminate\Http\Response
|
|
*/
|
|
public function flows()
|
|
{
|
|
$pool_id = $this->request->get('pool_id');
|
|
$month = $this->request->time('month');
|
|
|
|
if ($this->request->isMethod('GET')) {
|
|
if (!$flowPool = app(FlowPoolRepository::class)->find($pool_id)) {
|
|
throw new NotExistException('流量池不存在或已删除');
|
|
}
|
|
|
|
$flowPool = FlowPoolService::transformer(collect([$flowPool]), $month)->first();
|
|
|
|
if ($flowPool->setting_status) {
|
|
$repository = app(OrderCardPartitionRepository::class);
|
|
|
|
$minimum_settings = $flowPool['settings'][0]['minimum_settings'] ?? [];
|
|
|
|
$product_ids = array_pluck($minimum_settings, 'product_id');
|
|
|
|
$conditions = [
|
|
'type' => [0, 1, 2],
|
|
'month' => $month,
|
|
'product_id' => $product_ids
|
|
];
|
|
|
|
$cards = $repository->select([
|
|
'product_id',
|
|
DB::raw('count(distinct sim) as total'),
|
|
])->withConditions($conditions)->groupBy('product_id')->get();
|
|
|
|
$cards->map(function ($item) {
|
|
$item->product_name = ProductService::load($item->product_id)['name'];
|
|
});
|
|
} else {
|
|
$cards = collect();
|
|
}
|
|
|
|
$flowPoolData = FlowPoolData::where('month', $month->format('Ym'))->where('pool_id', $pool_id)->first();
|
|
|
|
if (!$flowPoolData) {
|
|
$flowPoolData = [
|
|
'pool_id' => $pool_id,
|
|
'month' => $month->format('Y-m'),
|
|
'total_flows' => 0,
|
|
'settings' => [],
|
|
];
|
|
}
|
|
|
|
$settings = array_keyBy($flowPoolData['settings'], 'product_id');
|
|
|
|
foreach ($cards as $card) {
|
|
if (!isset($settings[$card['product_id']])) {
|
|
$settings[$card['product_id']] = [
|
|
'product_id' => $card['product_id'],
|
|
'product_name' => $card['product_name'],
|
|
'total' => $card['total'],
|
|
'news' => $card['total'],
|
|
];
|
|
} else {
|
|
$chunk = $settings[$card['product_id']]['cards'];
|
|
|
|
foreach ($chunk as &$value) {
|
|
$value['flow_range'][0] = sprintf('%.02f', $value['flow_range'][0]/1024);
|
|
$value['flow_range'][1] = sprintf('%.02f', $value['flow_range'][1]/1024);
|
|
}
|
|
|
|
$settings[$card['product_id']]['cards'] = $chunk;
|
|
|
|
$settings[$card['product_id']]['news'] = $card['total'] - array_sum(array_pluck($settings[$card['product_id']]['cards'], 'counts'));
|
|
}
|
|
}
|
|
|
|
$flowPoolData['total_flows'] = sprintf('%.02f', $flowPoolData['total_flows']/1024);
|
|
|
|
return res(['flowPool' => $flowPool, 'settings' => array_values($settings), 'total' => array_sum(array_pluck($cards, 'total')), 'total_flows' => $flowPoolData['total_flows']], '数据设置');
|
|
}
|
|
|
|
$conditions = $this->request->all();
|
|
|
|
$res = $this->flowPoolService->flows($conditions);
|
|
|
|
return res($res, '设置成功');
|
|
}
|
|
|
|
/**
|
|
* 导出.
|
|
*
|
|
* @return \Illuminate\Http\Response
|
|
*/
|
|
public function export()
|
|
{
|
|
$conditions = $this->request->except(['page', 'limit']);
|
|
$conditions['limit'] = 0;
|
|
|
|
try {
|
|
$export = new FlowPoolExport($conditions);
|
|
$url = ExportService::store($export, $disk = 'public');
|
|
} catch (\Exception $e) {
|
|
throw $e;
|
|
}
|
|
|
|
return res($url, '导出成功', 201);
|
|
}
|
|
}
|