126 lines
4.1 KiB
PHP
126 lines
4.1 KiB
PHP
<?php
|
|
namespace App\Domains\Virtual\Services;
|
|
|
|
use App\Core\Service;
|
|
use App\Models\Virtual\Product;
|
|
use Illuminate\Validation\Rule;
|
|
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\PackageRepository;
|
|
use App\Domains\Virtual\Repositories\ProductRepository;
|
|
|
|
class ProductService extends Service
|
|
{
|
|
protected $productRepository;
|
|
|
|
/**
|
|
* 构造函数
|
|
*
|
|
* @return void
|
|
*/
|
|
public function __construct(ProductRepository $productRepository)
|
|
{
|
|
$this->productRepository = $productRepository;
|
|
}
|
|
|
|
/**
|
|
* 获取企业定价列表
|
|
*
|
|
* @param array $conditions
|
|
* @return Collection
|
|
*/
|
|
public function index(array $conditions = [])
|
|
{
|
|
$list = $this->productRepository->whereHas('package', function ($query) {
|
|
$query->whereNull('deleted_at');
|
|
})->whereHas('company', function ($query) {
|
|
$query->whereNull('deleted_at');
|
|
})->withConditions($conditions)->applyConditions()->get();
|
|
|
|
$list->map(function ($item) {
|
|
$item->company = app(CompanyService::class)->load($item->company_id);
|
|
$item->package = app(PackageService::class)->load($item->package_id);
|
|
$item->base_price = sprintf('%.02f', $item->base_price/100);
|
|
$item->renewal_price = sprintf('%.02f', $item->renewal_price/100);
|
|
});
|
|
|
|
return $list;
|
|
}
|
|
|
|
/**
|
|
* 存储企业定价
|
|
*
|
|
* @param array $attributes
|
|
* @return Product
|
|
*/
|
|
public function store(array $attributes = [])
|
|
{
|
|
$attributes['base_price'] = intval($attributes['base_price'] * 100);
|
|
$attributes['renewal_price'] = intval($attributes['renewal_price'] * 100);
|
|
|
|
$rule = [
|
|
'name' => ['required', 'between:2,32', Rule::unique($this->productRepository->getTable(), 'name')->ignore($attributes['id'])->whereNUll('deleted_at')->where('company_id', $attributes['company_id'])],
|
|
'company_id' => ['required', 'exists:virtual_companies,id'],
|
|
'package_id' => ['required'],
|
|
'base_price' => [],
|
|
'renewal_price' => [],
|
|
'remark' => [],
|
|
];
|
|
|
|
$message = [
|
|
'name.required' => '请输入定价名称',
|
|
'name.between' => '请输入2-32个字符',
|
|
'name.unique' => '定价名称已存在,请更新输入',
|
|
'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;
|
|
}
|
|
}
|