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

174 lines
5.5 KiB
PHP

<?php
namespace App\Admin\Services;
use App\Admin\Filters\EmployeeFilter;
use App\Admin\Services\System\AdminUserService;
use App\Enums\EmployeeStatus;
use App\Models\Employee;
use App\Models\EmployeeSignLog;
use App\Models\HolidayApply;
use App\Models\OfficalBusiness;
use App\Models\OvertimeApply;
use Illuminate\Support\Arr;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Validator;
use Slowlyo\OwlAdmin\Models\AdminUser;
class EmployeeService extends BaseService
{
protected array $withRelationships = ['jobs', 'adminUser', 'store'];
protected string $modelName = Employee::class;
protected string $modelFilterName = EmployeeFilter::class;
public function store($data): bool
{
$data = $this->resloveData($data);
$validate = $this->validate($data);
if ($validate !== true) {
$this->setError($validate);
return false;
}
// 添加管理员信息
$adminUserService = AdminUserService::make();
if (! $adminUserService->store(Arr::only($data, ['username', 'avatar', 'password', 'confirm_password', 'name']))) {
$this->setError($adminUserService->getError());
return false;
}
$adminUser = AdminUser::where('username', $data['username'])->first();
$data['admin_user_id'] = $adminUser->id;
$model = $this->modelName::create($data);
// 职位修改
if (isset($data['jobs'])) {
$jobs = is_array($data['jobs']) ? $data['jobs'] : explode(',', $data['jobs']);
$model->jobs()->sync($jobs);
}
return true;
}
public function update($primaryKey, $data): bool
{
$model = $this->query()->whereKey($primaryKey)->firstOrFail();
$data = $this->resloveData($data, $model);
$validate = $this->validate($data, $model);
if ($validate !== true) {
$this->setError($validate);
return false;
}
// 修改管理员
$adminUserService = AdminUserService::make();
if (! $adminUserService->update($model->admin_user_id, Arr::only($data, ['password', 'confirm_password', 'name', 'avatar']))) {
return $this->setError($adminUserService->getError());
}
// 职位修改
if (isset($data['jobs'])) {
if (is_array($data['jobs'])) {
if (count($data['jobs']) > 0 && is_array($data['jobs'][0])) {
$jobs = array_column($data['jobs'], 'key');
} else {
$jobs = $data['jobs'];
}
} else {
$jobs = explode(',', $data['jobs']);
}
$model->jobs()->sync($jobs);
}
return $model->update($data);
}
/**
* 员工离职/还原
*/
public function leave(Employee $user, $options = []): bool
{
if ($user->employee_status == EmployeeStatus::Online) {
// 店长不能离职
if ($user->isStoreMaster()) {
return $this->setError('请先设置新店长');
}
$user->update([
'leave_at' => $options['leave_at'] ?? now(),
'store_id' => 0,
'employee_status' => EmployeeStatus::Offline,
'is_sign' => 0,
]);
} else {
$user->update([
'join_at' => now(),
'leave_at' => null,
'employee_status' => EmployeeStatus::Online,
'is_sign' => 1,
]);
}
return true;
}
public function preDelete(array $ids): void
{
// 无打卡记录
if (EmployeeSignLog::whereIn('employee_id', $ids)->exists()) {
admin_abort('请先删除员工打卡记录');
}
// 无请假申请
if (HolidayApply::whereIn('employee_id', $ids)->exists()) {
admin_abort('请先删除员工请假申请');
}
// 无出差申请
if (OfficalBusiness::whereIn('employee_id', $ids)->exists()) {
admin_abort('请先删除员工出差申请');
}
// 加班申请
if (OvertimeApply::whereIn('employee_id', $ids)->exists()) {
admin_abort('请先删除员工加班申请');
}
// 店员关联
if (Employee::whereIn('id', $ids)->where('store_id', '>', 0)->exists()) {
admin_abort('员工已关联门店, 请先从门店中删除');
}
// 删除管理员
$adminUserIds = Employee::whereIn('id', $ids)->pluck('admin_user_id');
if (in_array(1, $adminUserIds->toArray())) {
admin_abort('admin 账户, 不能删除');
}
$adminUserService = AdminUserService::make();
$adminUserService->delete($adminUserIds->implode(','));
// 删除职位关联
DB::table('employee_jobs')->whereIn('employee_id', $ids)->delete();
}
public function validate($data, $model = null)
{
$createRules = [
'name' => ['required'],
'phone' => ['required', 'phone'],
'username' => ['required'],
'password' => ['required', 'min:6'],
];
$updateRules = [];
$validator = Validator::make($data, $model ? $updateRules : $createRules, [
'name.required' => __('employee.name').'必填',
'phone.required' => __('employee.phone').'必填',
]);
if ($validator->fails()) {
return $validator->errors()->first();
}
return true;
}
}