diff --git a/app/api/controller/VehicleController.php b/app/api/controller/VehicleController.php index 9d9aad16..110f521b 100644 --- a/app/api/controller/VehicleController.php +++ b/app/api/controller/VehicleController.php @@ -126,27 +126,31 @@ class VehicleController extends BaseApiController } } - public function cancelRent() { + public function cancelRent(): Json + { //获取参数 - $params = $this->request->post(['car_id','status']); - if(empty($params['car_id']) || empty($params['status'])){ + $carIds = $this->request->post('car_ids'); + if(empty($carIds)){ return $this->fail('参数错误'); } - //更新 - $vehicleRent = VehicleRent::where('car_id',$params['car_id'])->find(); - if(empty($vehicleRent)){ - return $this->fail('数据错误'); - } - $result1 = VehicleRent::where('car_id',$params['car_id'])->update([ - 'status' => $params['status'], - ]); - $result2 = Vehicle::where('id',$params['car_id'])->update([ - 'status' => 3, - ]); - if($result1 && $result2){ + Db::startTrans(); + try { + //删除租赁信息 + VehicleRent::where('car_id','in',$carIds)->delete(); + //重置车辆状态 + $vehicles = Vehicle::field('id,type')->where('id','in',$carIds)->select(); + foreach ($vehicles as $v) { + if($v['type'] == 1){ + Vehicle::where('id',$v['id'])->delete(); + }else{ + Vehicle::where('id',$v['id'])->update(['status'=>0]); + } + } + Db::commit(); return $this->success('请求成功'); - }else{ - return $this->fail('失败'); + }catch (\Exception $e) { + Db::rollback(); + return $this->fail($e->getMessage()); } }