151 lines
5.4 KiB
PHP
151 lines
5.4 KiB
PHP
<?php
|
|
namespace App\Domains\Virtual\Services;
|
|
|
|
use App\Dicts;
|
|
use App\Core\Service;
|
|
use Illuminate\Support\Str;
|
|
use App\Models\Virtual\Package;
|
|
use Illuminate\Validation\Rule;
|
|
use Illuminate\Support\Facades\DB;
|
|
use App\Exceptions\NotExistException;
|
|
use Illuminate\Support\Facades\Validator;
|
|
use App\Domains\Virtual\Services\CommonService;
|
|
use App\Domains\Virtual\Repositories\PackageRepository;
|
|
|
|
class PackageService extends Service
|
|
{
|
|
protected $packageRepository;
|
|
|
|
/**
|
|
* 构造函数
|
|
*
|
|
* @return void
|
|
*/
|
|
public function __construct(PackageRepository $packageRepository)
|
|
{
|
|
$this->packageRepository = $packageRepository;
|
|
}
|
|
|
|
/**
|
|
* 套餐列表
|
|
*
|
|
* @param array $conditions
|
|
* @return mixed
|
|
*/
|
|
public function index(array $conditions = [])
|
|
{
|
|
$limit = $conditions['limit'] ?? 20;
|
|
|
|
$packages = $this->packageRepository->withConditions($conditions)
|
|
->applyConditions()->paginate($limit);
|
|
|
|
$carrierOperators = app(Dicts::class)->get('carrier_operator');
|
|
|
|
$packages->map(function ($item) use ($carrierOperators) {
|
|
$item->cost_price = sprintf('%.02f', $item->cost_price/100);
|
|
$item->guide_price = sprintf('%.02f', $item->guide_price/100);
|
|
$item->renewal_cost_price = sprintf('%.02f', $item->renewal_cost_price/100);
|
|
$item->renewal_guide_price = sprintf('%.02f', $item->renewal_guide_price/100);
|
|
$item->carrier_operator_name = $carrierOperators[$item->carrier_operator];
|
|
});
|
|
|
|
return $packages;
|
|
}
|
|
|
|
/**
|
|
* 存储套餐
|
|
*
|
|
* @param array $attributes
|
|
* @param Package $parent
|
|
* @return Package
|
|
*/
|
|
public function store(array $attributes = [])
|
|
{
|
|
$attributes = array_only($attributes, array_merge(app(Package::class)->getFillable()));
|
|
|
|
$attributes['sn'] = $attributes['sn'] ?: CommonService::generatePackageSn($attributes['type'], $attributes['carrier_operator'], $attributes['flows'], $attributes['service_months']);
|
|
|
|
$rule = [
|
|
'sn' => ['required', 'between:2,32', Rule::unique($this->packageRepository->getTable(), 'sn')->ignore($attributes['id'])],
|
|
'name' => ['required', 'between:2,32', Rule::unique($this->packageRepository->getTable(), 'name')->ignore($attributes['id'])],
|
|
'type' => ['required', 'in:0,1,2'],
|
|
'carrier_operator' => ['required', 'in:0,1,2,3'],
|
|
'cost_price' => ['numeric', 'min:0'],
|
|
'guide_price' => ['numeric', 'min:0'],
|
|
'renewal_cost_price' => ['numeric', 'min:0'],
|
|
'renewal_guide_price' => ['numeric', 'min:0'],
|
|
'flows' => ['numeric', 'min:0'],
|
|
'voices' => ['numeric', 'min:0'],
|
|
'messages' => ['numeric', 'min:0'],
|
|
'has_messages' => ['in:0,1'],
|
|
'has_lbs' => ['in:0,1'],
|
|
'reset_months' => ['numeric', "max:{$attributes['service_months']}", 'min:0'],
|
|
'service_months' => ['numeric', 'min:0'],
|
|
'effect_months' => ['numeric', 'min:0'],
|
|
'delay_months' => ['numeric', 'min:0'],
|
|
'description' => ['max:255'],
|
|
];
|
|
|
|
$message = [
|
|
'sn.required' => '请输入套餐编号',
|
|
'sn.between' => '套餐编号长度不合法',
|
|
'sn.unique' => '套餐编号已存在,请重新输入',
|
|
'name.required' => '请输入套餐名称',
|
|
'name.between' => '套餐名称长度不合法',
|
|
'name.unique' => '套餐名称已经被其他用户所使用',
|
|
'type.required' => '请选择套餐类型',
|
|
'type.in' => '套餐类型不合法',
|
|
'carrier_operator.required' => '请选择运营商',
|
|
'carrier_operator.in' => '运营商不合法',
|
|
'cost_price.numeric' => '套餐价格必须是数字',
|
|
'guide_price.numeric' => '套餐指导价格必须是数字',
|
|
'flows.numeric' => '套餐指导价格必须是数字',
|
|
];
|
|
|
|
Validator::validate($attributes, $rule, $message);
|
|
|
|
$attributes['cost_price'] = floatval($attributes['cost_price']) * 100;
|
|
$attributes['guide_price'] = floatval($attributes['guide_price']) * 100;
|
|
$attributes['renewal_cost_price'] = floatval($attributes['renewal_cost_price']) * 100;
|
|
$attributes['renewal_guide_price'] = floatval($attributes['renewal_guide_price']) * 100;
|
|
|
|
if (!$attributes['id']) {
|
|
$maxId = Package::withTrashed()->where('type', $attributes['type'])->max('id');
|
|
$attributes['id'] = $maxId ? $maxId + 1 : 1;
|
|
$node = $this->packageRepository->create($attributes);
|
|
}
|
|
|
|
if ($attributes['id']) {
|
|
$node = $this->packageRepository->find($attributes['id']);
|
|
|
|
if (!$node) {
|
|
throw new NotExistException('套餐不存在或已删除');
|
|
}
|
|
|
|
// 不能修改的字段
|
|
unset($attributes['type']);
|
|
unset($attributes['sn']);
|
|
unset($attributes['carrier_operator']);
|
|
unset($attributes['flows']);
|
|
|
|
$this->packageRepository->setModel($node)->update($attributes);
|
|
}
|
|
|
|
return $node;
|
|
}
|
|
|
|
/**
|
|
* 删除
|
|
*
|
|
* @return bool
|
|
*/
|
|
public function destroy($ids)
|
|
{
|
|
$ids = is_array($ids) ? $ids : [$ids];
|
|
|
|
$this->packageRepository->destroy($ids);
|
|
|
|
return true;
|
|
}
|
|
}
|