generated from liutk/owl-admin-base
127 lines
3.7 KiB
PHP
127 lines
3.7 KiB
PHP
<?php
|
|
|
|
namespace App\Admin\Services;
|
|
|
|
use App\Enums\StoreRole;
|
|
use Illuminate\Validation\Rule;
|
|
use App\Models\{Store, Employee};
|
|
use App\Admin\Filters\StoreFilter;
|
|
use Illuminate\Support\Facades\DB;
|
|
use Illuminate\Support\Facades\Validator;
|
|
|
|
class StoreService extends BaseService
|
|
{
|
|
protected array $withRelationships = ['category', 'level', 'business', 'master'];
|
|
|
|
protected string $modelName = Store::class;
|
|
|
|
protected string $modelFilterName = StoreFilter::class;
|
|
|
|
public function store($data): bool
|
|
{
|
|
$data = $this->resloveData($data);
|
|
|
|
$validate = $this->validate($data);
|
|
if ($validate !== true) {
|
|
$this->setError($validate);
|
|
|
|
return false;
|
|
}
|
|
|
|
$model = $this->modelName::create($data);
|
|
|
|
// 设置店长
|
|
$model->employees()->attach([$data['master_id'] => ['role' => StoreRole::Master]]);
|
|
|
|
return true;
|
|
}
|
|
|
|
public function update($primaryKey, $data): bool
|
|
{
|
|
$model = $this->query()->whereKey($primaryKey)->firstOrFail();
|
|
$data = $this->resloveData($data, $model);
|
|
$validate = $this->validate($data, $model);
|
|
if ($validate !== true) {
|
|
$this->setError($validate);
|
|
|
|
return false;
|
|
}
|
|
|
|
// 修改店长
|
|
if (isset($data['master_id']) && $data['master_id'] != $model->master_id) {
|
|
$model->employees()->detach($model->master_id);
|
|
$model->employees()->attach([$data['master_id'] => ['role' => StoreRole::Master]]);
|
|
}
|
|
|
|
return $model->update($data);
|
|
}
|
|
|
|
// 添加员工
|
|
public function attachEmployee(Store $store, array $employeeIds)
|
|
{
|
|
$data = [];
|
|
$employees = Employee::whereIn('id', $employeeIds)->get();
|
|
// 每个员工只能有一个门店
|
|
foreach ($employees as $employee) {
|
|
if (DB::table('store_employees')->where('employee_id', $employee->id)->exists()) {
|
|
$this->setError($employee->name.' 已经是店员');
|
|
return false;
|
|
}
|
|
$data[$employee->id] = ['role' => StoreRole::Employee];
|
|
}
|
|
$store->employees()->attach($data);
|
|
|
|
return true;
|
|
}
|
|
|
|
// 移除员工
|
|
public function destroyEmployee(Store $store, array $employeeIds)
|
|
{
|
|
$store->employees()->detach($employeeIds);
|
|
return true;
|
|
}
|
|
|
|
public function resloveData($data, $model = null)
|
|
{
|
|
if (isset($data['location'])) {
|
|
$data['lon'] = data_get($data['location'], 'lng');
|
|
$data['lat'] = data_get($data['location'], 'lat');
|
|
$data['address'] = data_get($data['location'], 'address');
|
|
}
|
|
|
|
return $data;
|
|
}
|
|
|
|
public function validate($data, $model = null)
|
|
{
|
|
$createRules = [
|
|
'title' => ['required'],
|
|
'master_id' => ['required', Rule::unique('stores', 'master_id')],
|
|
'category_id' => ['required'],
|
|
'business_id' => ['required'],
|
|
'level_id' => ['required'],
|
|
'region' => ['required'],
|
|
'lon' => ['required'],
|
|
'lat' => ['required'],
|
|
];
|
|
$updateRules = [
|
|
'master_id' => [Rule::unique('stores', 'master_id')->ignore($model, 'master_id')],
|
|
];
|
|
$validator = Validator::make($data, $model ? $updateRules : $createRules, [
|
|
'master_id.unique' => '已经是店长了',
|
|
]);
|
|
if ($validator->fails()) {
|
|
return $validator->errors()->first();
|
|
}
|
|
|
|
return true;
|
|
}
|
|
|
|
public function preDelete(array $ids)
|
|
{
|
|
// 删除店员
|
|
DB::table('store_employees')->whereIn('store_id', $ids)->delete();
|
|
return true;
|
|
}
|
|
}
|