HotelCanteen.php 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  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. $model = new HotelCanteenRoomModel();
  97. $info = $model->getDetail(
  98. params: ['id'=>$params['room_id']],
  99. with: [
  100. 'canteen'
  101. ]
  102. );
  103. if (!$info) {
  104. return $this->error('餐桌不存在');
  105. }
  106. if (empty($info['canteen'])) {
  107. return $this->error('餐厅信息有误');
  108. }
  109. // 开始报名
  110. $data = [
  111. 'canteen_id' => $info['canteen_id'],
  112. 'room_id' => $info['id'],
  113. 'user_id' => $user_id,
  114. 'name' => $params['name'],
  115. 'phone' => $params['phone'],
  116. 'get_to_time' => strtotime($params['get_to_time']),
  117. 'sex' => $params['sex'],
  118. 'remark' => $params['remark'] ?? '',
  119. 'order_no' => createUniqueNo('C', $user_id),
  120. 'pay_amount' => bcmul($info['price'], 1, 2),
  121. 'status' => 1,
  122. 'create_time' => time()
  123. ];
  124. if (!Db::name('hotel_canteen_order')->insertGetId($data)) {
  125. return $this->error('订单创建失败');
  126. }
  127. return $this->success('提交成功',[
  128. 'order_no' => $data['order_no'],
  129. 'pay_amount' => $data['pay_amount'],
  130. 'order_type' => 'hotel_order',
  131. ]);
  132. }
  133. // 订单列表
  134. public function myApply()
  135. {
  136. $params = $this->request->param();
  137. $user_id = $this->auth->id;
  138. $query = HotelCanteenOrderModel::with([
  139. 'canteen'=>function ($query) {
  140. $query->field(['id','name','image']);
  141. },
  142. 'room'=>function ($query) {
  143. $query->field(['id','name','image']);
  144. }
  145. ])->where('user_id',$user_id);
  146. switch ($params['status']){
  147. case 1:
  148. $query->where('get_to_time','>',time())->where('status',1);
  149. break;
  150. case 2:
  151. $query->where('get_to_time','<=',time())->where('status',1);
  152. break;
  153. case 3:
  154. $query->where('status',0);
  155. break;
  156. default:
  157. break;
  158. }
  159. $list = $query->order('id','desc')->autopage()->select();
  160. foreach ($list as $key=>$val){
  161. if ($val['status'] == 1){
  162. if ($val['get_to_time'] > time()){
  163. $list[$key]['status_code'] = 1;
  164. $list[$key]['status_txt'] = '待用餐';
  165. }else{
  166. $list[$key]['status_code'] = 2;
  167. $list[$key]['status_txt'] = '已完成';
  168. }
  169. }else{
  170. $list[$key]['status_code'] = 3;
  171. $list[$key]['status_txt'] = '已取消';
  172. }
  173. $list[$key]['get_to_time'] = date('Y-m-d H:i');
  174. }
  175. return $this->success('获取成功',$list);
  176. }
  177. // 订单详情
  178. public function myApplyInfo()
  179. {
  180. $params = $this->request->param();
  181. $user_id = $this->auth->id;
  182. $query = HotelCanteenOrderModel::with([
  183. 'canteen'=>function ($query) {
  184. $query->field(['id','name','image']);
  185. },
  186. 'room'=>function ($query) {
  187. $query->field(['id','name','image']);
  188. }
  189. ])->where('id',$params['id'])->where('user_id',$user_id);
  190. $info = $query->find();
  191. if ($info['status'] == 1){
  192. if ($info['get_to_time'] > time()){
  193. $info['status_code'] = 1;
  194. $info['status_txt'] = '待用餐';
  195. }else{
  196. $info['status_code'] = 2;
  197. $info['status_txt'] = '已完成';
  198. }
  199. }else{
  200. $info['status_code'] = 3;
  201. $info['status_txt'] = '已取消';
  202. }
  203. $info['get_to_time'] = date('Y-m-d H:i');
  204. return $this->success('获取成功',$info);
  205. }
  206. // 订单取消
  207. public function applyCancel()
  208. {
  209. $params = $this->request->param();
  210. $user_id = $this->auth->id;
  211. $order = HotelCanteenOrderModel::where('user_id',$user_id)->where('id',$params['order_id'])->where('status',1)->find();
  212. if (!$order){
  213. return $this->error('订单不存在或已取消');
  214. }
  215. if (time() > $order['get_to_time']){
  216. return $this->error('已到预约时间,无法取消');
  217. }
  218. if (!HotelCanteenOrderModel::where('user_id',$user_id)->where('id',$params['order_id'])->where('status',1)->update(['status'=>0])){
  219. return $this->error('操作失败');
  220. }
  221. return $this->success('操作成功');
  222. }
  223. }