增加二级租赁接口

This commit is contained in:
unknown 2023-08-23 17:26:44 +08:00
parent b94539d0cd
commit d910d2645d
3 changed files with 117 additions and 176 deletions

View File

@ -11,13 +11,13 @@ use think\response\Json;
*/
class VehicleController extends BaseApiController
{
public array $notNeedLogin = ['getCompany','setContract','getRentRecord','tricycle','multipleRent','singleRent'];
public array $notNeedLogin = ['getCompany','setContract','getRentRecord','updateRentRecord','tricycle','multipleRent','singleRent'];
/*
*获取平台公司接口
*/
public function getCompany():Json {
$result = VehicleLogic::company();
$result = VehicleLogic::companyInfo();
if($result['code'] == 1){
return $this->success($result['msg'],$result['data']);
}else{
@ -26,15 +26,24 @@ class VehicleController extends BaseApiController
}
public function setContract():Json {
$params = $this->request->post(['party_a','party_b','num','start_time','end_time']);
//获取参数
$params = $this->request->post(['party_a','party_b','num','start_time','end_time','rent_type','car_id']);
//验证参数
if(empty($params['party_a']) || empty($params['party_b']) || empty($params['num']) || empty($params['start_time']) || empty($params['end_time'])){
if(empty($params['party_a']) || empty($params['party_b']) || empty($params['num']) || empty($params['start_time']) || empty($params['end_time']) || empty($params['rent_type'])){
return $this->fail('缺少必要的参数');
}
//验证时间格式
if(!checkDateIsValid($params['start_time']) || !checkDateIsValid($params['end_time'])){
return $this->fail('时间格式错误');
}
if(!in_array($params['rent_type'],[1,2])){
return $this->fail('rent_type数据格式错误');
}
if($params['rent_type'] == 2){
if(empty($params['car_id'])){
return $this->fail('缺少必要参数');
}
}
//生成数据
$result = VehicleLogic::rentRecord($params);
//返回数据
@ -52,7 +61,23 @@ class VehicleController extends BaseApiController
return $this->fail('确实必要参数');
}
//获取数据
$result = VehicleLogic::recordInfo($params);
$result = VehicleLogic::rendRecordInfo($params);
//返回数据
if($result['code'] == 1){
return $this->success($result['msg']);
}else{
return $this->fail($result['msg']);
}
}
public function updateRentRecord():Json {
//获取参数
$param = $this->request->post('contract_id');
if(empty($param)){
return $this->fail('缺少必要参数');
}
//获取数据
$result = VehicleLogic::rendRecordEdit($param);
//返回数据
if($result['code'] == 1){
return $this->success($result['msg'],$result['data']);
@ -60,65 +85,4 @@ class VehicleController extends BaseApiController
return $this->fail($result['msg']);
}
}
/*
* 获取去未出租的三轮车列表
*/
public function tricycle():Json
{
//获取参数
$params = $this->request->get(['page_no','page_size']);
//获取数据
$data = VehicleLogic::lists($params);
//返回数据
return $this->success('请求成功',$data);
}
/*
* 三轮车批量出租
*/
public function multipleRent():Json
{
//获取参数
$params = $this->request->post(['company_id','contract_id','car_ids','start_time','end_time']);
//验证参数
if(empty($params['company_id']) || empty($params['contract_id']) || empty($params['car_ids']) || empty($params['start_time']) || empty($params['end_time'])) {
return $this->fail('缺少必要参数');
}
if(!checkDateIsValid($params['start_time']) || !checkDateIsValid($params['end_time'])){
return $this->fail('时间格式错误');
}
//写入数据
$result = VehicleLogic::oneLevelRent($params);
//返回结果
if($result['code'] == 1){
return $this->success($result['msg']);
}else{
return $this->fail($result['msg']);
}
}
/*
* 三轮车单辆出租
*/
public function singleRent():Json
{
//获取参数
$params = $this->request->post(['lessor_company_id','tenant_company_id','contract_id','car_id','start_time','end_time']);
//验证参数
if(empty($params['lessor_company_id']) || empty($params['tenant_company_id']) || empty($params['contract_id']) || empty($params['car_id']) || empty($params['start_time']) || empty($params['end_time'])){
return $this->fail('缺少必要参数');
}
if(!checkDateIsValid($params['start_time']) || !checkDateIsValid($params['end_time'])){
return $this->fail('时间格式错误');
}
//写入数据
$result = VehicleLogic::twoLevelRent($params);
//返回结果
if($result['code'] == 1){
return $this->success($result['msg']);
}else{
return $this->fail($result['msg']);
}
}
}

View File

@ -29,106 +29,7 @@ use app\common\model\vehicle\VehicleRentRecord;
*/
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_contract_id' => $params['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($params['start_time']),
'lessee_one_end_time' => strtotime($params['end_time']),
'lessee_one_flag' => 1,
];
}
}
//写入数据
VehicleRent::startTrans();
try {
(new VehicleRent()) -> saveAll($data);
//更新车辆列表中的车辆状态
Vehicle::where('id','in',$ids)->update(['status'=>1]);
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_contract_id' => $params['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($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()];
}
}
public static function company() {
public static function companyInfo() {
$data = Company::field('id,company_name')->where('company_name',30)->select();
if($data){
return ['code'=>1,'msg'=>'请求成功','data'=>$data->toArray()];
@ -139,12 +40,16 @@ class VehicleLogic extends BaseLogic
public static function rentRecord($params):array {
//获取数据
$cars = Vehicle::field('id')->where('status',0)->where('type',0)->limit($params['num'])->select();
if(!$cars || $cars->count() < $params['num']){
return ['code'=>0,'msg'=>'车辆数量不足'];
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'];
}
$ids = array_column($cars->toArray(),'id');
$ids = implode(',',$ids);
//发起合同
VehicleRentRecord::startTrans();
try {
@ -173,6 +78,7 @@ class VehicleLogic extends BaseLogic
'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(),
@ -185,7 +91,7 @@ class VehicleLogic extends BaseLogic
}
}
public static function recordInfo($contract_id) {
public static function rendRecordInfo($contract_id) {
$record = VehicleRentRecord::field('car_ids,num,start_time,end_time')->where('contract_id',$contract_id)->find();
if(!$record){
return ['code'=>0,'msg'=>'数据不存在'];
@ -203,4 +109,78 @@ class VehicleLogic extends BaseLogic
];
return ['code'=>1,'msg'=>'请求成功','data'=>$data];
}
public static function rendRecordEdit($contract_id) {
$record = VehicleRentRecord::where('contract_id',$contract_id)->where('status',1)->find();
if(!$record){
return ['code'=>0,'msg'=>'数据不存在'];
}
VehicleRentRecord::startTrans();
try {
if($record['rent_type'] == 2){
//获取承租公司信息
$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()];
}
}
}

View File

@ -34,7 +34,4 @@ Route::rule('getCarHistory','Gps/getCarHistory','get');
Route::rule('getCompany','Vehicle/getCompany','get');
Route::rule('setContract','Vehicle/setContract','post');
Route::rule('getRentRecord','Vehicle/getRentRecord','get');
Route::rule('multipleRent','Vehicle/multipleRent','post');
Route::rule('singleRent','Vehicle/singleRent','post');
Route::rule('updateRentRecord','Vehicle/updateRentRecord','post');