118 lines
3.3 KiB
PHP
118 lines
3.3 KiB
PHP
<?php
|
|
namespace App\Domains\Virtual\Services;
|
|
|
|
use App\Dicts;
|
|
use App\Core\Service;
|
|
use Illuminate\Validation\Rule;
|
|
use App\Models\Virtual\FlowPool;
|
|
use Illuminate\Support\Facades\Validator;
|
|
use App\Domains\Virtual\Repositories\FlowPoolRepository;
|
|
|
|
class FlowPoolService extends Service
|
|
{
|
|
protected $flowPoolRepository;
|
|
protected $realFlowPoolRepository;
|
|
|
|
/**
|
|
* 构造函数
|
|
*
|
|
* @return void
|
|
*/
|
|
public function __construct(FlowPoolRepository $flowPoolRepository)
|
|
{
|
|
$this->flowPoolRepository = $flowPoolRepository;
|
|
$this->realFlowPoolRepository = $realFlowPoolRepository;
|
|
}
|
|
|
|
/**
|
|
* 流量池列表
|
|
*
|
|
* @param array $conditions
|
|
* @return mixed
|
|
*/
|
|
public function index(array $conditions = [])
|
|
{
|
|
$limit = $conditions['limit'] ?? 20;
|
|
|
|
$flowPools = $this->flowPoolRepository->withConditions($conditions)
|
|
->applyConditions()->paginate($limit);
|
|
|
|
$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];
|
|
});
|
|
|
|
return $flowPools;
|
|
}
|
|
|
|
/**
|
|
* 存储流量池
|
|
*
|
|
* @param array $attributes
|
|
* @return FlowPool
|
|
*/
|
|
public function store(array $attributes = [])
|
|
{
|
|
$rule = [
|
|
'name' => ['required', 'between:2,32', Rule::unique($this->flowPoolRepository->getTable(), 'name')->ignore($attributes['id'])->whereNUll('deleted_at')],
|
|
'carrier_operator' => ['required', 'in:0,1,2,3'],
|
|
'shared' => ['required', 'in:1,2'],
|
|
];
|
|
|
|
$message = [
|
|
'name.required' => '请输入流量池名称',
|
|
'name.between' => '流量池名称长度不合法',
|
|
'name.unique' => '流量池名称已经被其他用户所使用',
|
|
'carrier_operator.required' => '请选择运营商',
|
|
'carrier_operator.in' => '运营商不合法',
|
|
'shared.required' => '请选择共享类型',
|
|
'shared.in' => '共享类型不合法',
|
|
];
|
|
|
|
Validator::validate($attributes, $rule, $message);
|
|
|
|
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);
|
|
}
|
|
|
|
if ($attributes['id']) {
|
|
$node = $this->flowPoolRepository->find($attributes['id']);
|
|
|
|
if (!$node) {
|
|
throw new NotExistException('流量池不存在或已删除');
|
|
}
|
|
|
|
$this->flowPoolRepository->setModel($node)->update($attributes);
|
|
}
|
|
|
|
return $node;
|
|
}
|
|
|
|
/**
|
|
* 删除
|
|
*
|
|
* @return bool
|
|
*/
|
|
public function destroy($ids)
|
|
{
|
|
$ids = is_array($ids) ? $ids : [$ids];
|
|
|
|
$this->flowPoolRepository->destroy($ids);
|
|
|
|
return true;
|
|
}
|
|
|
|
public static function sn($id)
|
|
{
|
|
return sprintf('FP%011d', $id);
|
|
}
|
|
}
|