model = new \app\admin\model\SkillDict; // 从真实技能分类表中拉取分类列表 $categoryList = Db::name('skill_category') ->where('status', 'normal') ->order('weigh desc, id asc') ->column('name', 'id'); $categorySearchList = []; foreach ($categoryList as $catName) { $categorySearchList[$catName] = $catName; } $this->view->assign("categoryList", $categoryList); $this->assignconfig("categorySearchList", $categorySearchList); $this->view->assign("sourceList", $this->model->getSourceList()); $this->view->assign("statusList", $this->model->getStatusList()); } /** * 查看技能列表(支持 category_id 跳转过滤,并统计持证师傅人数) */ public function index() { $this->request->filter(['strip_tags', 'trim']); if (false === $this->request->isAjax()) { return $this->view->fetch(); } if ($this->request->request('keyField')) { return $this->selectpage(); } $catIdParam = (int)$this->request->get('category_id', 0); [$where, $sort, $order, $offset, $limit] = $this->buildparams(); $query = $this->model ->with(['master']) ->where($where); if ($catIdParam > 0) { $query->where('skill_dict.category_id', $catIdParam); } $list = $query->order($sort, $order)->paginate($limit); $rows = $list->items(); $names = []; foreach ($rows as $r) { if (!empty($r['name'])) { $names[] = (string)$r['name']; } } $masterCountMap = []; if ($names) { $stats = Db::name('master_skill') ->where('skill_name', 'in', array_unique($names)) ->field('skill_name, COUNT(DISTINCT master_id) AS cnt') ->group('skill_name') ->select(); foreach ($stats as $st) { $masterCountMap[$st['skill_name']] = (int)$st['cnt']; } } foreach ($rows as &$item) { $item['master_count'] = $masterCountMap[$item['name']] ?? 0; } unset($item); return json(['total' => $list->total(), 'rows' => $rows]); } /** * 添加技能 */ public function add() { if ($this->request->isPost()) { $params = $this->request->post("row/a"); if ($params) { if (!empty($params['category_id'])) { $cat = Db::name('skill_category')->where('id', $params['category_id'])->find(); if ($cat) { $params['category'] = $cat['name']; } } $result = $this->model->allowField(true)->save($params); if ($result !== false) { $this->success(); } else { $this->error($this->model->getError()); } } $this->error(__('Parameter %s can not be empty', '')); } $this->view->assign('defaultCategoryId', (int)$this->request->get('category_id', 0) ?: ''); return $this->view->fetch(); } /** * 编辑技能 */ public function edit($ids = null) { $row = $this->model->get($ids); if (!$row) { $this->error(__('No Results were found')); } if ($this->request->isPost()) { $params = $this->request->post("row/a"); if ($params) { if (!empty($params['category_id'])) { $cat = Db::name('skill_category')->where('id', $params['category_id'])->find(); if ($cat) { $params['category'] = $cat['name']; } } $result = $row->allowField(true)->save($params); if ($result !== false) { $this->success(); } else { $this->error($row->getError()); } } $this->error(__('Parameter %s can not be empty', '')); } $this->view->assign("row", $row); return $this->view->fetch(); } /** * 删除保护:已被师傅绑定的技能禁止物理删除 */ public function del($ids = null) { if (false === $this->request->isPost()) { $this->error(__('Invalid parameters')); } $ids = $ids ?: $this->request->post('ids'); $idArr = array_values(array_filter(array_map('intval', explode(',', (string)$ids)))); if (!$idArr) { $this->error(__('Parameter %s can not be empty', 'ids')); } $names = Db::name('skill_dict')->where('id', 'in', $idArr)->column('name'); if ($names) { $usedCnt = (int)Db::name('master_skill')->where('skill_name', 'in', $names)->count(); if ($usedCnt > 0) { $this->error('所选技能已被 ' . $usedCnt . ' 条师傅档案绑定,禁止物理删除,请改为「隐藏停用」'); } } return parent::del($ids); } }