Hotel.php 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  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['num'])) {
  105. return $this->error('参数缺失');
  106. }
  107. if (empty($params['name'])) {
  108. return $this->error('参数缺失');
  109. }
  110. if (empty($params['phone'])) {
  111. return $this->error('参数缺失');
  112. }
  113. $user_id = $this->auth->id;
  114. $info = HotelRoomModel::with([
  115. 'hotel'=>function ($query) {
  116. $query->field(['id','name','image','images','price','original_price']);
  117. }
  118. ])->where('id',$params['room_id'])->where('status',1)->find();
  119. if (!$info) {
  120. return $this->error('房间不存在');
  121. }
  122. if (empty($info['hotel'])) {
  123. return $this->error('酒店信息有误');
  124. }
  125. $days = (int)((strtotime($params['end_date']) - strtotime($params['start_date'])) / 86400);
  126. // 开始报名
  127. $data = [
  128. 'hotel_id' => $info['hotel_id'],
  129. 'room_id' => $info['id'],
  130. 'user_id' => $user_id,
  131. 'num' => $params['num'],
  132. 'name' => $params['name'],
  133. 'phone' => $params['phone'],
  134. 'start_date' => $params['start_date'],
  135. 'end_date' => $params['end_date'],
  136. 'days' => $days,
  137. 'order_no' => createUniqueNo('H', $user_id),
  138. 'pay_amount' => bcmul(bcmul($info['price'], $days, 2),$params['num'],2),
  139. 'status' => 1,
  140. 'create_time' => time()
  141. ];
  142. if (!Db::name('hotel_order')->insertGetId($data)) {
  143. return $this->error('订单创建失败');
  144. }
  145. return $this->success('提交成功');
  146. }
  147. // 订单列表
  148. public function myApply()
  149. {
  150. $params = $this->request->param();
  151. $user_id = $this->auth->id;
  152. $query = HotelOrderModel::with([
  153. 'room'=>function ($query) {
  154. $query->field(['id','name','image']);
  155. }
  156. ])->where('user_id',$user_id);
  157. switch ($params['status']){
  158. case 1:
  159. $query->where('start_date','>',date('Y-m-d'))->where('status',1);
  160. break;
  161. case 2:
  162. $query->where('start_date','<=',date('Y-m-d'))->where('status',1);
  163. break;
  164. case 3:
  165. $query->where('status',0);
  166. break;
  167. default:
  168. break;
  169. }
  170. $list = $query->order('id','desc')->autopage()->select();
  171. foreach ($list as $key=>$val){
  172. if ($val['status'] == 1){
  173. if ($val['start_date'] > date('Y-m-d')){
  174. $list[$key]['status_code'] = 1;
  175. $list[$key]['status_txt'] = '待入住';
  176. }else{
  177. $list[$key]['status_code'] = 2;
  178. $list[$key]['status_txt'] = '已入住';
  179. }
  180. }else{
  181. $list[$key]['status_code'] = 3;
  182. $list[$key]['status_txt'] = '已取消';
  183. }
  184. }
  185. return $this->success('获取成功',$list);
  186. }
  187. // 订单详情
  188. public function myApplyInfo()
  189. {
  190. $params = $this->request->param();
  191. $user_id = $this->auth->id;
  192. $query = HotelOrderModel::with([
  193. 'hotel' => function ($query) {
  194. $query->field(['id','name','image']);
  195. },
  196. 'room'=>function ($query) {
  197. $query->field(['id','name','image','space','floor','is_wifi','window','breakfast','people_num','bad']);
  198. }
  199. ])->where('id',$params['id'])->where('user_id',$user_id);
  200. $info = $query->find();
  201. if ($info['status'] == 1){
  202. if ($info['start_date'] > date('Y-m-d')){
  203. $info['status_code'] = 1;
  204. $info['status_txt'] = '待入住';
  205. }else{
  206. $info['status_code'] = 2;
  207. $info['status_txt'] = '已入住';
  208. }
  209. }else{
  210. $info['status_code'] = 3;
  211. $info['status_txt'] = '已取消';
  212. }
  213. return $this->success('获取成功',$info);
  214. }
  215. // 订单取消
  216. public function applyCancel()
  217. {
  218. $params = $this->request->param();
  219. $user_id = $this->auth->id;
  220. $order = HotelOrderModel::where('user_id',$user_id)->where('id',$params['order_id'])->where('status',1)->find();
  221. if (!$order){
  222. return $this->error('订单不存在或已取消');
  223. }
  224. if (time() > strtotime($order['start_date'] . ' 12:00:00')){
  225. return $this->error('订单已入住,无法取消');
  226. }
  227. if (!HotelOrderModel::where('user_id',$user_id)->where('id',$params['order_id'])->where('status',1)->update(['status'=>0])){
  228. return $this->error('操作失败');
  229. }
  230. return $this->success('操作成功');
  231. }
  232. }