From 9b04e4d9a07656cb61e0a5c58d685f0c461bf86e Mon Sep 17 00:00:00 2001 From: hant Date: Mon, 10 Mar 2025 20:30:39 +0800 Subject: [PATCH] =?UTF-8?q?=E5=B7=A5=E4=BA=BA=E5=BD=95=E5=85=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../admin/controller/workers/Worker.php | 165 +++++++++++++++++- application/admin/model/Item.php | 6 + application/admin/model/Worker.php | 55 ++++++ application/admin/model/WorkerItem.php | 59 +++++++ .../admin/view/workers/worker/add.html | 44 ++--- .../admin/view/workers/worker/edit.html | 64 ++++--- public/assets/js/backend/order.js | 5 +- public/assets/js/backend/workers/worker.js | 72 +++++++- 8 files changed, 400 insertions(+), 70 deletions(-) create mode 100644 application/admin/model/WorkerItem.php 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 @@ +
-
- -
- -
- {foreach name="statusList" item="vo"} - - {/foreach} -
- -
-
-
- -
-
-
- -
- -
-
-
- -
- +
+ +
+
@@ -55,6 +34,16 @@
+
+ +
+ + + +
+
+
+ + \ No newline at end of file diff --git a/application/admin/view/workers/worker/edit.html b/application/admin/view/workers/worker/edit.html index 5a1ccef..dd3b50e 100644 --- a/application/admin/view/workers/worker/edit.html +++ b/application/admin/view/workers/worker/edit.html @@ -3,73 +3,71 @@
- +
+
- +
- +
- {foreach name="statusList" item="vo"} - - {/foreach} + {foreach name="statusList" item="vo"} + + {/foreach}
-
- -
- -
+ +
+ +
-
- -
- -
-
-
- -
- -
- +
- +
- +
- -
-
-
- -
- + + + +
+ \ No newline at end of file diff --git a/public/assets/js/backend/order.js b/public/assets/js/backend/order.js index a295a9a..93f3ae1 100644 --- a/public/assets/js/backend/order.js +++ b/public/assets/js/backend/order.js @@ -188,8 +188,9 @@ define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefin var code = citypicker.getCode("district") || citypicker.getCode("city") || citypicker.getCode("province"); $("#area_id").val(code); }); - $("#c-city").on("click", function() { - Form.api.submit($("form[role=form]"),); + $("#mybuttom").on("click", function() { + Form.api.submit($("form[role=form]")); + Toastr.success('录入成功'); return false; }); Controller.api.bindevent(); diff --git a/public/assets/js/backend/workers/worker.js b/public/assets/js/backend/workers/worker.js index d01088d..f09ba4c 100644 --- a/public/assets/js/backend/workers/worker.js +++ b/public/assets/js/backend/workers/worker.js @@ -1,5 +1,19 @@ -define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) { - +define(['jquery', 'bootstrap', 'backend', 'table', 'form','jstree'], function ($, undefined, Backend, Table, Form) { + $.jstree.core.prototype.get_all_checked = function (full) { + var obj = this.get_selected(), i, j; + for (i = 0, j = obj.length; i < j; i++) { + obj = obj.concat(this.get_node(obj[i]).parents); + } + obj = $.grep(obj, function (v, i, a) { + return v != '#'; + }); + obj = obj.filter(function (itm, i, a) { + return i == a.indexOf(itm); + }); + return full ? $.map(obj, $.proxy(function (i) { + return this.get_node(i); + }, this)) : obj; + }; var Controller = { index: function () { // 初始化表格参数配置 @@ -50,6 +64,12 @@ define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefin Table.api.bindevent(table); }, add: function () { + $("#c-city").on("cp:updated", function() { + var citypicker = $(this).data("citypicker"); + var code = citypicker.getCode("district") || citypicker.getCode("city") || citypicker.getCode("province"); + $("#area_id").val(code); + }); + Controller.api.bindevent(); }, edit: function () { @@ -57,7 +77,53 @@ define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefin }, api: { bindevent: function () { - Form.api.bindevent($("form[role=form]")); + Form.api.bindevent($("form[role=form]"),null,null,function () { + if ($("#treeview").length > 0) { + var r = $("#treeview").jstree("get_all_checked"); + $("input[name='row[rules]']").val(r.join(',')); + } + return true; + }); + //渲染权限节点树 + //销毁已有的节点树 + $("#treeview").jstree("destroy"); + Controller.api.rendertree(nodeData); + //全选和展开 + $(document).on("click", "#checkall", function () { + $("#treeview").jstree($(this).prop("checked") ? "check_all" : "uncheck_all"); + }); + $(document).on("click", "#expandall", function () { + $("#treeview").jstree($(this).prop("checked") ? "open_all" : "close_all"); + }); + $("select[name='row[pid]']").trigger("change"); + }, + rendertree: function (content) { + $("#treeview") + .on('redraw.jstree', function (e) { + $(".layer-footer").attr("domrefresh", Math.random()); + }) + .jstree({ + "themes": {"stripes": true}, + "checkbox": { + "keep_selected_style": false, + }, + "types": { + "root": { + "icon": "fa fa-folder-open", + }, + "menu": { + "icon": "fa fa-folder-open", + }, + "file": { + "icon": "fa fa-file-o", + } + }, + "plugins": ["checkbox", "types"], + "core": { + 'check_callback': true, + "data": content + } + }); } } };