文件
chengduzhongchuliang/application/admin/controller/Granary.php

176 行
5.2 KiB
PHP

<?php
namespace app\admin\controller;
use app\common\controller\Backend;
use app\common\model\ChinaModel;
use app\common\model\GranaryModel;
use app\common\model\TestingModel;
/**
* 粮仓管理
* Class Granary
* @package app\admin\controller
*/
class Granary extends Backend
{
/**
* 模型对象
* @var \app\common\model\GranaryModel
*/
protected $model = null;
public function _initialize()
{
parent::_initialize();
$this->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)) {
$id = $this->model->where(['num' => $data['num']])->value('id');
$data['granary_type'] = $data['num'] > 18 ? 2 : 1;
$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' => '删除成功']);
}
}
}