generated from liutk/owl-admin-base
89 lines
2.8 KiB
PHP
89 lines
2.8 KiB
PHP
<?php
|
|
|
|
namespace App\Admin\Services;
|
|
|
|
use App\Admin\Filters\OvertimeApplyFilter;
|
|
use App\Models\Employee;
|
|
use App\Models\OvertimeApply;
|
|
use Carbon\Carbon;
|
|
use Illuminate\Support\Facades\Validator;
|
|
|
|
class OvertimeApplyService extends BaseService
|
|
{
|
|
protected array $withRelationships = ['store', 'employee', 'workflow'];
|
|
|
|
protected string $modelName = OvertimeApply::class;
|
|
|
|
protected string $modelFilterName = OvertimeApplyFilter::class;
|
|
|
|
public function resloveData($data, $model = null)
|
|
{
|
|
// 获取员工所在的门店
|
|
if (! isset($data['store_id']) && isset($data['employee_id'])) {
|
|
$data['store_id'] = Employee::where('id', $data['employee_id'])->value('store_id');
|
|
}
|
|
if (isset($data['datetime_range'])) {
|
|
$time = explode(',', $data['datetime_range']);
|
|
$start = Carbon::createFromTimestamp(data_get($time, 0));
|
|
$end = Carbon::createFromTimestamp(data_get($time, 1));
|
|
$data['start_at'] = $start;
|
|
$data['end_at'] = $end;
|
|
$data['date'] = $start->format('Y-m-d');
|
|
}
|
|
if (isset($data['start_at']) && isset($data['end_at'])) {
|
|
$start = $data['start_at'] instanceof \DateTime ? $data['start_at'] : Carbon::parse($data['start_at']);
|
|
$end = $data['end_at'] instanceof \DateTime ? $data['end_at'] : Carbon::parse($data['end_at']);
|
|
$data['hours'] = $start->diffInHours($end);
|
|
}
|
|
|
|
return $data;
|
|
}
|
|
|
|
public function validate($data, $model = null)
|
|
{
|
|
// todo 验证申请时间是否重复
|
|
$createRules = [
|
|
'employee_id' => ['required'],
|
|
'date' => ['required'],
|
|
'start_at' => ['required'],
|
|
'end_at' => ['required'],
|
|
];
|
|
$updateRules = [];
|
|
$validator = Validator::make($data, $model ? $updateRules : $createRules, []);
|
|
if ($validator->fails()) {
|
|
return $validator->errors()->first();
|
|
}
|
|
|
|
return true;
|
|
}
|
|
|
|
public function update($primaryKey, $data): bool
|
|
{
|
|
$model = $this->query()->whereKey($primaryKey)->firstOrFail();
|
|
if (!$model->canUpdate()) {
|
|
return $this->setError('审核中, 无法修改');
|
|
}
|
|
$data = $this->resloveData($data, $model);
|
|
$validate = $this->validate($data, $model);
|
|
if ($validate !== true) {
|
|
$this->setError($validate);
|
|
|
|
return false;
|
|
}
|
|
|
|
return $model->update($data);
|
|
}
|
|
|
|
public function delete(string $ids): mixed
|
|
{
|
|
$list = $this->query()->with(['workflow'])->whereIn('id', explode(',', $ids))->get();
|
|
foreach ($list as $item) {
|
|
if (!$item->canUpdate()) {
|
|
return $this->setError('审核中, 无法删除');
|
|
}
|
|
$item->delete();
|
|
}
|
|
return true;
|
|
}
|
|
}
|