Preorder.php 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. <?php
  2. namespace app\api\controller\company;
  3. use app\common\controller\Apic;
  4. use app\common\service\UserService;
  5. use think\Db;
  6. use think\Log;
  7. /**
  8. * 预约管理
  9. */
  10. class Preorder extends Apic
  11. {
  12. protected $noNeedLogin = [];
  13. protected $noNeedRight = '*';
  14. //列表
  15. public function lists(){
  16. $pre_order_status = input('pre_order_status',1);
  17. $where = [
  18. 'po.company_id' => $this->auth->company_id,
  19. 'po.pre_order_status' => $pre_order_status,
  20. ];
  21. $list = Db::name('pre_order')->alias('po')->field('po.*,servicetype.title,order.server_time')
  22. ->join('servicetype','po.servicetype_id = servicetype.id','LEFT')
  23. ->join('order','po.order_id = order.id','LEFT')
  24. ->where($where)->order('po.id desc')->autopage()->select();
  25. $this->success(1,$list);
  26. }
  27. //取消
  28. public function cancel(){
  29. $id = input('pre_order_id',0);
  30. $cancel_reason = input('cancel_reason','');
  31. $update = [
  32. 'cancel_reason' => $cancel_reason,
  33. 'cancel_time' => time(),
  34. 'updatetime' => time(),
  35. 'pre_order_status' => 0,
  36. ];
  37. Db::name('pre_order')->where('id',$id)->where('company_id',$this->auth->company_id)->update($update);
  38. $userService = new UserService();
  39. $params['pre_order_id'] = $id;
  40. $userService->msgPreOrder($params);
  41. $this->success('取消成功');
  42. }
  43. //详情
  44. public function info(){
  45. $id = input('pre_order_id',0);
  46. $info = Db::name('pre_order')->alias('po')
  47. ->field('po.*,servicetype.title as servicetype_title')
  48. ->join('servicetype','po.servicetype_id = servicetype.id','LEFT')
  49. ->where('po.id',$id)->find();
  50. $this->success(1,$info);
  51. }
  52. //车牌号或手机号搜用户
  53. public function searchuser(){
  54. $keyword = input('keyword','');
  55. //先搜手机
  56. $where = [
  57. 'mobile' => ['LIKE','%'.$keyword.'%']
  58. ];
  59. $user_ids = Db::name('user')->where($where)->column('id');
  60. //再搜车牌
  61. $where = [
  62. 'car_number' => ['LIKE','%'.$keyword.'%']
  63. ];
  64. $car_user_ids = Db::name('user_car')->where($where)->column('user_id');
  65. //合并用户ids
  66. $new_user_ids = array_merge($user_ids,$car_user_ids);
  67. $new_user_ids = array_flip(array_flip($new_user_ids));
  68. //搜用户
  69. $user_list = Db::name('user')->field('id,mobile,nickname')->where('id','IN',$new_user_ids)->autopage()->select();
  70. foreach($user_list as $key => &$val){
  71. $user_car = Db::name('user_car')->where('user_id',$val['id'])->select();
  72. $val['car_list'] = $user_car;
  73. $val['address'] = Db::name('user_wallet')->where('user_id',$val['id'])->where('company_id',$this->auth->company_id)->value('address');
  74. }
  75. $this->success(1,$user_list);
  76. }
  77. //预约开单 在线开单
  78. public function submit_order(){
  79. $pre_order_id = input('pre_order_id',0); //预约单id
  80. $data = request_post_hub([
  81. 'user_name','user_car_number','user_mobile','user_address',
  82. 'servicetype_id','server_time','server_info','server_images','pay_fee'
  83. ]);
  84. Db::startTrans();
  85. //预约单
  86. if($pre_order_id > 0){
  87. //check
  88. $map = ['id'=>$pre_order_id,'company_id'=>$this->auth->company_id,'pre_order_status'=>1];
  89. $pre_order = Db::name('pre_order')->where($map)->lock(true)->find();
  90. if(empty($pre_order)){
  91. Db::rollback();
  92. $this->error('不存在的预约单');
  93. }
  94. $data['pre_order_id'] = $pre_order_id;
  95. $data['user_id'] = $pre_order['user_id'];
  96. //修改状态
  97. $update = [
  98. 'order_time' => time(),
  99. 'updatetime' => time(),
  100. 'pre_order_status' => 2,
  101. ];
  102. $update_rs = Db::name('pre_order')->where('id',$pre_order_id)->update($update);
  103. if($update_rs === false){
  104. Db::rollback();
  105. $this->error('下单失败');
  106. }
  107. }else{
  108. $data['pre_order_id'] = 0;
  109. //检查用户
  110. $user_info = Db::name('user')->where('mobile',$data['user_mobile'])->find();
  111. if(empty($user_info)){
  112. Db::rollback();
  113. $this->error('不存在的用户,请先让客户扫店铺码注册');
  114. }
  115. $data['user_id'] = $user_info['id'];
  116. }
  117. //检索car_id,没必要了
  118. //准备数据
  119. $data['orderno'] = createUniqueNo('O',$this->auth->id);
  120. $data['ordertype'] = ($pre_order_id > 0) ? 1 : 2; //类型:1=预约下单,2=在线下单,3=套餐订单
  121. $data['company_id'] = $this->auth->company_id;
  122. $data['staff_id'] = $this->auth->id;
  123. $data['total_fee'] = $data['pay_fee'];
  124. $data['status'] = 2; //2=已支付,待处理
  125. $data['createtime'] = time();
  126. $order_id = Db::name('order')->insertGetId($data);
  127. if(!$order_id){
  128. Db::rollback();
  129. $this->error('下单失败');
  130. }
  131. if($pre_order_id > 0){
  132. $update = [
  133. 'order_id' => $order_id,
  134. ];
  135. $update_rs = Db::name('pre_order')->where('id',$pre_order_id)->update($update);
  136. if($update_rs === false){
  137. Db::rollback();
  138. $this->error('下单失败');
  139. }
  140. }
  141. Db::commit();
  142. $this->success('下单完成',$order_id);
  143. }
  144. //查看预约单下的订单
  145. }