Browse Source

车位属性更改 创建、编辑、删除、查看逻辑

master
wanghongjun 4 weeks ago
parent
commit
5f1abbd712
  1. 6
      app/Http/Controllers/Admin/ParkingSpaceTypeController.php
  2. 8
      app/Models/ParkingSpaceType.php
  3. 30
      app/Models/ParkingSpaceTypeAttr.php
  4. 79
      app/Services/ParkingSpaceTypeAttrService.php
  5. 42
      app/Services/ParkingSpaceTypeService.php
  6. 35
      database/migrations/2026_03_09_143728_create_parking_space_type_attr_table.php
  7. 6
      database/migrations/2026_03_09_143728_create_parking_space_type_table.php
  8. 4
      resources/lang/en/log.php
  9. 4
      resources/lang/zh-CN/log.php
  10. 4
      resources/lang/zh-TW/log.php

6
app/Http/Controllers/Admin/ParkingSpaceTypeController.php

@ -6,6 +6,7 @@ use App\Exceptions\CustomException;
use App\Models\ParkingSpaceAttributes;
use App\Models\ParkingSpaceType;
use App\Services\ApiResponseService;
use App\Services\ParkingSpaceTypeAttrService;
use App\Services\ParkingSpaceTypeService;
use Exception;
use Illuminate\Http\JsonResponse;
@ -150,11 +151,12 @@ class ParkingSpaceTypeController extends BaseController
{
try {
$this->validateId($id, ParkingSpaceType::class);
$item = ParkingSpaceType::query()->findOrFail($id)->toArray();
$item['attributes'] = ParkingSpaceTypeAttrService::getTypeAttrData($item['id']);
$data = [
'color_list' => $this->SpaceTypeService->getColorList(),
'attr_list' => ParkingSpaceAttributes::getList(),
'item' => ParkingSpaceType::query()->findOrFail($id)
->toArray()
'item' => $item
];
return $this->responseService->success($data);
} catch (Exception $e) {

8
app/Models/ParkingSpaceType.php

@ -20,13 +20,7 @@ class ParkingSpaceType extends Model
'default_color_vacant',
'default_color_warning',
'default_is_warning',
'default_is_flicker',
'attributes_id',
'attr_color_occupy',
'attr_color_vacant',
'attr_color_warning',
'attr_is_warning',
'attr_is_flicker'
'default_is_flicker'
];
protected $hidden
= [

30
app/Models/ParkingSpaceTypeAttr.php

@ -0,0 +1,30 @@
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
class ParkingSpaceTypeAttr extends Model
{
use HasFactory;
protected $table = 'parking_space_type_attr';
protected $fillable
= [
'space_type_id',
'space_attr_id',
'color_occupy',
'color_vacant',
'color_warning',
'is_warning',
'is_flicker'
];
protected $hidden
= [
'created_at',
'updated_at'
];
}

79
app/Services/ParkingSpaceTypeAttrService.php

@ -0,0 +1,79 @@
<?php
namespace App\Services;
use App\Models\ParkingSpaceTypeAttr;
class ParkingSpaceTypeAttrService
{
/**
* @var OperationLogService
*/
private OperationLogService $logService;
/**
* 构造函数
* @param OperationLogService $logService
*/
public function __construct(OperationLogService $logService)
{
$this->logService = $logService;
$this->logService->menuTitle = 'cat_type';
}
public function createData($space_type_id, $data)
{
$create = [
'space_type_id' => $space_type_id,
'space_attr_id' => $data['space_attr_id'],
'color_occupy' => $data['color_occupy'],
'color_vacant' => $data['color_vacant'],
'color_warning' => $data['color_warning'],
'is_warning' => $data['is_warning'],
'is_flicker' => $data['is_flicker'],
'created_at' => get_datetime(),
];
$model = ParkingSpaceTypeAttr::query()->create($create);
$this->logService->logCreated($model, 'space_type_attr.create');
}
public function createBatchData($space_type_id, array $data)
{
foreach ($data as $value) {
$this->createData($space_type_id, $value);
}
}
public function deleteBatchData($space_type_id)
{
$data = ParkingSpaceTypeAttr::query()->where('space_type_id', $space_type_id)
->select()->get()->toArray();
$this->logService->logDeletedData(
new ParkingSpaceTypeAttr(),
'space_type_attr.delete',
$data
);
ParkingSpaceTypeAttr::query()->where('space_type_id', $space_type_id)
->delete();
}
public static function getTypeAttrData($space_type_id)
{
$columns = [
'space_attr_id as attributes_id',
'color_occupy as attr_color_occupy',
'color_vacant as attr_color_vacant',
'color_warning as attr_color_warning',
'is_warning as attr_is_warning',
'is_flicker as attr_is_flicker'
];
return ParkingSpaceTypeAttr::query()->where(
'space_type_id',
$space_type_id
)
->select($columns)->get()->toArray();
}
}

42
app/Services/ParkingSpaceTypeService.php

@ -71,11 +71,20 @@ class ParkingSpaceTypeService
$createData = $this->getSaveData($data);
$createData['created_at'] = get_datetime();
$attributes = $createData['attributes'];
unset($createData['attributes']);
$model = ParkingSpaceType::query()->create($createData);
$this->logService->logCreated($model, 'space_type.create');
// 车位类型添加属性
if ($attributes) {
(new ParkingSpaceTypeAttrService(
$this->logService
))->createBatchData($model->id, $attributes);
}
DB::commit();
return $model;
} catch (Exception $e) {
@ -99,14 +108,17 @@ class ParkingSpaceTypeService
'default_is_warning' => $data['default_is_warning'] ?? '0',
'default_is_flicker' => $data['default_is_flicker'] ?? '0'
];
if (isset($data['attributes_id'])) {
$saveData['attributes_id'] = $data['attributes_id'];
$saveData['attributes'] = [];
foreach ($data['attributes'] as $value) {
$saveData['attributes'][] = [
'space_attr_id' => $value['attributes_id'],
'color_occupy' => $value['attr_color_occupy'],
'color_vacant' => $value['attr_color_vacant'],
'color_warning' => $value['attr_color_warning'],
'is_warning' => $value['attr_is_warning'],
'is_flicker' => $value['attr_is_flicker']
];
}
$saveData['attr_color_occupy'] = $data['attr_color_occupy'] ?? '';
$saveData['attr_color_vacant'] = $data['attr_color_vacant'] ?? '';
$saveData['attr_color_warning'] = $data['attr_color_warning'] ?? '';
$saveData['attr_is_warning'] = $data['attr_is_warning'] ?? 0;
$saveData['attr_is_flicker'] = $data['attr_is_flicker'] ?? 0;
return $saveData;
}
@ -135,10 +147,21 @@ class ParkingSpaceTypeService
$saveData = $this->getSaveData($data);
$saveData['updated_at'] = get_datetime();
$attributes = $saveData['attributes'];
unset($saveData['attributes']);
$model->update($saveData);
$this->logService->logUpdated($model, $oldValues, 'space_type.update');
if ($attributes) {
$TypeAttrService = new ParkingSpaceTypeAttrService(
$this->logService
);
$TypeAttrService->deleteBatchData($id);
$TypeAttrService->createBatchData($id, $attributes);
}
DB::commit();
return $model;
} catch (Exception $e) {
@ -166,6 +189,11 @@ class ParkingSpaceTypeService
$this->logService->logDeleted($model, 'space_type.delete');
// 同步删除属性
(new ParkingSpaceTypeAttrService(
$this->logService
))->deleteBatchData($id);
$model->delete();
DB::commit();

35
database/migrations/2026_03_09_143728_create_parking_space_type_attr_table.php

@ -0,0 +1,35 @@
<?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_space_type_attr', function (Blueprint $table) {
$table->id();
$table->integer('space_type_id')->comment('车位类型');
$table->integer('space_attr_id')->comment('车位属性');
$table->string('color_occupy', 50)->nullable()->comment('指示灯颜色(占用)');
$table->string('color_vacant', 50)->nullable()->comment('指示灯颜色(空置)');
$table->string('color_warning', 50)->nullable()->comment('报警指示灯颜色');
$table->tinyInteger('is_warning')->default(0)->comment('是否报警 0否 1是');
$table->tinyInteger('is_flicker')->default(0)->comment('是否闪烁 0否 1是');
$table->timestamps();
$table->innoDb();
});
}
/**
* Reverse the migrations.
*/
public function down(): void
{
Schema::dropIfExists('parking_space_type_attr');
}
};

