12 changed files with 307 additions and 0 deletions
@ -0,0 +1,132 @@ |
|||||
|
<?php |
||||
|
|
||||
|
namespace App\Http\Controllers\Admin; |
||||
|
|
||||
|
use App\Models\ParkingElectronicMap; |
||||
|
use App\Services\AdminFloorService; |
||||
|
use App\Services\ApiResponseService; |
||||
|
use App\Services\ParkingElectronicMapService; |
||||
|
use App\Services\ParkingSpaceService; |
||||
|
use Exception; |
||||
|
use Illuminate\Http\JsonResponse; |
||||
|
use Illuminate\Http\Request; |
||||
|
use Illuminate\Support\Facades\Validator; |
||||
|
use Illuminate\Validation\ValidationException; |
||||
|
|
||||
|
class ParkingElectronicMapController extends BaseController |
||||
|
{ |
||||
|
|
||||
|
protected ParkingElectronicMapService $service; |
||||
|
|
||||
|
/** |
||||
|
* 构造函数 |
||||
|
* @param ApiResponseService $responseService |
||||
|
* @param ParkingElectronicMapService $service |
||||
|
*/ |
||||
|
public function __construct( |
||||
|
ApiResponseService $responseService, |
||||
|
ParkingElectronicMapService $service |
||||
|
) { |
||||
|
parent::__construct($responseService); |
||||
|
$this->service = $service; |
||||
|
} |
||||
|
|
||||
|
/** |
||||
|
* 查询楼层 |
||||
|
* @return JsonResponse |
||||
|
*/ |
||||
|
public function floorList(): JsonResponse |
||||
|
{ |
||||
|
try { |
||||
|
$data = [ |
||||
|
'floor_list' => AdminFloorService::getSelectList() |
||||
|
]; |
||||
|
return $this->responseService->success($data); |
||||
|
} catch (Exception $e) { |
||||
|
$m_prefix = __('exception.exception_handler.resource'); |
||||
|
return $this->responseService->systemError( |
||||
|
$m_prefix . ':' . $e->getMessage() |
||||
|
); |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
/** |
||||
|
* 查询车位号码 |
||||
|
* @param $id |
||||
|
* @return JsonResponse |
||||
|
*/ |
||||
|
public function getParkingSpaceList($id): JsonResponse |
||||
|
{ |
||||
|
try { |
||||
|
$this->saveValidator(['floor_id' => $id]); |
||||
|
$list = ParkingSpaceService::getSelectList($id); |
||||
|
$data = [ |
||||
|
'parking_space_list' => $list |
||||
|
]; |
||||
|
return $this->responseService->success($data); |
||||
|
} catch (Exception $e) { |
||||
|
$m_prefix = __('exception.exception_handler.resource'); |
||||
|
return $this->responseService->systemError( |
||||
|
$m_prefix . ':' . $e->getMessage() |
||||
|
); |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
/** |
||||
|
* @param array $data |
||||
|
* @param int $is_save |
||||
|
* @return void |
||||
|
* @throws ValidationException |
||||
|
*/ |
||||
|
protected function saveValidator(array $data, int $is_save = 0): void |
||||
|
{ |
||||
|
$rules = [ |
||||
|
'floor_id' => 'required|numeric', |
||||
|
]; |
||||
|
$messages = [ |
||||
|
'floor_id.required' => __('validation.map.f_empty'), |
||||
|
'floor_id.numeric' => __('validation.map.f_number') |
||||
|
]; |
||||
|
if ($is_save) { |
||||
|
$rules['parking_space_id'] = 'required|numeric'; |
||||
|
$messages['parking_space_id.required'] = __('validation.map.p_empty'); |
||||
|
$messages['parking_space_id.numeric'] = __('validation.map.p_number'); |
||||
|
} |
||||
|
$validator = Validator::make($data, $rules, $messages); |
||||
|
|
||||
|
if ($validator->fails()) { |
||||
|
throw new ValidationException($validator); |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
public function save(Request $request): JsonResponse |
||||
|
{ |
||||
|
try { |
||||
|
$data = $request->all(); |
||||
|
$this->saveValidator($data, 1); |
||||
|
$this->service->saveModel($data); |
||||
|
return $this->responseService->success([], __('admin.save_succeeded')); |
||||
|
} catch (Exception $e) { |
||||
|
$m_prefix = __('admin.save_failed'); |
||||
|
return $this->responseService->systemError( |
||||
|
$m_prefix . ':' . $e->getMessage() |
||||
|
); |
||||
|
} |
||||
|
|
||||
|
} |
||||
|
|
||||
|
public function index(Request $request): JsonResponse |
||||
|
{ |
||||
|
try { |
||||
|
$data = $request->all(); |
||||
|
$this->saveValidator($data); |
||||
|
$list = $this->service->getList($data); |
||||
|
return $this->responseService->success($list); |
||||
|
} catch (Exception $e) { |
||||
|
$m_prefix = __('exception.exception_handler.resource'); |
||||
|
return $this->responseService->systemError( |
||||
|
$m_prefix . ':' . $e->getMessage() |
||||
|
); |
||||
|
} |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,27 @@ |
|||||
|
<?php |
||||
|
|
||||
|
namespace App\Models; |
||||
|
|
||||
|
use Illuminate\Database\Eloquent\Factories\HasFactory; |
||||
|
use Illuminate\Database\Eloquent\Model; |
||||
|
|
||||
|
class ParkingElectronicMap extends Model |
||||
|
{ |
||||
|
use HasFactory; |
||||
|
|
||||
|
protected $table = 'parking_electronic_map'; |
||||
|
|
||||
|
protected $fillable = [ |
||||
|
'floor_id', |
||||
|
'space_id', |
||||
|
'width', |
||||
|
'height', |
||||
|
'coordinate_x', |
||||
|
'coordinate_y' |
||||
|
]; |
||||
|
|
||||
|
protected $hidden = [ |
||||
|
'created_at', |
||||
|
'updated_at' |
||||
|
]; |
||||
|
} |
||||
@ -0,0 +1,72 @@ |
|||||
|
<?php |
||||
|
|
||||
|
namespace App\Services; |
||||
|
|
||||
|
use App\Models\AdminFloor; |
||||
|
use App\Models\ParkingElectronicMap; |
||||
|
use App\Models\ParkingSpace; |
||||
|
|
||||
|
class ParkingElectronicMapService extends BaseService |
||||
|
{ |
||||
|
|
||||
|
public function saveModel(array $data) |
||||
|
{ |
||||
|
$floor_id = $data['floor_id']; |
||||
|
$space_id = $data['parking_space_id']; |
||||
|
|
||||
|
$saveData = $where = [ |
||||
|
'floor_id' => $floor_id, |
||||
|
'space_id' => $space_id |
||||
|
]; |
||||
|
$res = ParkingElectronicMap::query()->where($where)->get()->toArray(); |
||||
|
$saveData['width'] = $data['width'] ?? '0'; |
||||
|
$saveData['height'] = $data['height'] ?? '0'; |
||||
|
$saveData['coordinate_x'] = $data['coordinate_x'] ?? ''; |
||||
|
$saveData['coordinate_y'] = $data['coordinate_y'] ?? ''; |
||||
|
if ($res) { |
||||
|
$saveData['update_at'] = get_datetime(); |
||||
|
$model = ParkingElectronicMap::query()->findOrFail($res[0]['id']); |
||||
|
$oldValues = $model->toArray(); |
||||
|
$model->update($saveData); |
||||
|
$this->logService->logUpdated($model, $oldValues, 'map.save'); |
||||
|
} else { |
||||
|
$saveData['create_at'] = get_datetime(); |
||||
|
$model = ParkingElectronicMap::query()->create($saveData); |
||||
|
$this->logService->logCreated($model, 'map.save'); |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
public function getList($params): array |
||||
|
{ |
||||
|
$floor_id = $params['floor_id']; |
||||
|
$where = [ |
||||
|
'floor_id' => $floor_id |
||||
|
]; |
||||
|
if (isset($params['parking_space_id']) |
||||
|
&& !empty($params['parking_space_id']) |
||||
|
) { |
||||
|
$where['space_id'] = $params['parking_space_id']; |
||||
|
} |
||||
|
$columns = [ |
||||
|
'id', |
||||
|
'floor_id', |
||||
|
'space_id', |
||||
|
'width', |
||||
|
'height', |
||||
|
'coordinate_x', |
||||
|
'coordinate_y' |
||||
|
]; |
||||
|
$list = ParkingElectronicMap::query()->where($where)->select($columns) |
||||
|
->get()->toArray(); |
||||
|
foreach ($list as &$item) { |
||||
|
$item['floor_name'] = AdminFloor::query()->where('id', $item['floor_id']) |
||||
|
->value('name'); |
||||
|
$item['parking_space_number'] = ParkingSpace::query()->where( |
||||
|
'id', |
||||
|
$item['space_id'] |
||||
|
)->value('number'); |
||||
|
unset($item['floor_id'], $item['space_id']); |
||||
|
} |
||||
|
return $list; |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,33 @@ |
|||||
|
<?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_electronic_map', function (Blueprint $table) { |
||||
|
$table->id(); |
||||
|
$table->integer('floor_id')->comment('楼层id'); |
||||
|
$table->integer('space_id')->default('')->comment('车位号码'); |
||||
|
$table->string('width', 20)->default('')->comment('宽'); |
||||
|
$table->string('height', 20)->default('')->comment('高'); |
||||
|
$table->string('coordinate_x', 20)->default('')->comment('坐标X'); |
||||
|
$table->string('coordinate_y', 20)->default('')->comment('坐标y'); |
||||
|
$table->timestamps(); |
||||
|
}); |
||||
|
} |
||||
|
|
||||
|
/** |
||||
|
* Reverse the migrations. |
||||
|
*/ |
||||
|
public function down(): void |
||||
|
{ |
||||
|
Schema::dropIfExists('parking_electronic_map'); |
||||
|
} |
||||
|
}; |
||||
Loading…
Reference in new issue