model = new GranaryModel(); } /** * 默认生成的控制器所继承的父类中有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); $key = input('param.keyword', ''); $type = input('param.type', ''); $startTime = input('param.start_time', ''); $endTime = input('param.end_time', ''); $granaryType = input('param.granary_type', ''); $china = input('param.china/a', ''); $varieties = input('param.varieties', ''); $result = $this->model->getLists([ 'keyword' => $key, 'type' => $type, 'china' => $china, 'start_time' => $startTime, 'end_time' => $endTime, 'granary_type' => $granaryType, 'varieties' => $varieties, ], $rows, $page); // 全国数据 $result['chinaList'] = (new ChinaModel())->getChinaData(); // 品种类型 $result['typeList'] = (new TestingModel())->getTypeList(); // 粮仓字段 $result['contentFields'] = $this->model->getContentField(); return json($result); } return $this->fetch(); } /** * 详情 * @return mixed * @throws \think\db\exception\DataNotFoundException * @throws \think\db\exception\ModelNotFoundException * @throws \think\exception\DbException */ public function detail() { $id = $this->request->get('id'); $detail = $this->model->getItem($id); $this->assign([ 'detail' => $detail ]); return $this->fetch(); } // 添加/编辑 public function add() { $data = $this->request->post(); if (!empty($data)) { $content = isset($data['content']) && is_array($data['content']) ? $data['content'] : []; $position = isset($content['screenPosition']) ? $content['screenPosition'] : ''; $positions = \app\common\library\GranaryScreen::extraPositions(); if ($position !== '' && !isset($positions[$position])) { return json(['code' => 400, 'msg' => '请选择有效的大屏建筑位置']); } if ($position !== '') { if (strpos($data['storage'], '习水') !== false) { return json(['code' => 400, 'msg' => '此建筑位置属于中心库区']); } foreach ($this->model->field('id,content')->select() as $existing) { $saved = $existing['content']; if (!is_array($saved)) $saved = json_decode($saved, true); if ((string)$existing['id'] !== (string)(isset($data['id']) ? $data['id'] : 0) && isset($saved['screenPosition']) && $saved['screenPosition'] === $position) { return json(['code' => 400, 'msg' => '此建筑已绑定仓库,请编辑对应仓库或选择其他位置']); } } } $data['content'] = $content; $id = $this->model->where(['num' => $data['num']])->value('id'); if (isset($data['granary_type']) && in_array(intval($data['granary_type']), [1, 2, 3])) { $data['granary_type'] = intval($data['granary_type']); } else { $data['granary_type'] = $data['num'] > 18 ? 2 : 1; } if ($position !== '') { $data['granary_type'] = strpos($position, 'center_silo_') === 0 ? 3 : 2; } if ($data['granary_type'] === 3) { $data['content']['warehouseType'] = '立式浅圆仓'; } $data['content'] = json_encode($data['content']); // 添加 if (empty($data['id'])) { if (!empty($id)) { return json([ 'code' => 401, 'msg' => '您输入的仓号已存在' ]); } unset($data['id'], $data['old_num'], $data['china']); $data['create_time'] = time(); if ($this->model->add($data)) { return json([ 'code' => 200, 'msg' => '添加成功' ]); } else { return json([ 'code' => 400, 'msg' => '添加失败' ]); } } if ($data['num'] != $data['old_num']) { if (!empty($id)) { return json([ 'code' => 401, 'msg' => '您输入的仓号已存在' ]); } } // 编辑 $data['update_time'] = time(); unset($data['old_num'], $data['china']); if ($this->model->edit($data)) { return json([ 'code' => 200, 'msg' => '编辑成功' ]); } else { return json([ 'code' => 400, 'msg' => '编辑失败' ]); } } } // 删除 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' => '删除成功']); } } }