You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
206 lines
5.6 KiB
206 lines
5.6 KiB
<?php
|
|
|
|
namespace App\Services;
|
|
|
|
use App\Models\AdminFloor;
|
|
use App\Models\AdminFloorRegion;
|
|
use App\Models\Parking;
|
|
use Exception;
|
|
use Illuminate\Support\Facades\DB;
|
|
|
|
class AdminFloorService
|
|
{
|
|
private static array $buildingFloor = [1 => 'MSCP', 2 => 'GSCP'];
|
|
|
|
private static array $statusArr = ['disabled', 'enable'];
|
|
|
|
/**
|
|
* @return array|string[]
|
|
*/
|
|
public static function getStatus(): array
|
|
{
|
|
$statusArr = self::$statusArr;
|
|
foreach ($statusArr as $key => $value) {
|
|
$statusArr[$key] = __('admin.' . $value);
|
|
}
|
|
return $statusArr;
|
|
}
|
|
|
|
/**
|
|
* @var OperationLogService
|
|
*/
|
|
private OperationLogService $logService;
|
|
|
|
/**
|
|
* 构造函数
|
|
* @param OperationLogService $logService
|
|
*/
|
|
public function __construct(OperationLogService $logService)
|
|
{
|
|
$this->logService = $logService;
|
|
$this->logService->menuTitle = 'floor_plan';
|
|
}
|
|
|
|
/**
|
|
* 创建角色
|
|
* @param array $data
|
|
* @throws Exception
|
|
*/
|
|
public function createModel(array $data)
|
|
{
|
|
try {
|
|
DB::beginTransaction();
|
|
|
|
if (AdminFloor::query()->where('name', $data['name'])->exists()) {
|
|
throw new Exception(__('service.admin_floor.name_exists'));
|
|
}
|
|
|
|
$model = AdminFloor::query()->create([
|
|
'name' => $data['name'],
|
|
'image_url' => $data['image_url'],
|
|
'open_time' => $data['open_time'],
|
|
'close_time' => $data['close_time'],
|
|
'building_floor' => $data['building_floor'] ?? 1,
|
|
'created_at' => get_datetime()
|
|
]);
|
|
|
|
$this->logService->logCreated($model, 'admin_floor.create');
|
|
|
|
AdminTranslationService::saveTranslation(
|
|
$data['name'],
|
|
$data['en_name'] ?? '',
|
|
$data['tw_name'] ?? '',
|
|
$model->id,
|
|
4
|
|
);
|
|
|
|
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 = [
|
|
['name', '=', $data['name']],
|
|
['id', '<>', $id]
|
|
];
|
|
if (AdminFloor::query()->where($existsWhere)->exists()) {
|
|
throw new Exception(__('service.admin_role.name_exists'));
|
|
}
|
|
|
|
// 更新
|
|
$model = AdminFloor::query()->findOrFail($id);
|
|
$oldValues = $model->toArray();
|
|
|
|
$model->update([
|
|
'name' => $data['name'],
|
|
'image_url' => $data['image_url'],
|
|
'open_time' => $data['open_time'],
|
|
'close_time' => $data['close_time'],
|
|
'building_floor' => $data['building_floor'] ?? 1,
|
|
'updated_at' => get_datetime()
|
|
]);
|
|
|
|
$this->logService->logUpdated($model, $oldValues, 'admin_floor.update');
|
|
|
|
AdminTranslationService::saveTranslation(
|
|
$data['name'],
|
|
$data['en_name'] ?? '',
|
|
$data['tw_name'] ?? '',
|
|
$id,
|
|
4
|
|
);
|
|
|
|
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 = AdminFloor::query()->findOrFail($id);
|
|
|
|
$this->logService->logDeleted($model, 'admin_floor.delete');
|
|
|
|
$model->delete();
|
|
|
|
AdminTranslationService::syncDelete($id, 4);
|
|
|
|
DB::commit();
|
|
return true;
|
|
} catch (Exception $e) {
|
|
DB::rollBack();
|
|
throw $e;
|
|
}
|
|
}
|
|
|
|
// 获取楼层列表
|
|
public static function getSelectList($building_floor, $column = []): array
|
|
{
|
|
if (empty($column)) {
|
|
$column = [
|
|
'id as floor_id',
|
|
'name as floor_name',
|
|
'image_url as pic_url',
|
|
'building_floor'
|
|
];
|
|
}
|
|
$list = AdminFloor::query()->where('building_floor', $building_floor)->whereNull('deleted_at')->select($column)
|
|
->get()->toArray();
|
|
foreach ($list as &$item) {
|
|
if (isset($item['pic_url'])) {
|
|
$item['pic_url'] = get_image_url($item['pic_url']);
|
|
}
|
|
$item['floor_name'] = AdminTranslationService::getTranslationTypeName($item['floor_id'], 4, $item['floor_name']);
|
|
}
|
|
return $list;
|
|
}
|
|
|
|
// 获取单个楼层信息
|
|
public static function getFloorData($id): array
|
|
{
|
|
$data = AdminFloor::query()->where('id', $id)->get()->toArray();
|
|
foreach ($data as &$item) {
|
|
if (isset($item['image_url'])) {
|
|
$item['pic_url'] = get_image_url($item['image_url']);
|
|
$item['name'] = AdminTranslationService::getTranslationTypeName($item['id'], 4, $item['name']);
|
|
}
|
|
}
|
|
return $data;
|
|
}
|
|
|
|
// 获取停车场
|
|
public static function getBuildingFloor(): array
|
|
{
|
|
$parkingData = Parking::getData();
|
|
$data = [];
|
|
foreach ($parkingData as $item) {
|
|
$data[$item['id']] = $item['name'];
|
|
}
|
|
return $data;
|
|
}
|
|
}
|
|
|