201 lines
9.2 KiB
PHP
201 lines
9.2 KiB
PHP
<?php
|
|
namespace app\adminapi\logic\vehicle;
|
|
|
|
use app\common\logic\GpsLogic;
|
|
use app\common\model\contract\Contract;
|
|
use app\common\model\logistics\Logistics;
|
|
use app\common\model\logistics\Product;
|
|
use app\common\model\vehicle\Vehicle;
|
|
use app\common\logic\BaseLogic;
|
|
use app\common\model\vehicle\VehicleRent;
|
|
use think\facade\Db;
|
|
|
|
|
|
/**
|
|
* Vehicle逻辑
|
|
* Class VehicleLogic
|
|
* @package app\adminapi\logic
|
|
*/
|
|
class VehicleLogic extends BaseLogic
|
|
{
|
|
/**
|
|
* @notes 添加
|
|
* @param array $params
|
|
* @return bool
|
|
* @author likeadmin
|
|
* @date 2023/08/17 09:23
|
|
*/
|
|
public static function add(array $params): bool
|
|
{
|
|
Db::startTrans();
|
|
try {
|
|
Vehicle::create([
|
|
'license' => $params['license'],
|
|
'gps_imei' => $params['gps_imei'],
|
|
'type' => 0,
|
|
'status' => 0,
|
|
]);
|
|
Db::commit();
|
|
return true;
|
|
} catch (\Exception $e) {
|
|
Db::rollback();
|
|
self::setError($e->getMessage());
|
|
return false;
|
|
}
|
|
}
|
|
|
|
|
|
/**
|
|
* @notes 编辑
|
|
* @param array $params
|
|
* @return bool
|
|
* @author likeadmin
|
|
* @date 2023/08/17 09:23
|
|
*/
|
|
public static function edit(array $params): bool
|
|
{
|
|
Db::startTrans();
|
|
try {
|
|
Vehicle::where('id', $params['id'])->update([
|
|
'license' => $params['license'],
|
|
'gps_imei' => $params['gps_imei'],
|
|
'type' => $params['type'],
|
|
'status' => $params['status'],
|
|
]);
|
|
Db::commit();
|
|
return true;
|
|
} catch (\Exception $e) {
|
|
Db::rollback();
|
|
self::setError($e->getMessage());
|
|
return false;
|
|
}
|
|
}
|
|
|
|
/**
|
|
* @notes 获取详情
|
|
* @param $params
|
|
* @return array
|
|
* @author likeadmin
|
|
* @date 2023/08/17 09:23
|
|
*/
|
|
public static function detail($params): array
|
|
{
|
|
$data = Vehicle::findOrEmpty($params['id']);
|
|
$data['mileage'] = 0;
|
|
//获取车辆运行轨迹
|
|
$gpsCarInfo = (new GpsLogic()) -> info($data['gps_imei']);
|
|
if($gpsCarInfo['code'] == 0){
|
|
$data['track_info'] = [];
|
|
}else{
|
|
//获取车辆行驶历史信息
|
|
$gpsCarHistory = (new GpsLogic()) -> history([
|
|
'gps_car_id' => $gpsCarInfo['data']['carId'],
|
|
'start_time' => date('Y-m-d').' 00:00:00',
|
|
'end_time' => date('Y-m-d').' 23:59:59'
|
|
]);
|
|
if($gpsCarHistory['code'] == 0){
|
|
$data['track_info'] = [];
|
|
}else{
|
|
$position = [];
|
|
foreach($gpsCarHistory['data'] as $k=> $v){
|
|
$position[$k]['latitude'] = $v['latc'];
|
|
$position[$k]['longitude'] = $v['lonc'];
|
|
}
|
|
$data['track_info'] = $position;
|
|
}
|
|
}
|
|
//获取车辆使用情况
|
|
$travel = (new GpsLogic()) -> mileage($gpsCarInfo['data']['carId'],date('Y-m-d H:i:s',(time() - 604800)),date('Y-m-d').' 23:59:59');
|
|
$data['travel_info'] = $travel['data'];
|
|
if($data['status'] == 2){
|
|
//获取租赁信息
|
|
$rentInfo = VehicleRent::where('car_id',$data['id'])->find();
|
|
$contract = Contract::where('id',$rentInfo['contract_id'])->find();
|
|
$rentInfo['contract_file'] = $contract['file'];
|
|
$data['rent_info'] = !empty($rentInfo) ? $rentInfo : [];
|
|
//获取物流信息
|
|
$logistic = Logistics::field('order_id,order_sn,shop_name,user_name,receiver_address,qh_time,ps_time')->where('courier_id',$rentInfo['use_user_id'])->where('status',1)->limit(10)->select()->each(function($item){
|
|
$qhTime = !empty($item['qh_time']) ? strtotime($item['qh_time']) : time();
|
|
$diffTime = timeDiff($qhTime,time());
|
|
$item['diff_time'] = $diffTime['day'].'天'.$diffTime['hour'].'小时'.$diffTime['min'].'分钟'.$diffTime['sec'].'秒';
|
|
$product_count = 0;
|
|
//获取产品信息
|
|
$item['products'] = Product::field('product_num,cart_info')->where('order_id', $item['order_id'])->select()->each(function($pro_item) use(&$product_count){
|
|
$pro_item['cart_info'] = json_decode($pro_item['cart_info'], true);
|
|
$pro_item['goods_name'] = $pro_item['cart_info']['product']['store_name'];
|
|
$pro_item['goods_unit'] = $pro_item['cart_info']['product']['unit_name'];
|
|
$product_count += $pro_item['product_num'];
|
|
unset($pro_item['cart_info']);
|
|
return $pro_item;
|
|
});
|
|
$item['product_count'] = $product_count;
|
|
});
|
|
$data['logistic_info'] = !empty($logistic) ? $logistic : [];
|
|
}
|
|
return $data->toArray();
|
|
}
|
|
|
|
public static function vehicleLogisticLists($params){
|
|
$info = Vehicle::field('id,license,status,type')->findOrEmpty($params['id'])->toArray();
|
|
$rent = VehicleRent::field('use_user_id,use_user_name,use_user_phone')->where('car_id',$info['id'])->findOrEmpty()->toArray();
|
|
$data = array_merge($info,$rent);
|
|
$data['total_package'] = Logistics::field('order_id')->where('courier_id',$data['use_user_id'])->where('status',1)->count();
|
|
$goodsTotal = 0;
|
|
Logistics::field('order_id')->where('courier_id',$data['use_user_id'])->where('status',1)->select()->each(function($item)use(&$goodsTotal){
|
|
$count = 0;
|
|
Product::field('product_num,cart_info')->where('order_id', $item['order_id'])->select()->each(function($pro_item)use(&$count){
|
|
$pro_item['cart_info'] = json_decode($pro_item['cart_info'], true);
|
|
$pro_item['goods_name'] = $pro_item['cart_info']['product']['store_name'];
|
|
$pro_item['goods_unit'] = $pro_item['cart_info']['product']['unit_name'];
|
|
$count += $pro_item['product_num'];
|
|
unset($pro_item['cart_info']);
|
|
})->toArray();
|
|
$case['count'] = $count;
|
|
$goodsTotal += $case['count'];
|
|
});
|
|
$data['total_goods'] = $goodsTotal;
|
|
$pageNo = isset($params['page_no']) ? $params['page_no'] : 1;
|
|
$pageSize = isset($params['page_size']) ? $params['page_size'] : 15;
|
|
//获取物流信息
|
|
$logistic = Logistics::field('order_id,order_sn,shop_name,user_name,receiver_address,qh_time,ps_time')->where('courier_id',$data['use_user_id'])->where('status',1)->page($pageNo,$pageSize)->order('qh_time desc')->select()->each(function($item){
|
|
$qhTime = !empty($item['qh_time']) ? strtotime($item['qh_time']) : time();
|
|
$diffTime = timeDiff($qhTime,time());
|
|
$item['diff_time'] = $diffTime['day'].'天'.$diffTime['hour'].'小时'.$diffTime['min'].'分钟'.$diffTime['sec'].'秒';
|
|
$product_count = 0;
|
|
//获取产品信息
|
|
$item['products'] = Product::field('product_num,cart_info')->where('order_id', $item['order_id'])->select()->each(function($pro_item) use(&$product_count){
|
|
$pro_item['cart_info'] = json_decode($pro_item['cart_info'], true);
|
|
$pro_item['goods_name'] = $pro_item['cart_info']['product']['store_name'];
|
|
$pro_item['goods_unit'] = $pro_item['cart_info']['product']['unit_name'];
|
|
$product_count += $pro_item['product_num'];
|
|
unset($pro_item['cart_info']);
|
|
return $pro_item;
|
|
});
|
|
$item['product_count'] = $product_count;
|
|
})->toArray();
|
|
return ['basic'=>$data,'lists'=>$logistic,'count'=>$data['total_package'],'page_no'=>$pageNo,'page_size'=>$pageSize];
|
|
}
|
|
|
|
public static function vehicleLogisticDetail($params){
|
|
$info = Vehicle::field('id,license,status,type')->findOrEmpty($params['car_id'])->toArray();
|
|
$rent = VehicleRent::field('use_user_id,use_user_name,use_user_phone')->where('car_id',$info['id'])->findOrEmpty()->toArray();
|
|
$logistic = Logistics::field('order_sn,user_name,receiver_address,shop_name,shop_address,create_time,qh_time')->where('order_id',$params['order_id'])->findOrEmpty()->toArray();
|
|
$qhTime = !empty($logistic['qh_time']) ? strtotime($logistic['qh_time']) : time();
|
|
$diffTime = timeDiff($qhTime,time());
|
|
$logistic['diff_time'] = $diffTime['day'].'天'.$diffTime['hour'].'小时'.$diffTime['min'].'分钟'.$diffTime['sec'].'秒';
|
|
$basic = array_merge($info,$rent,$logistic);
|
|
|
|
//获取商品
|
|
$goods = Product::field('product_num,cart_info')->where('order_id', $params['order_id'])->select()->each(function($pro_item){
|
|
$pro_item['cart_info'] = json_decode($pro_item['cart_info'], true);
|
|
$pro_item['goods_name'] = $pro_item['cart_info']['product']['store_name'];
|
|
$pro_item['goods_unit'] = $pro_item['cart_info']['product']['unit_name'];
|
|
$pro_item['goods_image'] = $pro_item['cart_info']['product']['image'];
|
|
$pro_item['goods_price'] = $pro_item['cart_info']['product']['price'];
|
|
unset($pro_item['cart_info']);
|
|
return $pro_item;
|
|
});
|
|
|
|
return ['basic'=>$basic,'goods'=>$goods];
|
|
}
|
|
} |