368 lines
11 KiB
PHP
368 lines
11 KiB
PHP
<?php
|
|
namespace App\Domains\Virtual\Http\Controllers;
|
|
|
|
use Carbon\Carbon;
|
|
use App\Core\Controller;
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\Support\Facades\DB;
|
|
use App\Models\Virtual\FlowPoolData;
|
|
use App\Exceptions\NotExistException;
|
|
use App\Models\Virtual\FlowPoolMonth;
|
|
use Illuminate\Support\Facades\Schema;
|
|
use App\Exceptions\NotAllowedException;
|
|
use Illuminate\Support\Facades\Validator;
|
|
use App\Exceptions\InvalidArgumentException;
|
|
use App\Domains\Export\Services\ExportService;
|
|
use App\Domains\Export\Services\ImportService;
|
|
use App\Domains\Virtual\Exports\FlowPoolExport;
|
|
|
|
use App\Domains\Virtual\Services\PackageService;
|
|
use App\Domains\Virtual\Services\ProductService;
|
|
use App\Domains\Virtual\Services\FlowPoolService;
|
|
use App\Domains\Virtual\Repositories\FlowPoolRepository;
|
|
use App\Domains\Virtual\Exports\FlowPoolExportDetailExport;
|
|
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 packages()
|
|
{
|
|
$list = $this->flowPoolService->packages();
|
|
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()
|
|
{
|
|
$page = $this->request->get('page');
|
|
$conditions = $this->request->all();
|
|
$res = $this->flowPoolService->show($conditions);
|
|
return res($res, '流量池详情', 201);
|
|
}
|
|
|
|
/**
|
|
* 明细导出.
|
|
*
|
|
* @return \Illuminate\Http\Response
|
|
*/
|
|
public function detailExport()
|
|
{
|
|
$conditions = $this->request->only(['pool_id', 'month']);
|
|
|
|
$pool_id = $conditions['pool_id'];
|
|
$month = Carbon::parse($conditions['month']);
|
|
|
|
$table = app(FlowPoolMonth::class)->getTable() . '_' . $month->format('Ym');
|
|
|
|
if (!Schema::hasTable($table)) {
|
|
throw new NotAllowedException('无该月数据');
|
|
}
|
|
|
|
$total = app(FlowPoolMonth::class)->setTable($table)->where('pool_id', $pool_id)->count();
|
|
|
|
try {
|
|
$export = new FlowPoolExportDetailExport($conditions);
|
|
$queue = $total > 30000;
|
|
$url = ExportService::store($export, $disk = 'public', $queue);
|
|
} catch (\Exception $e) {
|
|
throw $e;
|
|
}
|
|
|
|
return res($url, '导出成功', 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();
|
|
|
|
$package_ids = [];
|
|
|
|
if ($flowPool->setting_status) {
|
|
$repository = app(OrderCardPartitionRepository::class);
|
|
|
|
$package_ids = $flowPool->package_ids;
|
|
|
|
$conditions = [
|
|
'type' => [0, 1, 2],
|
|
'month' => $month,
|
|
'company_id' => $flowPool->company_id,
|
|
'package_id' => $package_ids,
|
|
'unit_price' => 0
|
|
];
|
|
|
|
$cards = $repository->select([
|
|
'package_id',
|
|
DB::raw('count(distinct sim) as total'),
|
|
])->withConditions($conditions)->groupBy('package_id')->get()->pluck('total', 'package_id');
|
|
} 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'], 'package_id');
|
|
|
|
$newSettings = [];
|
|
|
|
foreach ($package_ids as $package_id) {
|
|
if (!isset($settings[$package_id])) {
|
|
$newSettings[] = [
|
|
'package_id' => $package_id,
|
|
'package_name' => PackageService::load($package_id)['name'],
|
|
'total' => $cards[$package_id] ?? 0,
|
|
'news' => $cards[$package_id] ?? 0,
|
|
];
|
|
} else {
|
|
$setting = $settings[$package_id];
|
|
|
|
$chunk = $setting['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);
|
|
}
|
|
|
|
$setting['cards'] = $chunk;
|
|
|
|
$news = $cards[$package_id] ?? 0 - array_sum(array_pluck($setting['cards'], 'counts'));
|
|
|
|
$setting['news'] = $news < 0 ? 0 : $news;
|
|
|
|
$newSettings[] = $setting;
|
|
}
|
|
}
|
|
|
|
$flowPoolData['total_flows'] = sprintf('%.02f', $flowPoolData['total_flows']/1024);
|
|
|
|
return res(['flowPool' => $flowPool, 'settings' => $newSettings, 'total' => $cards->values()->sum(), '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);
|
|
}
|
|
|
|
/**
|
|
* 导入流量卡数据.
|
|
*
|
|
* @return \Illuminate\Http\Response
|
|
*/
|
|
public function importFlows()
|
|
{
|
|
$file = $this->request->file('file');
|
|
$data = ImportService::load($file, ['month', 'pool_id', 'sim', 'kilobyte']);
|
|
|
|
Validator::validate($data, [
|
|
'*.month' => ['required'],
|
|
'*.pool_id' => ['required'],
|
|
'*.sim' => ['required'],
|
|
'*.kilobyte' => ['required'],
|
|
]);
|
|
|
|
$flowPools = app(FlowPoolRepository::class)->withConditions(['id' => array_unique(array_pluck($data, 'pool_id'))])->get()->keyBy('id')->toArray();
|
|
|
|
$simPackage = [];
|
|
|
|
foreach (array_chunk($data, 1000) as $value) {
|
|
$cards = app(OrderCardPartitionRepository::class)->select(['sim', 'package_id'])->withConditions([
|
|
'type' => 0,
|
|
'sim' => array_pluck($value, 'sim'),
|
|
])->get()->pluck('package_id', 'sim')->toArray();
|
|
|
|
foreach ($cards as $sim => $package_id) {
|
|
$simPackage[$sim] = $package_id;
|
|
}
|
|
}
|
|
|
|
foreach ($data as &$item) {
|
|
if (!isset($simPackage[$item['sim']])) {
|
|
throw new NotExistException('卡不在VD上 #:' . $item['sim']);
|
|
}
|
|
|
|
$package_id = $simPackage[$item['sim']];
|
|
|
|
$pool = $flowPools[$item['pool_id']];
|
|
|
|
if (!in_array($package_id, $pool['package_ids'])) {
|
|
throw new InvalidArgumentException("卡不属于流量池 {$item['pool_id']} #: {$item['sim']}");
|
|
}
|
|
|
|
$item['package_id'] = $simPackage[$item['sim']];
|
|
}
|
|
|
|
DB::transaction(function () use ($data) {
|
|
$monthGroupBy = array_groupBy($data, 'month');
|
|
|
|
foreach ($monthGroupBy as $month => $values) {
|
|
$table = FlowPoolService::checkTable($month);
|
|
$simGroupBy = array_groupBy($values, 'sim');
|
|
|
|
foreach ($simGroupBy as $sim => $group) {
|
|
if (count($group) > 1) {
|
|
$temp = $group[0];
|
|
$temp['kilobyte'] = array_sum(array_pluck($group, 'kilobyte'));
|
|
$simGroupBy[$sim] = [$temp];
|
|
}
|
|
}
|
|
|
|
$values = array_collapse($simGroupBy);
|
|
|
|
foreach (array_chunk($values, 1000) as $news) {
|
|
app(FlowPoolMonth::class)->setTable($table)->upsert($news, ['sim', 'month']);
|
|
}
|
|
}
|
|
});
|
|
|
|
return res(true, '导入成功');
|
|
}
|
|
}
|