request->post(['num','company_id','company_name','company_code','company_user','company_phone','company_email']); //验证参数 if(empty($params['num']) || empty($params['company_id']) || empty($params['company_name']) || empty($params['company_code']) || empty($params['company_user']) || empty($params['company_phone']) || empty($params['company_email'])){ return $this->fail('缺少必要参数'); } //获取平台公司数据 $platform = Platform::find(1); if(!$platform || $platform->isEmpty()){ return $this->fail('甲方公司不存在'); } //设置数据 $data = [ 'contract_no' => time(), 'company_a_name' => $platform['company_name'], 'company_a_code' => $platform['company_organization_code'], 'company_a_user' => $platform['company_user'], 'company_a_phone' => $platform['company_phone'], 'company_a_email' => $platform['company_email'], 'company_b_id' => $params['company_id'], 'company_b_name' => $params['company_name'], 'company_b_code' => $params['company_code'], 'company_b_user' => $params['company_user'], 'company_b_phone' => $params['company_phone'], 'company_b_email' => $params['company_email'], 'num' => $params['num'], 'cars_info' => null, 'status' => 0, 'create_time' => time(), 'update_time' => time(), ]; Db::startTrans(); try { $res1 = Contract::create($data); if($res1->id){ $data['contract_logistic_id'] = $res1->id; $data['type'] = 0; Db::commit(); return $this->success('合同发起成功,等待审核',$data); }else{ Db::rollback(); return $this->fail('合同发起失败,请稍后重试'); } } catch (\Exception $e) { // 回滚事务 Db::rollback(); return $this->fail($e->getMessage()); } } public function contractUpdate() { //获取参数 $params = $this->request->post(); if(!isset($params['id']) || empty($params['id'])) { return $this->fail('缺少必要参数'); } $contract = Contract::where('id',$params['id'])->find(); if(empty($contract)) { return $this->fail('未找到相应数据'); } if(isset($params['signing_timer']) && isset($params['status']) && $params['signing_timer'] == 2 && $params['status'] == 3){ //获取签约后的合同 $signContractFile = app(JunziqianController::class)->download_file($contract['contract_no']); $params['contract_url'] = $signContractFile ?? ''; //获取租赁车辆 $cars = json_decode($contract['cars_info'], true); $data = []; foreach($cars as $v){ $data[] = [ 'car_id' => $v['id'], 'contract_id' => $contract['id'], 'company_id' => $contract['company_b_id'], 'company_name' => $contract['company_b_name'], 'company_user' => $contract['company_b_user'], 'company_phone' => $contract['company_b_phone'], 'create_time' => time(), 'status' => 0, 'use_user_id' => 0, 'use_user_name' => '', 'use_user_phone' => '' ]; } try{ //更新合同状态 $update_result = $contract->where('id', $contract['id'])->save($params); //更新本地车辆状态 $vehicle_result = Vehicle::where('id','in',array_column($cars,'id'))->update(['status'=>2]); //添加车辆到租赁列表 $create_result = (new VehicleRent()) -> saveAll($data); if($update_result && $vehicle_result && $create_result){ Db::commit(); return $this->success('更新成功1'); }else{ Db::rollback(); return $this->fail('更新失败1'); } }catch (\Exception){ Db::rollback(); return $this->fail('更新失败1.1'); } } try{ $update_result = $contract->where('id', $contract['id'])->save($params); if($update_result){ Db::commit(); return $this->success('更新成功2'); }else{ Db::rollback(); return $this->fail('更新失败2'); } }catch (\Exception){ Db::rollback(); return $this->fail('更新失败2.1'); } } public function checkCarNum() { //获取参数 $num = $this->request->post('num'); if(empty($num)){ return $this->fail('物理系统车辆数量不足'); } //获取车辆 $data = Vehicle::where('status',0)->where('type',0)->limit($num)->select(); //判断 if($data->count() < $num){ return $this->fail('物理系统车辆数量不足'); } return $this->success('物理系统车辆数量充足'); } public function getAvailableVehicles() { $data = Vehicle::field('id,license')->where('status',0)->where('type',0)->select(); return $this->success('请求成功',$data->toArray()); } }