diff --git a/application/admin/controller/workers/Worker.php b/application/admin/controller/workers/Worker.php index 03afa38..8db3d76 100644 --- a/application/admin/controller/workers/Worker.php +++ b/application/admin/controller/workers/Worker.php @@ -2,7 +2,13 @@ namespace app\admin\controller\workers; +use app\admin\model\AuthGroup; +use app\admin\model\Item; use app\common\controller\Backend; +use fast\Tree; +use think\Db; +use think\exception\PDOException; +use think\exception\ValidateException; /** * 师傅列管理 @@ -23,6 +29,30 @@ class Worker extends Backend parent::_initialize(); $this->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); } @@ -51,14 +81,14 @@ class Worker extends Backend list($where, $sort, $order, $offset, $limit) = $this->buildparams(); $list = $this->model - ->with(['area']) - ->where($where) - ->order($sort, $order) - ->paginate($limit); + ->with(['area']) + ->where($where) + ->order($sort, $order) + ->paginate($limit); foreach ($list as $row) { - - + + } $result = array("total" => $list->total(), "rows" => $list->items()); @@ -68,4 +98,127 @@ class Worker extends Backend 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); + } + $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) { + $insert [] = [ + 'worker_id' => $result, + 'item_id' => $item, + 'item_path_id' => $item_map[$item] ?? 0 + ]; + } + model('WorkerItem')->saveAll($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) { + $insert [] = [ + 'worker_id' => $ids, + 'item_id' => $item, + 'item_path_id' => $item_map[$item] ?? 0 + ]; + } + model('WorkerItem')->where('worker_id',$ids)->delete(); + model('WorkerItem')->batchInsert($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(); + } + } diff --git a/application/admin/model/Item.php b/application/admin/model/Item.php index 4314b80..351e58c 100644 --- a/application/admin/model/Item.php +++ b/application/admin/model/Item.php @@ -43,6 +43,12 @@ class Item extends Model return $list[$value] ?? ''; } + public function getAll(){ + return $this->where('status',1) + ->field('id,pid,level,title,key_word,sort,status') + ->select(); + } + diff --git a/application/admin/model/Worker.php b/application/admin/model/Worker.php index 261d311..ef829e6 100644 --- a/application/admin/model/Worker.php +++ b/application/admin/model/Worker.php @@ -2,6 +2,8 @@ namespace app\admin\model; +use Exception; +use think\Db; use think\Model; use traits\model\SoftDelete; @@ -50,4 +52,57 @@ class Worker extends Model { return $this->belongsTo('Area', 'area_id', 'id', [], 'LEFT')->setEagerlyType(0); } + + + /** + * 批量插入数据到数据库 + * + * @param string $table 数据表名 + * @param array $data 待插入的数据 + * @param int $batchSize 每次插入的数据量(默认 500) + * @return int 成功插入的总行数 + * @throws Exception 插入失败时抛出异常 + */ + function batchInsert( array $data, int $batchSize = 500): int + { + if (empty($data)) { + throw new Exception('插入数据不能为空'); + } + + // 提取字段名(确保所有数据的字段一致) + $columns = array_keys($data[0]); + $columnList = implode(', ', $columns); + $placeholders = '(' . implode(', ', array_fill(0, count($columns), '?')) . ')'; + + $totalInserted = 0; + + Db::startTrans(); + try { + // 数据分批插入 + foreach (array_chunk($data, $batchSize) as $chunk) { + $sql = "INSERT INTO {$this->getTable()} ({$columnList}) VALUES " . implode(', ', array_fill(0, count($chunk), $placeholders)); + $stmt = $this->prepare($sql); + + // 将数据展开填充 + $values = []; + foreach ($chunk as $row) { + $values = array_merge($values, array_values($row)); + } + + if ($stmt->execute($values)) { + $totalInserted += $stmt->rowCount(); + } else { + throw new Exception('批量插入失败'); + } + } + + Db::commit(); + return $totalInserted; + + } catch (Exception $e) { + Db::rollback(); + throw new Exception('批量插入失败:' . $e->getMessage()); + } + } + } diff --git a/application/admin/model/WorkerItem.php b/application/admin/model/WorkerItem.php new file mode 100644 index 0000000..2d3068a --- /dev/null +++ b/application/admin/model/WorkerItem.php @@ -0,0 +1,59 @@ +getTable()} ({$columnList}) VALUES " . implode(', ', array_fill(0, count($chunk), $placeholders)); + // 将数据展开填充 + $values = []; + foreach ($chunk as $row) { + $values = array_merge($values, array_values($row)); + } + Db::execute($sql,$values); + } + + Db::commit(); + return $totalInserted; + + } catch (Exception $e) { + Db::rollback(); + throw new Exception('批量插入失败:' . $e->getMessage()); + } + } +} diff --git a/application/admin/view/workers/worker/add.html b/application/admin/view/workers/worker/add.html index 4c64d2e..00fc0df 100644 --- a/application/admin/view/workers/worker/add.html +++ b/application/admin/view/workers/worker/add.html @@ -6,42 +6,21 @@ +