store-manage/app/Admin/Services/StoreEmployeeService.php

81 lines
2.1 KiB
PHP

<?php
namespace App\Admin\Services;
use App\Admin\Filters\EmployeeFilter;
use App\Models\Employee;
use Illuminate\Support\Facades\Validator;
use Illuminate\Validation\Rule;
class StoreEmployeeService extends BaseService
{
protected array $withRelationships = ['store'];
protected string $modelName = Employee::class;
protected string $modelFilterName = EmployeeFilter::class;
public function listQuery()
{
$model = $this->getModel();
$filter = $this->getModelFilter();
$query = $this->query()->where('store_id', '>', 0);
if ($this->withRelationships) {
$query->with($this->withRelationships);
}
if ($filter) {
$query->filter(request()->input(), $filter);
}
if ($this->modelSortAble) {
$query->sort();
}
$this->sortable($query);
return $query;
}
public function store($data): bool
{
$storeId = $data['store_id'];
$employees = Employee::whereIn('id', $data['employee_id'])->get();
foreach ($employees as $employee) {
if ($employee->store_id > 0 && $employee->store_id != $storeId) {
$this->setError($employee->name . ' 已经有门店了');
return false;
}
if ($employee->master_store_id > 0) {
$this->setError($employee->name . ' 已经是店长了');
return false;
}
}
Employee::whereIn('id', $data['employee_id'])->update(['store_id' => $storeId]);
return true;
}
public function delete(string $ids): mixed
{
$id = explode(',', $ids);
Employee::whereIn('id', $id)->update(['store_id' => 0]);
return true;
}
public function validate($data, $model = null)
{
$createRules = [
'employee_id' => ['required'],
'store_id' => ['required'],
];
$updateRules = [
];
$validator = Validator::make($data, $model ? $updateRules : $createRules);
if ($validator->fails()) {
return $validator->errors()->first();
}
return true;
}
}