logistics/app/api/controller/ContractController.php
unknown fc96a26498 a
2023-08-31 01:19:57 +08:00

85 lines
3.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 think\facade\Db;
class ContractController extends BaseApiController
{
public array $notNeedLogin = ['signContract','updateContract'];
public function signContract() {
//获取参数
$params = $this->request->post(['num','company_id','company_name','company_code','company_user','company_phone','company_email']);
//验证参数
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('甲方公司不存在');
}
//获取车辆数据
$cars = Vehicle::field('id,license')->where([
['type','=',0],
['status','=',0],
])->limit($params['num'])->select();
if($cars->isEmpty() || $cars->count() < $params['num']){
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' => $cars->toJson(),
'status' => 0,
'create_time' => time(),
'update_time' => time(),
];
Db::startTrans();
try {
$res1 = Contract::create($data);
$res2 = Vehicle::where('id','in',array_column($cars->toArray(),'id'))->update(['status'=>1,'update_time'=>time()]);
if($res1->id && $res2){
$data['contract_logistic_id'] = $res1->id;
$data['type'] = 0;
Db::commit();
return $this->success('合同发起成功,等待审核',$data);
}else{
Db::rollback();
return $this->fail('合同发起失败,请稍后重试');
}
} catch (\Exception $e) {
// 回滚事务
Db::rollback();
return $this->fail($e->getMessage());
}
}
public function updateContract() {
//获取参数
$params = $this->request->post();
if(empty($params['id'])){
return $this->fail('缺少必要参数');
}
//更新数据
$model = Contract::where('id',$params['id'])->find();
$result = $model->save($params);
//返回
return $result ? $this->success('更新成功') : $this->fail('更新失败');
}
}