文件
chengduzhongchuliang/application/common/model/TestingModel.php

1016 行
38 KiB
PHP

此文件含有模棱两可的 Unicode 字符

此文件含有可能会与其他字符混淆的 Unicode 字符。 如果您是想特意这样的,可以安全地忽略该警告。 使用 Escape 按钮显示他们。

<?php
namespace app\common\model;
use think\Model;
/**
* 检测模型
* Class TestingModel
* @package app\common\model
*/
class TestingModel extends Model
{
protected $name = 'testing';
/**
* 关联粮仓
* @return \think\model\relation\HasOne
*/
public function granary()
{
return $this->hasOne(GranaryModel::class, 'id', 'gid')
->field(['id', 'name', 'num', 'code', 'addr', 'storage', 'varieties']);
}
/**
* 关联检测参数
* @return \think\model\relation\HasMany
*/
public function testingParams()
{
return $this->hasMany(TestingParamsModel::class, 'tid', 'id')
->field('color,create_time,update_time', true);
}
/**
* 关联检测参数(只获取数值的)
* @return \think\model\relation\HasMany
*/
public function testingParamsNum()
{
return $this->hasMany(TestingParamsModel::class, 'tid', 'id')
->where(['is_number' => 1])
->field('color,create_time,update_time', true);
}
/**
* 检测类型以及字段
* @return array
*/
public function getTypeList()
{
// 类型:rapeseed=菜籽油、bean=大豆油、wheat=小麦、paddy=稻谷、corn=玉米
$typeArr = [
'rapeseed' => [
'name' => '菜籽油',
'content' => [
[
'num' => 1,
'name' => '色泽',
'unit' => '',
'val' => '',
'warning' => '',
'alert' => '',
'color' => '',
'is_number' => 2,
],
[
'num' => 2,
'name' => '透明度(20℃)',
'unit' => '',
'val' => '',
'warning' => '',
'alert' => '',
'color' => '',
'is_number' => 2,
],
[
'num' => 3,
'name' => '气味、滋味',
'unit' => '',
'val' => '',
'warning' => '具有菜籽油固有的气味和滋味,无异味',
'alert' => '不具有菜籽油固有的气味和滋味,有异味',
'color' => '',
'is_number' => 2,
],
[
'num' => 4,
'name' => '加热试验(280℃)',
'unit' => '',
'val' => '',
'warning' => '',
'alert' => '',
'color' => '',
'is_number' => 2,
],
[
'num' => 5,
'name' => '水分及挥发物',
'unit' => '%',
'val' => '',
'warning' => 0.18,
'alert' => 0.2,
'color' => '',
'is_number' => 1,
],
[
'num' => 6,
'name' => '不溶性杂质',
'unit' => '%',
'val' => '',
'warning' => 0.045,
'alert' => 0.05,
'color' => '',
'is_number' => 1,
],
[
'num' => 7,
'name' => '酸价',
'unit' => 'mg/g',
'val' => '',
'warning' => 2.7,
'alert' => 3,
'color' => '',
'is_number' => 1,
],
[
'num' => 8,
'name' => '过氧化值',
'unit' => 'g/100g',
'val' => '',
'warning' => 0.225,
'alert' => 0.25,
'color' => '',
'is_number' => 1,
],
[
'num' => 9,
'name' => '含皂量',
'unit' => '%',
'val' => '',
'warning' => 0.027,
'alert' => 0.03,
'color' => '',
'is_number' => 1,
],
[
'num' => 10,
'name' => '铅',
'unit' => 'mg/kg',
'val' => '',
'warning' => 0.072,
'alert' => 0.08,
'color' => '',
'is_number' => 1,
],
[
'num' => 11,
'name' => '总砷',
'unit' => 'mg/kg',
'val' => '',
'warning' => 0.09,
'alert' => 0.1,
'color' => '',
'is_number' => 1,
],
[
'num' => 12,
'name' => '苯并(a)芘',
'unit' => 'μg/kg',
'val' => '',
'warning' => 9,
'alert' => 10,
'color' => '',
'is_number' => 1,
],
[
'num' => 13,
'name' => '黄曲霉毒素B1',
'unit' => 'μg/kg',
'val' => '',
'warning' => 9,
'alert' => 10,
'color' => '',
'is_number' => 1,
],
[
'num' => 14,
'name' => '脂肪酸组成',
'unit' => '%',
'val' => '',
'warning' => '合格',
'alert' => '不合格',
'color' => '',
'is_number' => 2,
],
]
],
'bean' => [
'name' => '大豆油',
'content' => [
[
'num' => 1,
'name' => '气味、滋味',
'unit' => '',
'val' => '',
'warning' => '具有大豆原油固有的气味和滋味,无异味',
'alert' => '未有大豆原油固有的气味和滋味,有异味',
'color' => '',
'is_number' => 2,
],
[
'num' => 2,
'name' => '不溶性杂质含量',
'unit' => '%',
'val' => '',
'warning' => 0.18,
'alert' => 0.2,
'color' => '',
'is_number' => 1,
],
[
'num' => 3,
'name' => '过氧化值',
'unit' => 'g/100g',
'val' => '',
'warning' => 0.225,
'alert' => 0.25,
'color' => '',
'is_number' => 1,
],
[
'num' => 4,
'name' => '水分及挥发物含量',
'unit' => '%',
'val' => '',
'warning' => 0.18,
'alert' => 0.2,
'color' => '',
'is_number' => 1,
],
[
'num' => 5,
'name' => '酸价(KOH)',
'unit' => 'mg/g',
'val' => '',
'warning' => 3.6,
'alert' => 4,
'color' => '',
'is_number' => 1,
],
[
'num' => 6,
'name' => '铅',
'unit' => 'mg/kg',
'val' => '',
'warning' => 0.072,
'alert' => 0.08,
'color' => '',
'is_number' => 1,
],
[
'num' => 7,
'name' => '总砷',
'unit' => 'mg/kg',
'val' => '',
'warning' => 0.09,
'alert' => 0.1,
'color' => '',
'is_number' => 1,
],
[
'num' => 8,
'name' => '黄曲霉毒素B1',
'unit' => 'μg/kg',
'val' => '',
'warning' => 9,
'alert' => 10,
'color' => '',
'is_number' => 1,
],
[
'num' => 9,
'name' => '溶剂残留量',
'unit' => 'mg/kg',
'val' => '',
'warning' => 90,
'alert' => 100,
'color' => '',
'is_number' => 1,
],
[
'num' => 10,
'name' => '脂肪酸组成',
'unit' => '%',
'val' => '',
'warning' => '合格',
'alert' => '不合格',
'color' => '',
'is_number' => 2,
],
]
],
'wheat' => [
'name' => '小麦',
'content' => [
[
'num' => 1,
'name' => '容重',
'unit' => 'g/L',
'val' => '',
'warning' => '一等/大于等于790',
'alert' => '二等/770-789 三等/750-769',
'color' => '',
'is_number' => 2,
],
[
'num' => 2,
'name' => '不完善粒',
'unit' => '%',
'val' => '',
'warning' => 7.2,
'alert' => 8,
'color' => '',
'is_number' => 1,
],
[
'num' => 3,
'name' => '杂质',
'unit' => '%',
'val' => '',
'warning' => 0.9,
'alert' => 1,
'color' => '',
'is_number' => 1,
],
[
'num' => 4,
'name' => '水分',
'unit' => '%',
'val' => '',
'warning' => 12.3,
'alert' => 12.5,
'color' => '',
'is_number' => 1,
],
[
'num' => 5,
'name' => '无机杂质',
'unit' => 'g/100g',
'val' => '',
'warning' => 0.45,
'alert' => 0.5,
'color' => '',
'is_number' => 1,
],
[
'num' => 6,
'name' => '色泽、气味质量指标',
'unit' => '',
'val' => '',
'warning' => '达标',
'alert' => '未达标',
'color' => '',
'is_number' => 2,
],
[
'num' => 7,
'name' => '面筋吸水量',
'unit' => '%',
'val' => '',
'warning' => 185,
'alert' => 180,
'color' => '',
'is_number' => 1,
],
[
'num' => 8,
'name' => '品尝评分值',
'unit' => '分',
'val' => '',
'warning' => '',
'alert' => '',
'color' => '',
'is_number' => 2,
],
[
'num' => 9,
'name' => '色泽、气味储存指标',
'unit' => '',
'val' => '',
'warning' => '宜存',
'alert' => '不宜存',
'color' => '',
'is_number' => 2,
],
[
'num' => 10,
'name' => '铅',
'unit' => 'mg/kg',
'val' => '',
'warning' => 0.18,
'alert' => 0.2,
'color' => '',
'is_number' => 1,
],
[
'num' => 11,
'name' => '镉',
'unit' => 'mg/kg',
'val' => '',
'warning' => 0.09,
'alert' => 0.1,
'color' => '',
'is_number' => 1,
],
[
'num' => 12,
'name' => '总汞',
'unit' => 'mg/kg',
'val' => '',
'warning' => 0.018,
'alert' => 0.02,
'color' => '',
'is_number' => 1,
],
[
'num' => 13,
'name' => '总砷',
'unit' => 'mg/kg',
'val' => '',
'warning' => 0.45,
'alert' => 0.5,
'color' => '',
'is_number' => 1,
],
[
'num' => 14,
'name' => '黄曲霉毒素B1',
'unit' => 'μg/kg',
'val' => '',
'warning' => 4.5,
'alert' => 5,
'color' => '',
'is_number' => 1,
],
[
'num' => 15,
'name' => '脱氧雪腐镰刀菌烯醇',
'unit' => 'μg/g',
'val' => '',
'warning' => 0,
'alert' => 0.75,
'color' => 0.9,
'is_number' => 1,
],
[
'num' => 16,
'name' => '玉米赤霉烯酮',
'unit' => 'μg/kg',
'val' => '',
'warning' => 54,
'alert' => 60,
'color' => '',
'is_number' => 1,
],
]
],
'paddy' => [
'name' => '稻谷',
'content' => [
[
'num' => 1,
'name' => '水分',
'unit' => 'g/100g',
'val' => '',
'warning' => 13.3,
'alert' => 13.5,
'color' => '',
'is_number' => 1,
],
[
'num' => 2,
'name' => '杂质',
'unit' => '%',
'val' => '',
'warning' => 0.9,
'alert' => 1,
'color' => '',
'is_number' => 1,
],
[
'num' => 3,
'name' => '出糙率',
'unit' => '%',
'val' => '',
'warning' => '一等/大于等于79',
'alert' => '二等/77-78.9 三等/75-76.9',
'color' => '',
'is_number' => 2,
],
[
'num' => 4,
'name' => '整精米率',
'unit' => '%',
'val' => '',
'warning' => 44.5,
'alert' => 44,
'color' => '',
'is_number' => 1,
],
[
'num' => 5,
'name' => '黄粒米',
'unit' => '%',
'val' => '',
'warning' => 0.9,
'alert' => 1,
'color' => '',
'is_number' => 1,
],
[
'num' => 6,
'name' => '谷外糙米',
'unit' => '%',
'val' => '',
'warning' => 1.0,
'alert' => 2,
'color' => '',
'is_number' => 1,
],
[
'num' => 7,
'name' => '互混',
'unit' => '%',
'val' => '',
'warning' => 4.5,
'alert' => 5,
'color' => '',
'is_number' => 1,
],
[
'num' => 8,
'name' => '色泽、气味(质量指标)',
'unit' => '',
'val' => '',
'warning' => '达标',
'alert' => '不达标',
'color' => '',
'is_number' => 2,
],
[
'num' => 9,
'name' => '脂肪酸值(KOH/干基)',
'unit' => 'mg/100g',
'val' => '',
'warning' => 27,
'alert' => 30,
'color' => '',
'is_number' => 1,
],
[
'num' => 10,
'name' => '品尝评分值',
'unit' => '分',
'val' => '',
'warning' => '',
'alert' => '',
'color' => '',
'is_number' => 2,
],
[
'num' => 11,
'name' => '色泽、气味(储存指标)',
'unit' => '',
'val' => '',
'warning' => '宜存',
'alert' => '不宜存',
'color' => '',
'is_number' => 2,
],
[
'num' => 12,
'name' => '铅',
'unit' => 'mg/kg',
'val' => '',
'warning' => 0.18,
'alert' => 0.2,
'color' => '',
'is_number' => 1,
],
[
'num' => 13,
'name' => '镉',
'unit' => 'mg/kg',
'val' => '',
'warning' => 0.18,
'alert' => 0.2,
'color' => '',
'is_number' => 1,
],
[
'num' => 14,
'name' => '总汞',
'unit' => 'mg/kg',
'val' => '',
'warning' => 0.018,
'alert' => 0.02,
'color' => '',
'is_number' => 1,
],
[
'num' => 15,
'name' => '无机砷',
'unit' => 'mg/kg',
'val' => '',
'warning' => 0.315,
'alert' => 0.35,
'color' => '',
'is_number' => 1,
],
[
'num' => 16,
'name' => '黄曲霉毒素B1',
'unit' => 'μg/kg',
'val' => '',
'warning' => 9,
'alert' => 20,
'color' => '',
'is_number' => 1,
],
]
],
'corn' => [
'name' => '玉米',
'content' => [
[
'num' => 1,
'name' => '水分',
'unit' => 'g/100g',
'val' => '',
'warning' => 13.8,
'alert' => 14,
'color' => '',
'is_number' => 1,
],
[
'num' => 2,
'name' => '杂质',
'unit' => '%',
'val' => '',
'warning' => 0.9,
'alert' => 1,
'color' => '',
'is_number' => 1,
],
[
'num' => 3,
'name' => '不完善粒',
'unit' => '%',
'val' => '',
'warning' => 7.2,
'alert' => 8,
'color' => '',
'is_number' => 1,
],
[
'num' => 4,
'name' => '霉变粒',
'unit' => '%',
'val' => '',
'warning' => 1.8,
'alert' => 2,
'color' => '',
'is_number' => 1,
],
[
'num' => 5,
'name' => '容重',
'unit' => 'g/L',
'val' => '',
'warning' => '一等/大于等于720',
'alert' => '二等/690-719 三等/660-689',
'color' => '',
'is_number' => 2,
],
[
'num' => 6,
'name' => '色泽、气味质量指标',
'unit' => '',
'val' => '',
'warning' => '达标',
'alert' => '不达标',
'color' => '',
'is_number' => 2,
],
[
'num' => 7,
'name' => '脂肪酸值(KOH/干基)',
'unit' => 'mg/100g',
'val' => '',
'warning' => 62,
'alert' => 65,
'color' => '',
'is_number' => 1,
],
[
'num' => 8,
'name' => '品尝评分值',
'unit' => '分',
'val' => '',
'warning' => '',
'alert' => '',
'color' => '',
'is_number' => 2,
],
[
'num' => 9,
'name' => '色泽、气味储存指标',
'unit' => '',
'val' => '',
'warning' => '宜存',
'alert' => '不宜存',
'color' => '',
'is_number' => 2,
],
[
'num' => 10,
'name' => '铅',
'unit' => 'mg/kg',
'val' => '',
'warning' => 0.18,
'alert' => 0.2,
'color' => '',
'is_number' => 1,
],
[
'num' => 11,
'name' => '镉',
'unit' => 'mg/kg',
'val' => '',
'warning' => 0.09,
'alert' => 0.1,
'color' => '',
'is_number' => 1,
],
[
'num' => 12,
'name' => '总汞',
'unit' => 'mg/kg',
'val' => '',
'warning' => 0.018,
'alert' => 0.02,
'color' => '',
'is_number' => 1,
],
[
'num' => 13,
'name' => '总砷',
'unit' => 'mg/kg',
'val' => '',
'warning' => 0.45,
'alert' => 0.5,
'color' => '',
'is_number' => 1,
],
[
'num' => 14,
'name' => '脱氧雪腐镰刀菌烯醇',
'unit' => 'μg/kg',
'val' => '',
'warning' => 900,
'alert' => 1000,
'color' => '',
'is_number' => 1,
],
[
'num' => 15,
'name' => '黄曲霉毒素B1',
'unit' => 'μg/kg',
'val' => '',
'warning' => 18,
'alert' => 20,
'color' => '',
'is_number' => 1,
],
[
'num' => 16,
'name' => '玉米赤霉烯酮',
'unit' => 'μg/kg',
'val' => '',
'warning' => 54,
'alert' => 60,
'color' => '',
'is_number' => 1,
],
]
],
];
return $typeArr;
}
/**
* 获取列表
* @param $params
* @param int $rows
* @param int $page
* @param string $fields
* @return array
* @throws \think\exception\DbException
*/
public function getLists($params, $rows = 20, $page = 1, $fields = '*')
{
// 条件搜索
if (!empty($params['where'])) {
$this->where($params['where']);
} else {
if (!empty($params['keyword'])) {
}
if (!empty($params['gid'])) {
$this->where(['gid' => $params['gid']]);
}
if (!empty($params['type'])) {
$this->where(['type' => $params['type']]);
}
if (!empty($params['date'])) {
$date = explode('-', $params['date']);
$this->where(['year' => $date[0], 'month' => $date[1]]);
}
// 时间范围查询
if (!empty($params['start_time'])) {
$this->where('create_time', '>=', strtotime($params['start_time']));
}
if (!empty($params['end_time'])) {
$this->where('create_time', '<=', strtotime($params['end_time']));
}
if (!empty($params['season'])) {
$this->where('season', '=', $params['season']);
}
}
if (!empty($params['with'])) {
$this->with($params['with']);
}
// 排序
$order = !empty($params['order']) ? $params['order'] : 'id DESC';
$result = $this
->field($fields)
->order($order)
->paginate($rows, false, [
'page' => $page
]);
$arr = [
'list' => [],
'total' => 0,
'page' => 1,
'pageSize' => $rows,
'totalSize' => 0
];
// 格式化数据信息
if (!$result->isEmpty()) {
$arr = [
'list' => $result->items(),
'total' => $result->total(),
'page' => $result->currentPage(),
'pageSize' => $result->listRows(),
'totalSize' => $result->lastPage()
];
}
return $arr;
}
/**
* 添加
* @param $data
* @return bool|int|string
*/
public function add($data, $content, $date)
{
if (empty($data) || !is_array($data)) {
$this->error = '参数不正确';
return false;
}
return $this->transaction(function () use ($data, $content, $date) {
$date = explode('-', $date);
$data['year'] = $date[0];
$data['month'] = $date[1];
$data['test_date'] = $date[0] . $date[1];
$id = $this->insertGetId($data);
if (empty($data['sort'])) {
$this->update([
'id' => $id,
'sort' => $id
]);
}
// 添加参数
foreach ($content as $v) {
TestingParamsModel::insert([
'gid' => $data['gid'],
'tid' => $id,
'num' => $v['num'],
'name' => $v['name'],
'unit' => $v['unit'],
'val' => $v['val'],
'warning' => $v['warning'],
'alert' => $v['alert'],
'test_date' => $data['test_date'],
'year' => $data['year'],
'season' => $data['season'],
'is_number' => $v['is_number'],
'create_time' => time()
]);
}
return $id;
});
}
/**
* 编辑
* @param $data
* @return bool
*/
public function edit($data, $content, $date)
{
if (empty($data) || !is_array($data)) {
$this->error = '参数不正确';
return false;
}
return $this->transaction(function () use ($data, $content, $date) {
$date = explode('-', $date);
$data['year'] = $date[0];
$data['month'] = $date[1];
$data['test_date'] = $date[0] . $date[1];
$res = $this->update($data);
// 编辑参数
foreach ($content as $v) {
TestingParamsModel::update([
'gid' => $data['gid'],
'id' => $v['id'],
'num' => $v['num'],
'name' => $v['name'],
'unit' => $v['unit'],
'val' => $v['val'],
'warning' => $v['warning'],
'alert' => $v['alert'],
'test_date' => $data['test_date'],
'year' => $data['year'],
'season' => $data['season'],
'is_number' => $v['is_number'],
'update_time' => time()
]);
}
return $res;
});
}
/**
* 获取某条记录
* @param $where
* @return array|bool|false|\PDOStatement|string|Model
* @throws \think\db\exception\DataNotFoundException
* @throws \think\db\exception\ModelNotFoundException
* @throws \think\exception\DbException
*/
public function getItem($where, $fields = '*', $with = [])
{
if (empty($where)) {
$this->error = '参数不正确。';
return false;
}
if (floor($where) > 0) {
$where = ['id' => $where];
}
if (!empty($with)) {
$this->with($with);
}
return $this->where($where)->field($fields)->find();
}
/**
* 删除
* @param $id
* @return bool|mixed
*/
public function del($id)
{
if (empty($id)) {
$this->error = '参数不正确';
return false;
}
return $this->transaction(function () use ($id) {
TestingParamsModel::where(['tid' => $id])->delete();
return $this->where([
'id' => $id,
])->delete();
});
}
}