WokerTask/app/api/controller/VehicleController.php

870 lines
40 KiB
PHP

<?php
namespace app\api\controller;
use app\common\enum\notice\NoticeEnum;
use app\common\model\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\facade\Db;
class VehicleController extends BaseApiController
{
//镇街公司想物流平台申请租赁车辆
public function setContractByTownCompany()
{
//获取参数 测试1111
$params = $this->request->post(['num']);
//验证参数
if(empty($params['num'])){
return $this->fail('缺少必要参数');
}
//获取该公司已签约的小组服务公司数量
$villageCompany = Contract::field('id')->where('party_a',$this->userInfo['company_id'])->where('signing_timer',2)->count();
//已经租赁车辆
$rentCar = VehicleRent::field('id')->where('company_id',$this->userInfo['company_id'])->whereNotIn('type','1,2')->count();
//申请中的车辆
$applyCar = VehicleContract::field('num')->where('company_b_id',$this->userInfo['company_id'])->where('status','in','-1,0,1,2')->sum('num');
//自有车辆数量
$selfCar = VehicleContract::field('id')->where('company_a_id',$this->userInfo['company_id'])->where('type',1)->where('status','in','-1,0,1,2,3')->sum('num');
//购买车辆数量
//获取下面的小组服务公司
$villageCompanys = Contract::field('party_b')->where('party_a',$this->userInfo['company_id'])->where('signing_timer',2)->select()->toArray();
$buyCar = 0;
foreach ($villageCompanys as $v) {
$aa = VehicleContract::field('id')->where('company_b_id',$v['party_b'])->where('type',3)->where('status','in','-1,0,1,2,3')->findOrEmpty();
if(!$aa->isEmpty()){
$buyCar += 1;
}
}
//可在租车辆
$doubleRentCar = max($villageCompany - $rentCar - $applyCar - $selfCar - $buyCar,0);
if($params['num'] > $doubleRentCar ){
return $this->fail('数量超过可再租车辆数');
}
//判断物流车辆
$checkNum = curl_post(env('project.logistic_domain').'/api/checkCarNum',[],['num'=>$params['num']]);
if($checkNum == null){
return $this->fail('检查车辆数量错误');
}
if($checkNum['code'] == 0){
return $this->fail($checkNum['msg']);
}
//查找乙方公司信息
$party_b = Company::field('id,company_name,organization_code,master_name,master_phone,master_email,company_type')->where('id',$this->userInfo['company_id'])->find();
if(empty($party_b)) {
return $this->fail('数据不存在');
}
//判断是否是镇街公司
if($party_b['company_type'] != 41){
return ['code'=>0,'msg'=>'非镇街公司不能签约'];
}
//发送生成合同给物流信息
$curl_result = curl_post(env('project.logistic_domain').'/api/signContract',[],[
'num' => $params['num'],
'company_id' => $party_b['id'],
'company_name' => $party_b['company_name'],
'company_code' => $party_b['organization_code'],
'company_user' => $party_b['master_name'],
'company_phone' => $party_b['master_phone'],
'company_email' => $party_b['master_email'],
]);
if(empty($curl_result)){
return $this->fail('null return from logistic');
}
if($curl_result['code'] == 0){
return $this->fail($curl_result['msg'].' from logistic');
}
Db::startTrans();
try {
$create_result = VehicleContract::create($curl_result['data']);
if($create_result->id){
Db::commit();
return $this->success('合同发起成功,等待审核 from task');
}else{
Db::rollback();
return $this->success('合同发起失败,请稍后重试 from task');
}
} catch (\Exception $e) {
Db::rollback();
return $this->fail($e->getMessage().' from task');
}
}
//镇街公司生成小组服务公司租赁合同
public function setContractByVillageCompany(){
//获取参数
$params = $this->request->post(['id','car_id']);
if(empty($params['id'])){
return $this->fail('缺少必要参数');
}
//获取数据
$vehicleContract = VehicleContract::where('id',$params['id'])->find();
if(empty($vehicleContract)){
return $this->fail('未找到数据');
}
if($this->userInfo['company_id'] != $vehicleContract['company_a_id']){
return $this->fail('数据错误');
}
if($vehicleContract['status'] != -1) {
return $this->fail('数据状态错误');
}
if($vehicleContract['type'] == 0){
if(empty($params['car_id'])){
return $this->fail('缺少参数car_id');
}
}
if($vehicleContract['type'] == 0){
$car = VehicleRent::field('car_id as id,car_license as license')->where('car_id',$params['car_id'])->find();
if(empty($car)){
return $this->fail('车辆数据有误');
}
$json = json_encode([$car->toArray()]);
//更改车辆状态
VehicleRent::where('car_id',$params['car_id'])->update(['status'=>1]);
//更改状态
$result = VehicleContract::where('id',$params['id'])->update(['status'=>0,'cars_info'=>$json,'update_time'=>time()]);
}else{
//更改状态
$result = VehicleContract::where('id',$params['id'])->update(['status'=>0,'update_time'=>time()]);
}
//返回数据
if($result){
return $this->success('合同生成成功,等待风控部审核');
}else{
return $this->fail('合同生成失败,请稍后重试');
}
}
//镇街公司发起合同
public function initiatingRentCarContract() {
//获取参数
$params = $this->request->post(['id']);
if(empty($params['id'])){
return $this->fail('缺少必要参数');
}
//获取数据
$contract = VehicleContract::where('id',$params['id'])->find();
if(empty($contract)){
return $this->fail('数据不存在');
}
if($contract['status'] != 1){
return $this->fail('合同状态错误');
}
$signData = [
'name' => $contract['company_a_name'] . '的合同',
'signatories' => [
['fullName' => $contract['company_a_name'], 'identityType' => 12, 'identityCard' => $contract['company_a_code'], 'email' => $contract['company_a_email'], 'noNeedVerify' => 1, 'signLevel' => 1],
['fullName' => $contract['company_b_name'], 'identityType' => 12, 'identityCard' => $contract['company_b_code'], 'email' => $contract['company_b_email'], 'noNeedVerify' => 1, 'signLevel' => 1]
],
'url' => $contract['file']
];
$notify_url = '';
if($contract['type'] == 0){
$notify_url = env('project.website_domain').'/api/index/systemCarRent';
}elseif($contract['type'] == 1){
$notify_url = env('project.website_domain').'/api/index/selfCarRent';
}elseif($contract['type'] == 2){
$notify_url = env('project.website_domain').'/api/index/cancelRent';
}
$signRes = app(JunziqianController::class)->VehicleRentSigning($signData, $params['id'],$notify_url);
if ($signRes->success) {
$contract->save([
'id' => $contract['id'],
'contract_no' => $signRes->data,
'status' => 2
]);
if($contract['type'] == 2){
$smsTitle = '《解约合同》';
}else{
$smsTitle = '《租赁合同》';
}
$this->sendSms($params['id'],$smsTitle);
return $this->success('合同发送成功');
} else {
return $this->fail($signRes->msg);
}
}
public function sendSmsAgain() {
//获取参数
$id = $this->request->post('id');
if(empty($id)){
return $this->fail('参数错误');
}
//获取数据
$contract = VehicleContract::where('id',$id)->find();
if(empty($contract)){
return $this->fail('数据错误');
}
if($contract['type'] == 2){
$smsTitle = '《解约合同》';
}else{
$smsTitle = '《租赁合同》';
}
$this->sendSms($id,$smsTitle);
return $this->success('发送成功');
}
//小组服务公司租赁申请
public function rentApply() {
//验证请求类型
if(!$this->request->isPost()){
return $this->fail('请求类型错误');
}
//获取参数
$params = $this->request->post(['type','license','pic']);
if(!isset($params['type'])){
return $this->fail('缺少必要参数');
}
if($params['type'] == 1){
if(empty($params['license']) || empty($params['pic'])){
return $this->fail('缺少必要参数');
}
}
//获取当前公司信息
$party_b = Company::field('id,company_name,master_name,master_phone,master_email,is_contract,organization_code,company_type')->where('id',$this->userInfo['company_id'])->find();
if(empty($party_b)){
return $this->fail('账号异常');
}
if($party_b['company_type'] != 18){
return $this->fail('非小组服务公司不能申请');
}
if($party_b['is_contract'] != 1){
return $this->fail('当前小组服务公司未签约');
}
//获取签约信息
$contract = Contract::where('party_b',$this->userInfo['company_id'])->where('signing_timer',2)->findOrEmpty();
if($contract->isEmpty()){
return $this->fail('未找到签约镇街公司');
}
$party_a = Company::field('id,company_name,master_name,master_phone,master_email,is_contract,organization_code,company_type')->where('id',$contract['party_a'])->find();
if($party_a['company_type'] != 41){
return $this->fail('不能与非镇街公司签约');
}
if($party_a['is_contract'] != 1){
return $this->fail('当前镇街公司未签约');
}
//判断是否申请过
$vehicleContract = VehicleContract::where('company_b_id',$this->userInfo['company_id'])->whereNotIn('type','2,3')->whereNotIn('status','4,5,6')->find();
if(!empty($vehicleContract)){
return $this->fail('请勿重复申请');
}
if($params['type'] == 1){
$cars_info = json_encode([['license'=>$params['license'],'pic'=>$params['pic']]]);
$car_type = 1;
}else{
$cars_info = null;
$car_type = 0;
}
//设置数据
$data = [
'contract_logistic_id' => 0,
'contract_no' => time(),
'company_a_id' => $party_a['id'],
'company_a_name' => $party_a['company_name'],
'company_a_code' => $party_a['organization_code'],
'company_a_user' => $party_a['master_name'],
'company_a_phone' => $party_a['master_phone'],
'company_a_email' => $party_a['master_email'],
'company_b_id' => $party_b['id'],
'company_b_name' => $party_b['company_name'],
'company_b_code' => $party_b['organization_code'],
'company_b_user' => $party_b['master_name'],
'company_b_phone' => $party_b['master_phone'],
'company_b_email' => $party_b['master_email'],
'cars_info' => $cars_info,
'num' => 1,
'type' => $car_type,
'status' => -1,
'create_time' => time(),
'update_time' => time()
];
//写入数据
$result = VehicleContract::create($data);
if($result){
return $this->success('申请成功,待镇街公司审核');
}else{
return $this->fail('申请失败,请稍后重试');
}
}
//镇街公司驳回小组服务公司的租赁申请
public function rejectRentApply() {
//验证请求类型
if(!$this->request->isPost()){
return $this->fail('请求类型错误');
}
//获取参数
$params = $this->request->post(['id','content']);
if(empty($params['id']) || empty($params['content'])){
return $this->fail('缺少必要参数');
}
//获取数据
$vehicleContract = VehicleContract::where('id',$params['id'])->find();
if(empty($vehicleContract)){
return $this->fail('未查找到数据');
}
if($vehicleContract['company_a_id'] != $this->userInfo['company_id']){
return $this->fail('数据不匹配');
}
//判断合同类型
if($vehicleContract['type'] == 2){
Db::startTrans();
try{
//查找原合同
$car = json_decode($vehicleContract['cars_info'],true);
$carRentInfo = VehicleRent::where('car_id',$car['id'])->findOrEmpty();
$oldVehicleContractUpdate = VehicleContract::where('id',$carRentInfo['contract_id'])->update(['status'=>3]);
//更新
$result = VehicleContract::where('id',$params['id'])->update([
'status' => 4,
'reject_message' => $params['content']
]);
if($oldVehicleContractUpdate && $result){
Db::commit();
return $this->success('驳回成功');
}else{
Db::rollback();
return $this->fail('驳回失败');
}
}catch (\Exception $e) {
Db::rollback();
return $this->fail($e->getMessage());
}
}else{
//更新
$result = VehicleContract::where('id',$params['id'])->update([
'status' => 4,
'reject_message' => $params['content']
]);
if($result){
return $this->success('驳回成功');
}else{
return $this->fail('驳回失败');
}
}
}
//镇街公司收到的小组服务公司租赁申请列表
public function rentApplyList() {
$data = VehicleContract::where('company_a_id',$this->userInfo['company_id'])->where('status','<>',4)->select();
return $this->success('请求成功',$data->toArray());
}
//小组服务公司租赁申请详情
public function rentApplyInfo() {
//获取参数
$id = $this->request->get('id');
if(empty($id)){
return $this->fail('缺少必要参数');
}
//获取数据
$vehicleContract = VehicleContract::where('id',$id)->find();
$vehicleContract['cars_info'] = json_decode($vehicleContract['cars_info'],true);
if(empty($vehicleContract)){
return $this->fail('获取数据失败');
}
if($vehicleContract['company_a_id'] != $this->userInfo['company_id']){
return $this->fail('数据不匹配');
}
$vehicleRentCars = VehicleRent::where('company_id',$vehicleContract['company_a_id'])->where('status',0)->select()->toArray();
$data = [
'vehicleContract' => $vehicleContract->toArray(),
'vehicleRentCars' => $vehicleRentCars
];
return $this->success('请求成功',$data);
}
//镇街公司车辆管理首页
public function townCompanyVehicleIndex() {
//获取公司信息
$company = Company::where('id',$this->userInfo['company_id'])->find();
if(empty($company)){
return $this->fail('数据错误');
}
if($company['company_type'] != 41){
return $this->fail('非镇街公司不能访问');
}
//获取小组服务公司申请信息
$vehicleContract_pre = VehicleContract::where('company_a_id',$company['id'])->where('status','in','-1,0,1,2')->where('type','<>',2)->limit(4)->order('create_time desc')->select()->toArray();
$vehicleContract = [];
foreach ($vehicleContract_pre as $v) {
$vehicleBuy = VehicleBuyRecord::where('contract_id',$v['id'])->findOrEmpty();
if($vehicleBuy->isEmpty()){
$vehicleContract[] = $v;
}
}
//获取车辆列表
$vehicleRentCars = VehicleRent::where('company_id',$company['id'])->where('status','in','0,1,2')->select()->each(function($item){
if($item['status'] == 2){
$item['rent_user'] = Company::field('company_name,master_name as user_name,master_phone as user_phone,province,city,area,street,village,brigade')->where('id',$item['rent_company_id'])->find()->append(['province_name', 'city_name', 'area_name', 'street_name', 'village_name','brigade_name']);
}
})->toArray();
//获取该公司已签约的小组服务公司数量
$villageCompany = Contract::field('id')->where('party_a',$company['id'])->where('signing_timer',2)->count();
//已经租赁车辆
$rentCar = VehicleRent::field('id')->where('company_id',$company['id'])->whereNotIn('type','1,2')->count();
//申请中的车辆
$applyCar = VehicleContract::field('num')->where('company_b_id',$company['id'])->where('status','in','-1,0,1,2')->sum('num');
//自有车辆数量
$selfCar = VehicleContract::field('id')->where('company_a_id',$company['id'])->where('type',1)->where('status','in','-1,0,1,2,3')->sum('num');
//购买车辆数量
//获取下面的小组服务公司
$villageCompanys = Contract::field('party_b')->where('party_a',$company['id'])->where('signing_timer',2)->select()->toArray();
$buyCar = 0;
foreach ($villageCompanys as $v) {
$aa = VehicleContract::field('id')->where('company_b_id',$v['party_b'])->where('type',3)->where('status','in','-1,0,1,2,3')->findOrEmpty();
if(!$aa->isEmpty()){
$buyCar += 1;
}
}
//可在租车辆
$doubleRentCar = max($villageCompany - $rentCar - $applyCar - $selfCar - $buyCar,0);
//设置数据
$data = [
'apply' => $vehicleContract,
'car_list' => $vehicleRentCars,
'monitor_num' => $villageCompany,
'rent_num' => $rentCar,
'can_rent_num' => intval($doubleRentCar),
'self_num' => $selfCar,
'buy_num' => $buyCar,
'apply_num' => $applyCar
];
//返回
return $this->success('请求成功',$data);
}
public function villageCompanyIndex() {
//获取公司信息
$company = Company::field('id,master_name as user_name,master_phone as user_phone,company_name,company_type,province,city,area,street,village,brigade')->where('id',$this->userInfo['company_id'])->find()->append(['province_name', 'city_name', 'area_name', 'street_name', 'village_name','brigade_name']);
if(empty($company)){
return $this->fail('数据错误');
}
if($company['company_type'] != 18 ){
return $this->fail('非小组公司不能访问');
}
//获取购买车辆记录
$buyCarRent = VehicleBuyRecord::where('company_id',$company['id'])->where('status','<>',4)->findOrEmpty();
if($buyCarRent->isEmpty()){
$data = VehicleContract::where('company_b_id',$company['id'])->where('type','<>',2)->order('id desc')->findOrEmpty();
}else{
$data = $buyCarRent;
$data['status'] = 0;
$data['type'] = 0;
}
//获取签约合同
return $this->success('请求成功',$data->toArray());
}
//车辆详情
public function vehicleInfo() {
$id = $this->request->get('car_id');
//获取数据
if(!empty($id) && $id != 'undefined'){
$data = VehicleRent::where('car_id',$id)->where('status',2)->findOrEmpty();
}else{
$data = VehicleRent::where('rent_company_id',$this->userInfo['company_id'])->where('status',2)->findOrEmpty();
}
if($data->isEmpty()){
return $this->fail('数据不存在');
}
$data['mileage'] = 0;
$data['company'] = Company::field('company_name,master_name as user_name,master_phone as user_phone,province,city,area,street,village,brigade')->where('id',$data['rent_company_id'])->find()->append(['province_name', 'city_name', 'area_name', 'street_name', 'village_name','brigade_name']);
//获取合同
$data['contract'] = VehicleContract::field('id,contract_no,contract_url,file,status,create_time,update_time')->where('id',$data['contract_id'])->findOrEmpty();
//当前坐标位置
$position = curl_get(env('project.logistic_domain').'/api/getCarLocal?car_id='.$data['car_id']);
if($position && $position['code'] == 1){
$data['position'] = $position['data'];
}else{
$data['position'] = [];
}
return $this->success('请求成功',$data->toArray());
}
public function vehicleTrack() {
//获取参数
$params = $this->request->post(['car_id','start_time','end_time']);
if(empty($params['car_id']) || empty($params['start_time']) || empty($params['end_time'])){
return $this->fail('缺少必要参数');
}
//获取轨迹
$url = env('project.logistic_domain').'/api/getCarHistory?car_id='.$params['car_id'].'&start_time='.$params['start_time'].'&end_time='.$params['end_time'];
$result = curl_get($url);
$data = [];
foreach($result['data'] as $k => $v){
$data[$k]['latitude'] = $v['lat'];
$data[$k]['longitude'] = $v['lon'];
}
return $this->success('success',$data);
}
public function cancelContract() {
//获取参数
$params = $this->request->post(['car_id']);
if(empty($params['car_id'])){
return $this->fail('参数错误');
}
//获取车辆信息
$carInfo = VehicleRent::where('car_id',$params['car_id'])->where('type',1)->find();
if(empty($carInfo)){
return $this->fail('车辆信息错误');
}
if($carInfo['rent_company_id'] != $this->userInfo['company_id']){
return $this->fail('当前公司与车辆信息不匹配');
}
//获取原签约信息
$contract = VehicleContract::where('id',$carInfo['contract_id'])->where('status',3)->find();
if(empty($contract)){
return $this->fail('原签约信息错误');
}
//获取甲方公司信息
$party_a = Company::where('id',$carInfo['company_id'])->find();
if(empty($party_a)){
return $this->fail('甲方公司信息错误');
}
//获取乙方公司信息
$party_b = Company::where('id',$this->userInfo['company_id'])->find();
if(empty($party_b)){
return $this->fail('乙方公司信息错误');
}
//判断是否申请过
$vehicleContract = VehicleContract::where('company_b_id',$this->userInfo['company_id'])->where('type',3)->where('status','<>',4)->find();
if(!empty($vehicleContract)){
return $this->fail('请勿重复申请');
}
//设置数据
$data = [
'contract_logistic_id' => 0,
'contract_no' => time(),
'company_a_id' => $party_a['id'],
'company_a_name' => $party_a['company_name'],
'company_a_code' => $party_a['organization_code'],
'company_a_user' => $party_a['master_name'],
'company_a_phone' => $party_a['master_phone'],
'company_a_email' => $party_a['master_email'],
'company_b_id' => $party_b['id'],
'company_b_name' => $party_b['company_name'],
'company_b_code' => $party_b['organization_code'],
'company_b_user' => $party_b['master_name'],
'company_b_phone' => $party_b['master_phone'],
'company_b_email' => $party_b['master_email'],
'cars_info' => json_encode(['id'=>$carInfo['car_id'],'license'=>$carInfo['car_license']]),
'num' => 1,
'type' => 2,
'status' => -1,
'create_time' => time(),
'update_time' => time(),
'old_file' => $contract['contract_url'],
];
Db::startTrans();
try{
//写入数据
$new_result = VehicleContract::create($data);
//修改原合同状态
$old_result = VehicleContract::where('id',$contract['id'])->update(['status'=>5]);
if($new_result && $old_result){
Db::commit();
return $this->success('申请成功,待镇街公司审核');
}else{
Db::rollback();
return $this->fail('申请失败,请稍后重试');
}
}catch (\Exception $e) {
Db::rollback();
return $this->fail($e->getMessage());
}
}
public function townCompanyContractList() {
//获取公司信息
$company = Company::where('id',$this->userInfo['company_id'])->find();
if(empty($company)){
return $this->fail('请求错误');
}
if($company['company_type'] != 41){
return $this->fail('非镇街公司不能访问');
}
$data = VehicleContract::field('id,contract_no,contract_url,status,create_time,update_time')->where('contract_logistic_id','<>',0)->where('company_b_id',$company['id'])->select();
return $this->success('请求成功',$data->toArray());
}
public function sendSms($id,$title) {
//获取合同数据
$contract = VehicleContract::where('id',$id)->find();
if ($contract && !$contract->isEmpty() && $contract['file'] != '') {
//发送短信
$data = [
//甲方
[
"applyNo" => $contract['contract_no'],
"fullName" => $contract['company_a_name'],
"identityCard" => $contract['company_a_code'],
"identityType" => 12,
"master_phone" => $contract['company_a_phone'],
"type"=>"party_a"
],
//乙方
[
"applyNo" => $contract['contract_no'],
"fullName" => $contract['company_b_name'],
"identityCard" => $contract['company_b_code'],
"identityType" => 12,
"master_phone" => $contract['company_b_phone'],
"type"=>"party_b"
],
];
$url = [];
foreach ($data as $v) {
$res = app(JunziqianController::class)->SigningLink($v);
if (!$res->success) {
return false;
}
if ($v['type'] == 'party_a') {
$url['party_a'] =$res->data;
} else {
$url['party_b'] =$res->data;
}
//发送短信
$sms = [
'mobile' => $v['master_phone'],
'name' => $v['fullName'],
'type' => $title,
'code' => 'api/Hetong/info?id='.$id.'&type='.$v['type'],
'scene' => 'WQ'
];
$scene = NoticeEnum::getSceneByTag($sms['scene']);
if (empty($scene)) {
throw new \Exception('场景值异常');
}
event('Notice', [
'scene_id' => $scene,
'params' => $sms
]);
}
VehicleContract::where('id', $id)->update(['url' => json_encode($url)]);
return true;
}else{
return false;
}
}
//获取空闲车辆列表
public function getFreeCars(): \think\response\Json
{
//1、获取当前登录用户信息并判断其公司是否是小组服务公司
$xzCompany = Company::field('id,company_type')->where('id',$this->userInfo['company_id'])->findOrEmpty();
if($xzCompany->isEmpty() || $xzCompany['company_type'] != 18){
return $this->fail('当前用户非小组服务公司');
}
//2、获取小组服务公司签约的镇街公司
$zjCompany = Contract::field('party_a')->where('party_b',$this->userInfo['company_id'])->where('signing_timer',2)->findOrEmpty();
if($zjCompany->isEmpty()){
return $this->fail('未获取到小组服务公司签约的镇街公司');
}
//3、获取镇街公司向平台租赁的且未二次租赁给小组公司的车辆
$zjRentCars = VehicleRent::field('car_id')->where('company_id',$zjCompany['party_a'])->where('status',0)->select()->toArray();
$zjRentCars = array_column($zjRentCars,'car_id');
//4、获取小组公司自己租赁的车辆
$xzRentCars = VehicleRent::field('car_id')->where('company_id',$zjCompany['party_a'])->where('rent_company_id',$xzCompany['id'])->where('status',2)->where('type','<>',1)->where('type','<>',2)->select()->toArray();
$xzRentCars = array_column($xzRentCars,'car_id');
//5、获取平台未出租的车辆
$result = curl_post(env('project.logistic_domain').'/api/Vehicle/getFreeCars',[],['ids'=>implode(',',array_merge($zjRentCars,$xzRentCars))]);
foreach($result['data'] as $k => $v){
$result['data'][$k]['checked'] = [];
}
//6、返回
return $this->success('success',$result['data']);
}
//申请购车
public function buyCars() {
//验证请求类型
if(!$this->request->isPost()){
return $this->fail('请求类型错误');
}
//1、获取购买车辆的id参数
$cars = $this->request->post('cars');
$cars = json_decode($cars,true);
if(empty($cars)){
return $this->fail('参数错误');
}
//获取小组服务公司信息
$xzCompany = Company::field('id,company_name,master_name,master_phone,master_email,organization_code,company_type')->where('id',$this->userInfo['company_id'])->findOrEmpty();
if($xzCompany->isEmpty() || $xzCompany['company_type'] != 18){
return $this->fail('当前用户非小组服务公司');
}
//获取镇街公司信息
$zjCompany = Contract::field('party_a')->where('party_b',$this->userInfo['company_id'])->where('signing_timer',2)->findOrEmpty();
if($zjCompany->isEmpty()){
return $this->fail('签约镇街公司不存在');
}
//判断购买车辆中是否包含镇街公司租赁的车辆
$car_ids = array_column($cars,'id');
$zjRentCars = VehicleRent::field('car_id as id,car_license as license')->where('company_id',$zjCompany['party_a'])->where('car_id','in',$car_ids)->where('status',0)->where('type',0)->select();
//判断小组服务公司是否租车或者有自有车辆
$xzRentCars = VehicleRent::where('rent_company_id',$xzCompany['id'])->where('status','in','1,2')->where('type','<>',2)->findOrEmpty();
//如果没有租赁车俩和上传自有车辆,也没有购买镇街公司租赁的车辆 则直接发起购买合同
if($xzRentCars->isEmpty() && $zjRentCars->isEmpty()){
//发送购买合同给物流系统
$curl_result = curl_post(env('project.logistic_domain').'/api/signContract',[],[
'num' => count($cars),
'company_id' => $xzCompany['id'],
'company_name' => $xzCompany['company_name'],
'company_code' => $xzCompany['organization_code'],
'company_user' => $xzCompany['master_name'],
'company_phone' => $xzCompany['master_phone'],
'company_email' => $xzCompany['master_email'],
'cars_info' => json_encode($cars),
'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']);
}
//如果没有租赁车俩和上传自有车辆,但有购买镇街公司租赁的车辆 则发起镇街公司与平台公司的解约合同
if($xzRentCars->isEmpty() && !$zjRentCars->isEmpty()){
$zjCompanyInfo = Company::field('id,company_name,master_name,master_phone,master_email,organization_code')->where('id',$zjCompany['party_a'])->findOrEmpty();
//发送解约合同给物流系统
$curl_result = curl_post(env('project.logistic_domain').'/api/signContract',[],[
'num' => count($zjRentCars),
'company_id' => $zjCompanyInfo['id'],
'company_name' => $zjCompanyInfo['company_name'],
'company_code' => $zjCompanyInfo['organization_code'],
'company_user' => $zjCompanyInfo['master_name'],
'company_phone' => $zjCompanyInfo['master_phone'],
'company_email' => $zjCompanyInfo['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::create([
'company_id' => $xzCompany['id'],
'company_name' => $xzCompany['company_name'],
'company_code' => $xzCompany['organization_code'],
'company_user' => $xzCompany['master_name'],
'company_phone' => $xzCompany['master_phone'],
'company_email' => $xzCompany['master_email'],
'cars_info' => json_encode($cars),
'num' => count($cars),
'status' => 3,
'contract_id' => $res->id,
'create_time' => time()
]);
//更改本地车辆状态
VehicleRent::where('car_id','in',$car_ids)->update(['status'=>1]);
}
//如果有租赁车俩和上传自有车辆,但没有购买镇街公司租赁的车辆 则先向镇街公司发起解约合同
if(!$xzRentCars->isEmpty() && $zjRentCars->isEmpty()){
//获取镇街公司信息
$zjCompanyInfo = Company::field('id,company_name,master_name,master_phone,master_email,organization_code')->where('id',$zjCompany['party_a'])->findOrEmpty();
//生成本地解约合同
$res = VehicleContract::create([
'contract_no' => time(),
'contract_logistic_id' => 0,
'company_a_id' => $zjCompanyInfo['id'],
'company_a_name' => $zjCompanyInfo['company_name'],
'company_a_code' => $zjCompanyInfo['organization_code'],
'company_a_user' => $zjCompanyInfo['master_name'],
'company_a_phone' => $zjCompanyInfo['master_phone'],
'company_a_email' => $zjCompanyInfo['master_email'],
'company_b_id' => $xzCompany['id'],
'company_b_name' => $xzCompany['company_name'],
'company_b_code' => $xzCompany['organization_code'],
'company_b_user' => $xzCompany['master_name'],
'company_b_phone' => $xzCompany['master_phone'],
'company_b_email' => $xzCompany['master_email'],
'num' =>1,
'cars_info' => json_encode([['id'=>$xzRentCars['car_id'],'license'=>$xzRentCars['car_license']]]),
'type' => 2,
'status' => 0,
'create_time' => time(),
'update_time' => time(),
]);
//生成关联记录
if($xzRentCars['type'] == 0){
$status = 2;
}else{
$status = 1;
}
VehicleBuyRecord::create([
'company_id' => $xzCompany['id'],
'company_name' => $xzCompany['company_name'],
'company_code' => $xzCompany['organization_code'],
'company_user' => $xzCompany['master_name'],
'company_phone' => $xzCompany['master_phone'],
'company_email' => $xzCompany['master_email'],
'cars_info' => json_encode($cars),
'num' => count($cars),
'status' => $status,
'contract_id' => $res->id,
'create_time' => time()
]);
}
//如果有租赁车俩和上传自有车辆,也有购买镇街公司租赁的车辆 则先向镇街公司发起解约合同,再由镇街公司向平台公司发起解约合同
if(!$xzRentCars->isEmpty() && !$zjRentCars->isEmpty()){
//获取镇街公司信息
$zjCompanyInfo = Company::field('id,company_name,master_name,master_phone,master_email,organization_code')->where('id',$zjCompany['party_a'])->findOrEmpty();
//生成本地解约合同
$res = VehicleContract::create([
'contract_no' => time(),
'contract_logistic_id' => 0,
'company_a_id' => $zjCompanyInfo['id'],
'company_a_name' => $zjCompanyInfo['company_name'],
'company_a_code' => $zjCompanyInfo['organization_code'],
'company_a_user' => $zjCompanyInfo['master_name'],
'company_a_phone' => $zjCompanyInfo['master_phone'],
'company_a_email' => $zjCompanyInfo['master_email'],
'company_b_id' => $xzCompany['id'],
'company_b_name' => $xzCompany['company_name'],
'company_b_code' => $xzCompany['organization_code'],
'company_b_user' => $xzCompany['master_name'],
'company_b_phone' => $xzCompany['master_phone'],
'company_b_email' => $xzCompany['master_email'],
'num' =>1,
'cars_info' => json_encode([['id'=>$xzRentCars['car_id'],'license'=>$xzRentCars['car_license']]]),
'type' => 2,
'status' => 0,
'create_time' => time(),
'update_time' => time(),
]);
//生成关联记录
VehicleBuyRecord::create([
'company_id' => $xzCompany['id'],
'company_name' => $xzCompany['company_name'],
'company_code' => $xzCompany['organization_code'],
'company_user' => $xzCompany['master_name'],
'company_phone' => $xzCompany['master_phone'],
'company_email' => $xzCompany['master_email'],
'cars_info' => json_encode($cars),
'num' => count($cars),
'status' => 2,
'contract_id' => $res->id,
'create_time' => time()
]);
}
//更新物流系统
curl_post(env('project.logistic_domain').'/api/Vehicle/updateVehicleStatusToBuy',[],['car_ids'=>implode(',',$car_ids)]);
return $this->success('合同发起成功,等待审核 from task');
}
}