流量池

This commit is contained in:
邓皓元 2019-02-18 10:52:45 +08:00
parent b1c30f4959
commit a3a3856fbb
10 changed files with 249 additions and 36 deletions

View File

@ -16,7 +16,7 @@ class FlowPoolMonthRepository extends Repository
/**
* 是否开启数据转化
*
*
* @var bool
*/
protected $needTransform = false;
@ -59,4 +59,4 @@ class FlowPoolMonthRepository extends Repository
return $this;
}
}
}

View File

@ -0,0 +1,72 @@
<?php
namespace App\Domains\Virtual\Repositories;
use App\Core\Repository;
use App\Models\Virtual\FlowPoolPackage as Model;
class FlowPoolPackageRepository extends Repository
{
/**
* 是否关闭缓存
*
* @var boolean
*/
protected $cacheSkip = false;
/**
* 是否开启数据转化
*
* @var bool
*/
protected $needTransform = false;
/**
* @var array
*/
protected $fieldSearchable = [
'id' => '=',
'created_at' => 'like',
];
public function model() {
return Model::class;
}
/**
* 数据格式化
*
* @param mixed $result
*
* @return mixed
*/
public function transform($model)
{
return $model->toArray();
}
/**
* 查询条件
*
* @return void
*/
public function withConditions(array $conditions = [])
{
if (isset($conditions['id'])) {
$conditions['id'] = array_wrap($conditions['id']);
$this->model = $this->model->whereIn('id', $conditions['id']);
}
if (isset($conditions['pool_id'])) {
$conditions['pool_id'] = array_wrap($conditions['pool_id']);
$this->model = $this->model->whereIn('pool_id', $conditions['pool_id']);
}
if (isset($conditions['package_id'])) {
$conditions['package_id'] = array_wrap($conditions['package_id']);
$this->model = $this->model->whereIn('package_id', $conditions['package_id']);
}
return $this;
}
}

View File

@ -29,7 +29,8 @@ class FlowPoolRepository extends Repository
'created_at' => 'like',
];
public function model() {
public function model()
{
return Model::class;
}
@ -57,6 +58,20 @@ class FlowPoolRepository extends Repository
$this->model = $this->model->whereIn('id', $conditions['id']);
}
if (isset($conditions['carrier_operator'])) {
$this->model = $this->model->where('carrier_operator', $conditions['carrier_operator']);
}
if (isset($conditions['name'])) {
$this->model = $this->model->where('name', 'like', "%{$conditions['name']}%");
}
if (isset($conditions['company_name'])) {
$this->model = $this->model->whereHas('company', function ($relation) use ($conditions) {
$relation->withTrashed()->where('name', $conditions['company_name']);
});
}
return $this;
}
}

View File

