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

176 行
5.0 KiB
PHP

<?php
namespace app\admin\controller;
use app\common\controller\Backend;
use app\common\model\TestingModel;
use think\Db;
/**
* 检测参数管理
* Class Testing
* @package app\admin\controller
*/
class Testing extends Backend
{
/**
* 模型对象
* @var \app\common\model\TestingModel
*/
protected $model = null;
public function _initialize()
{
parent::_initialize();
$this->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', '');
$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'] = Db::name('granary')
->field(['id', 'name', 'varieties'])
->select();
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 string|\think\response\Json
*/
public function add()
{
$data = $this->request->post();
if (!empty($data)) {
$content = $data['content'];
$date = $data['record_date'];
$ruleError = \app\common\library\QualityRule::validate($content);
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' => '编辑失败'
]);
}
}
}
// 删除
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' => '删除成功']);
}
}
}