117 lines
3.4 KiB
PHP
117 lines
3.4 KiB
PHP
<?php
|
|
namespace App\Domains\Virtual\Services;
|
|
|
|
use App\Core\Service;
|
|
use App\Models\Virtual\Product;
|
|
use App\Exceptions\NotExistException;
|
|
use App\Exceptions\NotAllowedException;
|
|
use Illuminate\Support\Facades\Validator;
|
|
use App\Domains\Virtual\Repositories\PackageRepository;
|
|
use App\Domains\Virtual\Repositories\ProductRepository;
|
|
|
|
class ProductService extends Service
|
|
{
|
|
protected $productRepository;
|
|
|
|
/**
|
|
* 构造函数
|
|
*
|
|
* @return void
|
|
*/
|
|
public function __construct(ProductRepository $productRepository)
|
|
{
|
|
$this->productRepository = $productRepository;
|
|
}
|
|
|
|
/**
|
|
* 获取企业定价列表
|
|
*
|
|
* @param int $companyId
|
|
* @return Collection
|
|
*/
|
|
public function getCompanyProducts($companyId, $carrierOperator = null)
|
|
{
|
|
$conditions = [
|
|
'company_id' => $companyId,
|
|
'carrier_operator' => $carrierOperator,
|
|
];
|
|
|
|
$list = $this->productRepository->with(['company:id,name', 'package:id,name,carrier_operator'])
|
|
->withConditions($conditions)->get();
|
|
|
|
return $list;
|
|
}
|
|
|
|
/**
|
|
* 存储企业定价
|
|
*
|
|
* @param array $attributes
|
|
* @return Product
|
|
*/
|
|
public function store(array $attributes = [])
|
|
{
|
|
$attributes['base_price'] = floatval($attributes['base_price']) * 100;
|
|
$attributes['renewal_price'] = floatval($attributes['renewal_price']) * 100;
|
|
|
|
$rule = [
|
|
'name' => ['required', 'between:2,32'],
|
|
'company_id' => ['required', 'exists:virtual_companies,id'],
|
|
'package_id' => ['required'],
|
|
'base_price' => [],
|
|
'renewal_price' => [],
|
|
'remark' => [],
|
|
];
|
|
|
|
$message = [
|
|
'name.required' => '请输入定价名称',
|
|
'name.between' => '请输入2-32个字符',
|
|
'company_id.required' => '请输入企业ID',
|
|
'company_id.exists' => '企业不存在或已删除',
|
|
'package_id.required' => '请输入套餐ID',
|
|
];
|
|
|
|
Validator::validate($attributes, $rule, $message);
|
|
|
|
if (!$package = app(PackageRepository::class)->find($attributes['package_id'])) {
|
|
throw new NotExistException('套餐不存在或已删除');
|
|
}
|
|
|
|
$attributes['sn'] = $package['sn'] . '_' . $attributes['company_id'] . '_' . $attributes['base_price'];
|
|
|
|
if (!$attributes['id']) {
|
|
if ($this->productRepository->where('sn', $attributes['sn'])->count()) {
|
|
throw new NotAllowedException('已存在相同定价,请勿重复添加');
|
|
}
|
|
$node = $this->productRepository->create($attributes);
|
|
}
|
|
|
|
if ($attributes['id']) {
|
|
if (!$node = $this->productRepository->find($attributes['id'])) {
|
|
throw new NotExistException('定价不存在或已删除');
|
|
}
|
|
|
|
if ($this->productRepository->where('sn', $attributes['sn'])->where('id', '<>', $attributes['id'])->count()) {
|
|
throw new NotAllowedException('已存在相同定价,请核对后重试');
|
|
}
|
|
|
|
$this->productRepository->setModel($node)->update($attributes);
|
|
}
|
|
|
|
return $node;
|
|
}
|
|
|
|
/**
|
|
* 删除
|
|
*
|
|
* @return bool
|
|
*/
|
|
public function destroy($ids)
|
|
{
|
|
$ids = is_array($ids) ? $ids : [$ids];
|
|
|
|
$this->productRepository->destroy($ids);
|
|
|
|
return true;
|
|
}
|
|
}
|