This commit is contained in:
unknown 2023-10-11 17:41:19 +08:00
parent 60e42afaea
commit a9921963e0

View File

@ -69,8 +69,8 @@ class ContractController extends BaseApiController
if(!isset($params['id']) || empty($params['id'])) {
return $this->fail('缺少必要参数');
}
$contract = Contract::where('id',$params['id'])->find();
if(empty($contract)) {
$contract = Contract::where('id',$params['id'])->findOrEmpty();
if($contract->isEmpty()) {
return $this->fail('未找到相应数据');
}
if(isset($params['signing_timer']) && isset($params['status']) && $params['signing_timer'] == 2 && $params['status'] == 3){
@ -80,9 +80,10 @@ class ContractController extends BaseApiController
//获取租赁车辆
$cars = json_decode($contract['cars_info'], true);
$data = [];
foreach($cars as $v){
$hasCar = VehicleRent::where('car_id',$v['id'])->findOrEmpty();
if($hasCar->isEmpty()){
//更新合同状态
$contract->where('id', $contract['id'])->save($params);
if($contract['type'] == 0){
foreach($cars as $v){
$data[] = [
'car_id' => $v['id'],
'contract_id' => $contract['id'],
@ -97,46 +98,38 @@ class ContractController extends BaseApiController
'use_user_phone' => ''
];
}
}
Db::startTrans();
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');
$status = 2;
}else if($contract['type'] == 1) {
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' => 2,
'use_user_id' => 0,
'use_user_name' => $contract['company_b_user'],
'use_user_phone' => $contract['company_b_phone']
];
}
}catch (\Exception){
Db::rollback();
return $this->fail('更新失败1.1');
$status = 4;
}
//更新本地车辆状态
Vehicle::where('id','in',array_column($cars,'id'))->update(['status'=>$status]);
//添加车辆到租赁列表
(new VehicleRent()) -> saveAll($data);
return $this->success('更新成功1');
}
Db::startTrans();
try{
$update_result = $contract->where('id', $contract['id'])->save($params);
if(isset($params['cars_info']) && !empty($params['cars_info'])){
$cars = json_decode($params['cars_info'], true);
//更新本地车辆状态
Vehicle::where('id','in',array_column($cars,'id'))->update(['status'=>1]);
}
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');
$contract->where('id', $contract['id'])->save($params);
if(isset($params['cars_info']) && !empty($params['cars_info'])){
$cars = json_decode($params['cars_info'], true);
//更新本地车辆状态
Vehicle::where('id','in',array_column($cars,'id'))->update(['status'=>1]);
}
return $this->success('更新成功2');
}
public function checkCarNum() {