model = new \app\admin\model\Worker; $this->view->assign("statusList", $this->model->getStatusList()); $items = model('item')->getAll(); Tree::instance()->init($items)->icon = ['    ', '    ', '    ']; // dd($ruleList); $res = Tree::instance()->getTreeList(Tree::instance()->getTreeArray(0), 'title'); foreach ($res as &$v) { $v = $v->toArray(); $v['state'] = ['selected' => false]; $v['parent'] = $v['pid'] ?: '#'; $v['text'] = $v['title']; unset($v['pid']); unset($v['title']); unset($v['level']); unset($v['key_word']); unset($v['sort']); unset($v['status']); } $this->tree = $res; $this->view->assign("tree", $res); } /** * 默认生成的控制器所继承的父类中有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(); $area_code = request()->get('area_id'); $item_id = request()->get('item_id'); $keyword = request()->get('keyword'); $build = $this->model ->with(['area','admin' => function($q){ $q->withField(['id','username']); }]) ->where($where) ->field('worker.id,admin_id,type,name,tel,area_id,create_time,deposit_amount,update_time,status,star,rate,rate_remark,remark,images') ->order($sort, $order); if ($keyword) { $build->where(function ($q) use ($keyword) { $q->where('name', 'like', '%' . $keyword . '%')->whereOr('tel', 'like', '%' . $keyword . '%'); }); } if ($item_id) { $build->join('worker_item', 'worker_item.worker_id = worker.id', 'left'); $build->where('worker_item.item_id', $item_id); } if ($area_code) { $build->where('area_id', 'like', $this->getSelectAreaCode($area_code) . '%'); } $list = $build ->paginate($limit); $result = array("total" => $list->total(), "rows" => $list->items()); return json($result); } $items = Db::name('item') ->where('status', 1) ->field(['id', 'title', 'key_word', 'pid']) ->order('pid', 'asc') ->order('sort', 'desc') ->select(); $filtered = array_filter($items, function ($item) { return $item['pid'] == 0; }); $pid_map = array_column($filtered, null, 'id'); $res_items = []; foreach ($items as $item) { if ($item['pid'] != 0 && isset($pid_map[$item['pid']])) { $res_items [] = [ ...$item, 'ptitle' => $pid_map[$item['pid']]['title'] ]; } } $this->view->assign('items', $res_items); return $this->view->fetch(); } public function add() { if (false === $this->request->isPost()) { 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); } $params['admin_id'] = $this->auth->id; if(!empty($params['area_id'])){ $area = Area::getByCode($params['area_id']); if($area){ $location = getLocation($area->merge_name); if(!empty($location)){ $params['lng'] = $location['lng']; $params['lat'] = $location['lat']; } } } $result = $this->model->allowField(true)->save($params); $item_map = model('item')->getAll(); $item_map = array_column($item_map, 'level', 'id'); if ($result) { $items = explode(',', $params['rules']); if ($items) { $insert = []; foreach ($items as $item) { if ($item == '') continue; $insert [] = [ 'worker_id' => $this->model->id, 'item_id' => $item, 'item_path_id' => $item_map[$item] ?? 0 ]; } model('WorkerItem')->insertAll($insert); } } Db::commit(); } catch (ValidateException | PDOException | Exception $e) { Db::rollback(); $this->error($e->getMessage()); } if ($result === false) { $this->error(__('No rows were inserted')); } $this->success(); } public function edit($ids = null) { if (!$ids) { if (request()->isPost()) { $ids = input('id'); if (!$ids) { $this->error('缺少订单ID'); } } else { $this->error('缺少订单ID'); } } // 获取当前ID对应的订单信息 $worker = $this->model->get($ids); if (!$worker) { $this->error('订单不存在'); } // 判断是否为POST请求,进行更新操作 if (request()->isPost()) { // 获取表单提交的数据 $params = input('post.row/a'); $item_map = model('item')->getAll(); $item_map = array_column($item_map, 'level', 'id'); if ($ids) { $items = explode(',', $params['rules'] ?? ''); if ($items) { $insert = []; foreach ($items as $item) { if ($item == '') continue; $insert [] = [ 'worker_id' => $ids, 'item_id' => $item, 'item_path_id' => $item_map[$item] ?? 0 ]; } model('WorkerItem')->where('worker_id', $ids)->delete(); model('WorkerItem')->insertAll($insert); } } unset($params['rules']); unset($params['address']); $worker->save($params); // 返回成功信息 $this->success('更新成功', 'index'); } $area_name = model('area')->getNameByCode($worker->area_id); $worker->area_name = str_replace(',', '/', $area_name); $select_ids = model('WorkerItem')->where('worker_id', $ids)->field('item_id') ->select(); $select_ids = array_column($select_ids, 'item_id'); foreach ($this->tree as $index => $item) { if (in_array($item['parent'], $select_ids)) { $this->tree[$index]['state']['selected'] = true; } } // dd($area_name); // 将订单数据传递到视图 $this->assign('row', $worker); $this->assign("tree", $this->tree); // 渲染编辑页面 return $this->fetch(); } public function dispatchList() { $area_id = request()->get('area_id'); list($where, $sort, $order, $offset, $limit) = $this->buildparams(); $build = model('worker') ->where('status', 1) ->with(['area']) ->withCount(['myorder' => function ($query) { $query->where('status', Order::STATUS_FINISHED); return 'finish_order'; },]) ->withCount(['myorder' => function ($query) { $query->where('status', '<', Order::STATUS_FINISHED) ->where('status', '>=', Order::STATUS_DRAFT); return 'doing_order'; }]) ->field( ['id', 'name', 'tel', 'area_id', 'lng', 'lat'] ); if ($area_id) { $code = $this->getSelectAreaCode(substr_replace($area_id, '00', -2)); $build->where('area_id', 'like', $code . '%'); } $list = $build ->paginate($limit); $result = array("total" => $list->total(), "rows" => $list->items()); return json($result); } public function dispatchMapList() { $order_id = request()->get('order_id'); $search = request()->get('search'); list($where, $sort, $order, $offset, $limit) = $this->buildparams(); $order = (new Order())->where('id',$order_id)->field(['id','lng','lat'])->select(); if (!empty($order)) { $order = $order[0]; } $worker_distance = Db::query("SELECT id FROM ( SELECT id, ST_Distance_Sphere( point(lng, lat), point(?, ?) ) AS distance FROM fa_worker where status = 1 ) AS t WHERE distance < 50000 ORDER BY distance;",[$order->lng,$order->lat]); $worker_ids = array_column($worker_distance,'id'); $build = model('worker') ->where('status', 1) ->whereIn('id', $worker_ids); if ($search){ $build->where(function ($q)use($search){ $q->where('name','like','%'.$search.'%') ->whereor('tel','like','%'.$search.'%'); }); } $build->withCount(['myorder' => function ($query) { $query->where('status', Order::STATUS_FINISHED); return 'finish_order'; },]) ->withCount(['myorder' => function ($query) { $query->where('status', '<', Order::STATUS_FINISHED) ->where('status', '>=', Order::STATUS_DRAFT); return 'doing_order'; }]) ->field( ['id', 'name', 'tel', 'area_id', 'lng', 'lat','ST_Distance_Sphere( point(lng, lat), point('.$order->lng.','.$order->lat.') ) AS distance'] )->order('distance'); $list = $build ->paginate($limit); $data = []; foreach ($list->items() as $item){ $data [] = [ 'id' => $item->id, 'name' => $item->name, 'tel' => $item->tel, 'lat' => $item->lat, 'lng' => $item->lng, 'distance' => $item->distance, 'doing_order' => $item->doing_order, 'finish_order' => $item->finish_order, 'star' => $item->star, ]; } $result = array("total" => $list->total(), "rows" => $data,'order' => $order); return $result; } public function status(){ $ids = input()['ids'] ?? []; $status = input()['status'] ?? 1; $res = Db::name('worker')->whereIn('id',$ids)->update([ 'status' => $status ]); return json([]); } 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)->select(); $count = 0; Db::startTrans(); try { foreach ($list as $item) { $count += $item->delete(); } Db::commit(); } catch (PDOException|Exception $e) { Db::rollback(); $this->error($e->getMessage()); } if ($count) { $this->success(); } $this->error(__('No rows were deleted')); } }