logistics/app/api/logic/VehicleLogic.php
2023-08-23 18:58:45 +08:00

202 lines
8.9 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\model\vehicle\Company;
use app\common\model\vehicle\Vehicle;
use app\common\model\vehicle\VehicleRent;
use app\common\model\vehicle\VehicleRentRecord;
/**
* 车辆逻辑
* Class VehicleLogic
* @package app\api\logic
*/
class VehicleLogic extends BaseLogic
{
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']){
return ['code'=>0,'msg'=>'车辆数量不足'];
}
$ids = array_column($cars->toArray(),'id');
$ids = implode(',',$ids);
}else{
$ids = $params['car_id'];
}
//发起合同
VehicleRentRecord::startTrans();
try {
//获取token https://worker-task.lihaink.cn
$getToken = curl_post('https://worker-task.lihaink.cn/adminapi/login/account',[],['account'=>'admin','password'=>'123456','terminal'=>1]);
if($getToken['code'] == 0){
return ['code'=>0,'msg'=>'获取token失败'];
}
//发起合同
$setContract = curl_post('https://worker-task.lihaink.cn/adminapi/company/initiate_contract',[
'token:'.$getToken['data']['token']
],[
'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'],
'start_time' => strtotime($params['start_time']),
'end_time' => strtotime($params['end_time']),
'rent_type' => $params['rent_type'],
'status' => 1,
'create_time' => time(),
'update_time' => time(),
]);
VehicleRentRecord::commit();
return ['code'=>1,'msg'=>'发起成功,等待平台风控部上传合同'];
}catch(\Exception $e) {
VehicleRentRecord::rollback();
return ['code'=>0,'msg'=>$e->getMessage()];
}
}
public static function rendRecordInfo($contract_id):array {
$record = VehicleRentRecord::field('car_ids,num,start_time,end_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'],
'start_time' => $record['start_time'],
'end_time' => $record['end_time'],
'car_license' => $car_license
];
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();
try {
if($record['rent_type'] == 1){
//获取承租公司信息
$company = Company::field('id,user_id,company_name,master_name,master_phone')->where('id',$record['party_b'])->find();
//设置数据
$ids = explode(',',$record['car_ids']);
$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['start_time']),
'lessee_one_end_time' => strtotime($record['end_time']),
'lessee_one_flag' => 1,
];
}
}
(new VehicleRent()) -> saveAll($data);
//更新车辆列表中的车辆状态
Vehicle::where('id','in',$ids)->update(['status'=>1]);
}else{
//获取数据
$rent = VehicleRent::where('car_id',$record['car_ids'])->where('lessee_one_company_id',$record['party_a'])->find();
if(!$rent){
return ['code'=>0,'msg'=>'未找到车辆信息'];
}
//判断租赁时间
if(strtotime($record['start_time']) < strtotime($rent['lessee_one_start_time']) || strtotime($record['end_time']) > strtotime($rent['lessee_one_end_time'])){
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'=>'未找到承租公司'];
}
//设置数据
$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['start_time']),
'lessee_two_end_time' => strtotime($record['end_time']),
'lessee_two_flag' => 1,
];
//写入数据
VehicleRent::update($data);
}
VehicleRentRecord::where('id',$record['id'])->update(['status'=>2,'update_time'=>time()]);
VehicleRentRecord::commit();
return ['code'=>1,'msg'=>'操作成功'];
}catch(\Exception $e) {
VehicleRentRecord::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::field('car_id')->where($company_str,$params['company_id'])->select()->each(function($item){
$car = Vehicle::field('license,gps_imei')->where('id',$item['car_id'])->find();
$item['gps_imei'] = $car['gps_imei'];
$item['license'] = $car['license'];
return $item;
});
if($data){
return ['code'=>1,'msg'=>'请求成功','data'=>$data->toArray()];
}else{
return ['code'=>1,'msg'=>'请求失败'];
}
}
}