123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257 |
- <?php
- namespace app\api\controller;
- use addons\epay\library\Service;
- use app\common\controller\Api;
- use app\common\model\HotelModel;
- use app\common\model\HotelOrderModel;
- use app\common\model\HotelRoomModel;
- use app\common\model\PayOrderModel;
- use app\common\model\UniversityEventModel;
- use app\common\model\Wallet;
- use app\utils\CurlUtil;
- use app\utils\Service\Tencent\TencentIm;
- use think\Db;
- /**
- * 老年大学 活动板块
- */
- class Hotel extends Api
- {
- protected $noNeedLogin = [''];
- protected $noNeedRight = ['*'];
- protected $status = [
- 0 => '全部',
- 1 => '待入住',
- 2 => '已入住',
- 3 => '已取消',
- ];
- // 活动列表
- public function list()
- {
- $params = $this->request->param();
- $params['sort_distance'] = !empty($params['sort_distance']) ? $params['sort_distance'] : 1;
- $params['sort_price'] = !empty($params['sort_price']) ? $params['sort_price'] : 1;
- $field = ['id','name','image','price','original_price','tags','lat','lng'];
- if (!empty($params['lng']) && !empty($params['lng'])){
- $field[] = "(st_distance(point ({$params['lng']}, {$params['lat']}),point(lng,lat))*111195) as distance";
- }
- $query = HotelModel::field($field);
- if (!empty($params['is_home'])){
- $query->where('is_home',$params['is_home']);
- }
- 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 = HotelModel::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'] = HotelRoomModel::where('hotel_id',$info['id'])->where('status',1)->order('weigh desc')->order('id desc')->select();
- return $this->success('success', $info);
- }
- // 详情
- public function room_info()
- {
- $params = $this->request->param();
- $info = HotelRoomModel::where('id',$params['id'])->where('status',1)->find();
- $info['business_license'] = cdnurl(config('site.business_license'));
- return $this->success('success', $info);
- }
- public function apply()
- {
- $params = $this->request->param();
- if (empty($params['room_id'])) {
- return $this->error('参数缺失');
- }
- if (empty($params['start_date'])) {
- return $this->error('参数缺失');
- }
- if (empty($params['end_date'])) {
- return $this->error('参数缺失');
- }
- if (empty($params['num'])) {
- return $this->error('参数缺失');
- }
- if (empty($params['name'])) {
- return $this->error('参数缺失');
- }
- if (empty($params['phone'])) {
- return $this->error('参数缺失');
- }
- $user_id = $this->auth->id;
- $info = HotelRoomModel::with([
- 'hotel'=>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['hotel'])) {
- return $this->error('酒店信息有误');
- }
- $days = (int)((strtotime($params['end_date']) - strtotime($params['start_date'])) / 86400);
- // 开始报名
- $data = [
- 'hotel_id' => $info['hotel_id'],
- 'room_id' => $info['id'],
- 'user_id' => $user_id,
- 'num' => $params['num'],
- 'name' => $params['name'],
- 'phone' => $params['phone'],
- 'start_date' => $params['start_date'],
- 'end_date' => $params['end_date'],
- 'days' => $days,
- 'order_no' => createUniqueNo('H', $user_id),
- 'pay_amount' => bcmul(bcmul($info['price'], $days, 2),$params['num'],2),
- 'status' => 1,
- 'create_time' => time()
- ];
- if (!Db::name('hotel_order')->insertGetId($data)) {
- return $this->error('订单创建失败');
- }
- return $this->success('提交成功');
- }
- // 订单列表
- public function myApply()
- {
- $params = $this->request->param();
- $user_id = $this->auth->id;
- $query = HotelOrderModel::with([
- 'room'=>function ($query) {
- $query->field(['id','name','image']);
- }
- ])->where('user_id',$user_id);
- switch ($params['status']){
- case 1:
- $query->where('start_date','>',date('Y-m-d'))->where('status',1);
- break;
- case 2:
- $query->where('start_date','<=',date('Y-m-d'))->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['start_date'] > date('Y-m-d')){
- $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'] = '已取消';
- }
- }
- return $this->success('获取成功',$list);
- }
- // 订单详情
- public function myApplyInfo()
- {
- $params = $this->request->param();
- $user_id = $this->auth->id;
- $query = HotelOrderModel::with([
- 'hotel' => function ($query) {
- $query->field(['id','name','image']);
- },
- 'room'=>function ($query) {
- $query->field(['id','name','image','space','floor','is_wifi','window','breakfast','people_num','bad']);
- }
- ])->where('id',$params['id'])->where('user_id',$user_id);
- $info = $query->find();
- if ($info['status'] == 1){
- if ($info['start_date'] > date('Y-m-d')){
- $info['status_code'] = 1;
- $info['status_txt'] = '待入住';
- }else{
- $info['status_code'] = 2;
- $info['status_txt'] = '已入住';
- }
- }else{
- $info['status_code'] = 3;
- $info['status_txt'] = '已取消';
- }
- return $this->success('获取成功',$info);
- }
- // 订单取消
- public function applyCancel()
- {
- $params = $this->request->param();
- $user_id = $this->auth->id;
- $order = HotelOrderModel::where('user_id',$user_id)->where('id',$params['order_id'])->where('status',1)->find();
- if (!$order){
- return $this->error('订单不存在或已取消');
- }
- if (time() > strtotime($order['start_date'] . ' 12:00:00')){
- return $this->error('订单已入住,无法取消');
- }
- if (!HotelOrderModel::where('user_id',$user_id)->where('id',$params['order_id'])->where('status',1)->update(['status'=>0])){
- return $this->error('操作失败');
- }
- return $this->success('操作成功');
- }
- }
|