Hotel.php 10 KB

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