126 lines
4.8 KiB
PHP
126 lines
4.8 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\model\vehicle\Company;
|
||
use app\common\model\vehicle\Vehicle;
|
||
use app\common\model\vehicle\VehicleRent;
|
||
|
||
|
||
/**
|
||
* 车辆逻辑
|
||
* Class VehicleLogic
|
||
* @package app\api\logic
|
||
*/
|
||
class VehicleLogic extends BaseLogic
|
||
{
|
||
/*
|
||
* 获取未出租的车辆列表
|
||
* @return array
|
||
*/
|
||
public static function lists($params):array {
|
||
$offset = !empty($params['page_no']) ? $params['page_no'] : 1;
|
||
$length = !empty($params['page_size']) ? $params['page_size'] : 15;
|
||
return Vehicle::field('id,license,gps_imei')->where('status',0)->where('is_del',0)->order('create_time desc')->page($offset, $length)->select()->toArray();
|
||
}
|
||
|
||
/*
|
||
* 批量出租(一级出租)
|
||
* return array
|
||
*/
|
||
public static function oneLevelRent($params):array {
|
||
//获取承租公司信息
|
||
$company = Company::field('id,user_id,company_name,master_name,master_phone')->where('id',$params['company_id'])->find();
|
||
//验证公司信息
|
||
if(!$company){
|
||
return ['code'=>0,'msg'=>'未找到承租公司'];
|
||
}
|
||
//设置数据
|
||
$ids = explode(',',$params['car_ids']);
|
||
$data = [];
|
||
foreach($ids as $v){
|
||
$car = Vehicle::where('id',$v)->find();
|
||
if($car){
|
||
$data[] = [
|
||
'car_id' => $v,
|
||
'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($params['start_time']),
|
||
'lessee_one_end_time' => strtotime($params['end_time']),
|
||
'lessee_one_flag' => 1,
|
||
];
|
||
}
|
||
}
|
||
//写入数据
|
||
VehicleRent::startTrans();
|
||
try {
|
||
(new VehicleRent()) -> saveAll($data);
|
||
VehicleRent::commit();
|
||
return ['code'=>1,'msg'=>'请求成功'];
|
||
}catch (\Exception $e) {
|
||
VehicleRent::rollback();
|
||
return ['code'=>0,'msg'=>$e->getMessage()];
|
||
}
|
||
}
|
||
|
||
/*
|
||
* 单个出租(二级出租)
|
||
*/
|
||
public static function twoLevelRent($params):array {
|
||
//获取数据
|
||
$rent = VehicleRent::where('car_id',$params['car_id'])->where('lessee_one_company_id',$params['lessor_company_id'])->find();
|
||
if(!$rent){
|
||
return ['code'=>0,'msg'=>'未找到车辆信息'];
|
||
}
|
||
//判断租赁时间
|
||
if(strtotime($params['start_time']) < strtotime($rent['lessee_one_start_time']) || strtotime($params['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',$params['tenant_company_id'])->find();
|
||
//验证公司信息
|
||
if(!$company){
|
||
return ['code'=>0,'msg'=>'未找到承租公司'];
|
||
}
|
||
//设置数据
|
||
$data = [
|
||
'id' => $rent['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($params['start_time']),
|
||
'lessee_two_end_time' => strtotime($params['end_time']),
|
||
'lessee_two_flag' => 1,
|
||
];
|
||
//写入数据
|
||
VehicleRent::startTrans();
|
||
try {
|
||
VehicleRent::update($data);
|
||
VehicleRent::commit();
|
||
return ['code'=>1,'msg'=>'请求成功'];
|
||
}catch (\Exception $e) {
|
||
VehicleRent::rollback();
|
||
return ['code'=>0,'msg'=>$e->getMessage()];
|
||
}
|
||
}
|
||
}
|