6
database/migrations/2026_03_09_143728_create_parking_space_type_table.php

@ -20,12 +20,6 @@ return new class extends Migration
$table->string('default_color_warning', 50)->nullable()->comment('默认报警指示灯颜色');
$table->tinyInteger('default_is_warning')->default(0)->comment('默认是否报警 0否 1是');
$table->tinyInteger('default_is_flicker')->default(0)->comment('默认是否闪烁 0否 1是');
$table->integer('attributes_id')->nullable()->comment('车位属性ID');
$table->string('attr_color_occupy', 50)->nullable()->comment('车位属性指示灯颜色(占用)');
$table->string('attr_color_vacant', 50)->nullable()->comment('车位属性指示灯颜色(空置)');
$table->string('attr_color_warning', 50)->nullable()->comment('车位属性报警指示灯颜色');
$table->tinyInteger('attr_is_warning')->nullable()->comment('车位属性是否报警 0否 1是');
$table->tinyInteger('attr_is_flicker')->default(0)->comment('车位属性是否闪烁 0否 1是');
$table->timestamps();
$table->softDeletes();
$table->innoDb();

4
resources/lang/en/log.php

@ -86,5 +86,9 @@ return [
'update' => 'Update Activity Calendar',
'delete' => 'Delete Activity Calendar',
'import' => 'Import Activity Calendar'
],
'space_type_attr' => [
'create' => 'Create parking space type attributes',
'delete' => 'Delete parking space type attribute'
]
];

4
resources/lang/zh-CN/log.php

@ -86,5 +86,9 @@ return [
'update' => '更新活动行事历',
'delete' => '删除活动行事历',
'import' => '导入活动行事历'
],
'space_type_attr' => [
'create' => '创建车位类型属性',
'delete' => '删除车位类型属性'
]
];

4
resources/lang/zh-TW/log.php

@ -86,5 +86,9 @@ return [
'update' => '更新活動行事曆',
'delete' => '删除活動行事曆',
'import' => '導入活動行事曆'
],
'space_type_attr' => [
'create' => '創建車位類型内容',
'delete' => '删除車位類型内容'
]
];

Loading…
Cancel
Save