generated from liutk/owl-admin-base
112 lines
3.3 KiB
PHP
112 lines
3.3 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers\Api\Hr;
|
|
|
|
use App\Http\Controllers\Api\Controller;
|
|
use App\Models\{Employee, Store, AdminUser};
|
|
use Illuminate\Http\{Request, Response};
|
|
use App\Enums\UserRole;
|
|
use App\Http\Resources\EmployeeResource;
|
|
use App\Admin\Services\EmployeeService;
|
|
use App\Exceptions\RuntimeException;
|
|
use Illuminate\Support\Facades\DB;
|
|
use App\Enums\EmployeeStatus;
|
|
|
|
/**
|
|
* 员工管理
|
|
*/
|
|
class EmployeeController extends Controller
|
|
{
|
|
public function index(Request $request)
|
|
{
|
|
$user = $this->guard()->user();
|
|
$role = $user->userRole();
|
|
$filter = $request->all();
|
|
$query = Employee::with(['jobs', 'store', 'adminUser'])->filter($filter)->enable();
|
|
|
|
if (!in_array(UserRole::Admin->value, $role)) {
|
|
$query->whereIn('store_id', [$user->store_id]);
|
|
}
|
|
$list = $query->paginate($request->input('per_page'));
|
|
|
|
return EmployeeResource::collection($list);
|
|
}
|
|
|
|
public function show($id)
|
|
{
|
|
$info = Employee::with(['jobs', 'store'])->findOrFail($id);
|
|
|
|
return EmployeeResource::make($info);
|
|
}
|
|
|
|
public function store(Request $request)
|
|
{
|
|
try {
|
|
DB::beginTransaction();
|
|
$service = EmployeeService::make();
|
|
$data = $request->all();
|
|
if (!$service->store($data)) {
|
|
throw new RuntimeException($service->getError());
|
|
}
|
|
DB::commit();
|
|
} catch (\Exception $e) {
|
|
DB::rollback();
|
|
throw new RuntimeException($e->getMessage());
|
|
}
|
|
return response('', Response::HTTP_CREATED);
|
|
}
|
|
|
|
public function update($id, Request $request)
|
|
{
|
|
try {
|
|
DB::beginTransaction();
|
|
$service = EmployeeService::make();
|
|
$data = $request->only(['name', 'avatar', 'password', 'confirm_password', 'phone', 'store_id']);
|
|
if (!$service->update($id, $data)) {
|
|
throw new RuntimeException($service->getError());
|
|
}
|
|
DB::commit();
|
|
} catch (\Exception $e) {
|
|
DB::rollback();
|
|
throw new RuntimeException($e->getMessage());
|
|
}
|
|
return response('', Response::HTTP_OK);
|
|
}
|
|
|
|
public function destroy($id)
|
|
{
|
|
try {
|
|
DB::beginTransaction();
|
|
$service = EmployeeService::make();
|
|
if (!$service->delete($id)) {
|
|
throw new RuntimeException($service->getError());
|
|
}
|
|
DB::commit();
|
|
} catch (\Exception $e) {
|
|
DB::rollback();
|
|
throw new RuntimeException($e->getMessage());
|
|
}
|
|
return response('', Response::HTTP_OK);
|
|
}
|
|
|
|
public function leave($id)
|
|
{
|
|
$info = Employee::findOrFail($id);
|
|
try {
|
|
DB::beginTransaction();
|
|
if ($info->employee_status != EmployeeStatus::Online) {
|
|
throw new RuntimeException('未入职');
|
|
}
|
|
$service = EmployeeService::make();
|
|
if (!$service->leave($info)) {
|
|
throw new RuntimeException($service->getError());
|
|
}
|
|
DB::commit();
|
|
} catch (\Exception $e) {
|
|
DB::rollback();
|
|
throw new RuntimeException($e->getMessage());
|
|
}
|
|
return response('', Response::HTTP_OK);
|
|
}
|
|
}
|