model = new TestingModel(); } /** * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法 * 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑 * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改 */ /** * 获取列表 * @return mixed|string|\think\response\Json * @throws \think\exception\DbException */ public function index() { if (request()->isAjax()) { $page = input('param.page/d', 1); $rows = input('param.rows/d', 20); $gid = input('param.gid', ''); $type = input('param.type', ''); $date = input('param.date', ''); $startTime = input('param.start_time', ''); $endTime = input('param.end_time', ''); $season = input('param.season', ''); $this->scopeRecords($this->model); $result = $this->model->getLists([ 'gid' => $gid, 'type' => $type, 'date' => $date, 'start_time' => $startTime, 'end_time' => $endTime, 'season' => $season, 'order' => 'sort DESC', 'with' => ['granary', 'testingParams'] ], $rows, $page); // 仓库 $result['granaryList'] = GranarySite::granaries($this->granarySites); if (!empty($result['granaryList'])) { foreach ($result['granaryList'] as &$v) { $w = getVarietiesInfo($v['varieties']); $v['type'] = $w['type']; } } // 类型 $result['typeList'] = $this->model->getTypeList(); return json($result); } return $this->fetch(); } /** * 排序 * @return \think\response\Json */ public function sort() { $data = $this->request->post(); $data['update_time'] = time(); $res = $this->model->update($data); if (!empty($res)) { return json([ 'code' => 200, 'msg' => '更改成功', ]); } else { return json([ 'code' => 400, 'msg' => '更改失败', ]); } } /** * 获取仓房列表下拉数据 * @return array */ protected function getGranaryOptions() { $granaryList = GranarySite::granaries($this->granarySites); if (!empty($granaryList)) { foreach ($granaryList as &$v) { $w = getVarietiesInfo($v['varieties']); $v['type'] = $w['type']; } } return $granaryList; } /** * 添加(GET返回独立页面,POST执行添加) * @return string|\think\response\Json */ public function add() { if ($this->request->isPost()) { $data = $this->request->post(); if (!empty($data)) { $content = $data['content']; $date = $data['record_date']; $types = $this->model->getTypeList(); $originalRules = isset($types[$data['type']]) ? $types[$data['type']]['content'] : []; if (!empty($data['id'])) { $existing = $this->model->getItem((int)$data['id'], '*', ['testingParams']); if (!$existing) { return json(['code' => 400, 'msg' => '检测记录不存在']); } $existing = $existing->toArray(); $originalRules = $existing['testing_params']; $allowedIds = array_column($originalRules, 'id'); foreach (is_array($content) ? $content : [] as $item) { if (!is_array($item) || empty($item['id']) || !in_array($item['id'], $allowedIds)) { return json(['code' => 400, 'msg' => '检测参数不属于当前记录,请刷新后重试']); } } } $ruleError = \app\common\library\QualityRule::validate($content, $originalRules); if ($ruleError !== '') { return json(['code' => 400, 'msg' => $ruleError]); } // 添加 if (empty($data['id'])) { unset($data['id'], $data['content'], $data['record_date']); $data['create_time'] = time(); if ($this->model->add($data, $content, $date)) { return json([ 'code' => 200, 'msg' => '添加成功' ]); } else { return json([ 'code' => 400, 'msg' => '添加失败' ]); } } // 编辑 $data['update_time'] = time(); unset($data['content'], $data['record_date']); if ($this->model->edit($data, $content, $date)) { return json([ 'code' => 200, 'msg' => '编辑成功' ]); } else { return json([ 'code' => 400, 'msg' => '编辑失败' ]); } } } $granaryList = $this->getGranaryOptions(); $typeList = $this->model->getTypeList(); $this->assign('item', null); $this->assign('granaryList', $granaryList); $this->assign('typeList', $typeList); $this->assign('siteOptions', $this->granarySites); return $this->fetch('edit'); } /** * 独立编辑页面 * @param null $ids * @return string * @throws \think\Exception */ public function edit($ids = null) { $id = $ids ?: input('param.ids', input('param.id')); $item = $this->model->getItem((int)$id, '*', ['granary', 'testingParams']); if (!$item) { $this->error('检测记录不存在'); } $item = $item->toArray(); $granaryList = $this->getGranaryOptions(); $typeList = $this->model->getTypeList(); $this->assign('item', $item); $this->assign('granaryList', $granaryList); $this->assign('typeList', $typeList); $this->assign('siteOptions', $this->granarySites); return $this->fetch('edit'); } /** * 独立详情页面 * @param null $ids * @return string * @throws \think\Exception */ public function detail($ids = null) { $id = $ids ?: input('param.ids', input('param.id')); $item = $this->model->getItem((int)$id, '*', ['granary', 'testingParams']); if (!$item) { $this->error('检测记录不存在'); } $item = $item->toArray(); $granaryList = $this->getGranaryOptions(); $typeList = $this->model->getTypeList(); $this->assign('item', $item); $this->assign('granaryList', $granaryList); $this->assign('typeList', $typeList); $this->assign('siteOptions', $this->granarySites); return $this->fetch('detail'); } // 删除 public function dell() { $id = input('id'); if (!empty($id)) { $ids = strpos($id, ',') !== false ? explode(',', $id) : [$id]; foreach ($ids as $v) { if (!$this->model->getItem((int)$v)) { return json(['code' => 401, 'msg' => '您要删除的检测参数不存在']); } } foreach ($ids as $v) { $this->model->del($v); } return json(['code' => 200, 'msg' => '删除成功']); } } }