121 lines
3.2 KiB
PHP
121 lines
3.2 KiB
PHP
<?php
|
|
namespace App\Domains\Virtual\Services;
|
|
|
|
use App\Core\Service;
|
|
use App\Models\Virtual\Company;
|
|
use Illuminate\Validation\Rule;
|
|
use App\Exceptions\NotExistException;
|
|
use Illuminate\Support\Facades\Validator;
|
|
use App\Domains\Virtual\Services\CommonService;
|
|
use App\Domains\Virtual\Repositories\CompanyRepository;
|
|
|
|
class CompanyService extends Service
|
|
{
|
|
protected $companyRepository;
|
|
|
|
protected static $companies;
|
|
|
|
/**
|
|
* 构造函数
|
|
*
|
|
* @return void
|
|
*/
|
|
public function __construct(CompanyRepository $companyRepository)
|
|
{
|
|
$this->companyRepository = $companyRepository;
|
|
}
|
|
|
|
/**
|
|
* 企业列表
|
|
*
|
|
* @param array $conditions
|
|
* @return mixed
|
|
*/
|
|
public function index(array $conditions = [])
|
|
{
|
|
$limit = $conditions['limit'] ?? 20;
|
|
|
|
$companies = $this->companyRepository->withConditions($conditions)
|
|
->applyConditions()->paginate($limit);
|
|
|
|
$companies->map(function ($item) {
|
|
$item->status = $item->deleted_at ? 2 : $item->status;
|
|
});
|
|
|
|
return $companies;
|
|
}
|
|
|
|
/**
|
|
* 存储企业
|
|
*
|
|
* @param array $attributes
|
|
* @param Company $parent
|
|
* @return Company
|
|
*/
|
|
public function store(array $attributes = [])
|
|
{
|
|
$attributes = array_only($attributes, array_merge(app(Company::class)->getFillable()));
|
|
|
|
$rule = [
|
|
'name' => ['required', 'between:2,32', Rule::unique($this->companyRepository->getTable(), 'name')->ignore($attributes['id'])->whereNUll('deleted_at')],
|
|
'contacts' => ['string', 'between:2,32'],
|
|
'mobile' => ['string', 'cn_phone'],
|
|
'address' => ['string'],
|
|
'extends' => ['array'],
|
|
];
|
|
|
|
$message = [
|
|
'name.required' => '请输入企业名称',
|
|
'name.between' => '企业名称长度不合法',
|
|
'name.unique' => '企业名称已经被其他用户所使用',
|
|
'contacts.between' => '联系人长度不合法',
|
|
'mobile.cn_phone' => '手机号不合法',
|
|
];
|
|
|
|
Validator::validate($attributes, $rule, $message);
|
|
|
|
if (!$attributes['id']) {
|
|
$maxId = Company::withTrashed()->max('id');
|
|
$attributes['id'] = $maxId ? $maxId + 1 : 1;
|
|
$attributes['sn'] = CommonService::stringifyCompanyId($attributes['id']);
|
|
|
|
$node = $this->companyRepository->create($attributes);
|
|
}
|
|
|
|
if ($attributes['id']) {
|
|
$node = $this->companyRepository->find($attributes['id']);
|
|
|
|
if (!$node) {
|
|
throw new NotExistException('企业不存在或已删除');
|
|
}
|
|
|
|
$this->companyRepository->setModel($node)->update($attributes);
|
|
}
|
|
|
|
return $node;
|
|
}
|
|
|
|
/**
|
|
* 删除
|
|
*
|
|
* @return bool
|
|
*/
|
|
public function destroy($ids)
|
|
{
|
|
$ids = is_array($ids) ? $ids : [$ids];
|
|
|
|
$this->companyRepository->destroy($ids);
|
|
|
|
return true;
|
|
}
|
|
|
|
public static function load($id)
|
|
{
|
|
if (!self::$companies) {
|
|
self::$companies = app(CompanyRepository::class)->select(['id', 'name', 'status'])->withTrashed()->get()->keyBy('id');
|
|
}
|
|
|
|
return self::$companies[$id];
|
|
}
|
|
}
|