logistics/app/api/controller/ContractController.php
2023-10-13 14:21:05 +08:00

163 lines
7.4 KiB
PHP

<?php
namespace app\api\controller;
use app\common\model\contract\Contract;
use app\common\model\platform\Platform;
use app\common\model\vehicle\Vehicle;
use app\common\model\vehicle\VehicleRent;
use think\facade\Db;
class ContractController extends BaseApiController
{
public array $notNeedLogin = ['signContract','contractUpdate','checkCarNum','getAvailableVehicles'];
public function signContract() {
//获取参数
$params = $this->request->post(['num','company_id','company_name','company_code','company_user','company_phone','company_email','cars_info','type']);
//验证参数
if(empty($params['num']) || empty($params['company_id']) || empty($params['company_name']) || empty($params['company_code']) || empty($params['company_user']) || empty($params['company_phone']) || empty($params['company_email'])){
return $this->fail('缺少必要参数');
}
//获取平台公司数据
$platform = Platform::find(1);
if(!$platform || $platform->isEmpty()){
return $this->fail('甲方公司不存在');
}
//设置数据
$data = [
'contract_no' => time(),
'company_a_name' => $platform['company_name'],
'company_a_code' => $platform['company_organization_code'],
'company_a_user' => $platform['company_user'],
'company_a_phone' => $platform['company_phone'],
'company_a_email' => $platform['company_email'],
'company_b_id' => $params['company_id'],
'company_b_name' => $params['company_name'],
'company_b_code' => $params['company_code'],
'company_b_user' => $params['company_user'],
'company_b_phone' => $params['company_phone'],
'company_b_email' => $params['company_email'],
'num' => $params['num'],
'cars_info' => isset($params['cars_info']) && !empty($params['cars_info']) ? $params['cars_info'] : null,
'type' => isset($params['type']) && !empty($params['type']) ? $params['type'] : 0,
'status' => 0,
'create_time' => time(),
'update_time' => time(),
];
Db::startTrans();
try {
$res1= Contract::create($data);
if(isset($params['cars_info']) && !empty($params['cars_info'])){
$cars = json_decode($params['cars_info'], true);
Vehicle::where('id','in',array_column($cars,'id'))->update(['status'=>1]);
}
$data['contract_logistic_id'] = $res1->id;
Db::commit();
return $this->success('合同发起成功,等待审核',$data);
} catch (\Exception $e) {
// 回滚事务
Db::rollback();
return $this->fail($e->getMessage());
}
}
public function contractUpdate() {
//获取参数
$params = $this->request->post();
if(!isset($params['id']) || empty($params['id'])) {
return $this->fail('缺少必要参数');
}
$contract = Contract::where('id',$params['id'])->findOrEmpty();
if($contract->isEmpty()) {
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 = [];
//更新合同状态
$contract->where('id', $contract['id'])->save([
'signing_timer' => $params['signing_timer'],
'status' => $params['status'],
'contract_url' => $params['contract_url'],
'contract_evidence' => $params['contract_evidence']
]);
if($contract['type'] == 0){
foreach($cars as $v){
$hasCar = VehicleRent::where('car_id',$v['id'])->findOrEmpty();
if($hasCar->isEmpty()){
$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' => ''
];
}
}
$status = 2;
}else if($contract['type'] == 3) {
foreach($cars as $v){
$hasCar = VehicleRent::where('car_id',$v['id'])->findOrEmpty();
if($hasCar->isEmpty()){
$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' => 2,
'use_user_id' =>$params['use_user_id'],
'use_user_name' =>$params['use_user_name'],
'use_user_phone' =>$params['use_user_phone'],
];
}
}
$status = 4;
}
//更新本地车辆状态
Vehicle::where('id','in',array_column($cars,'id'))->update(['status'=>$status]);
//添加车辆到租赁列表
(new VehicleRent()) -> saveAll($data);
return $this->success('更新成功1');
}
$contract->where('id', $contract['id'])->save($params);
if($contract['type'] == 0 && isset($params['cars_info']) && !empty($params['cars_info'])){
$cars = json_decode($params['cars_info'], true);
//更新本地车辆状态
Vehicle::where('id','in',array_column($cars,'id'))->update(['status'=>1]);
}
return $this->success('更新成功2');
}
public function checkCarNum() {
//获取参数
$num = $this->request->post('num');
if(empty($num)){
return $this->fail('物理系统车辆数量不足');
}
//获取车辆
$data = Vehicle::where('status',0)->where('type',0)->limit($num)->select();
//判断
if($data->count() < $num){
return $this->fail('物理系统车辆数量不足');
}
return $this->success('物理系统车辆数量充足');
}
public function getAvailableVehicles() {
$data = Vehicle::field('id,license')->where('status',0)->where('type',0)->select();
return $this->success('请求成功',$data->toArray());
}
}