diff --git a/app/adminapi/lists/vehicle/VehicleLists.php b/app/adminapi/lists/vehicle/VehicleLists.php index 07f36340..0f6f840b 100644 --- a/app/adminapi/lists/vehicle/VehicleLists.php +++ b/app/adminapi/lists/vehicle/VehicleLists.php @@ -37,7 +37,8 @@ class VehicleLists extends BaseAdminDataLists implements ListsSearchInterface public function setSearch(): array { return [ - '=' => ['license', 'gps_imei', 'gps_carid', 'status'], + '%like%' => ['gps_imei'], + '=' => ['license','status'], ]; } @@ -53,11 +54,14 @@ class VehicleLists extends BaseAdminDataLists implements ListsSearchInterface */ public function lists(): array { - return Vehicle::where($this->searchWhere) + return Vehicle::where($this->searchWhere)->where('is_del',0) ->field(['id', 'license', 'gps_imei', 'gps_carid', 'status']) ->limit($this->limitOffset, $this->limitLength) ->order(['id' => 'desc']) ->select() + ->each(function($item){ + $item['status_name'] = $item->status_name; + }) ->toArray(); } diff --git a/app/adminapi/logic/vehicle/VehicleLogic.php b/app/adminapi/logic/vehicle/VehicleLogic.php index 4edc4d04..eae834c4 100644 --- a/app/adminapi/logic/vehicle/VehicleLogic.php +++ b/app/adminapi/logic/vehicle/VehicleLogic.php @@ -41,12 +41,16 @@ class VehicleLogic extends BaseLogic try { //获取gps_car_id $car = (new GpsLogic()) -> info($params['gps_imei']); - if($car['code'] == 0) self::setError('添加失败'); + if($car['code'] == 0){ + self::setError('添加失败'); + return false; + } Vehicle::create([ 'license' => $params['license'], 'gps_imei' => $params['gps_imei'], 'gps_carid' => $car['data']['carId'], - 'status' => $params['status'] + 'status' => $params['status'], + 'is_del' => 0 ]); Db::commit(); @@ -70,13 +74,23 @@ class VehicleLogic extends BaseLogic { Db::startTrans(); try { + //获取数据 + $data = Vehicle::where('id',$params['id'])->find(); + if($data['gps_imei'] != $params['gps_imei']){ + //获取gps_car_id + $car = (new GpsLogic()) -> info($params['gps_imei']); + if($car['code'] == 0){ + self::setError('添加失败'); + return false; + } + $data['gps_carid'] = $car['data']['carId']; + } Vehicle::where('id', $params['id'])->update([ 'license' => $params['license'], 'gps_imei' => $params['gps_imei'], - 'gps_carid' => $params['gps_carid'], + 'gps_carid' => $data['gps_carid'], 'status' => $params['status'] ]); - Db::commit(); return true; } catch (\Exception $e) { @@ -96,7 +110,17 @@ class VehicleLogic extends BaseLogic */ public static function delete(array $params): bool { - return Vehicle::destroy($params['id']); + try { + $data = Vehicle::where('id', $params['id'])->find(); + if($data['is_del'] !== 0){ + return false; + } + Vehicle::where('id', $params['id'])->update(['is_del' => 1]); + return true; + }catch (\Exception $e){ + self::setError($e->getMessage()); + return false; + } } @@ -109,6 +133,8 @@ class VehicleLogic extends BaseLogic */ public static function detail($params): array { - return Vehicle::findOrEmpty($params['id'])->toArray(); + $data = Vehicle::findOrEmpty($params['id']); + $data['status_name'] = $data->status_name; + return $data->toArray(); } } \ No newline at end of file diff --git a/app/common/model/vehicle/Vehicle.php b/app/common/model/vehicle/Vehicle.php index e89ae14d..4f73d96b 100644 --- a/app/common/model/vehicle/Vehicle.php +++ b/app/common/model/vehicle/Vehicle.php @@ -12,4 +12,10 @@ class Vehicle extends BaseModel { return date('Y-m-d H:i:s',$value); } + + public function getStatusNameAttr($value,$data): string + { + $status = [0=>'未出租',1=>'一级出租',2=>'二级出租']; + return $status[$data['status']]; + } } \ No newline at end of file