generated from liutk/owl-admin-base
83 lines
2.5 KiB
PHP
83 lines
2.5 KiB
PHP
<?php
|
|
|
|
namespace App\Admin\Services;
|
|
|
|
use App\Admin\Filters\HolidayApplyFilter;
|
|
use App\Models\Employee;
|
|
use App\Models\HolidayApply;
|
|
use Carbon\Carbon;
|
|
use Illuminate\Support\Facades\Validator;
|
|
|
|
class HolidayApplyService extends BaseService
|
|
{
|
|
protected array $withRelationships = ['store', 'employee', 'type', 'workflow'];
|
|
|
|
protected string $modelName = HolidayApply::class;
|
|
|
|
protected string $modelFilterName = HolidayApplyFilter::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['date_range']) && ! isset($data['start_at']) && ! isset($data['end_at'])) {
|
|
$dates = explode(',', $data['date_range']);
|
|
$data['start_at'] = Carbon::createFromFormat('Y-m-d', data_get($dates, 0))->startOfDay();
|
|
$data['end_at'] = Carbon::createFromFormat('Y-m-d', data_get($dates, 1))->endOfDay();
|
|
}
|
|
|
|
return $data;
|
|
}
|
|
|
|
public function validate($data, $model = null)
|
|
{
|
|
// todo 验证申请时间是否重复
|
|
$createRules = [
|
|
'employee_id' => ['required'],
|
|
'type_id' => ['required'],
|
|
'reason' => ['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 = HolidayApply::with(['workflow'])->whereIn('id', explode(',', $ids))->get();
|
|
foreach ($list as $item) {
|
|
if (!$item->canUpdate()) {
|
|
return $this->setError('审核中, 无法删除');
|
|
}
|
|
$item->delete();
|
|
}
|
|
return true;
|
|
}
|
|
}
|