Hotel.php 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  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. $info['business_license'] = cdnurl(config('site.business_license'));
  87. return $this->success('success', $info);
  88. }
  89. public function apply()
  90. {
  91. $params = $this->request->param();
  92. if (empty($params['room_id'])) {
  93. return $this->error('参数缺失');
  94. }
  95. if (empty($params['start_date'])) {
  96. return $this->error('参数缺失');
  97. }
  98. if (empty($params['end_date'])) {
  99. return $this->error('参数缺失');
  100. }
  101. if (empty($params['name'])) {
  102. return $this->error('参数缺失');
  103. }
  104. if (empty($params['phone'])) {
  105. return $this->error('参数缺失');
  106. }
  107. $user_id = $this->auth->id;
  108. $info = HotelRoomModel::with([
  109. 'hotel'=>function ($query) {
  110. $query->field(['id','name','image','images','price','original_price']);
  111. }
  112. ])->where('id',$params['room_id'])->where('status',1)->find();
  113. if (!$info) {
  114. return $this->error('房间不存在');
  115. }
  116. if (empty($info['hotel'])) {
  117. return $this->error('酒店信息有误');
  118. }
  119. // 开始报名
  120. $data = [
  121. 'hotel_id' => $info['hotel_id'],
  122. 'room_id' => $info['id'],
  123. 'user_id' => $user_id,
  124. 'name' => $params['name'],
  125. 'phone' => $params['phone'],
  126. 'start_date' => $params['start_date'],
  127. 'end_date' => $params['end_date'],
  128. 'days' => (int)((strtotime($params['end_date']) - strtotime($params['start_date'])) / 86400),
  129. 'order_no' => createUniqueNo('H', $user_id),
  130. 'pay_amount' => bcmul($info['price'], 1, 2),
  131. 'status' => 1,
  132. 'create_time' => time()
  133. ];
  134. if (!Db::name('hotel_order')->insertGetId($data)) {
  135. return $this->error('订单创建失败');
  136. }
  137. return $this->success('提交成功');
  138. }
  139. // 订单列表
  140. public function myApply()
  141. {
  142. $params = $this->request->param();
  143. $user_id = $this->auth->id;
  144. $query = HotelOrderModel::with([
  145. 'room'=>function ($query) {
  146. $query->field(['id','name','image']);
  147. }
  148. ])->where('user_id',$user_id);
  149. switch ($params['status']){
  150. case 1:
  151. $query->where('start_date','<',date('Y-m-d'))->where('status',1);
  152. break;
  153. case 2:
  154. $query->where('start_date','>=',date('Y-m-d'))->where('status',1);
  155. break;
  156. case 3:
  157. $query->where('status',0);
  158. break;
  159. default:
  160. break;
  161. }
  162. $list = $query->order('id','desc')->autopage()->select();
  163. foreach ($list as $key=>$val){
  164. if ($val['status'] == 1){
  165. if ($val['start_date'] < date('Y-m-d')){
  166. $list[$key]['status_txt'] = '待入住';
  167. }else{
  168. $list[$key]['status_txt'] = '已入住';
  169. }
  170. }else{
  171. $list[$key]['status_txt'] = '已取消';
  172. }
  173. }
  174. return $this->success('获取成功',$list);
  175. }
  176. // 订单取消
  177. public function applyCancel()
  178. {
  179. $params = $this->request->param();
  180. $user_id = $this->auth->id;
  181. $order = HotelOrderModel::where('user_id',$user_id)->where('id',$params['order_id'])->where('status',1)->find();
  182. if (!$order){
  183. return $this->error('订单不存在或已取消');
  184. }
  185. if (date('Y-m-d H:i:s') > ($order['start_date'] . ' 12:00:00')){
  186. return $this->error('订单已入住,无法取消');
  187. }
  188. if (!HotelOrderModel::where('user_id',$user_id)->where('id',$params['order_id'])->where('status',1)->update(['status'=>0])){
  189. return $this->error('操作失败');
  190. }
  191. return $this->success('操作成功');
  192. }
  193. }