model = new \app\admin\model\Order; $this->view->assign("ordertypeList", $this->model->getOrdertypeList()); $this->view->assign("statusList", $this->model->getStatusList()); $this->view->assign("paytypeList", $this->model->getPaytypeList()); } /** * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法 * 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑 * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改 */ /** * 查看 */ public function index() { //当前是否为关联查询 $this->relationSearch = true; //设置过滤方法 $this->request->filter(['strip_tags', 'trim']); if ($this->request->isAjax()) { //如果发送的来源是Selectpage,则转发到Selectpage if ($this->request->request('keyField')) { return $this->selectpage(); } list($where, $sort, $order, $offset, $limit) = $this->buildparams(); //只能看自己的 $where_op = []; if($this->auth->company_id){ $where_op['order.company_id'] = $this->auth->company_id; } $list = $this->model ->with(['company','staff','user','preorder','servicetype']) ->where($where) ->where($where_op) ->order($sort, $order) ->paginate($limit); foreach ($list as $row) { $row->getRelation('company')->visible(['name']); $row->getRelation('staff')->visible(['truename','mobile']); $row->getRelation('user')->visible(['nickname','mobile']); $row->getRelation('preorder')->visible(['pre_order_no']); $row->getRelation('servicetype')->visible(['title']); } $result = array("total" => $list->total(), "rows" => $list->items()); return json($result); } return $this->view->fetch(); } /** * 在线开单 */ public function submitordernew(){ if(!$this->request->isPost()){ $this->assign('staff_id',$this->auth->staff_id); return $this->view->fetch(); } $data = request_post_hub([ 'user_name','user_car_number','user_mobile','user_address', 'servicetype_id','server_time','server_info','server_images','pay_fee' ]); Db::startTrans(); $data['pre_order_id'] = 0; //检查用户 $user_info = Db::name('user')->where('mobile',$data['user_mobile'])->find(); if(empty($user_info)){ Db::rollback(); $this->error('不存在的用户,请先让客户扫店铺码注册'); } $data['user_id'] = $user_info['id']; //检索car_id,没必要了 //准备数据 $data['orderno'] = createUniqueNo('O',$user_info['id']); $data['ordertype'] = 2; //类型:1=预约下单,2=在线下单,3=套餐订单 $data['company_id'] = $this->auth->company_id; $data['staff_id'] = $this->auth->staff_id; $data['total_fee'] = $data['pay_fee']; $data['status'] = 2; //2=已支付,待处理 $data['createtime'] = time(); $order_id = Db::name('order')->insertGetId($data); if(!$order_id){ Db::rollback(); $this->error('下单失败'); } Db::commit(); $this->success('下单完成',$order_id); } /** * 取消 */ public function cancel(){ $id = input('id',0); if(!$this->request->ispost()){ $this->assign('id',$id); return $this->view->fetch(); } $reason = input('cancel_reason',''); $info = Db::name('order')->where('id',$id)->find(); if($info['status'] == 4){ $this->error('当前订单已经取消'); } if($info['status'] != 2){ $this->error('当前订单状态不能取消'); } if($info['ordertype'] == 3){ $this->error('套餐订单不能取消'); } $time = time(); //同步到预约单,这一步不该有 if (!empty($info['pre_order_id'])) { $preOrderWhere['id'] = $info['pre_order_id']; $preOrder = Db::name('pre_order')->where($preOrderWhere)->find(); if (!empty($preOrder) && $preOrder['pre_order_status'] != 0) { $preOrderData = ['pre_order_status'=>0,'cancel_time'=>$time,'cancel_reason'=>$reason]; $preOrderRes = Db::name('pre_order')->where($preOrderWhere)->update($preOrderData); if (!$preOrderRes) { $this->error('预约单取消失败'); } } } //取消 $rs = Db::name('order')->where('id',$id)->update(['status'=>4,'cancel_time'=>time(),'finish_time'=>time(),'cancel_reason'=>$reason]); if($rs === false){ $this->error('取消失败'); } //订单取消发送消息 $userService = new UserService(); $params['order_id'] = $id; $userService->msgOrder($params); $this->success('取消成功'); } /** * 完成 */ public function finish(){ $id = input('id',0); if(!$this->request->ispost()){ $this->assign('id',$id); $info = Db::name('order')->where('id',$id)->find(); $this->assign('info',$info); $this->assign('staff_id',$this->auth->staff_id); //是否弹出保养 $baoyang_switch = Db::name('servicetype')->where('id',$info['servicetype_id'])->value('baoyang_switch'); $this->assign('baoyang_switch',$baoyang_switch); return $this->view->fetch(); } Db::startTrans(); $info = Db::name('order')->where('id',$id)->lock(true)->find(); if($info['status'] != 2){ Db::rollback(); $this->error('当前订单不能完成'); } //保养更新 $next_date = input('next_date',''); $next_carlicheng = input('next_carlicheng',''); $baoyang_data = [ 'next_date' => $next_date, 'next_carlicheng' => $next_carlicheng, ]; //完成 $time = time(); $updateData = ['status'=>3,'finish_time'=>time(),'staff_id'=>$this->auth->staff_id]; $updateData = array_merge($updateData,$baoyang_data); $rs = Db::name('order')->where('id',$id)->update($updateData); if($rs === false){ Db::rollback(); $this->error('操作失败'); } //给门店加钱 if($info['ordertype'] == 3 && $info['paytype'] == 3 && $info['pay_fee'] > 0){ $wallet_rs = model('walletcompany')->lockChangeAccountRemain($this->auth->company_id,'money',$info['pay_fee'],203,$remark='套餐订单完成服务','order',$id); if($wallet_rs['status'] === false){ Db::rollback(); $this->error($wallet_rs['msg']); } } Db::commit(); //订单取消发送消息 $userService = new UserService(); $params['order_id'] = $id; $userService->msgOrder($params); $this->success('操作成功'); } }