88 lines
1.9 KiB
PHP
88 lines
1.9 KiB
PHP
<?php
|
|
namespace App\Domains\Virtual\Services;
|
|
|
|
use App\Core\Service;
|
|
use App\Models\Virtual\Product;
|
|
use App\Exceptions\NotExistException;
|
|
use Illuminate\Support\Facades\Validator;
|
|
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 = [])
|
|
{
|
|
$rule = [
|
|
];
|
|
|
|
$message = [
|
|
];
|
|
|
|
Validator::validate($attributes, $rule, $message);
|
|
|
|
if (!$attributes['id']) {
|
|
$node = $this->productRepository->create($attributes);
|
|
}
|
|
|
|
if ($attributes['id']) {
|
|
if (!$node = $this->productRepository->find($attributes['id'])) {
|
|
throw new NotExistException('地址不存在或已删除');
|
|
}
|
|
|
|
$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;
|
|
}
|
|
}
|