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

127 lines
3.9 KiB
PHP

<?php
namespace App\Admin\Services;
use App\Admin\Filters\HolidayApplyFilter;
use App\Enums\CheckStatus;
use App\Models\Employee;
use App\Models\HolidayApply;
use Carbon\Carbon;
use Illuminate\Support\Facades\Validator;
use Slowlyo\OwlAdmin\Admin;
class HolidayApplyService extends BaseService
{
protected array $withRelationships = ['store', 'employee', 'type', 'workflow'];
protected string $modelName = HolidayApply::class;
protected string $modelFilterName = HolidayApplyFilter::class;
public function list()
{
$query = $this->listQuery();
if (request()->input('_all')) {
$list = (clone $query)->get();
$items = $list->all();
$total = $list->count();
} else {
$list = (clone $query)->paginate(request()->input('perPage', 20));
$items = $list->items();
$total = $list->total();
}
$user = Admin::user();
foreach ($items as &$item) {
$item->row_actions = $this->rowActions($user, $item);
}
return compact('items', 'total');
}
public function rowActions($user, $model)
{
// view, edit, delete
// apply, cancel
$actions = [];
if ($user->can('admin.hr.holiday.view')) {
array_push($actions, 'view');
}
if ($user->can('admin.hr.holiday.delete') && ! in_array($model->workflow->check_status, [CheckStatus::Processing, CheckStatus::Success])) {
array_push($actions, 'delete');
}
if (in_array($model->workflow->check_status, [CheckStatus::None, CheckStatus::Cancel, CheckStatus::Fail])) {
array_push($actions, 'apply');
}
if (in_array($model->workflow->check_status, [CheckStatus::Processing])) {
array_push($actions, 'cancel');
}
return $actions;
}
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', 'date'],
'end_at' => ['required', 'date'],
];
$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;
}
$model->update($data);
$this->currentModel = $model;
return true;
}
public function delete(string $ids): mixed
{
$list = $this->query()->with(['workflow'])->whereIn('id', explode(',', $ids))->get();
foreach ($list as $item) {
$item->delete();
}
return true;
}
}