Hotel.php 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  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_distance'] = !empty($params['sort_distance']) ? $params['sort_distance'] : 1;
  32. $params['sort_price'] = !empty($params['sort_price']) ? $params['sort_price'] : 1;
  33. $field = ['id','name','image','price','original_price','tags','lat','lng'];
  34. if (!empty($params['lng']) && !empty($params['lng'])){
  35. $field[] = "(st_distance(point ({$params['lng']}, {$params['lat']}),point(lng,lat))*111195) as distance";
  36. }
  37. $query = HotelModel::field($field);
  38. if (!empty($params['lng']) && !empty($params['lng'])){
  39. if ($params['sort_distance'] == 1){
  40. $query->order('distance asc');
  41. }else{
  42. $query->order('distance desc');
  43. }
  44. }
  45. if ($params['sort_price'] == 1){
  46. $query->order('price asc');
  47. }else{
  48. $query->order('price desc');
  49. }
  50. $list = $query->where('status', 1)
  51. ->order('weigh desc')
  52. ->order('id desc')
  53. ->autopage()
  54. ->select();
  55. foreach ($list as $k => $v) {
  56. // 计算距离
  57. $list[$k]['distance'] = distance_ext($v['distance'] ?? -1);
  58. }
  59. return $this->success('success', $list);
  60. }
  61. // 详情
  62. public function info()
  63. {
  64. $params = $this->request->param();
  65. $field = ['id','name','image','images','price','original_price','tags','lat','lng','address'];
  66. if (!empty($params['lng']) && !empty($params['lng'])){
  67. $field[] = "(st_distance(point ({$params['lng']}, {$params['lat']}),point(lng,lat))*111195) as distance";
  68. }
  69. $query = HotelModel::field($field);
  70. $query->where('id', $params['id']);
  71. $info = $query->where('status', 1)->find();
  72. if (!$info){
  73. return $this->error('信息不存在');
  74. }
  75. // 计算距离
  76. $info['distance'] = distance_ext($info['distance'] ?? -1);
  77. // 房间列表
  78. $info['room_list'] = HotelRoomModel::where('hotel_id',$info['id'])->where('status',1)->order('weigh desc')->order('id desc')->select();
  79. return $this->success('success', $info);
  80. }
  81. // 详情
  82. public function room_info()
  83. {
  84. $params = $this->request->param();
  85. $info = HotelRoomModel::where('id',$params['id'])->where('status',1)->find();
  86. return $this->success('success', $info);
  87. }
  88. public function apply()
  89. {
  90. $params = $this->request->param();
  91. if (empty($params['room_id'])) {
  92. return $this->error('参数缺失');
  93. }
  94. if (empty($params['start_date'])) {
  95. return $this->error('参数缺失');
  96. }
  97. if (empty($params['end_date'])) {
  98. return $this->error('参数缺失');
  99. }
  100. if (empty($params['name'])) {
  101. return $this->error('参数缺失');
  102. }
  103. if (empty($params['phone'])) {
  104. return $this->error('参数缺失');
  105. }
  106. $user_id = $this->auth->id;
  107. $info = HotelRoomModel::with([
  108. 'hotel'=>function ($query) {
  109. $query->field(['id','name','image','images','price','original_price']);
  110. }
  111. ])->where('id',$params['room_id'])->where('status',1)->find();
  112. if (!$info) {
  113. return $this->error('房间不存在');
  114. }
  115. if (empty($info['hotel'])) {
  116. return $this->error('酒店信息有误');
  117. }
  118. // 开始报名
  119. $data = [
  120. 'hotel_id' => $info['hotel_id'],
  121. 'room_id' => $info['id'],
  122. 'user_id' => $user_id,
  123. 'name' => $params['name'],
  124. 'phone' => $params['phone'],
  125. 'start_date' => $params['start_date'],
  126. 'end_date' => $params['end_date'],
  127. 'days' => (int)((strtotime($params['end_date']) - strtotime($params['start_date'])) / 86400),
  128. 'order_no' => createUniqueNo('H', $user_id),
  129. 'pay_amount' => bcmul($info['price'], 1, 2),
  130. 'status' => 1,
  131. 'create_time' => time()
  132. ];
  133. if (!Db::name('hotel_order')->insertGetId($data)) {
  134. return $this->error('订单创建失败');
  135. }
  136. return $this->success('提交成功');
  137. }
  138. // 订单列表
  139. public function myApply()
  140. {
  141. $params = $this->request->param();
  142. $user_id = $this->auth->id;
  143. $query = HotelOrderModel::with([
  144. 'room'=>function ($query) {
  145. $query->field(['id','name','image']);
  146. }
  147. ])->where('user_id',$user_id);
  148. switch ($params['status']){
  149. case 1:
  150. $query->where('start_date','<',date('Y-m-d'))->where('status',1);
  151. break;
  152. case 2:
  153. $query->where('start_date','>=',date('Y-m-d'))->where('status',1);
  154. break;
  155. case 3:
  156. $query->where('status',0);
  157. break;
  158. default:
  159. break;
  160. }
  161. $list = $query->order('id','desc')->autopage()->select();
  162. foreach ($list as $key=>$val){
  163. if ($val['status'] == 1){
  164. if ($val['start_date'] < date('Y-m-d')){
  165. $list[$key]['status_txt'] = '待入住';
  166. }else{
  167. $list[$key]['status_txt'] = '已入住';
  168. }
  169. }else{
  170. $list[$key]['status_txt'] = '已取消';
  171. }
  172. }
  173. return $this->success('获取成功',$list);
  174. }
  175. // 订单取消
  176. public function applyCancel()
  177. {
  178. $params = $this->request->param();
  179. $user_id = $this->auth->id;
  180. $order = HotelOrderModel::where('user_id',$user_id)->where('id',$params['order_id'])->where('status',1)->find();
  181. if (!$order){
  182. return $this->error('订单不存在或已取消');
  183. }
  184. if (date('Y-m-d H:i:s') > ($order['start_date'] . ' 12:00:00')){
  185. return $this->error('订单已入住,无法取消');
  186. }
  187. if (!HotelOrderModel::where('user_id',$user_id)->where('id',$params['order_id'])->where('status',1)->update(['status'=>0])){
  188. return $this->error('操作失败');
  189. }
  190. return $this->success('操作成功');
  191. }
  192. }