15 changed files with 468 additions and 0 deletions
@ -0,0 +1,225 @@ |
|||||
|
<?php |
||||
|
|
||||
|
namespace App\Http\Controllers\Admin; |
||||
|
|
||||
|
use App\Exceptions\CustomException; |
||||
|
use App\Models\ParkingDepartureReason; |
||||
|
use App\Services\AdminTranslationService; |
||||
|
use App\Services\ApiResponseService; |
||||
|
use App\Services\ParkingDepartureReasonService; |
||||
|
use Exception; |
||||
|
use Illuminate\Http\JsonResponse; |
||||
|
use Illuminate\Http\Request; |
||||
|
use Illuminate\Support\Facades\Validator; |
||||
|
use Illuminate\Validation\ValidationException; |
||||
|
|
||||
|
class ParkingDepartureReasonController extends BaseController |
||||
|
{ |
||||
|
protected string $menuUri = 'departureReasonManagement'; |
||||
|
/** |
||||
|
* @var ParkingDepartureReasonService |
||||
|
*/ |
||||
|
protected ParkingDepartureReasonService $service; |
||||
|
|
||||
|
/** |
||||
|
* 构造函数 |
||||
|
* @param ApiResponseService $responseService |
||||
|
* @param ParkingDepartureReasonService $service |
||||
|
*/ |
||||
|
public function __construct( |
||||
|
ApiResponseService $responseService, |
||||
|
ParkingDepartureReasonService $service |
||||
|
) { |
||||
|
parent::__construct($responseService); |
||||
|
$this->service = $service; |
||||
|
} |
||||
|
|
||||
|
/** |
||||
|
* @param Request $request |
||||
|
* @return JsonResponse |
||||
|
*/ |
||||
|
public function index(Request $request): JsonResponse |
||||
|
{ |
||||
|
try { |
||||
|
$query = ParkingDepartureReason::query(); |
||||
|
|
||||
|
if ($request->has('reason')) { |
||||
|
$reason = $request->input('reason'); |
||||
|
if ($reason) { |
||||
|
$query->where('reason', 'like', "%{$reason}%"); |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
if ($request->has('create_start_time') |
||||
|
&& $request->has( |
||||
|
'create_end_time' |
||||
|
) |
||||
|
) { |
||||
|
$create_start_time = $request->input('create_start_time'); |
||||
|
$create_end_time = $request->input('create_end_time'); |
||||
|
if ($create_start_time && $create_end_time) { |
||||
|
$query->whereBetween('created_at', [ |
||||
|
$create_start_time . ' 00:00:00', |
||||
|
$create_end_time . ' 23:59:59' |
||||
|
]); |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
// 分页 |
||||
|
$page = $request->input('page', 1); |
||||
|
$perPage = $request->input('per_page', 10); |
||||
|
$total = $query->count(); |
||||
|
$items = $query->latest()->forPage($page, $perPage)->get()->each( |
||||
|
function ($item) { |
||||
|
$item['reason'] |
||||
|
= AdminTranslationService::getTranslationTypeName( |
||||
|
$item['id'], |
||||
|
8, |
||||
|
$item['reason'] |
||||
|
); |
||||
|
return $item; |
||||
|
} |
||||
|
); |
||||
|
|
||||
|
return $this->responseService->success([ |
||||
|
'items' => $items, |
||||
|
'total' => $total, |
||||
|
'page' => $page, |
||||
|
'per_page' => $perPage, |
||||
|
'last_page' => ceil($total / $perPage), |
||||
|
]); |
||||
|
} catch (Exception $e) { |
||||
|
$m_prefix = __('exception.exception_handler.resource'); |
||||
|
return $this->responseService->systemError( |
||||
|
$m_prefix . ':' . $e->getMessage() |
||||
|
); |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
/** |
||||
|
* @param Request $request |
||||
|
* @return JsonResponse |
||||
|
* @throws CustomException |
||||
|
* @throws ValidationException |
||||
|
*/ |
||||
|
public function store(Request $request): JsonResponse |
||||
|
{ |
||||
|
try { |
||||
|
$this->saveValidator($request->all()); |
||||
|
$this->service->createModel($request->all()); |
||||
|
return $this->responseService->success( |
||||
|
null, |
||||
|
__('admin.save_succeeded') |
||||
|
); |
||||
|
} catch (ValidationException|CustomException $e) { |
||||
|
throw $e; |
||||
|
} catch (Exception $e) { |
||||
|
return $this->responseService->systemError( |
||||
|
__('admin.operation_failed') . ':' |
||||
|
. $e->getMessage() |
||||
|
); |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
/** |
||||
|
* @param array $data |
||||
|
* @param int $id |
||||
|
* @return void |
||||
|
* @throws ValidationException |
||||
|
*/ |
||||
|
protected function saveValidator(array $data, int $id = 0): void |
||||
|
{ |
||||
|
$rules = [ |
||||
|
'reason' => 'required' |
||||
|
]; |
||||
|
$messages = [ |
||||
|
'reason.required' => __( |
||||
|
'validation.departure_management.n_empty' |
||||
|
) |
||||
|
]; |
||||
|
if ($id) { |
||||
|
$this->validateId($id, ParkingDepartureReason::class); |
||||
|
} |
||||
|
$validator = Validator::make($data, $rules, $messages); |
||||
|
|
||||
|
if ($validator->fails()) { |
||||
|
throw new ValidationException($validator); |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
/** |
||||
|
* @param string $id |
||||
|
* @return JsonResponse |
||||
|
*/ |
||||
|
public function edit(string $id): JsonResponse |
||||
|
{ |
||||
|
try { |
||||
|
$this->validateId($id, ParkingDepartureReason::class); |
||||
|
$data = [ |
||||
|
'item' => ParkingDepartureReason::query()->find($id) |
||||
|
]; |
||||
|
$Translation = AdminTranslationService::getTranslation( |
||||
|
$data['item']['id'], |
||||
|
8 |
||||
|
); |
||||
|
$data['item']['en_reason'] = $Translation['en'] ?? ''; |
||||
|
$data['item']['tw_reason'] = $Translation['zh_tw'] ?? ''; |
||||
|
return $this->responseService->success($data); |
||||
|
} catch (Exception $e) { |
||||
|
return $this->responseService->systemError( |
||||
|
__('exception.get_data_failed') . ':' . $e->getMessage() |
||||
|
); |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
/** |
||||
|
* @param Request $request |
||||
|
* @param string $id |
||||
|
* @return JsonResponse |
||||
|
* @throws CustomException |
||||
|
* @throws ValidationException |
||||
|
*/ |
||||
|
public function update(Request $request, string $id): JsonResponse |
||||
|
{ |
||||
|
try { |
||||
|
$this->saveValidator($request->all(), $id); |
||||
|
$this->service->updateModel($request->all(), $id); |
||||
|
return $this->responseService->success( |
||||
|
null, |
||||
|
__('admin.update_succeeded') |
||||
|
); |
||||
|
} catch (ValidationException|CustomException $e) { |
||||
|
throw $e; |
||||
|
} catch (Exception $e) { |
||||
|
return $this->responseService->systemError( |
||||
|
__('admin.operation_failed') . ':' |
||||
|
. $e->getMessage() |
||||
|
); |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
/** |
||||
|
* @param string $id |
||||
|
* @return JsonResponse |
||||
|
* @throws CustomException |
||||
|
* @throws ValidationException |
||||
|
*/ |
||||
|
public function destroy(string $id): JsonResponse |
||||
|
{ |
||||
|
try { |
||||
|
$this->validateId($id, ParkingDepartureReason::class); |
||||
|
$this->service->deleteModel($id); |
||||
|
return $this->responseService->success( |
||||
|
null, |
||||
|
__('admin.delete_succeeded') |
||||
|
); |
||||
|
} catch (ValidationException|CustomException $e) { |
||||
|
throw $e; |
||||
|
} catch (Exception $e) { |
||||
|
return $this->responseService->systemError( |
||||
|
__('admin.operation_failed') . ':' |
||||
|
. $e->getMessage() |
||||
|
); |
||||
|
} |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,31 @@ |
|||||
|
<?php |
||||
|
|
||||
|
namespace App\Models; |
||||
|
|
||||
|
use Illuminate\Database\Eloquent\Factories\HasFactory; |
||||
|
use Illuminate\Database\Eloquent\Model; |
||||
|
use Illuminate\Database\Eloquent\SoftDeletes; |
||||
|
|
||||
|
class ParkingDepartureReason extends Model |
||||
|
{ |
||||
|
use HasFactory, SoftDeletes; |
||||
|
|
||||
|
protected $table = 'parking_departure_reason'; |
||||
|
|
||||
|
protected $fillable |
||||
|
= [ |
||||
|
'reason', |
||||
|
'remark' |
||||
|
]; |
||||
|
|
||||
|
protected $hidden |
||||
|
= [ |
||||
|
'updated_at', |
||||
|
'deleted_at' |
||||
|
]; |
||||
|
|
||||
|
public function getCreatedAtAttribute($value): string |
||||
|
{ |
||||
|
return $value ? date("Y-m-d H:i:s", strtotime($value)) : $value; |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,138 @@ |
|||||
|
<?php |
||||
|
|
||||
|
namespace App\Services; |
||||
|
|
||||
|
use App\Models\ParkingDepartureReason; |
||||
|
use Exception; |
||||
|
use Illuminate\Support\Facades\DB; |
||||
|
|
||||
|
class ParkingDepartureReasonService extends BaseService |
||||
|
{ |
||||
|
protected string $menuTitle = 'departure_management'; |
||||
|
|
||||
|
/** |
||||
|
* @param array $data |
||||
|
* @throws Exception |
||||
|
*/ |
||||
|
public function createModel(array $data) |
||||
|
{ |
||||
|
try { |
||||
|
DB::beginTransaction(); |
||||
|
|
||||
|
$existsWhere = [ |
||||
|
['reason', '=', $data['reason']] |
||||
|
]; |
||||
|
if (ParkingDepartureReason::query()->where($existsWhere)->exists() |
||||
|
) { |
||||
|
throw new Exception( |
||||
|
__('service.' . $this->menuTitle . '.reason_exists') |
||||
|
); |
||||
|
} |
||||
|
|
||||
|
$model = ParkingDepartureReason::query()->create([ |
||||
|
'reason' => $data['reason'], |
||||
|
'remark' => $data['remark'] ?? '', |
||||
|
'created_at' => get_datetime() |
||||
|
]); |
||||
|
|
||||
|
$this->logService->logCreated( |
||||
|
$model, |
||||
|
$this->menuTitle . '.create' |
||||
|
); |
||||
|
|
||||
|
AdminTranslationService::saveTranslation( |
||||
|
$data['reason'], |
||||
|
$data['en_reason'] ?? '', |
||||
|
$data['tw_reason'] ?? '', |
||||
|
$model->id, |
||||
|
8 |
||||
|
); |
||||
|
|
||||
|
DB::commit(); |
||||
|
return $model; |
||||
|
} catch (Exception $e) { |
||||
|
DB::rollBack(); |
||||
|
throw $e; |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
/** |
||||
|
* @param array $data |
||||
|
* @param int $id |
||||
|
* @throws Exception |
||||
|
*/ |
||||
|
public function updateModel(array $data, int $id) |
||||
|
{ |
||||
|
try { |
||||
|
DB::beginTransaction(); |
||||
|
|
||||
|
// 验证 |
||||
|
$existsWhere = [ |
||||
|
['reason', '=', $data['reason']], |
||||
|
['id', '<>', $id] |
||||
|
]; |
||||
|
if (ParkingDepartureReason::query()->where($existsWhere)->exists() |
||||
|
) { |
||||
|
throw new Exception( |
||||
|
__('service.' . $this->menuTitle . '.reason_exists') |
||||
|
); |
||||
|
} |
||||
|
|
||||
|
// 更新 |
||||
|
$model = ParkingDepartureReason::query()->findOrFail($id); |
||||
|
$oldValues = $model->toArray(); |
||||
|
|
||||
|
$model->update([ |
||||
|
'reason' => $data['reason'], |
||||
|
'remark' => $data['remark'] ?? '', |
||||
|
'updated_at' => get_datetime() |
||||
|
]); |
||||
|
|
||||
|
$this->logService->logUpdated( |
||||
|
$model, |
||||
|
$oldValues, |
||||
|
$this->menuTitle . '.update' |
||||
|
); |
||||
|
|
||||
|
AdminTranslationService::saveTranslation( |
||||
|
$data['reason'], |
||||
|
$data['en_reason'] ?? '', |
||||
|
$data['tw_reason'] ?? '', |
||||
|
$id, |
||||
|
8 |
||||
|
); |
||||
|
|
||||
|
DB::commit(); |
||||
|
return $model; |
||||
|
} catch (Exception $e) { |
||||
|
DB::rollBack(); |
||||
|
throw $e; |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
/** |
||||
|
* @param $id |
||||
|
* @return bool |
||||
|
* @throws Exception |
||||
|
*/ |
||||
|
public function deleteModel($id): bool |
||||
|
{ |
||||
|
try { |
||||
|
DB::beginTransaction(); |
||||
|
|
||||
|
$model = ParkingDepartureReason::query()->findOrFail($id); |
||||
|
|
||||
|
$this->logService->logDeleted($model, $this->menuTitle . '.delete'); |
||||
|
|
||||
|
$model->delete(); |
||||
|
|
||||
|
AdminTranslationService::syncDelete($id, 8); |
||||
|
|
||||
|
DB::commit(); |
||||
|
return true; |
||||
|
} catch (Exception $e) { |
||||
|
DB::rollBack(); |
||||
|
throw $e; |
||||
|
} |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,32 @@ |
|||||
|
<?php |
||||
|
|
||||
|
use Illuminate\Database\Migrations\Migration; |
||||
|
use Illuminate\Database\Schema\Blueprint; |
||||
|
use Illuminate\Support\Facades\Schema; |
||||
|
|
||||
|
return new class extends Migration |
||||
|
{ |
||||
|
/** |
||||
|
* Run the migrations. |
||||
|
*/ |
||||
|
public function up(): void |
||||
|
{ |
||||
|
Schema::create('parking_guard_booth', function (Blueprint $table) { |
||||
|
$table->id(); |
||||
|
$table->string('reason', 255)->comment('离场原因'); |
||||
|
$table->string('remark', 255)->default('')->comment('备注'); |
||||
|
$table->timestamps(); |
||||
|
$table->softDeletes(); |
||||
|
$table->innoDb(); |
||||
|
$table->comment('离职原因'); |
||||
|
}); |
||||
|
} |
||||
|
|
||||
|
/** |
||||
|
* Reverse the migrations. |
||||
|
*/ |
||||
|
public function down(): void |
||||
|
{ |
||||
|
Schema::dropIfExists('parking_guard_booth'); |
||||
|
} |
||||
|
}; |
||||
Loading…
Reference in new issue