TeahouseController.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. <?php
  2. namespace api\teahouse\controller;
  3. use app\teahouse\model\LockBookModel;
  4. use app\teahouse\model\LockModel;
  5. use app\teahouse\model\LockUserModel;
  6. use cmf\controller\RestBaseController;
  7. class TeahouseController extends RestBaseController
  8. {
  9. public function getTeahouseList()
  10. {
  11. $param = $this->request->param();
  12. $page = empty($param['page']) ? 1 : $param['page'];
  13. $size = empty($param['size']) ? 10 : $param['size'];
  14. //搜索条件
  15. $list = LockModel::where('is_show', 1)->page($page, $size)->select();
  16. //数据处理
  17. if (!$list->isEmpty()) {
  18. foreach ($list as $v) {
  19. $v['image'] = cmf_get_image_preview_url($v['image']);
  20. }
  21. }
  22. $this->success('成功', $list);
  23. }
  24. public function getTimeByDate()
  25. {
  26. $lock_id = $this->request->param('lock_id');
  27. $date = $this->request->param('date');
  28. $date = str_replace(['年', '月', '日'], ['-', '-', ''], $date);
  29. $date_start = strtotime($date);
  30. $date_end = strtotime($date . ' +1 day');
  31. $list = LockBookModel::where('lock_id', $lock_id)
  32. ->where('start_time', '>=', $date_start)
  33. ->where('end_time', '<=', $date_end)
  34. ->select();
  35. $res = [];
  36. if (!$list->isEmpty()) {
  37. foreach ($list as $v) {
  38. $res[] = date('H:i', $v['start_time']) . ' - ' . date('H:i', $v['end_time']);
  39. }
  40. }
  41. $this->success('成功', $res);
  42. }
  43. public function reservation()
  44. {
  45. //茶室
  46. $lock_id = $this->request->param('lock_id');
  47. if (empty($lock_id)) {
  48. $this->error('参数异常');
  49. }
  50. $lock = LockModel::where('id', $lock_id)->find();
  51. //日期
  52. $date = $this->request->param('date');
  53. if (empty($date)) {
  54. $this->error('请选择要预约的日期');
  55. }
  56. $date = str_replace(['年', '月', '日'], ['-', '-', ''], $date);
  57. //时间
  58. $time_list = $this->request->param('time');
  59. if (empty($time_list)) {
  60. $this->error('请选择要预约的时间');
  61. }
  62. if (count($time_list) > 3) {
  63. $this->error('一次最多预约3个小时');
  64. }
  65. //爽约次数
  66. $unuse = LockBookModel::where('user', $this->user['id'])->where('is_use',2)->count();
  67. if ($unuse >= 3) {
  68. $this->error('您爽约和预约的总时长超过3小时,无法再预约,请联系管理员');
  69. }
  70. //添加预约信息
  71. $user = LockUserModel::where('user_id', $this->user['id'])->find();
  72. $now = time();
  73. $relation_id = uniqid();
  74. foreach ($time_list as $v) {
  75. $add = [];
  76. $time = explode('-', $v);
  77. $add['lock_id'] = $lock_id;
  78. $add['user_id'] = $user['user_id'];
  79. $add['lock_name'] = $lock['name'];
  80. $add['user_name'] = $user['name'];
  81. $add['start_time'] = strtotime($date . ' ' . trim($time[0]) . ':00');
  82. $add['end_time'] = strtotime($date . ' ' . trim($time[1]) . ':00');
  83. $add['relation_id'] = $relation_id;
  84. $add['create_time'] = $now;
  85. LockBookModel::create($add);
  86. }
  87. $this->success('成功');
  88. }
  89. }