@ -5,23 +5,45 @@ use App\Dicts;
use App\Core\Service;
use Illuminate\Validation\Rule;
use App\Models\Virtual\FlowPool;
use Illuminate\Support\Facades\DB;
use App\Exceptions\NotExistException;
use App\Exceptions\NotAllowedException;
use Illuminate\Support\Facades\Validator;
use App\Domains\Virtual\Services\CompanyService;
use App\Domains\Virtual\Services\PackageService;
use App\Domains\Virtual\Repositories\FlowPoolRepository;
use App\Domains\Virtual\Repositories\FlowPoolCardRepository;
use App\Domains\Virtual\Repositories\FlowPoolMonthRepository;
use App\Domains\Virtual\Repositories\FlowPoolPackageRepository;
use App\Domains\Virtual\Repositories\FlowPoolSettingRepository;
class FlowPoolService extends Service
{
protected $flowPoolRepository;
protected $realFlowPoolRepository;
protected $flowPoolPackageRepository;
protected $flowPoolSettingRepository;
protected $flowPoolCardRepository;
protected $flowPoolMonthRepository;
protected $pools;
/**
* 构造函数
*
* @return void
*/
public function __construct(FlowPoolRepository $flowPoolRepository)
{
public function __construct(
FlowPoolRepository $flowPoolRepository,
FlowPoolPackageRepository $flowPoolPackageRepository,
FlowPoolSettingRepository $flowPoolSettingRepository,
FlowPoolCardRepository $flowPoolCardRepository,
FlowPoolMonthRepository $flowPoolMonthRepository
) {
$this->flowPoolRepository = $flowPoolRepository;
$this->realFlowPoolRepository = $realFlowPoolRepository;
$this->flowPoolPackageRepository = $flowPoolPackageRepository;
$this->flowPoolSettingRepository = $flowPoolSettingRepository;
$this->flowPoolCardRepository = $flowPoolCardRepository;
$this->flowPoolMonthRepository = $flowPoolMonthRepository;
}
/**
@ -37,13 +59,19 @@ class FlowPoolService extends Service
$flowPools = $this->flowPoolRepository->withConditions($conditions)
->applyConditions()->paginate($limit);
$packages = $this->flowPoolPackageRepository->withConditions([
'pool_id' => $flowPools->pluck('id')->toArray(),
])->get()->groupBy('pool_id');
$carrierOperators = app(Dicts::class)->get('carrier_operator');
$shares = app(Dicts::class)->get('shares');
$flowPools->map(function ($item) use ($carrierOperators, $shares) {
$item->company_name = app(CompanyService::class)->load($item->company_id)['name'];
$item->carrier_operator_name = $carrierOperators[$item->carrier_operator];
$item->shared_name = $shares[$item->shared];
$flowPools->map(function ($item) use ($carrierOperators, $shares, $packages) {
$item->company_name = app(CompanyService::class)->load($item['company_id'])['name'];
$item->carrier_operator_name = $carrierOperators[$item['carrier_operator']];
$item->shared_name = $shares[$item['shared']];
$item->package_ids = $packages[$item->id] ? array_pluck($packages[$item->id], 'package_id') : [];
$item->real_pool_ids = empty($item->real_pool_ids) ? [] : $item->real_pool_ids;
});
return $flowPools;
@ -75,12 +103,25 @@ class FlowPoolService extends Service
Validator::validate($attributes, $rule, $message);
if (empty($attributes['package_ids'])) {
throw new NotAllowedException('请至少加入一个套餐');
}
if (!$attributes['id']) {
$maxId = FlowPool::withTrashed()->max('id');
$attributes['id'] = $maxId ? $maxId + 1 : 1;
$attributes['sn'] = self::sn($attributes['id']);
$node = $this->flowPoolRepository->create($attributes);
DB::transaction(function () use ($attributes) {
$node = $this->flowPoolRepository->create(array_except($attributes, ['package_ids']));
foreach ($attributes['package_ids'] as $package_id) {
$this->flowPoolPackageRepository->create([
'pool_id' => $node->id,
'package_id' => $package_id,
]);
}
});
}
if ($attributes['id']) {
@ -90,7 +131,25 @@ class FlowPoolService extends Service
throw new NotExistException('流量池不存在或已删除');
}
$this->flowPoolRepository->setModel($node)->update($attributes);
DB::transaction(function () use ($attributes, $node) {
$exists = $this->flowPoolPackageRepository->withConditions([
'pool_id' => $node->id,
])->get()->pluck('package_id')->toArray();
$this->flowPoolRepository->setModel($node)->update(array_except($attributes, ['package_ids']));
foreach ($exists as $package_id) {
if (!in_array($package_id, $attributes['package_ids'])) {
$this->flowPoolPackageRepository->withConditions(['package_id' => $package_id])->delete();
}
}
foreach ($attributes['package_ids'] as $package_id) {
if (!in_array($package_id, $exists)) {
$this->flowPoolPackageRepository->create(['pool_id' => $node->id, 'package_id' => $package_id]);
}
}
});
}
return $node;
@ -114,4 +173,13 @@ class FlowPoolService extends Service
{
return sprintf('FP%011d', $id);
}
public function load($id)
{
if (!$this->pools) {
$this->pools = $this->flowPoolRepository->withTrashed()->get()->keyBy('id');
}
return $this->pools[$id];
}
}

View File

@ -3,6 +3,8 @@
namespace App\Models\Virtual;
use App\Core\Model;
use App\Models\Virtual\Company;
use App\Models\Virtual\Package;
use Illuminate\Database\Eloquent\SoftDeletes;
class FlowPool extends Model
@ -15,4 +17,14 @@ class FlowPool extends Model
'package_ids' => 'array',
'real_pool_ids' => 'array',
];
public function company()
{
return $this->belongsTo(Company::class, 'company_id', 'id');
}
public function packages()
{
return $this->belongsToMany(Package::class, 'virtual_flow_pool_packages', 'pool_id', 'package_id');
}
}

View File

@ -0,0 +1,25 @@
<?php
namespace App\Models\Virtual;
use App\Core\Model;
use App\Models\Virtual\Package;
use App\Models\Virtual\FlowPool;
use Illuminate\Database\Eloquent\SoftDeletes;
class FlowPoolPackage extends Model
{
use SoftDeletes;
protected $table = 'virtual_flow_pool_packages';
public function package()
{
return $this->belongsTo(Package::class, 'package_id');
}
public function pool()
{
return $this->belongsTo(FlowPool::class, 'pool_id');
}
}

View File

@ -41,6 +41,7 @@ class CreateFlowPoolTables extends Migration
$table->integer('flows')->default(255)->comment('流量值 -1不限流量 单位MB');
$table->tinyInteger('carrier_operator')->unsigned()->default(255)->comment('运营商(0:联通 1:移动 2:电信)');
$table->tinyInteger('shared')->unsigned()->default(0)->comment('共享类型 0:未知 1纵向共享 2横向共享');
$table->text('package_ids')->nullable()->comment('RD套餐ID');
$table->text('real_pool_ids')->nullable()->comment('RD流量池ID');
$table->text('remark')->nullable()->comment('流量池备注');
$table->tinyInteger('status')->unsigned()->default(0)->comment('状态 0:正常 1:禁用');

View File

@ -36,7 +36,7 @@
@on-search="handleCompleteCompanies"
icon="ios-search"
placeholder="请输入企业名称"
v-model.trim="params.name"
v-model.trim="options.company_name"
>
<Option
:key="item.id"
@ -47,11 +47,11 @@
</li>
<li class="handle-item w-250">
<Input clearable placeholder="流量池名称" v-model.trim="params.name"></Input>
<Input clearable placeholder="流量池名称" v-model.trim="options.name"></Input>
</li>
<li class="handle-item w-250">
<Select clearable placeholder="运营商" v-model="params.carrier_operator">
<Select clearable placeholder="运营商" v-model="options.carrier_operator">
<Option :value="0">联通</Option>
<Option :value="1">移动</Option>
<Option :value="2">电信</Option>
@ -64,7 +64,7 @@
placeholder="请选择时间"
placement="bottom-start"
type="month"
v-model.trim="params.month"
v-model.trim="options.month"
></DatePicker>
</li>

View File

@ -32,8 +32,8 @@ export default {
carrier_operator: '',
shared: '',
company_id: '',
package_ids: '',
real_pool_ids: '',
package_ids: [],
real_pool_ids: [],
status: 0,
remark: ''
}
@ -141,12 +141,16 @@ export default {
for (let k in this.params) {
if (k == 'status') {
this.params[k] = 0;
} else if (k == 'real_pool_ids' || k == 'package_ids') {
this.params[k] = [];
} else {
this.params[k] = '';
}
}
this.my_show = false;
this.package_ids = [];
this.real_pool_ids = [];
},
transferPackages(ids) {
this.package_ids = ids;

View File

@ -7,10 +7,12 @@ export default {
},
data() {
return {
params: {
name: ''
options: {
company_name: null,
name: null,
carrier_operator: null,
month: this.moment().subtract('2', 'months').startOf('month').format('YYYY-MM')
},
trashed: null,
list_data: null,
reals: [],
editObj: {
@ -31,18 +33,13 @@ export default {
width: 80
},
{
title: '运营商',
key: 'carrier_operator_name',
title: '名称',
key: 'name',
width: 110
},
{
title: '编号',
key: 'sn',
width: 150
},
{
title: '名称',
key: 'name',
title: '运营商',
key: 'carrier_operator_name',
width: 110
},
{
@ -168,7 +165,7 @@ export default {
click: () => {
this.$Modal.confirm({
title: '提示',
content: '删除后该企业不可使用,请谨慎操作',
content: '删除后该流量池不可使用,请谨慎操作',
onOk: () => {
API.destroy({
ids: row.id
@ -203,7 +200,7 @@ export default {
* @return {[type]} [description]
*/
index(page = 1) {
let data = this.searchDataHandle(this.params, { page }, { 'trashed': this.trashed, 'orderBy': 'id', 'sortedBy': 'asc' });
let data = this.searchDataHandle({}, { page }, this.params());
this.isShowLoading(true);
API.index(data).then(res => {
this.isShowLoading(false);
@ -215,6 +212,21 @@ export default {
});
},
params() {
if (!this.options.month) {
this.options.month = this.moment().subtract('2', 'months').startOf('month').format('YYYY-MM');
}
let params = {
name: this.options.name,
company_name: this.options.company_name,
carrier_operator: this.options.carrier_operator,
month: this.moment(this.options.month).format('YYYY-MM')
};
return params;
},
/**
* [openEdit 打开编辑弹窗]
* @return {[type]} [description]
@ -242,10 +254,14 @@ export default {
},
resetSearch() {
for (let k in this.params) {
this.params[k] = '';
for (let k in this.options) {
if (k === 'month') {
this.options[k] = this.moment().subtract('2', 'months').startOf('month').format('YYYY-MM');
} else {
this.options[k] = null;
}
}
this.trashed = null;
this.index(1);
}
}