HotelCanteen.php 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. <?php
  2. namespace app\api\controller;
  3. use addons\epay\library\Service;
  4. use app\common\controller\Api;
  5. use app\common\model\HotelCanteenModel;
  6. use app\common\model\HotelCanteenOrderModel;
  7. use app\common\model\HotelCanteenRoomModel;
  8. use app\common\model\HotelModel;
  9. use app\common\model\HotelOrderModel;
  10. use app\common\model\HotelRoomModel;
  11. use app\common\model\PayOrderModel;
  12. use app\common\model\UniversityEventModel;
  13. use app\common\model\Wallet;
  14. use app\utils\CurlUtil;
  15. use app\utils\Service\Tencent\TencentIm;
  16. use think\Db;
  17. /**
  18. * 老年大学 活动板块
  19. */
  20. class HotelCanteen extends Api
  21. {
  22. protected $noNeedLogin = [''];
  23. protected $noNeedRight = ['*'];
  24. // 活动列表
  25. public function list()
  26. {
  27. $params = $this->request->param();
  28. $params['sort_type'] = !empty($params['sort_type']) ? $params['sort_type'] : 1;
  29. $field = ['id','name','image','price','original_price','tags','lat','lng','address'];
  30. if (!empty($params['lng']) && !empty($params['lng'])){
  31. $field[] = "(st_distance(point ({$params['lng']}, {$params['lat']}),point(lng,lat))*111195) as distance";
  32. }
  33. $query = HotelCanteenModel::field($field);
  34. if (!empty($params['lng']) && !empty($params['lng'])){
  35. if ($params['sort_distance'] == 1){
  36. $query->order('distance asc');
  37. }else{
  38. $query->order('distance desc');
  39. }
  40. }
  41. if ($params['sort_price'] == 1){
  42. $query->order('price asc');
  43. }else{
  44. $query->order('price desc');
  45. }
  46. $list = $query->where('status', 1)
  47. ->order('weigh desc')
  48. ->order('id desc')
  49. ->autopage()
  50. ->select();
  51. foreach ($list as $k => $v) {
  52. // 计算距离
  53. $list[$k]['distance'] = distance_ext($v['distance'] ?? -1);
  54. }
  55. return $this->success('success', $list);
  56. }
  57. // 详情
  58. public function info()
  59. {
  60. $params = $this->request->param();
  61. $field = ['id','name','image','images','price','original_price','tags','lat','lng','address'];
  62. if (!empty($params['lng']) && !empty($params['lng'])){
  63. $field[] = "(st_distance(point ({$params['lng']}, {$params['lat']}),point(lng,lat))*111195) as distance";
  64. }
  65. $query = HotelCanteenModel::field($field);
  66. $query->where('id', $params['id']);
  67. $info = $query->where('status', 1)->find();
  68. if (!$info){
  69. return $this->error('信息不存在');
  70. }
  71. // 计算距离
  72. $info['distance'] = distance_ext($info['distance'] ?? -1);
  73. // 房间列表
  74. $info['room_list'] = HotelCanteenRoomModel::where('canteen_id',$info['id'])->where('status',1)->order('weigh desc')->order('id desc')->select();
  75. return $this->success('success', $info);
  76. }
  77. public function apply()
  78. {
  79. $params = $this->request->param();
  80. if (empty($params['room_id'])) {
  81. return $this->error('参数缺失');
  82. }
  83. if (empty($params['get_to_time'])) {
  84. return $this->error('参数缺失');
  85. }
  86. if (empty($params['name'])) {
  87. return $this->error('参数缺失');
  88. }
  89. if (empty($params['phone'])) {
  90. return $this->error('参数缺失');
  91. }
  92. if (empty($params['sex'])) {
  93. return $this->error('参数缺失');
  94. }
  95. $user_id = $this->auth->id;
  96. $info = HotelCanteenRoomModel::with([
  97. 'canteen'=>function ($query) {
  98. $query->field(['id','name','image','images','price','original_price']);
  99. }
  100. ])->where('id',$params['room_id'])->where('status',1)->find();
  101. if (!$info) {
  102. return $this->error('餐桌不存在');
  103. }
  104. if (empty($info['canteen'])) {
  105. return $this->error('餐厅信息有误');
  106. }
  107. // 开始报名
  108. $data = [
  109. 'canteen_id' => $info['canteen_id'],
  110. 'room_id' => $info['id'],
  111. 'user_id' => $user_id,
  112. 'name' => $params['name'],
  113. 'phone' => $params['phone'],
  114. 'get_to_time' => strtotime($params['get_to_time']),
  115. 'sex' => $params['sex'],
  116. 'remark' => $params['remark'] ?? '',
  117. 'order_no' => createUniqueNo('H', $user_id),
  118. 'pay_amount' => bcmul($info['price'], 1, 2),
  119. 'status' => 1,
  120. 'create_time' => time()
  121. ];
  122. if (!Db::name('hotel_canteen_order')->insertGetId($data)) {
  123. return $this->error('订单创建失败');
  124. }
  125. return $this->success('提交成功');
  126. }
  127. // 订单列表
  128. public function myApply()
  129. {
  130. $params = $this->request->param();
  131. $user_id = $this->auth->id;
  132. $query = HotelCanteenOrderModel::with([
  133. 'canteen'=>function ($query) {
  134. $query->field(['id','name','image']);
  135. },
  136. 'room'=>function ($query) {
  137. $query->field(['id','name','image']);
  138. }
  139. ])->where('user_id',$user_id);
  140. switch ($params['status']){
  141. case 1:
  142. $query->where('get_to_time','>',time())->where('status',1);
  143. break;
  144. case 2:
  145. $query->where('get_to_time','<=',time())->where('status',1);
  146. break;
  147. case 3:
  148. $query->where('status',0);
  149. break;
  150. default:
  151. break;
  152. }
  153. $list = $query->order('id','desc')->autopage()->select();
  154. foreach ($list as $key=>$val){
  155. if ($val['status'] == 1){
  156. if ($val['get_to_time'] > time()){
  157. $list[$key]['status_code'] = 1;
  158. $list[$key]['status_txt'] = '待用餐';
  159. }else{
  160. $list[$key]['status_code'] = 2;
  161. $list[$key]['status_txt'] = '已完成';
  162. }
  163. }else{
  164. $list[$key]['status_code'] = 3;
  165. $list[$key]['status_txt'] = '已取消';
  166. }
  167. $list[$key]['get_to_time'] = date('Y-m-d H:i');
  168. }
  169. return $this->success('获取成功',$list);
  170. }
  171. // 订单详情
  172. public function myApplyInfo()
  173. {
  174. $params = $this->request->param();
  175. $user_id = $this->auth->id;
  176. $query = HotelCanteenOrderModel::with([
  177. 'canteen'=>function ($query) {
  178. $query->field(['id','name','image']);
  179. },
  180. 'room'=>function ($query) {
  181. $query->field(['id','name','image']);
  182. }
  183. ])->where('id',$params['id'])->where('user_id',$user_id);
  184. $info = $query->find();
  185. if ($info['status'] == 1){
  186. if ($info['get_to_time'] > time()){
  187. $info['status_code'] = 1;
  188. $info['status_txt'] = '待用餐';
  189. }else{
  190. $info['status_code'] = 2;
  191. $info['status_txt'] = '已完成';
  192. }
  193. }else{
  194. $info['status_code'] = 3;
  195. $info['status_txt'] = '已取消';
  196. }
  197. $info['get_to_time'] = date('Y-m-d H:i');
  198. return $this->success('获取成功',$info);
  199. }
  200. // 订单取消
  201. public function applyCancel()
  202. {
  203. $params = $this->request->param();
  204. $user_id = $this->auth->id;
  205. $order = HotelCanteenOrderModel::where('user_id',$user_id)->where('id',$params['order_id'])->where('status',1)->find();
  206. if (!$order){
  207. return $this->error('订单不存在或已取消');
  208. }
  209. if (time() > $order['get_to_time']){
  210. return $this->error('已到预约时间,无法取消');
  211. }
  212. if (!HotelCanteenOrderModel::where('user_id',$user_id)->where('id',$params['order_id'])->where('status',1)->update(['status'=>0])){
  213. return $this->error('操作失败');
  214. }
  215. return $this->success('操作成功');
  216. }
  217. }