request->param(); $params['sort_type'] = !empty($params['sort_type']) ? $params['sort_type'] : 1; $field = ['id','name','image','price','original_price','tags','lat','lng','address']; if (!empty($params['lng']) && !empty($params['lng'])){ $field[] = "(st_distance(point ({$params['lng']}, {$params['lat']}),point(lng,lat))*111195) as distance"; } $query = HotelCanteenModel::field($field); if (!empty($params['lng']) && !empty($params['lng'])){ if ($params['sort_distance'] == 1){ $query->order('distance asc'); }else{ $query->order('distance desc'); } } if ($params['sort_price'] == 1){ $query->order('price asc'); }else{ $query->order('price desc'); } $list = $query->where('status', 1) ->order('weigh desc') ->order('id desc') ->autopage() ->select(); foreach ($list as $k => $v) { // 计算距离 $list[$k]['distance'] = distance_ext($v['distance'] ?? -1); } return $this->success('success', $list); } // 详情 public function info() { $params = $this->request->param(); $field = ['id','name','image','images','price','original_price','tags','lat','lng','address']; if (!empty($params['lng']) && !empty($params['lng'])){ $field[] = "(st_distance(point ({$params['lng']}, {$params['lat']}),point(lng,lat))*111195) as distance"; } $query = HotelCanteenModel::field($field); $query->where('id', $params['id']); $info = $query->where('status', 1)->find(); if (!$info){ return $this->error('信息不存在'); } // 计算距离 $info['distance'] = distance_ext($info['distance'] ?? -1); // 房间列表 $info['room_list'] = HotelCanteenRoomModel::where('canteen_id',$info['id'])->where('status',1)->order('weigh desc')->order('id desc')->select(); return $this->success('success', $info); } public function apply() { $params = $this->request->param(); if (empty($params['room_id'])) { return $this->error('参数缺失'); } if (empty($params['get_to_time'])) { return $this->error('参数缺失'); } if (empty($params['name'])) { return $this->error('参数缺失'); } if (empty($params['phone'])) { return $this->error('参数缺失'); } if (empty($params['sex'])) { return $this->error('参数缺失'); } $user_id = $this->auth->id; $info = HotelCanteenRoomModel::with([ 'canteen'=>function ($query) { $query->field(['id','name','image','images','price','original_price']); } ])->where('id',$params['room_id'])->where('status',1)->find(); if (!$info) { return $this->error('餐桌不存在'); } if (empty($info['canteen'])) { return $this->error('餐厅信息有误'); } // 开始报名 $data = [ 'canteen_id' => $info['canteen_id'], 'room_id' => $info['id'], 'user_id' => $user_id, 'name' => $params['name'], 'phone' => $params['phone'], 'get_to_time' => strtotime($params['get_to_time']), 'sex' => $params['sex'], 'remark' => $params['remark'] ?? '', 'order_no' => createUniqueNo('H', $user_id), 'pay_amount' => bcmul($info['price'], 1, 2), 'status' => 1, 'create_time' => time() ]; if (!Db::name('hotel_canteen_order')->insertGetId($data)) { return $this->error('订单创建失败'); } return $this->success('提交成功'); } // 订单列表 public function myApply() { $params = $this->request->param(); $user_id = $this->auth->id; $query = HotelCanteenOrderModel::with([ 'canteen'=>function ($query) { $query->field(['id','name','image']); }, 'room'=>function ($query) { $query->field(['id','name','image']); } ])->where('user_id',$user_id); switch ($params['status']){ case 1: $query->where('get_to_time','>',time())->where('status',1); break; case 2: $query->where('get_to_time','<=',time())->where('status',1); break; case 3: $query->where('status',0); break; default: break; } $list = $query->order('id','desc')->autopage()->select(); foreach ($list as $key=>$val){ if ($val['status'] == 1){ if ($val['get_to_time'] > time()){ $list[$key]['status_code'] = 1; $list[$key]['status_txt'] = '待用餐'; }else{ $list[$key]['status_code'] = 2; $list[$key]['status_txt'] = '已完成'; } }else{ $list[$key]['status_code'] = 3; $list[$key]['status_txt'] = '已取消'; } $list[$key]['get_to_time'] = date('Y-m-d H:i'); } return $this->success('获取成功',$list); } // 订单详情 public function myApplyInfo() { $params = $this->request->param(); $user_id = $this->auth->id; $query = HotelCanteenOrderModel::with([ 'canteen'=>function ($query) { $query->field(['id','name','image']); }, 'room'=>function ($query) { $query->field(['id','name','image']); } ])->where('id',$params['id'])->where('user_id',$user_id); $info = $query->find(); if ($info['status'] == 1){ if ($info['get_to_time'] > time()){ $info['status_code'] = 1; $info['status_txt'] = '待用餐'; }else{ $info['status_code'] = 2; $info['status_txt'] = '已完成'; } }else{ $info['status_code'] = 3; $info['status_txt'] = '已取消'; } $info['get_to_time'] = date('Y-m-d H:i'); return $this->success('获取成功',$info); } // 订单取消 public function applyCancel() { $params = $this->request->param(); $user_id = $this->auth->id; $order = HotelCanteenOrderModel::where('user_id',$user_id)->where('id',$params['order_id'])->where('status',1)->find(); if (!$order){ return $this->error('订单不存在或已取消'); } if (time() > $order['get_to_time']){ return $this->error('已到预约时间,无法取消'); } if (!HotelCanteenOrderModel::where('user_id',$user_id)->where('id',$params['order_id'])->where('status',1)->update(['status'=>0])){ return $this->error('操作失败'); } return $this->success('操作成功'); } }