TaskSystem/app/api/controller/VehicleController.php
2023-08-30 18:57:41 +08:00

355 lines
14 KiB
PHP

<?php
namespace app\api\controller;
use app\common\enum\notice\NoticeEnum;
use app\common\logic\vehicle\VehicleLogic;
use app\common\model\Company;
use app\common\model\contract\Contract;
use app\common\model\contract\VehicleContract;
use app\common\model\vehicle\VehicleRent;
use app\common\model\vehicle\VehicleVillageApply;
use think\facade\Db;
use think\response\Json;
class VehicleController extends BaseApiController
{
//镇街公司想物流平台申请租赁车辆
public function setContractByTownCompany()
{
//获取参数
$params = $this->request->post(['num']);
//验证参数
if(empty($params['num'])){
return $this->fail('缺少必要参数');
}
if(empty($this->userInfo['company_id'])){
return $this->fail('签约公司不存在');
}
$params['party_b'] = $this->userInfo['company_id'];
$result = VehicleLogic::initiate_contract_logistic($params);
//返回数据
if($result && $result['code'] == 1){
return $this->success($result['msg']);
}else{
return $this->fail($result['msg']);
}
}
//镇街公司生成小组服务公司租赁合同
public function setContractByVillageCompany(){
//获取参数
$params = $this->request->post(['apply_id','car_id']);
if(empty($params['apply_id'])){
return $this->fail('缺少必要参数');
}
$apply = VehicleVillageApply::where('id',$params['apply_id'])->find();
if($apply['type'] == 0){
if(empty($params['car_id'])){
return $this->fail('缺少必要参数');
}
}
if(empty($this->userInfo['company_id'])){
return $this->fail('签约公司不存在');
}
$params['party_a'] = $this->userInfo['company_id'];
$result = VehicleLogic::initiate_contract_town($params);
//返回数据
if($result && $result['code'] == 1){
return $this->success($result['msg']);
}else{
return $this->fail($result['msg']);
}
}
//镇街公司发起合同
public function initiatingRentCarContract() {
//获取参数
$params = $this->request->post(['id']);
if(empty($params['id'])){
return $this->fail('缺少必要参数');
}
//获取数据
$contract = VehicleContract::where('id',$params['id'])->find();
if(!$contract || $contract->isEmpty()){
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']
];
$signRes = app(JunziqianController::class)->Signing($signData, $params['id'],'https://worker-task.lihaink.cn/api/notifyVillageCarRent');
if ($signRes->success) {
$contract->save([
'id' => $contract['id'],
'contract_no' => $signRes->data,
'status' => 2
]);
$this->sendSms($params['id']);
return $this->success('合同发送成功');
} else {
return $this->fail($signRes->msg);
}
}
//小组服务公司租赁申请
public function rentApply() {
if(!$this->request->isPost()){
return $this->fail('请求类型错误');
}
//获取参数
$params = $this->request->post('license');
//获取当前公司信息
$village_company = Company::field('id,company_name,master_name,master_phone,is_contract,company_type')->where('id',$this->userInfo['company_id'])->find();
if($village_company->isEmpty() || $village_company['company_type'] != 18){
return $this->fail('非小组服务公司不能申请');
}
if($village_company['is_contract'] != 1){
return $this->fail('当前小组服务公司未签约');
}
//获取签约信息
$contract = Contract::field('party_a')->where('party_b',$this->userInfo['company_id'])->where('signing_timer',2)->find();
if($contract->isEmpty()){
return $this->fail('未找到签约镇街公司');
}
//判断是否申请过
$apply = VehicleVillageApply::where('village_company_id',$this->userInfo['company_id'])->where('status','<>',2)->find();
if(!$apply->isEmpty()){
return $this->fail('已申请了,请勿重复申请');
}
//设置数据
$result = VehicleVillageApply::create([
'town_company_id' => $contract['party_a'],
'village_company_id' => $village_company['id'],
'village_company_name' => $village_company['company_name'],
'village_company_user' => $village_company['master_phone'],
'village_company_phone' => $village_company['id'],
'car_license' => !empty($params) ? $params : '',
'type' => !empty($params) ? 1 : 0,
'status' => 0,
'create_time' => time(),
'update_time' => time(),
]);
if($result && $result->id){
return $this->success('申请成功');
}else{
return $this->fail('申请失败');
}
}
//镇街公司驳回小组服务公司的租赁申请
public function rejectRentApply() {
//获取参数
$params = $this->request->post(['apply_id','content']);
if(empty($params['apply_id']) || empty($params['content'])){
return $this->fail('缺少必要参数');
}
//获取数据
$apply = VehicleVillageApply::where('id',$params['apply_id'])->where('status',0)->find();
if($apply->isEmpty()){
return $this->fail('数据错误');
}
if($apply['town_company_id'] != $this->userInfo['company_id']){
return $this->fail('镇街公司信息错误');
}
//更新
$apply -> status = 2;
$apply -> notice = $params['content'];
$apply -> update_time = time();
$result = $apply->save();
if($result){
return $this->success('驳回成功');
}else{
return $this->fail('驳回失败');
}
}
//镇街公司收到的小组服务公司租赁申请列表
public function rentApplyList() {
$data = VehicleVillageApply::where('town_company_id',$this->userInfo['company_id'])->where('status',0)->select();
return $this->success('请求成功',$data->toArray());
}
//小组服务公司租赁申请详情
public function rentApplyInfo() {
//获取参数
$id = $this->request->get('id');
if(empty($id)){
return $this->fail('缺少必要参数');
}
$this->userInfo['company_id'] = 177;
//获取数据
$data = VehicleVillageApply::where('id',$id)->find();
if($data->isEmpty() || $data['town_company_id'] != $this->userInfo['company_id'] || $data['status'] != 0){
return $this->fail('获取数据失败');
}
//获取镇街公司可租赁车辆列表
$cars = VehicleRent::field('id,car_license')->where('company_id',$this->userInfo['company_id'])->where('status',0)->where('type',0)->select();
return $this->success('请求成功',[
'apply' => $data->toArray(),
'cars' => $cars->toArray()
]);
}
//镇街公司车辆管理首页
public function vehicleIndex() {
//获取参数
$params = $this->request->get();
$company_id = $this->userInfo['company_id'];
if(empty($company_id)){
return $this->fail('公司信息不存在');
}
//获取小组服务公司申请信息
$apply = VehicleVillageApply::where('town_company_id',$company_id)->where('status',0)->select();
//获取车辆列表
$carList = VehicleRent::where('company_id',$company_id)->select()->each(function($item){
$item['mileage'] = 0;
if($item['status'] == 1){
$company = Company::where('id',$item['rent_company_id'])->find()->append(['province_name', 'city_name', 'area_name', 'street_name', 'village_name','brigade_name']);
$item['rent_company'] = $company->toArray();
}
});
//获取该公司已签约的小组服务公司数量
$villageCompany = Contract::where('party_a',$company_id)->where('signing_timer',2)->select();
//系统车辆数量
$rentCar = VehicleRent::field('id')->where('type',0)->select();
//可租赁车辆
$canRentCar = VehicleRent::field('id')->where('type',0)->where('status',0)->select();
//自有车辆数量
$selfCar = VehicleRent::field('id')->where('type',1)->select();
//设置数据
$data = [
'apply' => $apply->toArray(),
'car_list' => $carList->toArray(),
'monitor_num' => $villageCompany->count(),
'rent_num' => $rentCar->count(),
'can_rent_num' => $canRentCar->count(),
'self_num' => $selfCar->count(),
];
//返回
return $this->success('请求成功',$data);
}
//车辆详情
public function vehicleInfo() {
//获取参数
$carId = $this->request->get('car_id');
if(empty($carId)){
return $this->fail('缺少必要参数');
}
//获取数据
$data = VehicleRent::where('car_id',$carId)->find();
$data['mileage'] = 0;
if($data['status'] == 1){
$data['company'] = Company::where('id',$data['rent_company_id'])->find()->append(['province_name', 'city_name', 'area_name', 'street_name', 'village_name','brigade_name']);
}
//当前坐标位置
$position = curl_post('http://logistics.lihaink.cn/api/getCarLocal',[],['car_id'=>$carId]);
if($position && $position['code'] == 1){
$data['position'] = $position['data'];
}
return $this->success('请求成功',$data->toArray());
}
public function vehicleTrack() {
//获取参数
$params = $this->request->get(['car_id','start_time','end_time']);
if(empty($params['car_id']) || empty($params['start_time']) || empty($params['end_time'])){
return $this->fail('缺少必要参数');
}
//获取轨迹
$url = 'http://logistics.lihaink.cn/api/getCarHistory?car_id='.$params['car_id'].'&start_time='.$params['start_time'].'&end_time='.$params['end_time'];
$result = curl_get($url);
return $result;
}
//镇街公司合同列表
public function contractList() {
$data = VehicleContract::where('company_a_id',$this->userInfo['company_id'])->where('status',0)->select();
return $this->success('success',$data->toArray());
}
//镇街公司合同详情
public function contractInfo() {
//获取参数
$id = $this->request->get('id');
if(empty($id)){
return $this->fail('缺少必要参数');
}
//获取数据
$data = VehicleContract::where('id',$id)->find();
$data['cars_info'] = json_decode($data['cars_info'],true);
return $this->success('success',$data->toArray());
}
public function sendSms($id) {
//获取合同数据
$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' => '《租赁合同》',
'code' => 'api/Hetong/url?id='.$id.'&type='.$v['type'],
'scene' => 'WQTZ'
];
$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;
}
}
}