logistics/app/api/logic/VehicleLogic.php
2023-08-30 17:27:05 +08:00

286 lines
12 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<?php
// +----------------------------------------------------------------------
// | likeadmin快速开发前后端分离管理后台PHP版
// +----------------------------------------------------------------------
// | 欢迎阅读学习系统程序代码,建议反馈是我们前进的动力
// | 开源版本可自由商用可去除界面版权logo
// | gitee下载https://gitee.com/likeshop_gitee/likeadmin
// | github下载https://github.com/likeshop-github/likeadmin
// | 访问官网https://www.likeadmin.cn
// | likeadmin团队 版权所有 拥有最终解释权
// +----------------------------------------------------------------------
// | author: likeadminTeam
// +----------------------------------------------------------------------
namespace app\api\logic;
use app\common\logic\BaseLogic;
use app\common\logic\GpsLogic;
use app\common\model\vehicle\Vehicle;
use app\common\model\vehicle\VehicleRent;
use Exception;
/**
* 车辆逻辑
* Class VehicleLogic
* @package app\api\logic
*/
class VehicleLogic extends BaseLogic
{
public static function checkNum($num):array {
try {
//获取数据
$cars = Vehicle::field('id,company_id')->where('status',1)->where('type',1)->limit($num)->select();
//验证数据
if($cars->count() < $num){
return ['code'=>0,'msg'=>'车辆数量不足'];
}
$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 setRent($param):array {
VehicleRent::startTrans();
try {
if($param['rent_type'] == 1){
//设置数据
$ids = explode(',',$param['car_id']);
$data = [];
foreach($ids as $v){
$car = Vehicle::where('id',$v)->find();
$rent = VehicleRent::where('car_id',$v)->find();
if($car && !$rent){
$data[] = [
'car_id' => $v,
'lessee_one_contract_id' => $param['contract_id'],
'lessee_one_company_id' => $param['company_id'],
'lessee_one_company' => $param['company_name'],
'lessee_one_user_id' => $param['company_user_id'],
'lessee_one_user' => $param['company_user_name'],
'lessee_one_phone' => $param['company_user_phone'],
'lessee_one_start_time' => time(),
'lessee_one_flag' => 1,
];
}
}
$res = (new VehicleRent()) -> saveAll($data);
//更新车辆列表中的车辆状态
Vehicle::where('id','in',$ids)->update(['status'=>2]);
}elseif($param['rent_type'] == 2){
//获取数据
$rent = VehicleRent::where('car_id',$param['car_id'])->find();
//设置数据
$data = [
'id' => $rent['id'],
'lessee_two_contract_id' => $param['contract_id'],
'lessee_two_company_id' => $param['company_id'],
'lessee_two_company_name' => $param['company_name'],
'lessee_two_user_id' => $param['company_user_id'],
'lessee_two_user' => $param['company_user_name'],
'lessee_two_phone' => $param['company_user_phone'],
'lessee_two_start_time' => time(),
'lessee_two_flag' => 1,
];
//写入数据
$res = VehicleRent::update($data);
}else{
return ['code'=>0,'msg'=>'操作失败'];
}
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 companyCarList($params):array {
if($params['is_rent'] == 1){
$lessee_two_flag = 0;
}else{
$lessee_two_flag = 1;
}
$pageNo = $params['page_no'] ?? 1;
$pageSize = $params['page_size'] ?? 10;
$license = $params['license'] ?? '';
$companyName = $params['company_name'] ?? '';
$rent = VehicleRent::alias('r')
->leftJoin('vehicle v','r.car_id = v.id')
->field('r.*,v.license,v.type')
->where('r.lessee_one_company_id',$params['company_id'])
->where('r.lessee_two_flag',$lessee_two_flag)
->where('v.license','like','%'.$license.'%')
->where('r.lessee_two_company_name','like','%'.$companyName.'%')
->page($pageNo,$pageSize)->order('r.lessee_one_start_time desc,r.car_id desc')->select()->each(function ($item){
//todo
$item['mileage'] = 10.0;
return $item;
});
if($rent){
return ['code'=>1,'msg'=>'请求成功','data'=>$rent->toArray()];
}else{
return ['code'=>0,'msg'=>'请求失败'];
}
}
public static function companyCar($company_id):array {
//查询是否有自有车辆
$selfCar = Vehicle::field('id,company_id,license,type,gps_carid')->where('company_id',$company_id)->where('type',2)->find();
if($selfCar){
$data['id'] = $selfCar['id'];
$data['company_id'] = $selfCar['company_id'];
$data['license'] = $selfCar['license'];
$data['type'] = $selfCar['type'];
//todo 获取里程
$data['mileage'] = 10;
return ['code'=>1,'msg'=>'请求成功','data'=>$data];
}else{
//租赁车辆
$rent = VehicleRent::field('lessee_one_company,lessee_one_user,lessee_one_phone,lessee_one_start_time,lessee_two_company_id,lessee_two_user,lessee_two_phone,lessee_two_start_time,lessee_two_flag')->where('lessee_two_company_id',$company_id)->find();
$info = Vehicle::field('id,license,type,gps_carid')->where('id',$rent['car_id'])->find();
if($rent && $info){
$data['lessee_one_company'] = $rent['lessee_one_company'];
$data['lessee_one_user'] = $rent['lessee_one_user'];
$data['lessee_one_phone'] = $rent['lessee_one_phone'];
$data['lessee_one_start_time'] = $rent['lessee_one_start_time'];
$data['lessee_two_company_id'] = $rent['lessee_two_company_id'];
$data['lessee_two_company_name'] = $rent['lessee_two_company_name'];
$data['lessee_two_user'] = $rent['lessee_two_user'];
$data['lessee_two_phone'] = $rent['lessee_two_phone'];
$data['lessee_two_start_time'] = $rent['lessee_two_start_time'];
$data['lessee_two_flag'] = $rent['lessee_two_flag'];
$data['id'] = $info['id'];
$data['license'] = $info['license'];
$data['type'] = $info['type'];
//todo 获取里程
$data['mileage'] = 10;
return ['code'=>1,'msg'=>'请求成功','data'=>$data];
}else{
return ['code'=>0,'msg'=>'请求失败'];
}
}
}
public static function addVehicle($params):array {
//获取车辆
$vehicle = Vehicle::where('company_id',$params['company_id'])->where('license',$params['license'])->find();
if($vehicle){
return ['code'=>0,'msg'=>'该公司已添加车辆'];
}
//创建
$result = Vehicle::create([
'company_id' => $params['company_id'],
'license' => $params['license'],
'gps_imei' => '',
'gps_carid' => 0,
'type' => 2,
'status' => 1,
'is_check' => 1,
'is_del' => 1,
'create_time' => time(),
'update_time' => time(),
]);
if($result){
return ['code'=>1,'msg'=>'添加成功,等待审核'];
}else{
return ['code'=>1,'msg'=>'添加失败'];
}
}
public static function carInfo($params):array {
//判断车辆类型
if($params['car_type'] == 1){
//租赁车辆
$rent = VehicleRent::field('lessee_one_company,lessee_one_user,lessee_one_phone,lessee_one_start_time,lessee_two_company_id,lessee_two_user,lessee_two_phone,lessee_two_start_time,lessee_two_flag')->where('car_id',$params['car_id'])->find();
$info = Vehicle::field('id,license,type,gps_carid')->where('id',$params['car_id'])->find();
if($rent && $info){
$data['lessee_one_company'] = $rent['lessee_one_company'];
$data['lessee_one_user'] = $rent['lessee_one_user'];
$data['lessee_one_phone'] = $rent['lessee_one_phone'];
$data['lessee_one_start_time'] = $rent['lessee_one_start_time'];
$data['lessee_two_company_id'] = $rent['lessee_two_company_id'];
$data['lessee_two_company_name'] = $rent['lessee_two_company_name'];
$data['lessee_two_user'] = $rent['lessee_two_user'];
$data['lessee_two_phone'] = $rent['lessee_two_phone'];
$data['lessee_two_start_time'] = $rent['lessee_two_start_time'];
$data['lessee_two_flag'] = $rent['lessee_two_flag'];
$data['id'] = $info['id'];
$data['license'] = $info['license'];
$data['type'] = $info['type'];
//todo 获取里程
$data['mileage'] = 10;
return ['code'=>1,'msg'=>'请求成功','data'=>$data];
}else{
return ['code'=>0,'msg'=>'请求失败'];
}
}elseif($params['car_type'] == 2){
//自有车辆
$data = Vehicle::field('id,company_id,license,type')->where('id',$params['car_id'])->where('type',2)->find();
//获取车辆当前位置
if($data){
//todo 获取里程
$data['mileage'] = 10;
return ['code'=>1,'msg'=>'请求成功','data'=>$data->toArray()];
}else{
return ['code'=>0,'msg'=>'请求失败'];
}
}else{
return ['code'=>0,'msg'=>'参数错误'];
}
}
public static function getCarLocal($params):array {
$car = Vehicle::field('gps_imei')->where('id',$params['car_id'])->find();
if(!$car){
return ['code'=>0,'msg'=>'车辆不存在'];
}
//获取车辆在gps系统中的id
$gps_car_id = (new GpsLogic()) -> info($car['gps_imei']);
//获取车辆当前位置
$local = (new GpsLogic()) -> status($gps_car_id['carId']);
if($local['code'] == 1){
$data = [
'car_id' => $params['car_id'],
'lat' => $local['data']['carStatus']['latc'],
'lon' => $local['data']['carStatus']['lonc'],
];
return ['code'=>1,'msg'=>'请求成功','data'=>$data];
}else{
return ['code'=>0,'msg'=>$local['msg']];
}
}
public static function getCarHistory($params):array {
//获取车辆信息
$car = Vehicle::where('id',$params['car_id'])->find();
if(empty($car)){
return ['code'=>0,'msg'=>'车辆不存在'];
}
$params['gps_imei'] = $car['gps_imei'];
//获取车辆行驶历史信息
$result = (new GpsLogic()) -> history($params);
//返回数据
if($result['code'] == 1){
$data = [];
foreach ($result['data'] as $k => $v) {
$data[$k]['lat'] = $v['latc'];
$data[$k]['lon'] = $v['lonc'];
}
return ['code'=>1,'msg'=>$result['msg'],'data'=>$data];
}else{
return ['code'=>0,'msg'=>$result['msg']];
}
}
}