data_center/app/api/controller/NotifyController.php

516 lines
22 KiB
PHP
Raw Normal View History

<?php
namespace app\api\controller;
use app\common\controller\JunziqianController;
use app\common\model\company\Company;
use app\common\model\contract\Contract;
use app\common\model\contract\VehicleContract;
use app\common\model\vehicle\VehicleBuyRecord;
use app\common\model\vehicle\VehicleRent;
use think\response\Json;
class NotifyController extends BaseApiController
{
public array $notNeedLogin = ['townCarRent','systemCarRent', 'selfCarRent', 'cancelRent', 'buyCar'];
//镇街车辆租赁回调
public function townCarRent(): Json
{
$id = Request()->get('id');
if(empty($id)){
return json(['success' => false, 'msg' => '缺少参数']);
}
//获取合同数据
$contract = VehicleContract::where('id', $id)->find();
if (empty($contract)) {
return json(['success' => false, 'msg' => '获取数据失败']);
}
if($contract['status'] != 2){
return json(['success' => false, 'msg' => '合同状态错误']);
}
if ($contract['signing_timer'] == 0) {
//更新本地合同状态
$updateLocalRes = VehicleContract::where('id',$contract['id'])->update(['signing_timer'=>1]);
//更新远程
$updateSverRes =curl_post(env('project.logistic_domain').'/api/contractUpdate',[],[
'id' => $contract['contract_logistic_id'],
'signing_timer' => 1,
]);
if(!$updateLocalRes || $updateSverRes['code']==0){
return json(['success' => false, 'msg' => '更新失败']);
}
return json(['success' => true, 'msg' => '成功']);
}else if($contract['signing_timer'] == 1){
//获取签约后的合同
$signContractFile = app(JunziqianController::class)->downloadVehicleContractFile($contract['contract_no']);
$signContractFile = $signContractFile ?? '';
//获取签约后的证据
$signContractEvidenceToPartyA = app(JunziqianController::class)->downloadVehicleContractEvidence($contract['contract_no'],$contract['company_a_name'],$contract['company_a_code']);
$signContractEvidenceToPartyA = $signContractEvidenceToPartyA ?? '';
$signContractEvidenceToPartyB = app(JunziqianController::class)->downloadVehicleContractEvidence($contract['contract_no'],$contract['company_b_name'],$contract['company_b_code']);
$signContractEvidenceToPartyB = $signContractEvidenceToPartyB ?? '';
$contractEvidence = json_encode([
'party_a' => $signContractEvidenceToPartyA,
'party_b' => $signContractEvidenceToPartyB
]);
//更新本地合同状态
$updateLocalRes = VehicleContract::where('id',$contract['id'])->update(['signing_timer'=>2,'status' => 3,'contract_url'=>$signContractFile,'contract_evidence'=>$contractEvidence]);
//将车辆加入到本地租赁列表
$cars = json_decode($contract['cars_info'], true);
$data = [];
foreach ($cars as $k => $v) {
$hasCar = VehicleRent::where('car_id',$v['id'])->findOrEmpty();
if($hasCar->isEmpty()){
$data[$k]['contract_id'] = $contract['id'];
$data[$k]['company_id'] = $contract['company_b_id'];
$data[$k]['car_id'] = $v['id'];
$data[$k]['car_license'] = $v['license'];
$data[$k]['type'] = 0;
$data[$k]['status'] = 0;
$data[$k]['rent_contract_id'] = 0;
$data[$k]['rent_company_id'] = 0;
$data[$k]['rent_time'] = 0;
$data[$k]['create_time'] = strtotime($contract['create_time']);
}
}
$vehicleRent = new VehicleRent();
$vehicleRent->saveAll($data);
//更新远程
$updateSverRes = curl_post(env('project.logistic_domain').'/api/contractUpdate',[],[
'id' => $contract['contract_logistic_id'],
'signing_timer' => 2,
'status' => 3,
'contract_url'=>$signContractFile,
'contract_evidence'=>$contractEvidence
]);
if(!$updateLocalRes || $updateSverRes['code']==0){
return json(['success' => false, 'msg' => '更新失败']);
}else{
return json(['success' => true, 'msg' => '更新成功']);
}
}
return json(['success' => true, 'msg' => '更新成功@']);
}
//系统车辆租赁回来
public function systemCarRent(): Json
{
//获取参数
$id = $this->request->get('id');
if (empty($id)) {
return json(['success' => false, 'msg' => '失败1.1']);
}
//获取合同数据
$contract = VehicleContract::where('id', $id)->find();
if (empty($contract)) {
return json(['success' => false, 'msg' => '失败1.2']);
}
if ($contract['type'] != 0) {
return json(['success' => false, 'msg' => '失败1.3']);
}
if ($contract['status'] != 2) {
return json(['success' => false, 'msg' => '失败1.4']);
}
//判断签约方
if ($contract['signing_timer'] == 0) {
//更新合同的签约次数
$res = VehicleContract::where('id', $id)->update(['signing_timer' => 1]);
if (!$res) {
return json(['success' => false, 'msg' => '失败1.5']);
}
return json(['success' => true, 'msg' => '成功']);
} else if ($contract['signing_timer'] == 1) {
//获取签约后的合同
$signContractFile = app(JunziqianController::class)->downloadVehicleContractFile($contract['contract_no']);
$signContractFile = $signContractFile ?? '';
//获取签约后的证据
$signContractEvidenceToPartyA = app(JunziqianController::class)->downloadVehicleContractEvidence($contract['contract_no'],$contract['company_a_name'],$contract['company_a_code']);
$signContractEvidenceToPartyA = $signContractEvidenceToPartyA ?? '';
$signContractEvidenceToPartyB = app(JunziqianController::class)->downloadVehicleContractEvidence($contract['contract_no'],$contract['company_b_name'],$contract['company_b_code']);
$signContractEvidenceToPartyB = $signContractEvidenceToPartyB ?? '';
$contractEvidence = json_encode([
'party_a' => $signContractEvidenceToPartyA,
'party_b' => $signContractEvidenceToPartyB
]);
//更改合同状态
VehicleContract::where('id', $id)->update(['signing_timer' => 2, 'status' => 3,'contract_url'=>$signContractFile,'contract_evidence'=>$contractEvidence]);
//添加车辆到租赁列表
$vehicle = json_decode($contract['cars_info'], true);
VehicleRent::where('car_id', $vehicle[0]['id'])->update([
'status' => 2,
'rent_contract_id' => $contract['id'],
'rent_company_id' => $contract['company_b_id'],
'rent_time' => time(),
]);
$party_b = Company::where('id', $contract['company_b_id'])->find();
//通知物流系统跟新
curl_post(env('project.logistic_domain').'/api/updateVehicleRent', [], [
'car_id' => $vehicle[0]['id'],
'use_user_id' => $party_b['user_id'],
'use_user_name' => $party_b['master_name'],
'use_user_phone' => $party_b['master_phone']
]);
return json(['success' => true, 'msg' => '成功']);
} else {
return json(['success' => true, 'msg' => '成功']);
}
}
//自有车辆租赁回答
public function selfCarRent(): Json
{
//获取参数
$id = $this->request->get('id');
if (empty($id)) {
return json(['success' => false, 'msg' => '失败2.1']);
}
//获取合同数据
$contract = VehicleContract::where('id', $id)->find();
if (empty($contract)) {
return json(['success' => false, 'msg' => '失败2.2']);
}
if ($contract['type'] != 1) {
return json(['success' => false, 'msg' => '失败2.3']);
}
if ($contract['status'] != 2) {
return json(['success' => false, 'msg' => '失败2.4']);
}
//判断签约方
if ($contract['signing_timer'] == 0) {
$res = VehicleContract::where('id', $id)->update(['signing_timer' => 1]);
if (!$res) {
return json(['success' => false, 'msg' => '失败2.5']);
}
return json(['success' => true, 'msg' => '成功']);
} else if ($contract['signing_timer'] == 1) {
//添加车辆到物流系统
$vehicle = json_decode($contract['cars_info'], true);
$curl_res = curl_post(env('project.logistic_domain').'/api/addSelfCar', [], [
'license' => $vehicle[0]['license'],
'pic' => $vehicle[0]['pic'],
'company_id' => $contract['company_a_id'],
'company_name' => $contract['company_a_name'],
'company_user' => $contract['company_a_user'],
'company_phone' => $contract['company_a_phone'],
]);
if (!$curl_res || $curl_res['code'] == 0) {
return json(['success' => false, 'msg' => '失败2.6']);
}
$cars_info = json_encode([['id' => $curl_res['data']['car_id'], 'license' => $vehicle[0]['license']]]);
//获取签约后的合同
$signContractFile = app(JunziqianController::class)->downloadVehicleContractFile($contract['contract_no']);
$signContractFile = $signContractFile ?? '';
//获取签约后的证据
$signContractEvidenceToPartyA = app(JunziqianController::class)->downloadVehicleContractEvidence($contract['contract_no'],$contract['company_a_name'],$contract['company_a_code']);
$signContractEvidenceToPartyA = $signContractEvidenceToPartyA ?? '';
$signContractEvidenceToPartyB = app(JunziqianController::class)->downloadVehicleContractEvidence($contract['contract_no'],$contract['company_b_name'],$contract['company_b_code']);
$signContractEvidenceToPartyB = $signContractEvidenceToPartyB ?? '';
$contractEvidence = json_encode([
'party_a' => $signContractEvidenceToPartyA,
'party_b' => $signContractEvidenceToPartyB
]);
VehicleContract::where('id', $id)->update(['cars_info'=>$cars_info,'update_time'=>time(),'signing_timer'=>2,'status'=>3,'contract_url'=>$signContractFile,'contract_evidence'=>$contractEvidence]);
VehicleRent::create([
'car_id' => $curl_res['data']['car_id'],
'contract_id' => $contract['id'],
'company_id' => $contract['company_a_id'],
'car_license' => $vehicle[0]['license'],
'status' => 2,
'rent_time' => time(),
'rent_contract_id' => $contract['id'],
'rent_company_id' => $contract['company_b_id'],
'create_time' => time(),
'type' => 1
]);
$car_id = $curl_res['data']['car_id'];
$party_b = Company::where('id', $contract['company_b_id'])->find();
//通知物流系统跟新
curl_post(env('project.logistic_domain').'/api/updateVehicleRent', [], [
'car_id' => $car_id,
'use_user_id' => $party_b['user_id'],
'use_user_name' => $party_b['master_name'],
'use_user_phone' => $party_b['master_phone']
]);
return json(['success' => true, 'msg' => '成功']);
} else {
return json(['success' => true, 'msg' => '成功']);
}
}
//解除合同回调
public function cancelRent()
{
//获取参数
$id = $this->request->get('id');
if (empty($id)) {
return json(['success' => false, 'msg' => '失败3.1']);
}
//获取合同数据
$contract = VehicleContract::where('id', $id)->find();
if (empty($contract)) {
return json(['success' => false, 'msg' => '失败3.2']);
}
if ($contract['type'] != 2) {
return json(['success' => false, 'msg' => '失败3.3']);
}
if ($contract['status'] != 2) {
return json(['success' => false, 'msg' => '失败3.4']);
}
if ($contract['signing_timer'] == 0) {
$res = VehicleContract::where('id', $id)->update(['signing_timer' => 1]);
if (!$res) {
return json(['success' => false, 'msg' => '失败3.5']);
}
return json(['success' => true, 'msg' => '成功']);
} else if ($contract['signing_timer'] == 1) {
//获取签约后的合同
$signContractFile = app(JunziqianController::class)->downloadVehicleContractFile($contract['contract_no']);
$signContractFile = $signContractFile ?? '';
//获取签约后的证据
$signContractEvidenceToPartyA = app(JunziqianController::class)->downloadVehicleContractEvidence($contract['contract_no'],$contract['company_a_name'],$contract['company_a_code']);
$signContractEvidenceToPartyA = $signContractEvidenceToPartyA ?? '';
$signContractEvidenceToPartyB = app(JunziqianController::class)->downloadVehicleContractEvidence($contract['contract_no'],$contract['company_b_name'],$contract['company_b_code']);
$signContractEvidenceToPartyB = $signContractEvidenceToPartyB ?? '';
$contractEvidence = json_encode([
'party_a' => $signContractEvidenceToPartyA,
'party_b' => $signContractEvidenceToPartyB
]);
$cars = json_decode($contract['cars_info'],true);
$cars_ids = array_column($cars,'id');
//更改合同状态
VehicleContract::where('id', $id)->update(['signing_timer'=>2,'status'=>3,'contract_url'=>$signContractFile,'contract_evidence'=>$contractEvidence]);
if(!empty($contract['contract_logistic_id'])){
curl_post(env('project.logistic_domain').'/api/contractUpdate',[],[
'id' => $contract['contract_logistic_id'],
'signing_timer' => 2,
'status' => 3,
'contract_url'=>$signContractFile,
'contract_evidence'=>$contractEvidence,
]);
}
//判断合同是否存在购买记录表中
$vehicleBuyRecord = VehicleBuyRecord::where('contract_id',$contract['id'])->findOrEmpty();
if(!$vehicleBuyRecord->isEmpty()){
//小组公司与镇街公司解约
if($vehicleBuyRecord['status'] == 1){
//获取租赁车辆信息
$rentCarsInfo = VehicleRent::where('car_id',$cars_ids[0])->findOrEmpty();
if($rentCarsInfo['type'] == 0){
//修改租赁车俩状态
VehicleRent::where('id',$rentCarsInfo['id'])->update(['status'=>0,'rent_company_id'=>0,'rent_contract_id'=>0,'rent_time'=>0]);
}
if($rentCarsInfo['type'] == 1){
//修改租赁车俩状态
VehicleRent::where('id',$rentCarsInfo['id'])->delete();
}
//删除原合同
VehicleContract::where('id',$rentCarsInfo['rent_contract_id'])->delete();
//修改物流系统车辆租赁信息
curl_post(env('project.logistic_domain').'/api/Vehicle/delRentUseInfo', [], [
'car_id' => $cars_ids[0]
]);
//发送购买合同给物流系统
$curl_result = curl_post(env('project.logistic_domain').'/api/signContract',[],[
'num' => $vehicleBuyRecord['num'],
'company_id' => $vehicleBuyRecord['company_id'],
'company_name' => $vehicleBuyRecord['company_name'],
'company_code' => $vehicleBuyRecord['company_code'],
'company_user' => $vehicleBuyRecord['company_user'],
'company_phone' => $vehicleBuyRecord['company_phone'],
'company_email' => $vehicleBuyRecord['company_email'],
'cars_info' => $vehicleBuyRecord['cars_info'],
'type' => 3
]);
if(empty($curl_result)){
return $this->fail('null return from logistic');
}
if($curl_result['code'] == 0){
return $this->fail($curl_result['msg'].' from logistic');
}
//生成本地合同
VehicleContract::create($curl_result['data']);
VehicleBuyRecord::where('id',$vehicleBuyRecord['id'])->update(['status'=>4]);
return json(['success' => true, 'msg' => '成功']);
}
//小组公司与镇街公司解约,然后镇街公司与平台公司解约
if($vehicleBuyRecord['status'] == 2){
//获取租赁车辆信息
$rentCarsInfo = VehicleRent::where('car_id',$cars_ids[0])->findOrEmpty();
if($rentCarsInfo['type'] == 0){
//修改租赁车俩状态
VehicleRent::where('id',$rentCarsInfo['id'])->update(['status'=>0,'rent_company_id'=>0,'rent_contract_id'=>0,'rent_time'=>0]);
}
if($rentCarsInfo['type'] == 1){
//修改租赁车俩状态
VehicleRent::where('id',$rentCarsInfo['id'])->delete();
}
//删除原合同
VehicleContract::where('id',$rentCarsInfo['rent_contract_id'])->delete();
//修改物流系统车辆租赁信息
curl_post(env('project.logistic_domain').'/api/Vehicle/delRentUseInfo', [], [
'car_id' => $cars_ids[0]
]);
//获取镇街公司信息
$zjCompany = Company::where('id',$rentCarsInfo['company_id'])->findOrEmpty();
//判断购买车辆中是否包含镇街公司租赁的车辆
$car_ids = array_column(json_decode($vehicleBuyRecord['cars_info'],true),'id');
$zjRentCars = VehicleRent::field('car_id as id,car_license as license')->where('company_id',$zjCompany['id'])->where('car_id','in',$car_ids)->where('status',0)->where('type',0)->select();
//发送镇公司与平台公司的解约合同
$curl_result = curl_post(env('project.logistic_domain').'/api/signContract',[],[
'num' => count($zjRentCars),
'company_id' => $zjCompany['id'],
'company_name' => $zjCompany['company_name'],
'company_code' => $zjCompany['organization_code'],
'company_user' => $zjCompany['master_name'],
'company_phone' => $zjCompany['master_phone'],
'company_email' => $zjCompany['master_email'],
'cars_info' => json_encode($zjRentCars),
'type' => 2
]);
if(empty($curl_result)){
return $this->fail('null return from logistic');
}
if($curl_result['code'] == 0){
return $this->fail($curl_result['msg'].' from logistic');
}
//生成本地合同
$res = VehicleContract::create($curl_result['data']);
VehicleBuyRecord::where('id',$vehicleBuyRecord['id'])->update(['status'=>3,'contract_id'=>$res->id]);
return json(['success' => true, 'msg' => '成功']);
}
//镇街公司与平台公司解约
if($vehicleBuyRecord['status'] == 3){
//删除本地租赁信息
VehicleRent::where('car_id','in',$cars_ids)->delete();
//删除物流系统租赁信息
curl_post(env('project.logistic_domain').'/api/cancelRent', [], [
'car_ids' => implode(',',$cars_ids)
]);
//发送购买合同给物流系统
$curl_result = curl_post(env('project.logistic_domain').'/api/signContract',[],[
'num' => $vehicleBuyRecord['num'],
'company_id' => $vehicleBuyRecord['company_id'],
'company_name' => $vehicleBuyRecord['company_name'],
'company_code' => $vehicleBuyRecord['company_code'],
'company_user' => $vehicleBuyRecord['company_user'],
'company_phone' => $vehicleBuyRecord['company_phone'],
'company_email' => $vehicleBuyRecord['company_email'],
'cars_info' => $vehicleBuyRecord['cars_info'],
'type' => 3
]);
if(empty($curl_result)){
return $this->fail('null return from logistic');
}
if($curl_result['code'] == 0){
return $this->fail($curl_result['msg'].' from logistic');
}
//生成本地合同
VehicleContract::create($curl_result['data']);
VehicleBuyRecord::where('id',$vehicleBuyRecord['id'])->update(['status'=>4]);
return json(['success' => true, 'msg' => '成功']);
}
}else{
//更改租赁列表车辆状态
$vehicle = json_decode($contract['cars_info'], true);
//获取租赁车辆信息
$vehicleRentInfo = VehicleRent::where('car_id', $vehicle[0]['id'])->find();
//更新原始合同类型
VehicleContract::where('id', $vehicleRentInfo['rent_contract_id'])->delete();
VehicleRent::where('car_id', $vehicle[0]['id'])->delete();
//通知物流系统跟新
curl_post(env('project.logistic_domain').'/api/Vehicle/delRentUseInfo', [], [
'car_id' => $vehicle[0]['id']
]);
return json(['success' => true, 'msg' => '成功']);
}
} else {
return json(['success' => true, 'msg' => '成功']);
}
}
//购买合同回调
public function buyCar(): Json
{
//获取参数
$id = $this->request->get('id');
if (empty($id)) {
return json(['success' => false, 'msg' => '失败4.1']);
}
//获取合同数据
$contract = VehicleContract::where('id', $id)->find();
if (empty($contract)) {
return json(['success' => false, 'msg' => '失败4.2']);
}
if ($contract['type'] != 3) {
return json(['success' => false, 'msg' => '失败4.3']);
}
if ($contract['status'] != 2) {
return json(['success' => false, 'msg' => '失败4.4']);
}
if ($contract['signing_timer'] == 0) {
$res = VehicleContract::where('id', $id)->update(['signing_timer' => 1]);
if (!$res) {
return json(['success' => false, 'msg' => '失败4.5']);
}
return json(['success' => true, 'msg' => '成功']);
}else if ($contract['signing_timer'] == 1) {
//获取签约后的合同
$signContractFile = app(JunziqianController::class)->downloadVehicleContractFile($contract['contract_no']);
$signContractFile = $signContractFile ?? '';
//获取签约后的证据
$signContractEvidenceToPartyA = app(JunziqianController::class)->downloadVehicleContractEvidence($contract['contract_no'],$contract['company_a_name'],$contract['company_a_code']);
$signContractEvidenceToPartyA = $signContractEvidenceToPartyA ?? '';
$signContractEvidenceToPartyB = app(JunziqianController::class)->downloadVehicleContractEvidence($contract['contract_no'],$contract['company_b_name'],$contract['company_b_code']);
$signContractEvidenceToPartyB = $signContractEvidenceToPartyB ?? '';
$contractEvidence = json_encode([
'party_a' => $signContractEvidenceToPartyA,
'party_b' => $signContractEvidenceToPartyB
]);
//更新本地合同状态
$updateLocalRes = VehicleContract::where('id',$contract['id'])->update(['signing_timer'=>2,'status' => 3,'contract_url'=>$signContractFile,'contract_evidence'=>$contractEvidence]);
//判断是否有监管车辆
$jgCars = VehicleRent::where('rent_company_id',$contract['company_b_id'])->where('status',2)->where('type',2)->findOrEmpty();
if($jgCars->isEmpty()){
//获取镇街公司信息
$zjCompany = Contract::field('party_a')->where('party_b',$contract['company_b_id'])->where('signing_timer',2)->findOrEmpty();
//将车辆加入到本地租赁列表
$cars = json_decode($contract['cars_info'], true);
//写入数据
$data = [
'car_id' => $cars[0]['id'],
'car_license' => $cars[0]['license'],
'type' => 2,
'status' => 2,
'company_id' => $zjCompany['party_a'],
'rent_time' => time(),
'rent_company_id' => $contract['company_b_id'],
'contract_id' => $contract['id'],
'create_time' => time()
];
$vehicleRent = new VehicleRent();
$vehicleRent->save($data);
}
//获取签约公司信息
$compay = Company::where('id',$contract['company_b_id'])->findOrEmpty();
//更新远程
$updateSverRes = curl_post(env('project.logistic_domain').'/api/contractUpdate',[],[
'id' => $contract['contract_logistic_id'],
'signing_timer' => 2,
'status' => 3,
'contract_url'=>$signContractFile,
'contract_evidence'=>$contractEvidence,
'use_user_id' =>$compay['user_id'],
'use_user_name' =>$compay['master_name'],
'use_user_phone' =>$compay['master_phone'],
]);
if(!$updateLocalRes || $updateSverRes['code']==0){
return json(['success' => false, 'msg' => '更新失败']);
}else{
return json(['success' => true, 'msg' => '更新成功']);
}
}else{
return json(['success' => true, 'msg' => '成功']);
}
}
}