文件
chengduzhongchuliang/application/api/controller/Index.php

216 行
6.6 KiB
PHP

<?php
namespace app\api\controller;
use app\common\controller\Api;
use app\common\model\ContentModel;
use app\common\model\GranaryModel;
use app\common\model\TestingModel;
use app\common\model\TestingParamsModel;
use think\Db;
/**
* 首页接口
*/
class Index extends Api
{
protected $noNeedLogin = ['*'];
protected $noNeedRight = ['*'];
/**
* 首页
*/
public function index()
{
$this->success('请求成功');
}
/**
* 获取仓库列表
* @param GranaryModel $granaryModel
* @throws \think\db\exception\DataNotFoundException
* @throws \think\db\exception\ModelNotFoundException
* @throws \think\exception\DbException
*/
public function granaryList(GranaryModel $granaryModel)
{
$list = $granaryModel
->field('province_id,city_id,update_time', true)
->select();
// 数据处理
if (!empty($list)) {
$list = collection($list)->toArray();
foreach ($list as &$v) {
$v['varieties'] = getVarietiesInfo($v['varieties']);
$v['harvest_year'] = date('Y-m-d', $v['harvest_year']);
$v['enter_time'] = date('Y-m-d', $v['enter_time']);
$v['create_time'] = date('Y-m-d', $v['create_time']);
}
}
$this->success('获取成功', [
'list' => $list
]);
}
/**
* 仓库详情
* @param GranaryModel $granaryModel
* @param TestingModel $testingModel
* @param TestingParamsModel $testingParamsModel
* @throws \think\db\exception\DataNotFoundException
* @throws \think\db\exception\ModelNotFoundException
* @throws \think\exception\DbException
*/
public function granaryDetail(GranaryModel $granaryModel, TestingModel $testingModel, TestingParamsModel $testingParamsModel)
{
$id = $this->request->get('id');
$type = $this->request->get('type');
if (empty($id) || empty($type)) {
$this->error('参数错误', [], 401);
}
$granary = $granaryModel->where(['id' => $id])
->field('province_id,city_id,update_time', true)
->find();
if (empty($granary)) {
$this->error('您要查看的仓库不存在', [], 402);
}
// 获取农作物最新检测参数
$test = $testingModel
->with(['testingParams'])
->where(['gid' => $id, 'type' => $type])
->field(['id', 'type', 'test_date'])
->order('test_date DESC')
->find();
// 折线图统计数据
$seriesData = [];
$xAxisData = [];
if (!empty($test)) {
$seriesData = Db::name('testing_params')
->where(['tid' => $test->id, 'name' => ['NEQ', '容重'], 'is_number' => 1])
->field(['id', 'name', 'unit', 'color'])
->select();
$testList = Db::name('testing')
->field(['id', 'year'])
->where(['gid' => $id])
->group('year')
->order('sort DESC')
->limit(6)
->select();
// 重新排序
// $yearArr = array_column($testList, 'year');
// array_multisort($yearArr, SORT_ASC, $testList);
foreach ($testList as $index => $value) {
// 获取季度
$list = Db::name('testing')
->where(['gid' => $id, 'year' => $value['year']])
->field('season')
->select();
foreach ($list as $v) {
switch ($v['season']) {
case 1 :
$season = '春季';
break;
case 2 :
$season = '秋季';
break;
case 3 :
$season = '满仓';
break;
}
array_push($xAxisData, $value['year'] . $season);
}
foreach ($seriesData as &$item) {
// 获取参数数值
foreach ($list as $val) {
$val = Db::name('testing_params')->where([
'gid' => $id,
'name' => $item['name'],
'year' => $value['year'],
'season' => $val['season'],
])->value('val');
$item['data'][] = !empty($val) ? $val : 0;
$item['color'] = getColorCode($item['name']);
}
}
}
}
// 质检报告列表
$contentList = Db::name('content')
->where(['gid' => $id, 'status' => 1])
->field(['id', 'type', 'test_date'])
->order('test_date ASC')
->select();
if (!empty($contentList)) {
foreach ($contentList as &$v) {
switch ($v['type']) {
case 1 :
$v['title'] = $v['test_date'] . '满仓验收';
break;
case 2 :
$v['title'] = $v['test_date'] . '春普';
break;
case 3 :
$v['title'] = $v['test_date'] . '秋普';
break;
}
}
}
// 时间处理
$granary->create_time = date('Y-m-d H:i:s', $granary->create_time);
$this->success('获取成功', [
'granary' => $granary,
'test' => $test,
'xAxisData' => $xAxisData,
'seriesData' => $seriesData,
'contentList' => $contentList
]);
}
/**
* 获取质检报告详情
* @param ContentModel $contentModel
* @throws \think\db\exception\DataNotFoundException
* @throws \think\db\exception\ModelNotFoundException
* @throws \think\exception\DbException
*/
public function contentDetail(ContentModel $contentModel)
{
$id = $this->request->get('id');
if (empty($id)) {
$this->error('参数错误', [], 401);
}
$content = $contentModel->getItem($id, [
'id',
'content'
]);
if (empty($content)) {
$this->error('您要查看的质检报告不存在', [], 402);
}
$this->success('获取成功', [
'content' => $content,
]);
}
}