logistics/app/api/controller/VehicleController.php
2023-09-06 09:47:43 +08:00

215 lines
8.0 KiB
PHP

<?php
namespace app\api\controller;
use app\api\logic\VehicleLogic;
use app\common\model\contract\Contract;
use app\common\model\vehicle\Vehicle;
use app\common\model\vehicle\VehicleRent;
use think\facade\Db;
use think\response\Json;
/**
* 车辆管理
* Class VehicleController
* @package app\api\controller
*/
class VehicleController extends BaseApiController
{
public array $notNeedLogin = ['getCarLocal','getCarHistory','addSelfCar','updateVehicleRent','cancelRent','contractUpdate'];
//获取车辆当前位置
public function getCarLocal():Json {
$params = $this->request->get(['car_id']);
if(empty($params['car_id'])){
return $this->fail('缺少必要参数');
}
$result = VehicleLogic::getCarLocal($params);
//返回数据
if($result['code'] == 1){
return $this->success($result['msg'],$result['data']);
}else{
return $this->fail($result['msg']);
}
}
public function getCarHistory():Json {
//获取参数
$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('缺少必要参数');
}
//验证时间格式
if(!checkDateIsValid($params['start_time'],['Y-m-d H:i:s']) || !checkDateIsValid($params['end_time'],['Y-m-d H:i:s'])) {
return $this->fail('日期格式错误');
}
//获取数据
$result = VehicleLogic::getCarHistory($params);
//返回数据
if($result['code'] == 1){
return $this->success($result['msg'],$result['data']);
}else{
return $this->fail($result['msg']);
}
}
//添加自有车辆
public function addSelfCar() {
//获取参数
$params = $this->request->post(['license','company_id','company_name','company_user','company_phone']);
if(empty($params['license']) || empty($params['company_id']) || empty($params['company_name']) || empty($params['company_user']) || empty($params['company_phone'])){
return $this->fail('缺少必要参数');
}
Db::startTrans();
try {
//获取数据
$vehicle = Vehicle::where('license',$params['license'])->find();
if(!empty($vehicle)){
return $this->fail('该车牌号的车辆已存在');
}
//写入数据
$vehicle = Vehicle::create([
'license' => $params['license'],
'gps_imei' => '',
'type' => 1,
'status' => 2,
'create_time' => time(),
'update_time' => time(),
]);
$vehicleRent = VehicleRent::create([
'car_id'=> $vehicle->id,
'contract_id' => 0,
'company_id' => $params['company_id'],
'company_name' => $params['company_name'],
'company_user' => $params['company_user'],
'company_phone' => $params['company_phone'],
'create_time' => time()
]);
if($vehicle->id && $vehicleRent->id){
Db::commit();
return $this->success('success',['car_id'=>$vehicle->id]);
}else{
Db::rollback();
return $this->fail('error');
}
}catch (\Exception $e) {
Db::rollback();
return $this->fail($e->getMessage());
}
}
public function updateVehicleRent(){
//获取参数
$params = $this->request->post(['car_id','use_user_id','use_user_name','use_user_phone']);
if(empty($params['car_id']) || empty($params['use_user_id']) || empty($params['use_user_name']) || empty($params['use_user_phone'])){
return $this->fail('参数错误');
}
//更新
$vehicleRent = VehicleRent::where('car_id',$params['car_id'])->find();
if(empty($vehicleRent)){
return $this->fail('数据错误');
}
$result = VehicleRent::where('car_id',$params['car_id'])->update([
'use_user_id' => $params['use_user_id'],
'use_user_name' => $params['use_user_name'],
'use_user_phone' => $params['use_user_phone'],
]);
if($result){
return $this->success('请求成功');
}else{
return $this->fail('失败');
}
}
public function cancelRent() {
//获取参数
$params = $this->request->post(['car_id','status']);
if(empty($params['car_id']) || empty($params['status'])){
return $this->fail('参数错误');
}
//更新
$vehicleRent = VehicleRent::where('car_id',$params['car_id'])->find();
if(empty($vehicleRent)){
return $this->fail('数据错误');
}
$result1 = VehicleRent::where('car_id',$params['car_id'])->update([
'status' => $params['status'],
]);
$result2 = Vehicle::where('id',$params['car_id'])->update([
'status' => 3,
]);
if($result1 && $result2){
return $this->success('请求成功');
}else{
return $this->fail('失败');
}
}
public function contractUpdate() {
//获取参数
$params = $this->request->post();
if(!isset($params['id']) || empty($params['id'])) {
return $this->fail('缺少必要参数');
}
$contract = Contract::where('id',$params['id'])->find();
if(empty($contract)) {
return $this->fail('未找到相应数据');
}
if(isset($params['signing_timer']) && isset($params['status']) && $params['signing_timer'] == 2 && $params['status'] == 3){
//获取签约后的合同
$signContractFile = app(JunziqianController::class)->download_file($contract['contract_no']);
$params['contract_url'] = $signContractFile ?? '';
//获取租赁车辆
$cars = json_decode($contract['cars_info'], true);
$data = [];
foreach($cars as $v){
$data[] = [
'car_id' => $v['id'],
'contract_id' => $contract['id'],
'company_id' => $contract['company_b_id'],
'company_name' => $contract['company_b_name'],
'company_user' => $contract['company_b_user'],
'company_phone' => $contract['company_b_phone'],
'create_time' => time(),
'status' => 0,
'use_user_id' => 0,
'use_user_name' => '',
'use_user_phone' => ''
];
}
try{
//更新合同状态
$update_result = $contract->where('id', $contract['id'])->save($params);
//更新本地车辆状态
$vehicle_result = Vehicle::where('id','in',array_column($cars,'id'))->update(['status'=>2]);
//添加车辆到租赁列表
$create_result = (new VehicleRent()) -> saveAll($data);
if($update_result && $vehicle_result && $create_result){
Db::commit();
return $this->success('更新成功1');
}else{
Db::rollback();
return $this->fail('更新失败1');
}
}catch (\Exception){
Db::rollback();
return $this->fail('更新失败1.1');
}
}
try{
$update_result = $contract->where('id', $contract['id'])->save($params);
if($update_result){
Db::commit();
return $this->success('更新成功2');
}else{
Db::rollback();
return $this->fail('更新失败2');
}
}catch (\Exception){
Db::rollback();
return $this->fail('更新失败2.1');
}
}
}