travel/service/app/api/controller/OrderController.php

114 lines
4.2 KiB
PHP

<?php
namespace app\api\controller;
use app\model\OrderBooks;
use app\model\Orders;
use app\model\ProductSchedules;
use Respect\Validation\Validator;
use support\Request;
/**
*
*/
class OrderController extends base
{
/**
* 订单查询
* @param Request $request
* @return \support\Response
* @throws \think\db\exception\DbException
*/
public function index(Request $request) {
$mobile = $request->get('mobile');
if (empty($mobile) || !Validator::mobile()->validate($mobile)) {
return $this->error('手机号格式错误');
}
$where = [];
$os = [1, 7];
$query = Orders::where($where)
->whereLike('mobile', $mobile)
->whereIn('os', $os)
->order('create_at','desc')
->order('id','desc');
$orders = $query->paginate($request->get('limit',10));
$list = $orders->hidden(['check_sn'])->append(['h5_os_status_name', 'h5_os_name' , 'can_book']);
return $this->success($list);
}
/**
* 订单详情
* @param Request $request
* @return \support\Response
* @throws \think\db\exception\DataNotFoundException
* @throws \think\db\exception\DbException
* @throws \think\db\exception\ModelNotFoundException
*/
public function detail(Request $request) {
$id = $request->get('order_id');
if (empty($id)) {
return $this->error('请选择订单');
}
$orderDetail = Orders::where('id', $id)->with(['admin', 'product'])->find();
if (empty($orderDetail)) {
return $this->error('订单未找到');
}
$orderDetail->admin->job = '金牌旅行管家';
$orderDetail->admin->service_promise = '我以真诚、热情、专业的态度服务好每一位顾客,全力确保您的旅行体验美好、愉快、安全。';
// 旅游合同
$orderDetail->travel_contract = 'http://192.168.0.100:8787/%E6%8A%96%E9%9F%B3%E6%96%B0%E5%9B%BD%E6%97%85%E6%B7%BB%E5%8A%A0%E5%88%86%E5%8D%95%E7%B3%BB%E7%BB%9F%E7%9A%84%E5%95%86%E5%93%81.xlsx';
return $this->success($orderDetail);
}
/**
* 订单预约
* @param Request $request
* @return \support\Response
*/
public function book(Request $request) {
try {
$data = $request->post();
Validator::input($data, [
'order_id' => Validator::number()->length(1, 10)->setName('订单号'),
'travel_date' => Validator::date()->setName('出游日期'),
'num' => Validator::number()->length(1, 3)->setName('出游人数'),
'name' => Validator::length(1, 10)->setName('出行人名称'),
'mobile' => Validator::mobile()->setName('联系电话'),
]);
if (!$order = Orders::where('id', $data['order_id'])->find()) {
return $this->error('订单未找到');
}
if ($order->is_apply_appointment == 1) {
return $this->error('已申请预约');
}
if ($data['code_pic'] && is_array($data['code_pic'])) {
$data['code_pic'] = json_encode($data['code_pic']);
}
// 检查排期
$schedules = ProductSchedules::where(['date' => $data['travel_date'], 'third_product_id' => $order->product_id])->find();
if (!empty($schedules) && $schedules->num > 0) {
// 已预约人数
$booksNum = OrderBooks::join('orders', 'orders.id=order_books.order_id')
->where(['order_books.travel_date' => $data['travel_date'], 'orders.product_id' => $order->product_id])
->sum('order_books.num');
if (($schedules->num-$booksNum) < $data['num']) {
return $this->error('当前日期预约已满,请选择其他日期');
}
}
$res = OrderBooks::create($data);
if (!empty($res)) {
$order->is_apply_appointment = 1;
$order->save();
}
return $this->success($res);
} catch (\Exception $exception) {
return $this->error($exception->getMessage());
}
}
}