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

124 lines
3.8 KiB
PHP

<?php
namespace App\Admin\Services;
use App\Admin\Filters\OfficalBusinessFilter;
use App\Models\{Employee, OfficalBusiness};
use Carbon\Carbon;
use Illuminate\Support\Facades\Validator;
use Slowlyo\OwlAdmin\Admin;
use App\Enums\CheckStatus;
class OfficalBusinessService extends BaseService
{
protected array $withRelationships = ['store', 'employee', 'workflow'];
protected string $modelName = OfficalBusiness::class;
protected string $modelFilterName = OfficalBusinessFilter::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.business.view')) {
array_push($actions, 'view');
}
if ($user->can('admin.hr.business.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'])) {
$time = explode(',', $data['date_range']);
$start = Carbon::createFromTimestamp(data_get($time, 0))->startOfDay();
$end = Carbon::createFromTimestamp(data_get($time, 1))->endOfDay();
$data['start_at'] = $start;
$data['end_at'] = $end;
}
return $data;
}
public function validate($data, $model = null)
{
// todo 验证申请时间是否重复
$createRules = [
'employee_id' => ['required'],
'start_at' => ['required'],
'end_at' => ['required'],
'address' => ['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;
}
$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;
}
}