Hotel.php 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. <?php
  2. namespace app\api\controller;
  3. use addons\epay\library\Service;
  4. use app\common\controller\Api;
  5. use app\common\model\HotelModel;
  6. use app\common\model\HotelOrderModel;
  7. use app\common\model\HotelRoomModel;
  8. use app\common\model\PayOrderModel;
  9. use app\common\model\UniversityEventModel;
  10. use app\common\model\Wallet;
  11. use app\utils\CurlUtil;
  12. use app\utils\Service\Tencent\TencentIm;
  13. use think\Db;
  14. /**
  15. * 老年大学 活动板块
  16. */
  17. class Hotel extends Api
  18. {
  19. protected $noNeedLogin = [''];
  20. protected $noNeedRight = ['*'];
  21. protected $status = [
  22. 0 => '全部',
  23. 1 => '待入住',
  24. 2 => '已入住',
  25. 3 => '已取消',
  26. ];
  27. // 活动列表
  28. public function list()
  29. {
  30. $params = $this->request->param();
  31. $params['sort_type'] = !empty($params['sort_type']) ? $params['sort_type'] : 1;
  32. $field = ['id','name','image','price','original_price','tags','lat','lng'];
  33. if (!empty($params['lng']) && !empty($params['lng'])){
  34. $field[] = "(st_distance(point ({$params['lng']}, {$params['lat']}),point(lng,lat))*111195) as distance";
  35. }
  36. $query = HotelModel::field($field);
  37. if (!empty($params['lng']) && !empty($params['lng']) && $params['sort_type'] == 1){
  38. $query->order('distance asc');
  39. }
  40. if ($params['sort_type'] == 2){
  41. $query->order('price asc');
  42. }
  43. $list = $query->where('status', 1)
  44. ->order('weigh desc')
  45. ->order('id desc')
  46. ->autopage()
  47. ->select();
  48. foreach ($list as $k => $v) {
  49. // 计算距离
  50. $list[$k]['distance'] = distance_ext($v['distance'] ?? -1);
  51. }
  52. return $this->success('success', $list);
  53. }
  54. // 详情
  55. public function info()
  56. {
  57. $params = $this->request->param();
  58. $field = ['id','name','image','images','price','original_price','tags','lat','lng','address'];
  59. if (!empty($params['lng']) && !empty($params['lng'])){
  60. $field[] = "(st_distance(point ({$params['lng']}, {$params['lat']}),point(lng,lat))*111195) as distance";
  61. }
  62. $query = HotelModel::field($field);
  63. $query->where('id', $params['id']);
  64. $info = $query->where('status', 1)->find();
  65. if (!$info){
  66. return $this->error('信息不存在');
  67. }
  68. // 计算距离
  69. $info['distance'] = distance_ext($info['distance'] ?? -1);
  70. // 房间列表
  71. $info['room_list'] = HotelRoomModel::where('hotel_id',$info['id'])->where('status',1)->order('weigh desc')->order('id desc')->select();
  72. return $this->success('success', $info);
  73. }
  74. // 详情
  75. public function room_info()
  76. {
  77. $params = $this->request->param();
  78. $info = HotelRoomModel::where('id',$params['id'])->where('status',1)->find();
  79. return $this->success('success', $info);
  80. }
  81. public function apply()
  82. {
  83. $params = $this->request->param();
  84. if (empty($params['room_id'])) {
  85. return $this->error('参数缺失');
  86. }
  87. if (empty($params['start_date'])) {
  88. return $this->error('参数缺失');
  89. }
  90. if (empty($params['end_date'])) {
  91. return $this->error('参数缺失');
  92. }
  93. if (empty($params['name'])) {
  94. return $this->error('参数缺失');
  95. }
  96. if (empty($params['phone'])) {
  97. return $this->error('参数缺失');
  98. }
  99. $user_id = $this->auth->id;
  100. $info = HotelRoomModel::with([
  101. 'hotel'=>function ($query) {
  102. $query->field(['id','name','image','images','price','original_price']);
  103. }
  104. ])->where('id',$params['room_id'])->where('status',1)->find();
  105. if (!$info) {
  106. return $this->error('房间不存在');
  107. }
  108. if (empty($info['hotel'])) {
  109. return $this->error('酒店信息有误');
  110. }
  111. // 开始报名
  112. $data = [
  113. 'hotel_id' => $info['hotel_id'],
  114. 'room_id' => $info['id'],
  115. 'user_id' => $user_id,
  116. 'name' => $params['name'],
  117. 'phone' => $params['phone'],
  118. 'start_date' => $params['start_date'],
  119. 'end_date' => $params['end_date'],
  120. 'days' => (int)((strtotime($params['end_date']) - strtotime($params['start_date'])) / 86400),
  121. 'order_no' => createUniqueNo('H', $user_id),
  122. 'pay_amount' => bcmul($info['price'], 1, 2),
  123. 'status' => 1,
  124. 'create_time' => time()
  125. ];
  126. if (!Db::name('hotel_order')->insertGetId($data)) {
  127. return $this->error('订单创建失败');
  128. }
  129. return $this->success('提交成功');
  130. }
  131. // 订单列表
  132. public function myApply()
  133. {
  134. $params = $this->request->param();
  135. $user_id = $this->auth->id;
  136. $query = HotelOrderModel::with([
  137. 'room'=>function ($query) {
  138. $query->field(['id','name','image']);
  139. }
  140. ])->where('user_id',$user_id);
  141. switch ($params['status']){
  142. case 1:
  143. $query->where('start_date','<',date('Y-m-d'))->where('status',1);
  144. break;
  145. case 2:
  146. $query->where('start_date','>=',date('Y-m-d'))->where('status',1);
  147. break;
  148. case 3:
  149. $query->where('status',0);
  150. break;
  151. default:
  152. break;
  153. }
  154. $list = $query->order('id','desc')->autopage()->select();
  155. foreach ($list as $key=>$val){
  156. if ($val['status'] == 1){
  157. if ($val['start_date'] < date('Y-m-d')){
  158. $list[$key]['status_txt'] = '待入住';
  159. }else{
  160. $list[$key]['status_txt'] = '已入住';
  161. }
  162. }else{
  163. $list[$key]['status_txt'] = '已取消';
  164. }
  165. }
  166. return $this->success('获取成功',$list);
  167. }
  168. // 订单取消
  169. public function applyCancel()
  170. {
  171. $params = $this->request->param();
  172. $user_id = $this->auth->id;
  173. $order = HotelOrderModel::where('user_id',$user_id)->where('id',$params['order_id'])->where('status',1)->find();
  174. if (!$order){
  175. return $this->error('订单不存在或已取消');
  176. }
  177. if (date('Y-m-d H:i:s') > ($order['start_date'] . ' 12:00:00')){
  178. return $this->error('订单已入住,无法取消');
  179. }
  180. if (!HotelOrderModel::where('user_id',$user_id)->where('id',$params['order_id'])->where('status',1)->update(['status'=>0])){
  181. return $this->error('操作失败');
  182. }
  183. return $this->success('操作成功');
  184. }
  185. }