change vehicle api
This commit is contained in:
parent
2de53c0ae8
commit
fc1b51703d
@ -11,54 +11,20 @@ use think\response\Json;
|
||||
*/
|
||||
class VehicleController extends BaseApiController
|
||||
{
|
||||
public array $notNeedLogin = ['getCompany','setContract','getRentRecord','updateRentRecord','getCompanyCars','getCarLocal','getCarHistory'];
|
||||
public array $notNeedLogin = ['checkNum','vehicleRent','getCarLocal','getCarHistory'];
|
||||
|
||||
/*
|
||||
*获取平台公司接口
|
||||
*/
|
||||
public function getCompany():Json {
|
||||
$result = VehicleLogic::companyInfo();
|
||||
if($result['code'] == 1){
|
||||
return $this->success($result['msg'],$result['data']);
|
||||
}else{
|
||||
return $this->fail($result['msg']);
|
||||
}
|
||||
}
|
||||
|
||||
public function setContract():Json {
|
||||
//验证租赁数量
|
||||
public function checkNum() {
|
||||
//获取参数
|
||||
$params = $this->request->post(['party_a','party_b','num','rent_type','car_id']);
|
||||
|
||||
//验证参数
|
||||
if(empty($params['party_a']) || empty($params['party_b']) || empty($params['num']) || empty($params['rent_type'])){
|
||||
return $this->fail('缺少必要的参数');
|
||||
$param = $this->request->get(['num']);
|
||||
if(empty($param['num'])){
|
||||
return $this->fail('缺少必要参数');
|
||||
}
|
||||
if(!in_array($params['rent_type'],[1,2])){
|
||||
return $this->fail('rent_type数据格式错误');
|
||||
if(!intval($param['num'])){
|
||||
return $this->fail('参数格式错误');
|
||||
}
|
||||
if($params['rent_type'] == 2){
|
||||
if(empty($params['car_id'])){
|
||||
return $this->fail('缺少必要参数');
|
||||
}
|
||||
}
|
||||
//生成数据
|
||||
$result = VehicleLogic::rentRecord($params);
|
||||
//返回数据
|
||||
if($result['code'] == 1){
|
||||
return $this->success($result['msg']);
|
||||
}else{
|
||||
return $this->fail($result['msg']);
|
||||
}
|
||||
}
|
||||
|
||||
public function getRentRecord():Json {
|
||||
//获取参数
|
||||
$params = $this -> request -> get('contract_id');
|
||||
if(empty($params)){
|
||||
return $this->fail('确实必要参数');
|
||||
}
|
||||
//获取数据
|
||||
$result = VehicleLogic::rendRecordInfo($params);
|
||||
$result = VehicleLogic::checkNum($param['num']);
|
||||
//返回数据
|
||||
if($result['code'] == 1){
|
||||
return $this->success($result['msg'],$result['data']);
|
||||
@ -67,14 +33,14 @@ class VehicleController extends BaseApiController
|
||||
}
|
||||
}
|
||||
|
||||
public function updateRentRecord():Json {
|
||||
public function vehicleRent():Json {
|
||||
//获取参数
|
||||
$param = $this->request->post('contract_id');
|
||||
if(empty($param)){
|
||||
$param = $this->request->post(['contract_id','car_id','company_id','company_name','company_user_id','company_user_name','company_user_phone','rent_type']);
|
||||
if(empty($param['contract_id']) || empty($param['car_id']) || empty($param['company_id']) || empty($param['company_name']) || empty($param['company_user_id']) || empty($param['company_user_name']) || empty($param['company_user_phone']) || empty($param['rent_type'])){
|
||||
return $this->fail('缺少必要参数');
|
||||
}
|
||||
//获取数据
|
||||
$result = VehicleLogic::rendRecordEdit($param);
|
||||
$result = VehicleLogic::setRent($param);
|
||||
//返回数据
|
||||
if($result['code'] == 1){
|
||||
return $this->success($result['msg']);
|
||||
@ -83,20 +49,6 @@ class VehicleController extends BaseApiController
|
||||
}
|
||||
}
|
||||
|
||||
public function getCompanyCars():Json {
|
||||
$params = $this->request->get(['company_id','company_type']);
|
||||
if(empty($params['company_id']) || empty($params['company_type'])){
|
||||
return $this->fail('缺少必要参数');
|
||||
}
|
||||
$result = VehicleLogic::getCompanyCars($params);
|
||||
//返回数据
|
||||
if($result['code'] == 1){
|
||||
return $this->success($result['msg'],$result['data']);
|
||||
}else{
|
||||
return $this->fail($result['msg']);
|
||||
}
|
||||
}
|
||||
|
||||
//获取车辆当前位置
|
||||
public function getCarLocal():Json {
|
||||
$params = $this->request->get(['car_id']);
|
||||
|
@ -17,12 +17,9 @@ namespace app\api\logic;
|
||||
|
||||
use app\common\logic\BaseLogic;
|
||||
use app\common\logic\GpsLogic;
|
||||
use app\common\model\vehicle\Company;
|
||||
use app\common\model\vehicle\Vehicle;
|
||||
use app\common\model\vehicle\VehicleRent;
|
||||
use app\common\model\vehicle\VehicleRentRecord;
|
||||
use think\facade\Cache;
|
||||
use think\facade\Log;
|
||||
use Exception;
|
||||
|
||||
|
||||
/**
|
||||
@ -32,184 +29,81 @@ use think\facade\Log;
|
||||
*/
|
||||
class VehicleLogic extends BaseLogic
|
||||
{
|
||||
public static function getTaskToken() {
|
||||
//获取缓存数据
|
||||
$task_token = Cache::get('task_token');
|
||||
//验证缓存数据是否存在,如果缓存数据存在则直接返回缓存数据
|
||||
if(!empty($task_token)) return $task_token;
|
||||
|
||||
$getToken = curl_post('https://worker-task.lihaink.cn/adminapi/login/account',[],['account'=>'admin','password'=>'123456','terminal'=>1]);
|
||||
if($getToken['code'] == 0){
|
||||
return '';
|
||||
}
|
||||
//设置缓存数据
|
||||
Cache::set('task_token', $getToken['data']['token'], 86400);
|
||||
return $getToken['data']['token'];
|
||||
}
|
||||
|
||||
public static function companyInfo():array {
|
||||
$data = Company::field('id,company_name')->where('company_name',30)->select();
|
||||
if($data){
|
||||
return ['code'=>1,'msg'=>'请求成功','data'=>$data->toArray()];
|
||||
}else{
|
||||
return ['code'=>0,'msg'=>'请求失败'];
|
||||
}
|
||||
}
|
||||
|
||||
public static function rentRecord($params):array {
|
||||
//获取数据
|
||||
if($params['rent_type'] ==1){
|
||||
$cars = Vehicle::field('id')->where('status',0)->where('type',0)->limit($params['num'])->select();
|
||||
if(!$cars || $cars->count() < $params['num']){
|
||||
public static function checkNum($num):array {
|
||||
try {
|
||||
//获取数据
|
||||
$cars = Vehicle::field('id,company_id')->where('status',0)->where('type',0)->limit($num)->select();
|
||||
//验证数据
|
||||
if($cars->count() < $num){
|
||||
return ['code'=>0,'msg'=>'车辆数量不足'];
|
||||
}
|
||||
$ids = array_column($cars->toArray(),'id');
|
||||
$ids = implode(',',$ids);
|
||||
}else{
|
||||
$ids = $params['car_id'];
|
||||
}
|
||||
//发起合同
|
||||
VehicleRentRecord::startTrans();
|
||||
try {
|
||||
//发起合同
|
||||
$setContract = curl_post('https://worker-task.lihaink.cn/adminapi/company/initiate_contract',[
|
||||
'token:'.self::getTaskToken()
|
||||
],[
|
||||
'id' => $params['party_b'],
|
||||
'party_a' => $params['party_a'],
|
||||
'contract_type' => 29,
|
||||
]);
|
||||
if($setContract['code'] == 0){
|
||||
return ['code'=>0,'msg'=>$setContract['msg']];
|
||||
}
|
||||
//写入数据
|
||||
$res = VehicleRentRecord::create([
|
||||
'car_ids' => $ids,
|
||||
'contract_id' => $setContract['data']['id'],
|
||||
'party_a' => $params['party_a'],
|
||||
'party_b' => $params['party_b'],
|
||||
'num' => $params['num'],
|
||||
'rent_type' => $params['rent_type'],
|
||||
'status' => 1,
|
||||
'create_time' => time(),
|
||||
'update_time' => time(),
|
||||
]);
|
||||
VehicleRentRecord::commit();
|
||||
return ['code'=>1,'msg'=>'发起成功,等待平台风控部上传合同'];
|
||||
}catch(\Exception $e) {
|
||||
VehicleRentRecord::rollback();
|
||||
$ids = implode(',',array_column($cars->toArray(),'id'));
|
||||
$company_id = array_column($cars->toArray(),'company_id')[0];
|
||||
return ['code'=>1,'msg'=>'车辆数量充足','data'=>['car_ids'=>$ids,'company_id'=>$company_id]];
|
||||
}catch (Exception $e) {
|
||||
return ['code'=>0,'msg'=>$e->getMessage()];
|
||||
}
|
||||
}
|
||||
|
||||
public static function rendRecordInfo($contract_id):array {
|
||||
$record = VehicleRentRecord::field('car_ids,num,create_time')->where('contract_id',$contract_id)->find();
|
||||
if(!$record){
|
||||
return ['code'=>0,'msg'=>'数据不存在'];
|
||||
}
|
||||
$cars = Vehicle::field('license')->where('id','in',$record['car_ids'])->select();
|
||||
if(!$cars){
|
||||
return ['code'=>0,'msg'=>'车辆不存在'];
|
||||
}
|
||||
$car_license = array_column($cars->toArray(),'license');
|
||||
$data = [
|
||||
'num' => $record['num'],
|
||||
'car_license' => $car_license,
|
||||
'create_time' => $record['create_time']
|
||||
];
|
||||
return ['code'=>1,'msg'=>'请求成功','data'=>$data];
|
||||
}
|
||||
|
||||
public static function rendRecordEdit($contract_id):array {
|
||||
$record = VehicleRentRecord::where('contract_id',$contract_id)->where('status',1)->find();
|
||||
if(!$record){
|
||||
return ['code'=>0,'msg'=>'数据不存在'];
|
||||
}
|
||||
VehicleRentRecord::startTrans();
|
||||
public static function setRent($data):array {
|
||||
VehicleRent::startTrans();
|
||||
try {
|
||||
if($record['rent_type'] == 1){
|
||||
//获取承租公司信息
|
||||
$company = Company::field('id,user_id,company_name,master_name,master_phone')->where('id',$record['party_b'])->find();
|
||||
if($data['rent_type'] == 1){
|
||||
//设置数据
|
||||
$ids = explode(',',$record['car_ids']);
|
||||
$ids = explode(',',$data['car_id']);
|
||||
$data = [];
|
||||
foreach($ids as $v){
|
||||
$car = Vehicle::where('id',$v)->find();
|
||||
if($car){
|
||||
$data[] = [
|
||||
'car_id' => $v,
|
||||
'lessee_one_contract_id' => $contract_id,
|
||||
'lessee_one_company_id' => $company['id'],
|
||||
'lessee_one_company' => $company['company_name'],
|
||||
'lessee_one_user_id' => $company['user_id'],
|
||||
'lessee_one_user' => $company['master_name'],
|
||||
'lessee_one_phone' => $company['master_phone'],
|
||||
'lessee_one_start_time' => strtotime($record['create_time']),
|
||||
'lessee_one_contract_id' => $data['contract_id'],
|
||||
'lessee_one_company_id' => $data['company_id'],
|
||||
'lessee_one_company' => $data['company_name'],
|
||||
'lessee_one_user_id' => $data['company_user_id'],
|
||||
'lessee_one_user' => $data['company_user_name'],
|
||||
'lessee_one_phone' => $data['company_user_phone'],
|
||||
'lessee_one_start_time' => time(),
|
||||
'lessee_one_flag' => 1,
|
||||
];
|
||||
}
|
||||
}
|
||||
(new VehicleRent()) -> saveAll($data);
|
||||
$res = (new VehicleRent()) -> saveAll($data);
|
||||
//更新车辆列表中的车辆状态
|
||||
Vehicle::where('id','in',$ids)->update(['status'=>1]);
|
||||
}else{
|
||||
}elseif($data['rent_type'] == 2){
|
||||
//获取数据
|
||||
$rent = VehicleRent::where('car_id',$record['car_ids'])->where('lessee_one_company_id',$record['party_a'])->find();
|
||||
if(!$rent){
|
||||
return ['code'=>0,'msg'=>'未找到车辆信息'];
|
||||
}
|
||||
//获取第二承租方公司信息
|
||||
$company = Company::field('id,user_id,company_name,master_name,master_phone')->where('id',$record['party_b'])->find();
|
||||
//验证公司信息
|
||||
if(!$company){
|
||||
return ['code'=>0,'msg'=>'未找到承租公司'];
|
||||
}
|
||||
$rent = VehicleRent::where('car_id',$data['car_id'])->find();
|
||||
//设置数据
|
||||
$data = [
|
||||
'id' => $rent['id'],
|
||||
'lessee_two_contract_id' => $record['contract_id'],
|
||||
'lessee_two_company_id' => $company['id'],
|
||||
'lessee_two_company_name' => $company['company_name'],
|
||||
'lessee_two_user_id' => $company['user_id'],
|
||||
'lessee_two_user' => $company['master_name'],
|
||||
'lessee_two_phone' => $company['master_phone'],
|
||||
'lessee_two_start_time' => strtotime($record['create_time']),
|
||||
'lessee_two_contract_id' => $data['contract_id'],
|
||||
'lessee_two_company_id' => $data['company_id'],
|
||||
'lessee_two_company_name' => $data['company_name'],
|
||||
'lessee_two_user_id' => $data['company_user_id'],
|
||||
'lessee_two_user' => $data['company_user_name'],
|
||||
'lessee_two_phone' => $data['company_user_phone'],
|
||||
'lessee_two_start_time' => time(),
|
||||
'lessee_two_flag' => 1,
|
||||
];
|
||||
//写入数据
|
||||
VehicleRent::update($data);
|
||||
$res = VehicleRent::update($data);
|
||||
}else{
|
||||
return ['code'=>0,'msg'=>'操作失败'];
|
||||
}
|
||||
$res = curl_post('https://worker-task.lihaink.cn/v1/notify_property',[
|
||||
'token:'.self::getTaskToken(),
|
||||
'authority:worker-task.lihaink.cn',
|
||||
],[
|
||||
'company_id'=> $record['party_b'],
|
||||
'object_id' => $record['car_ids']
|
||||
]);
|
||||
Log::write($res);
|
||||
VehicleRentRecord::where('id',$record['id'])->update(['status'=>2,'update_time'=>time()]);
|
||||
VehicleRentRecord::commit();
|
||||
return ['code'=>1,'msg'=>'操作成功'];
|
||||
}catch(\Exception $e) {
|
||||
VehicleRentRecord::rollback();
|
||||
if($res){
|
||||
VehicleRent::commit();
|
||||
return ['code'=>1,'msg'=>'操作成功'];
|
||||
}else{
|
||||
VehicleRent::rollback();
|
||||
return ['code'=>0,'msg'=>'操作失败'];
|
||||
}
|
||||
}catch(Exception $e) {
|
||||
VehicleRent::rollback();
|
||||
return ['code'=>0,'msg'=>$e->getMessage()];
|
||||
}
|
||||
}
|
||||
|
||||
public static function getCompanyCars($params):array {
|
||||
$company_str = $params['company_type'] == 1 ? 'lessee_one_company_id' : 'lessee_two_company_id';
|
||||
$data = VehicleRent::where($company_str,$params['company_id'])->select()->each(function($item){
|
||||
$car = Vehicle::field('license')->where('id',$item['car_id'])->find();
|
||||
$item['license'] = $car['license'];
|
||||
return $item;
|
||||
});
|
||||
if($data){
|
||||
return ['code'=>1,'msg'=>'请求成功','data'=>$data->toArray()];
|
||||
}else{
|
||||
return ['code'=>1,'msg'=>'请求失败'];
|
||||
}
|
||||
}
|
||||
|
||||
public static function getCarLocal($params):array {
|
||||
$car = Vehicle::field('gps_carid')->where('id',$params['car_id'])->find();
|
||||
if(!$car){
|
||||
|
@ -25,10 +25,9 @@ Route::rule('hasCourier','Logistics/hasCourier','get');
|
||||
|
||||
|
||||
/*-------------------------------------------------------------------------------------------*/
|
||||
Route::rule('getCompany','Vehicle/getCompany','get');
|
||||
Route::rule('setContract','Vehicle/setContract','post');
|
||||
Route::rule('getRentRecord','Vehicle/getRentRecord','get');
|
||||
Route::rule('updateRentRecord','Vehicle/updateRentRecord','post');
|
||||
Route::rule('getCompanyCars','Vehicle/getCompanyCars','get');
|
||||
Route::rule('vehicleRent','Vehicle/vehicleRent','post');
|
||||
Route::rule('getCarLocal','Vehicle/getCarLocal','get');
|
||||
Route::rule('getCarHistory','Vehicle/getCarHistory','get');
|
||||
Route::rule('getCarHistory','Vehicle/getCarHistory','get');
|
||||
|
||||
|
||||
Route::rule('checkNum','Vehicle/checkNum','get');
|
Loading…
x
Reference in New Issue
Block a user