Preorder.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. <?php
  2. namespace app\admin\controller;
  3. use app\common\controller\Backend;
  4. use think\Db;
  5. use app\common\service\UserService;
  6. /**
  7. * 预约单
  8. *
  9. * @icon fa fa-circle-o
  10. */
  11. class Preorder extends Backend
  12. {
  13. /**
  14. * Preorder模型对象
  15. * @var \app\admin\model\Preorder
  16. */
  17. protected $model = null;
  18. public function _initialize()
  19. {
  20. parent::_initialize();
  21. $this->model = new \app\admin\model\Preorder;
  22. $this->view->assign("preOrderStatusList", $this->model->getPreOrderStatusList());
  23. }
  24. /**
  25. * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法
  26. * 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑
  27. * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
  28. */
  29. /**
  30. * 查看
  31. */
  32. public function index()
  33. {
  34. //当前是否为关联查询
  35. $this->relationSearch = true;
  36. //设置过滤方法
  37. $this->request->filter(['strip_tags', 'trim']);
  38. if ($this->request->isAjax()) {
  39. //如果发送的来源是Selectpage,则转发到Selectpage
  40. if ($this->request->request('keyField')) {
  41. return $this->selectpage();
  42. }
  43. list($where, $sort, $order, $offset, $limit) = $this->buildparams();
  44. //只能看自己的
  45. $where_op = [];
  46. if($this->auth->company_id){
  47. $where_op['preorder.company_id'] = $this->auth->company_id;
  48. }
  49. $list = $this->model
  50. ->with(['company','user','servicetype','order'])
  51. ->where($where)
  52. ->where($where_op)
  53. ->order($sort, $order)
  54. ->paginate($limit);
  55. foreach ($list as $row) {
  56. $row->getRelation('company')->visible(['name']);
  57. $row->getRelation('user')->visible(['nickname','mobile']);
  58. $row->getRelation('servicetype')->visible(['title']);
  59. $row->getRelation('order')->visible(['orderno']);
  60. }
  61. $result = array("total" => $list->total(), "rows" => $list->items());
  62. return json($result);
  63. }
  64. return $this->view->fetch();
  65. }
  66. /**
  67. * 取消
  68. */
  69. public function cancel(){
  70. $id = input('id',0);
  71. if(!$this->request->ispost()){
  72. $this->assign('id',$id);
  73. return $this->view->fetch();
  74. }
  75. $cancel_reason = input('cancel_reason','');
  76. $update = [
  77. 'cancel_reason' => $cancel_reason,
  78. 'cancel_time' => time(),
  79. 'updatetime' => time(),
  80. 'pre_order_status' => 0,
  81. ];
  82. Db::name('pre_order')->where('id',$id)->update($update);
  83. $userService = new UserService();
  84. $params['pre_order_id'] = $id;
  85. $userService->msgPreOrder($params);
  86. $this->success('取消成功');
  87. }
  88. /**
  89. * 预约开单
  90. */
  91. public function submitorder(){
  92. $pre_order_id = input('id',0); //预约单id
  93. $map = ['id'=>$pre_order_id];
  94. $pre_order = Db::name('pre_order')->where($map)->find();
  95. if(!$this->request->ispost()){
  96. $this->assign('id',$pre_order_id);
  97. $this->assign('row',$pre_order);
  98. $this->assign('staff_id',$this->auth->staff_id);
  99. return $this->view->fetch();
  100. }
  101. $data = request_post_hub([
  102. 'user_name','user_car_number','user_mobile','user_address',
  103. 'servicetype_id','server_time','server_info','server_images','pay_fee'
  104. ]);
  105. Db::startTrans();
  106. //预约单
  107. $map = ['id'=>$pre_order_id,'pre_order_status'=>1];
  108. $pre_order = Db::name('pre_order')->where($map)->lock(true)->find();
  109. if(empty($pre_order)){
  110. Db::rollback();
  111. $this->error('不存在的预约单');
  112. }
  113. $data['pre_order_id'] = $pre_order_id;
  114. $data['user_id'] = $pre_order['user_id'];
  115. //修改状态
  116. $update = [
  117. 'order_time' => time(),
  118. 'updatetime' => time(),
  119. 'pre_order_status' => 2,
  120. ];
  121. $update_rs = Db::name('pre_order')->where('id',$pre_order_id)->update($update);
  122. if($update_rs === false){
  123. Db::rollback();
  124. $this->error('下单失败');
  125. }
  126. //检索car_id,没必要了
  127. //准备数据
  128. $data['orderno'] = createUniqueNo('O',$pre_order_id);
  129. $data['ordertype'] = 1; //类型:1=预约下单,2=在线下单,3=套餐订单
  130. $data['company_id'] = $pre_order['company_id'];
  131. $data['staff_id'] = $this->auth->staff_id;
  132. $data['total_fee'] = $data['pay_fee'];
  133. $data['status'] = 2; //2=已支付,待处理
  134. $data['createtime'] = time();
  135. $order_id = Db::name('order')->insertGetId($data);
  136. if(!$order_id){
  137. Db::rollback();
  138. $this->error('下单失败');
  139. }
  140. $update = [
  141. 'order_id' => $order_id,
  142. ];
  143. $update_rs = Db::name('pre_order')->where('id',$pre_order_id)->update($update);
  144. if($update_rs === false){
  145. Db::rollback();
  146. $this->error('下单失败');
  147. }
  148. Db::commit();
  149. $this->success('下单完成',$order_id);
  150. }
  151. }