model = new \app\admin\model\OrderDispatch; $this->view->assign("typeList", $this->model->getTypeList()); $this->view->assign("statusList", $this->model->getStatusList()); } /** * 默认生成的控制器所继承的父类中有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(); $list = $this->model ->with(['order']) ->where($where) ->order($sort, $order) ->paginate($limit); foreach ($list as &$row) { $row->btn_edit = (in_array($row->status, $this->model->btnActiveStatusList('btn_edit'))) ? true : false; $row->btn_cancel = (in_array($row->status, $this->model->btnActiveStatusList('btn_cancel'))) ? true : false; $row->btn_abnormal = (in_array($row->status, $this->model->btnActiveStatusList('btn_abnormal'))) ? true : false; // $row->btn_income = (in_array($row->status, $this->model->btnActiveStatusList('btn_income')) && in_array($row->order->status, $orderModel->incomeBtnStatus())) ? true : false; } $result = array("total" => $list->total(), "rows" => $list->items()); return json($result); } return $this->view->fetch(); } /** * 添加 * * @return string * @throws \think\Exception */ public function add() { if (false === $this->request->isPost()) { $id = $this->request->param('order_id'); $order = model('order')->where('id', $id)->find(); if (!$order) { $this->error(__('No results were found')); } $items = Db::name('item') ->where('status',1) ->field(['id','title','key_word','pid']) ->order('pid','asc') ->order('sort','desc') ->select(); $tree = $this->buildTree($items); $formattedTree = $this->formatTree($tree); $area_name = model('area')->getNameByCode($order->area_id); $order->area_name = str_replace(',','/',$area_name); $this->view->assign('items', $formattedTree); $this->view->assign('row', $order); return $this->view->fetch(); } $params = $this->request->post('row/a'); if (empty($params)) { $this->error(__('Parameter %s can not be empty', '')); } $params = $this->preExcludeFields($params); if ($this->dataLimit && $this->dataLimitFieldAutoFill) { $params[$this->dataLimitField] = $this->auth->id; } $result = false; Db::startTrans(); try { //是否采用模型验证 if ($this->modelValidate) { $name = str_replace("\\model\\", "\\validate\\", get_class($this->model)); $validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.add' : $name) : $this->modelValidate; $this->model->validateFailException()->validate($validate); } $order = model('order')->where('id',$params['order_id'])->find(); if (!$order){ $this->error(__('No results were found')); } $insert = [ 'admin_id' => $this->auth->id, 'admin_user' => $this->auth->nickname, 'order_id' => $params['order_id'], 'type' => 1, 'worker_id' => $params['worker_id'], 'plan_time' => $order['plan_time'], 'is_receipt' => $order['receive_type'] == 1 ]; $worker = model('worker')->where('id',$params['worker_id'])->find(); $insert ['worker_name'] = $worker->name; $insert ['worker_tel'] = $worker->tel; $result = $this->model->allowField(true)->save($insert); $order->status = Order::STATUS_DISPATCHED; $order->save(); Db::commit(); } catch (ValidateException | PDOException | Exception $e) { Db::rollback(); $this->error($e->getMessage()); } if ($result === false) { $this->error(__('No rows were inserted')); } $this->success(); } /** * 编辑 * * @param $ids * @return string * @throws DbException * @throws \think\Exception */ public function edit($ids = null) { $row = $this->model->get($ids); if (!$row) { $this->error(__('No Results were found')); } $adminIds = $this->getDataLimitAdminIds(); if (is_array($adminIds) && !in_array($row[$this->dataLimitField], $adminIds)) { $this->error(__('You have no permission')); } if (false === $this->request->isPost()) { $this->view->assign('row', $row); return $this->view->fetch(); } $params = $this->request->post('row/a'); if (empty($params)) { $this->error(__('Parameter %s can not be empty', '')); } $params = $this->preExcludeFields($params); $result = false; Db::startTrans(); try { //是否采用模型验证 if ($this->modelValidate) { $name = str_replace("\\model\\", "\\validate\\", get_class($this->model)); $validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.edit' : $name) : $this->modelValidate; $row->validateFailException()->validate($validate); } $result = $row->allowField(true)->save($params); Db::commit(); } catch (ValidateException | PDOException | Exception $e) { Db::rollback(); $this->error($e->getMessage()); } if (false === $result) { $this->error(__('No rows were updated')); } $this->success(); } /** * 取消派单 * * @param $ids * @return void * @throws DbException * @throws DataNotFoundException * @throws ModelNotFoundException */ // public function del($ids = null) // { // if (false === $this->request->isPost()) { // $this->error(__("Invalid parameters")); // } // $ids = $ids ?: $this->request->post("ids"); // if (empty($ids)) { // $this->error(__('Parameter %s can not be empty', 'ids')); // } // $pk = $this->model->getPk(); // $adminIds = $this->getDataLimitAdminIds(); // if (is_array($adminIds)) { // $this->model->where($this->dataLimitField, 'in', $adminIds); // } // $list = $this->model->where($pk, 'in', $ids)->whereIn('status', $this->model->deleteStatusList())->select(); // // $remark = $this->request->param('remark', ''); // // $count = 0; // Db::startTrans(); // try { // foreach ($list as $item) { // //$count += $item->delete(); // $order = Order::where('id', $item->order_id)->where('status', Order::STATUS_DISPATCHED)->find(); // if (!$order) { // $this->error('订单状态已变更,请刷新后操作'); // } // //取消 // $item->save(['status' => OrderDispatch::STATUS_CANCEL, 'remark' => $remark]); // //回退订单状态 // $order->allowField(true)->save(['status' => Order::STATUS_DISPATCHING]); // // $params['order'] = $order->ref; // $params['role'] = 1; // $params['auth'] = $this->auth; // $params['remark'] = '派单被取消[ID:' . $item->id . '],订单状态回退'; // if (!empty($remark)) { // $params['remark'] .= ',操作备注:' . $remark; // } // Hook::listen('order_change', $params); // } // Db::commit(); // } catch (PDOException | Exception $e) { // Db::rollback(); // throw $e; // $this->error($e->getMessage()); // } // // $this->error(__('取消成功')); // } /** * 取消派单 * * @param $ids * @return void * @throws DbException * @throws DataNotFoundException * @throws ModelNotFoundException */ public function del() { if (false === $this->request->isPost()) { $this->error(__("Invalid parameters")); } $ids = $this->request->post("ids/a"); $reason = $this->request->post("reason"); if (empty($ids)) { $this->error(__('Parameter %s can not be empty', 'ids')); } $pk = $this->model->getPk(); $adminIds = $this->getDataLimitAdminIds(); if (is_array($adminIds)) { $this->model->where($this->dataLimitField, 'in', $adminIds); } $list = $this->model->where($pk, 'in', $ids)->whereIn('status', $this->model->deleteStatusList())->select(); if(count($list) != count($ids)){ $this->error(__("任务状态不可操作,请刷新页面后再操作")); } $remark = $reason; $count = 0; Db::startTrans(); try { foreach ($list as $item) { //$count += $item->delete(); $order = Order::where('id', $item->order_id)->where('status', Order::STATUS_DISPATCHED)->find(); if (!$order) { $this->error('订单状态已变更,请刷新后操作'); } //取消 $item->save(['status' => OrderDispatch::STATUS_CANCEL, 'remark' => $remark]); //回退订单状态 $order->allowField(true)->save(['status' => Order::STATUS_DISPATCHING]); $params['order'] = $order; $params['role'] = 1; $params['auth'] = $this->auth; $params['remark'] = '派单被取消[ID:' . $item->id . '],订单状态回退'; if (!empty($remark)) { $params['remark'] .= ',操作备注:' . $remark; } Hook::listen('order_change', $params); } Db::commit(); } catch (PDOException | Exception $e) { Db::rollback(); $this->error($e->getMessage()); } /* if ($count) { $this->success(); }*/ $this->success(__('取消成功')); } }