Departmentmodel = new DepartmentModel; $this->Positionmodel = new PositionModel; } public function index() { if (request()->isAjax()) { $list = $this->Departmentmodel->with('position')->where('pid', 0)->select(); $data = array(); $length = 0; for($i = 0;$i < count($list); $i++){ $data[$length++] = [ 'd_id' => $list[$i]['id'], 'id' => -1, 'title' => $list[$i]['title'], 'did' => 0, 'remark' => $list[$i]['remark'], 'status' => $list[$i]['status'], ]; $position = $list[$i]['position']; for($j = 0;$j < count($position); $j++){ $data[$length++] = [ 'd_id' => -1, 'id' => $position[$j]['id'], 'title' => $position[$j]['title'], 'did' => $position[$j]['did'], 'remark' => $position[$j]['remark'], 'status' => $position[$j]['status'], ]; } } // halt($data); return to_assign(0, '', $data); } else { return view(); } } //添加&编辑 public function add() { $param = get_params(); if (request()->isAjax()) { if (!empty($param['id']) && $param['id'] > 0) { try { validate(PositionCheck::class)->scene('edit')->check($param); } catch (ValidateException $e) { // 验证失败 输出错误信息 return to_assign(1, $e->getError()); } $res = Db::name('Position')->where(['id' => $param['id']])->strict(false)->field(true)->update($param); if($res!==false){ add_log('edit', $param['id'], $param); return to_assign(); } else{ return to_assign(1, '提交失败'); } } else { try { validate(PositionCheck::class)->scene('add')->check($param); } catch (ValidateException $e) { // 验证失败 输出错误信息 return to_assign(1, $e->getError()); } $pid = Db::name('Position')->strict(false)->field(true)->insertGetId($param); if($pid>0){ add_log('add', $pid, $param); return to_assign(); } else{ return to_assign(1, '提交失败'); } } } else{ $id = isset($param['id']) ? $param['id'] : 0; if ($id > 0) { $detail = Db::name('Position')->where(['id' => $id])->find(); View::assign('detail', $detail); } View::assign('id', $id); return view(); } } //删除 public function delete() { $id = get_params("id"); if ($id == 1) { return to_assign(0, "超级岗位,不能删除"); } $data['status'] = '-1'; $data['id'] = $id; $data['update_time'] = time(); if (Db::name('Position')->update($data) !== false) { add_log('delete', $id); return to_assign(0, "删除岗位成功"); } else { return to_assign(1, "删除失败"); } } }