WokerTask/app/api/controller/PropertyController.php

325 lines
13 KiB
PHP
Raw Permalink Normal View History

2023-12-27 14:06:33 +08:00
<?php
namespace app\api\controller;
use app\common\logic\UserLogic as CommonUserLogic;
use app\common\model\auth\Admin;
use app\common\model\Company;
use app\common\model\contract\Contract;
use think\facade\Db;
/**
* 搜索
* Class PropertyController
* @package app\api\controller
*/
class PropertyController extends BaseApiController
{
public array $notNeedLogin = ['propertyContractByCompany','propertyContractByPerson','propertyRentByCompany','propertyRentByPerson','propertyRentLists','propertyRentInf','carInfo','carTravelList','carSituationList','addCarSituation'];
//公司签约
public function propertyContractByCompany() {
//获取参数
$param = $this->request->param();
$param['admin_id'] = $this->userId = 107;
if(!isset($param['company_id'])) {
return $this->fail('请选择公司');
}
if(!isset($param['num'])) {
return $this->fail('请填写数量');
}
if(!isset($param['start_time'])) {
return $this->fail('请选择开始时间');
}
if(!isset($param['end_time'])) {
return $this->fail('请选择结束时间');
}
$party_a = Db::name('user')->where('id',$param['admin_id'])->value('company_id');
if(!$party_a){
return $this->fail('甲方公司不存在,请先完善公司信息');
}
//组装数据
$value['party_a'] = $party_a;
$value['party_b'] = $param['company_id'];
$value['num'] = $param['num'];
$value['type'] = 1;
$value['start_time'] = strtotime($param['start_time']);
$value['end_time'] = strtotime($param['end_time']);
$value['admin_id'] = $param['admin_id'];
$value['contract_no'] = 'HT'.date('YmdHis').rand(1000,9999);
$value['create_time'] = time();
//生成合同
$res = Db::name('property_contract')->save($value);
if($res){
return $this->success('发起成功,等待平台风控部上传合同');
}
return $this->fail('发起失败');
}
//个人签约
public function propertyContractByPerson() {
//获取参数
$param = $this->request->param();
$param['admin_id'] = $this->userId = 107;
if(!isset($param['user_id'])) {
return $this->fail('请选择签约人');
}
if(!isset($param['car_id'])) {
return $this->fail('请选择三轮车');
}
if(!isset($param['start_time'])) {
return $this->fail('请选择开始时间');
}
if(!isset($param['end_time'])) {
return $this->fail('请选择结束时间');
}
if(!isset($param['rent_id'])) {
return $this->fail('缺少关键参数');
}
//组装数据
$value['party_a'] = Db::name('user')->where('id',$param['admin_id'])->value('company_id');
$value['user_id'] = $param['user_id'];
$value['num'] = 1;
$value['type'] = 2;
$value['start_time'] = strtotime($param['start_time']);
$value['end_time'] = strtotime($param['end_time']);
$value['admin_id'] = $param['admin_id'];
$value['contract_no'] = 'HT'.date('YmdHis').rand(1000,9999);
$value['create_time'] = time();
//生成合同
Db::startTrans();
try {
$insertId = Db::name('property_contract')->insertGetId($value);
Db::name('property_contract_info')->save(
[
'contract_id' => $insertId,
'car_id' => $param['car_id'],
'rent_id' => $param['rent_id'],
'user_id' => $param['user_id'],
]
);
Db::commit();
return $this->success('发起成功,等待平台风控部上传合同');
}catch(\Exception $e){
Db::rollback();
return $this->fail('发起失败'.$e->getMessage());
}
}
//生成公司租赁信息
public function propertyRentByCompany() {
//获取参数
$contract_id = $this->request->param('contract_id');
//验证参数
if(!isset($contract_id)){
return $this->fail('请选择合同');
}
try {
//获取合同数据
$contractInfo = Db::name('property_contract')->where('id',$contract_id)->find();
$carsids = Db::name('property')->where('status',0)->limit($contractInfo['num'])->column('id');
}catch (\Exception $e) {
return $this->fail($e->getMessage());
}
$data['cars'] = [];
foreach($carsids as $v){
$arr['id'] = $v;
$arr['status'] = 0;
array_push($data['cars'],$arr);
}
$value['company_id'] = $contractInfo['party_b'];
$value['company_from_id'] = $contractInfo['party_a'];
$value['contract_id'] = $contractInfo['id'];
$value['num'] = $contractInfo['num'];
$value['start_time'] = $contractInfo['start_time'];
$value['end_time'] = $contractInfo['end_time'];
$value['cars'] = json_encode($data['cars']);
//生成数据
Db::startTrans();
try {
Db::name('property_rent')->save($value);
Db::name('property')->where('id','in',$carsids)->update(['status'=>1]);
Db::commit();
return $this->success('租赁成功');
}catch(\Exception $e){
Db::rollback();
return $this->fail('租赁失败');
}
}
//生成个人租赁信息
public function propertyRentByPerson(){
//获取参数
$contract_id = $this->request->param('contract_id');
//验证参数
if(!isset($contract_id)){
return $this->fail('请选择合同');
}
try {
//获取合同数据
$contractInfo = Db::name('property_contract')->alias('c')->leftJoin('property_contract_info i','c.id = i.contract_id')->where('c.id',$contract_id)->find();
}catch (\Exception $e) {
return $this->fail($e->getMessage());
}
$value['user_id'] = $contractInfo['user_id'];
$value['company_from_id'] = $contractInfo['party_a'];
$value['contract_id'] = $contract_id;
$value['num'] = 1;
$value['start_time'] = $contractInfo['start_time'];
$value['end_time'] = $contractInfo['end_time'];
$value['cars'] = json_encode(['id'=>$contractInfo['car_id'],'status'=>1]);
//生成数据
Db::startTrans();
try {
Db::name('property_rent')->save($value);
$rentInfo = Db::name('property_rent')->where('id',$contractInfo['rent_id'])->find();
$cars = json_decode($rentInfo['cars'],true);
foreach($cars as $k=>$v) {
if($v['id'] == $contractInfo['car_id']) {
$cars[$k]['status'] = 1;
}
}
$cars = json_encode($cars);
Db::name('property_rent')->where('id',$contractInfo['rent_id'])->update(['cars'=>$cars]);
Db::commit();
return $this->success('租赁成功');
}catch(\Exception $e){
Db::rollback();
return $this->fail('租赁失败');
}
}
//获取租赁列表
public function propertyRentLists() {
//获取参数
$company_id = $this->request->param('company_id');
//获取数据
$data = Db::name('property_rent')->withoutField('cars')->where('company_id',$company_id)->select()->each(function($item,$key){
$item['diif_time'] = ceil(($item['end_time']-$item['start_time'])/86400);
$item['start_time'] = date('Y-m-d',$item['start_time']);
$item['end_time'] = date('Y-m-d',$item['end_time']);
$company = Db::name('company')->where('id',$item['company_from_id'])->find();
$item['company_user'] = $company['master_name'];
$item['company_phone'] = $company['master_phone'];
$item['contract_file'] = Db::name('property_contract')->where('id',$item['contract_id'])->value('contract_url');
$item['model'] = '电动三轮车';
return $item;
})->toArray();
//返回
return $this->success('获取成功',$data,1,1);
}
//获取租赁详情列表
public function propertyRentInfo() {
//获取参数
$rent_id = $this->request->param('rent_id');
//获取数据
$data = Db::name('property_rent')->where('id',$rent_id)->find();
$company = Db::name('company')->where('id',$data['company_from_id'])->find();
$data['company_name'] = $company['company_name'];
$data['company_phone'] = $company['master_phone'];
$data['cars'] = json_decode($data['cars'],true);
foreach($data['cars'] as $k=>$v) {
$data['cars'][$k]['status_name'] = $v['status'] == 1 ? '已租赁' : '未租赁';
$data['cars'][$k]['car_info'] = Db::name('property')->where('id',$v['id'])->find();
if($v['status'] == 1) {
$rentInfo = Db::name('property_contract_info')->where('rent_id',$rent_id)->where('car_id',$v['id'])->find();
$data['cars'][$k]['user_info'] = Db::name('user')->field('nickname,mobile')->where('id',$rentInfo['user_id'])->find();
}
}
//返回
return $this->success('获取成功',$data,1,1);
}
//车辆详情
public function carInfo() {
//获取参数
$car_id = $this->request->param('car_id');
$rent_id = $this->request->param('rent_id');
if(!isset($car_id) || !isset($rent_id)) {
return $this->fail('参数错误');
}
//获取数据
$data = Db::name('property_rent')->alias('p')
->leftJoin('company c','p.company_from_id = c.id')
->leftJoin('user u','u.id = p.user_id')
->field('p.*,c.company_name,c.master_phone as company_phone,u.nickname as user_name,u.mobile as user_phone')
->where('p.id',$rent_id)->find();
$data['cars'] = json_decode($data['cars'],true);
if($car_id != $data['cars']['id']) {
return $this->fail('参数错误');
}
$carInfo = Db::name('property')->field('name as car_name,pic as car_pic')->where('id',$car_id)->find();
$data['cars']['car_name'] = $carInfo['car_name'];
$data['cars']['car_pic'] = $carInfo['car_pic'];
//返回
return $this->success('获取成功',$data,1,1);
}
//车辆行驶记录
public function carTravelList() {
//获取参数
$car_id = $this->request->param('car_id');
$rent_id = $this->request->param('rent_id');
$page_size = $this->request->param('page_size',10);
$page_num = $this->request->param('page_num',1);
if(!isset($car_id) || !isset($rent_id)) {
return $this->fail('参数错误');
}
$kilometers_count = 0;
$data = Db::name('property_rent_car_travel')->field('create_time,kilometers')->where('car_id',$car_id)->where('rent_id',$rent_id)->order('create_time desc')->paginate([
'list_rows'=> $page_size,
'page' => $page_num,
])->each(function($item,$key) use (&$kilometers_count){
$kilometers_count += $item['kilometers'];
return $item;
})->toArray();
$data['kilometers_count'] = $kilometers_count;
return $this->success('获取成功',$data,1,1);
}
//三轮车近况列表
public function carSituationList() {
//获取参数
$car_id = $this->request->param('car_id');
$rent_id = $this->request->param('rent_id');
$page_size = $this->request->param('page_size',10);
$page_num = $this->request->param('page_num',1);
if(!isset($car_id) || !isset($rent_id)) {
return $this->fail('参数错误');
}
$data = Db::name('property_rent_car_situation')->field('create_time,content')->where('car_id',$car_id)->where('rent_id',$rent_id)->order('create_time desc')->paginate([
'list_rows'=> $page_size,
'page' => $page_num,
])->toArray();
return $this->success('获取成功',$data,1,1);
}
//填写三轮车近况
public function addCarSituation() {
//获取参数
$car_id = $this->request->param('car_id');
$rent_id = $this->request->param('rent_id');
$content = $this->request->param('content');
if(!isset($car_id) || !isset($rent_id) || !isset($content)) {
return $this->fail('参数错误');
}
$data = [
'car_id' => $car_id,
'rent_id' => $rent_id,
'content' => $content,
'create_time' => time(),
];
$res = Db::name('property_rent_car_situation')->insert($data);
if($res) {
return $this->success('添加成功');
}else {
return $this->fail('添加失败');
}
}
}