178 lines
7.1 KiB
PHP
178 lines
7.1 KiB
PHP
<?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',0)->where('type',0)->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'=>1]);
|
||
}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')
|
||
->where('r.lessee_one_company_id',$params['company_id'])
|
||
->where('r.lessee_two_flag',$lessee_two_flag)
|
||
->where('v.license','like','%'.$license.'%')
|
||
->where('v.lessee_two_company_name','like','%'.$companyName.'%')
|
||
->page($pageNo,$pageSize)->order('r.lessee_one_start_time desc,r.car_id desc')->select()->each(function ($item){
|
||
$item['mileage'] = 10.0;
|
||
return $item;
|
||
});
|
||
if($rent){
|
||
return ['code'=>1,'msg'=>'请求成功','data'=>$rent->toArray()];
|
||
}else{
|
||
return ['code'=>0,'msg'=>'请求失败'];
|
||
}
|
||
}
|
||
|
||
public static function getCarLocal($params):array {
|
||
$car = Vehicle::field('gps_carid')->where('id',$params['car_id'])->find();
|
||
if(!$car){
|
||
return ['code'=>0,'msg'=>'车辆不存在'];
|
||
}
|
||
//获取车辆当前位置
|
||
$local = (new GpsLogic()) -> status($car['gps_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'=>'车辆不存在'];
|
||
}else{
|
||
$params['gps_car_id'] = $car['gps_carid'];
|
||
}
|
||
//获取车辆行驶历史信息
|
||
$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']];
|
||
}
|
||
}
|
||
}
|