VisitorOrder.php 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. <?php
  2. namespace app\api\controller\h5;
  3. use app\hander\HelpHander;
  4. use think\facade\Request;
  5. use think\Db;
  6. class VisitorOrder extends Base
  7. {
  8. // 获取我的预约记录
  9. public function list(){
  10. $lists = Db::name('visitor_order')
  11. ->field('id,org_id,status,service_id,service_time_id,name,phone')
  12. ->where('user_id',$this->userId)
  13. ->where('del',0)
  14. ->whereIn('type',[1,2])
  15. ->select();
  16. foreach ($lists as $k=>$v){
  17. $lists[$k]['org_name'] = Db::name('org')->where('id',$v['org_id'])->value('name');
  18. $lists[$k]['title'] = '';
  19. $lists[$k]['day'] = '';
  20. $lists[$k]['start'] = '';
  21. $lists[$k]['end'] = '';
  22. if($v['service_time_id'] > 0){
  23. $time = Db::name('service_time')->where('id',$v['service_time_id'])->find();
  24. $lists[$k]['day'] = $time?$time['day']:'';
  25. $lists[$k]['start'] = date('H:i',strtotime($time['day'].' '.$time['start']));
  26. $lists[$k]['end'] = date('H:i',strtotime($time['day'].' '.$time['end']));
  27. $lists[$k]['title'] = Db::name('service')->where('id',$v['service_id'])->value('title');
  28. $v = $this->autoCheck($v,$time);
  29. $lists[$k]['status'] = $v['status'];
  30. }
  31. $lists[$k]['qrcode'] = Request::domain().'/admin/Qrcode/qrcode?code='.get_qrcode_str('visitor',$v['id']);
  32. }
  33. HelpHander::success($lists);
  34. }
  35. // 获取我的预约记录--小程序
  36. public function list2(){
  37. $type = input('type/d',0);
  38. if(!in_array($type,[0,1,2])){
  39. HelpHander::error('参数错误');
  40. }
  41. $map[] = ['del','=',0];
  42. $map[] = ['user_id','=',$this->userId];
  43. if($type==1){
  44. $map[] = ['status','=',0];
  45. }else if($type==2){
  46. $map[] = ['status','in',[1,4]];
  47. }
  48. $lists = Db::name('visitor_order')->where($map)->select();
  49. $lists = $lists?$lists:[];
  50. foreach ($lists as $k=>$v){
  51. $lists[$k]['org_name'] = Db::name('org')->where('id',$v['org_id'])->value('name');
  52. $lists[$k]['from_user_name'] = Db::name('user')->where('id',$v['from_user_id'])->value('real_name');
  53. $lists[$k]['qrcode'] = Request::domain().'/admin/Qrcode/qrcode?code='.get_qrcode_str('visitor',$v['id']);
  54. }
  55. HelpHander::success($lists);
  56. }
  57. // 提交确认
  58. public function order(){
  59. $type = input('type');
  60. $serviceTimeId = input('serviceTimeId',0);
  61. $name = input('name','','trim');
  62. $phone = input('phone','','trim');
  63. if(!in_array($type,[1,2])){
  64. HelpHander::error('参数错误');
  65. }
  66. if($type == 1 && $serviceTimeId <= 0){
  67. HelpHander::error('未选择预约时间');
  68. }
  69. if(!$name){
  70. HelpHander::error('请填写姓名');
  71. }
  72. if(!check_mobile($phone)){
  73. HelpHander::error('手机号格式不正确');
  74. }
  75. $data = [
  76. 'name' => $name,
  77. 'phone' => $phone,
  78. 'org_id' => $this->orgId,
  79. 'user_id' => $this->userId,
  80. 'status' => 0,
  81. 'type' => $type,
  82. 'create_time' => date('Y-m-d H:i:s')
  83. ];
  84. if($type == 1){
  85. $service = Db::name('service_time')
  86. ->alias('st')
  87. ->join('service s','s.id = st.service_id')
  88. ->where('st.id',$serviceTimeId)
  89. ->where('s.enable',1)
  90. ->where('s.del',0)
  91. ->where('st.enable',1)
  92. ->where('st.del',0)
  93. ->find();
  94. if(!$service){
  95. HelpHander::error('该服务已关闭');
  96. }
  97. // 检查时间段是否已过
  98. $endTime = strtotime($service['day'].' '.$service['end']);
  99. if(time() >= $endTime){
  100. HelpHander::error('该时间段已过,无法预约');
  101. }
  102. // 检查是否还有名额
  103. $count = Db::name('visitor_order')
  104. ->where('service_time_id',$serviceTimeId)
  105. ->whereIn('status',[0,1,2,3])
  106. ->where('del',0)
  107. ->count();
  108. if($service['limit'] <= $count){
  109. HelpHander::error('该时间段已无预约名额');
  110. }
  111. // 检查用户是否已预约改时间段
  112. $ret = Db::name('visitor_order')
  113. ->where('user_id',$this->userId)
  114. ->where('service_time_id',$serviceTimeId)
  115. ->whereIn('status',[0,1,2,3])
  116. ->where('del',0)
  117. ->find();
  118. if($ret){
  119. HelpHander::error('已预约该时段,无重复预约');
  120. }
  121. $data['service_id'] = $service['service_id'];
  122. $data['service_time_id'] = $serviceTimeId;
  123. }
  124. $add = Db::name('visitor_order')->insert($data);
  125. if ($add) {
  126. HelpHander::success([],'提交成功');
  127. }else{
  128. HelpHander::error('提交失败');
  129. }
  130. }
  131. // 提交确认--小程序
  132. public function order2(){
  133. $type = 3;
  134. $company = input('company','','trim');
  135. $name = input('name','','trim');
  136. $phone = input('phone','','trim');
  137. $plateSn = input('plateSn','','trim');
  138. $bookTime = input('bookTime','','trim');
  139. $content = input('content','','trim');
  140. $fromUserId = input('fromUserId/d',0);
  141. if(!$fromUserId){
  142. HelpHander::error('参数错误');
  143. }
  144. if($fromUserId == $this->userId){
  145. HelpHander::error('不能填写自己分享的预约单');
  146. }
  147. if(!$company){
  148. HelpHander::error('请填写单位');
  149. }
  150. if(!$name){
  151. HelpHander::error('请填写姓名');
  152. }
  153. if(!check_mobile($phone)){
  154. HelpHander::error('手机号格式不正确');
  155. }
  156. if(!$bookTime){
  157. HelpHander::error('请选择到访时间');
  158. }
  159. if(!$content){
  160. HelpHander::error('请填写到访事由');
  161. }
  162. $data = [
  163. 'name' => $name,
  164. 'phone' => $phone,
  165. 'org_id' => $this->orgId,
  166. 'user_id' => $this->userId,
  167. 'from_user_id' => $fromUserId,
  168. 'status' => 0,
  169. 'type' => $type,
  170. 'create_time' => date('Y-m-d H:i:s'),
  171. 'book_time' => $bookTime,
  172. 'plate_sn' => $plateSn,
  173. 'company' => $company,
  174. 'content' => $content
  175. ];
  176. $ret = Db::name('visitor_order')->insert($data);
  177. if ($ret) {
  178. HelpHander::success([],'提交成功');
  179. }else{
  180. HelpHander::error('提交失败');
  181. }
  182. }
  183. public function detail(){
  184. $id = input('id/d',0);
  185. $info = Db::name('visitor_order')->where('id',$id)->where('del',0)->find();
  186. if($info){
  187. $info['ORG_NAME'] = Db::name('org')->where('id',$info['org_id'])->value('name');
  188. $info['from_user_name'] = Db::name('user')->where('id',$info['from_user_id'])->value('real_name');
  189. $info['qrcode'] = Request::domain().'/admin/Qrcode/qrcode?code='.get_qrcode_str('visitor',$info['id']);
  190. }else{
  191. HelpHander::error('记录不存在');
  192. }
  193. HelpHander::success($info);
  194. }
  195. // 自动检测预约已失效
  196. private function autoCheck($info,$time){
  197. if($info['status'] == 1){
  198. $curTime = time();
  199. $endTime = strtotime($time['day'].' '.$time['end']);
  200. if($curTime >= $endTime){
  201. Db::name('visitor_order')->where('id',$info['id'])->update(['status'=>3]);
  202. $info['status'] = 3;
  203. }
  204. }
  205. return $info;
  206. }
  207. }