old-hotel-new/app/Services/Admin/OldmenService.php

72 lines
1.6 KiB
PHP

<?php
namespace App\Services\Admin;
use App\Models\Oldmen;
use App\Filters\Admin\OldmenFilter;
/**
* @method Oldmen getModel()
* @method Oldmen|\Illuminate\Database\Query\Builder query()
*/
class OldmenService extends BaseService
{
protected string $modelName = Oldmen::class;
protected string $modelFilterName = OldmenFilter::class;
public function store($data): bool
{
if ($this->hasRepeated($data)) {
return false;
}
$columns = $this->getTableColumns();
$model = $this->getModel();
foreach ($data as $k => $v) {
if (!in_array($k, $columns)) {
continue;
}
$model->setAttribute($k, $v);
}
return $model->save();
}
public function update($primaryKey, $data): bool
{
if ($this->hasRepeated($data, $primaryKey)) {
return false;
}
$columns = $this->getTableColumns();
$model = $this->query()->whereKey($primaryKey)->first();
foreach ($data as $k => $v) {
if (!in_array($k, $columns)) {
continue;
}
$model->setAttribute($k, $v);
}
return $model->save();
}
public function hasRepeated($data, $id = 0): bool
{
$query = $this->query()->when($id, fn($query) => $query->where('id', '<>', $id));
if ((clone $query)->where('card_no', $data['card_no'])->exists()) {
$this->setError('该身份证号码已存在');
return true;
}
return false;
}
}