Hotel.php 7.8 KB

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