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

321 行
11 KiB
PHP

<?php
namespace app\admin\controller;
use app\admin\model\Admin;
use app\admin\model\User;
use app\common\controller\Backend;
use app\common\model\Attachment;
use app\common\model\GranaryModel;
use app\common\model\TestingModel;
use fast\Date;
use think\Db;
/**
* 控制台
*
* @icon fa fa-dashboard
* @remark 用于展示当前系统中的统计数据、统计报表及重要实时数据
*/
class Dashboard extends Backend
{
public function index()
{
// 今日开始时间和结束时间
$startTime = strtotime(date('Y-m-d 00:00:00'));
$endTime = strtotime(date('Y-m-d 23:59:59'));
// 本月开始时间和结束时间
$startMonthTime = strtotime(date('Y-m-01 00:00:00'));
$endMonthTime = strtotime(date('Y-m-t 23:59:59'));
$data = [
// 圆形仓库
'roundGranary' => [
// 总计
'total' => Db::name('granary')->where(['granary_type' => 1])->count(),
// 今日
'current' => Db::name('granary')
->where("granary_type = 1 AND create_time >= {$startTime} AND create_time <= {$endTime}")
->count(),
// 本月
'month' => Db::name('granary')
->where("granary_type = 1 AND create_time >= {$startMonthTime} AND create_time <= {$endMonthTime}")
->count(),
],
// 方形仓库
'squareGranary' => [
// 总计
'total' => Db::name('granary')->where(['granary_type' => 2])->count(),
// 今日
'current' => Db::name('granary')
->where("granary_type = 2 AND create_time >= {$startTime} AND create_time <= {$endTime}")
->count(),
// 本月
'month' => Db::name('granary')
->where("granary_type = 2 AND create_time >= {$startMonthTime} AND create_time <= {$endMonthTime}")
->count(),
],
// 检测参数
'params' => [
// 总计
'total' => Db::name('testing')->count(),
// 今日
'current' => Db::name('testing')
->where("create_time >= {$startTime} AND create_time <= {$endTime}")
->count(),
// 本月
'month' => Db::name('testing')
->where("create_time >= {$startMonthTime} AND create_time <= {$endMonthTime}")
->count(),
],
// 质检报告
'content' => [
// 总计
'total' => Db::name('content')->count(),
// 今日
'current' => Db::name('content')
->where("create_time >= {$startTime} AND create_time <= {$endTime}")
->count(),
// 本月
'month' => Db::name('content')
->where("create_time >= {$startMonthTime} AND create_time <= {$endMonthTime}")
->count(),
],
];
$this->assign([
'data' => $data
]);
return $this->view->fetch();
}
/**
* 获取折线图
* @return \think\response\Json
* @throws \think\db\exception\DataNotFoundException
* @throws \think\db\exception\ModelNotFoundException
* @throws \think\exception\DbException
*/
public function getParamsLineCharts()
{
$year = $this->request->get('year', date('Y'));
$gid = $this->request->get('gid');
$type = $this->request->get('type');
$lineCharts = $this->paramsQuarterLineChartsData($gid, $year, $type);
return json([
'code' => 200,
'msg' => '获取成功',
'data' => [
'lineCharts' => $lineCharts
]
]);
}
/**
* 获取初始化数据
* @return \think\response\Json
* @throws \think\db\exception\DataNotFoundException
* @throws \think\db\exception\ModelNotFoundException
* @throws \think\exception\DbException
*/
public function getInitData(TestingModel $testingModel)
{
$granaryList = Db::name('granary')
->field(['id', 'name', 'varieties'])
->select();
$defaultType = 'rapeseed';
if (!empty($granaryList)) {
foreach ($granaryList as &$g) {
$vInfo = getVarietiesInfo($g['varieties'] ?? '');
$g['type'] = $vInfo['type'] ?? 'rapeseed';
}
unset($g);
$defaultType = $granaryList[0]['type'] ?? 'rapeseed';
}
$typeList = $testingModel->getTypeList();
$lineCharts = $this->paramsQuarterLineChartsData(!empty($granaryList) ? $granaryList[0]['id'] : 0, date('Y'), $defaultType);
return json([
'code' => 200,
'msg' => '获取成功',
'data' => [
'granaryList' => $granaryList,
'typeList' => $typeList,
'defaultType' => $defaultType,
'lineCharts' => $lineCharts,
'year' => date('Y')
]
]);
}
/**
* 参数季度折线图
* @param $gid
* @param $year
* @param $type
* @return array|false|\PDOStatement|string|\think\Collection
* @throws \think\db\exception\DataNotFoundException
* @throws \think\db\exception\ModelNotFoundException
* @throws \think\exception\DbException
*/
private function paramsQuarterLineChartsData($gid, $year, $type)
{
// 检测参数
$test = Db::name('testing')
->where(['gid' => $gid, 'type' => $type])
->field(['id', 'type', 'test_date'])
->order('test_date DESC')
->find();
$seriesData = [];
if (empty($test)) {
return $seriesData = [];
}
$seriesData = Db::name('testing_params')
->where(['tid' => $test['id'], 'name' => ['NEQ', '容重'], 'is_number' => 1])
->field(['name', 'unit', 'color'])
->select();
$list = [
$year . '春季',
$year . '秋季'
];
foreach ($list as $k => $v) {
foreach ($seriesData as &$item) {
// 获取参数数值
$val = Db::name('testing_params')->where([
'gid' => $gid,
'name' => $item['name'],
'year' => $year,
'season' => $k + 1
])->value('val');
$item['data'][] = !empty($val) ? $val : 0;
$item['color'] = getColorCode($item['name']);
}
}
return [
'list' => $list,
'seriesData' => $seriesData
];
}
/**
* 参数月份折线图
* @param $gid
* @param $year
* @param $type
* @return array|false|\PDOStatement|string|\think\Collection
* @throws \think\db\exception\DataNotFoundException
* @throws \think\db\exception\ModelNotFoundException
* @throws \think\exception\DbException
*/
private function paramsLineChartsData($gid, $year, $type)
{
// 检测参数
$test = Db::name('testing')
->where(['gid' => $gid, 'type' => $type])
->field(['id', 'type', 'test_date'])
->order('test_date DESC')
->find();
$seriesData = [];
if (empty($test)) {
return $seriesData = [];
}
$seriesData = Db::name('testing_params')
->where(['tid' => $test['id'], 'is_number' => 1])
->field(['name', 'unit', 'color'])
->select();
for ($i = 1; $i <= 12; $i++) {
$month = $i < 10 ? '0' . $i : $i;
$testDate = $year . $month;
foreach ($seriesData as &$item) {
// 获取参数数值
$val = Db::name('testing_params')->where([
'gid' => $gid,
'tid' => $test['id'],
'name' => $item['name'],
'test_date' => $testDate
])->value('val');
$item['data'][] = !empty($val) ? $val : 0;
$item['color'] = getColorCode($item['name']);
}
}
return $seriesData;
}
/**
* 查看
*/
// public function index()
// {
// try {
// \think\Db::execute("SET @@sql_mode='';");
// } catch (\Exception $e) {
//
// }
// $column = [];
// $starttime = Date::unixtime('day', -6);
// $endtime = Date::unixtime('day', 0, 'end');
// $joinlist = Db("user")->where('jointime', 'between time', [$starttime, $endtime])
// ->field('jointime, status, COUNT(*) AS nums, DATE_FORMAT(FROM_UNIXTIME(jointime), "%Y-%m-%d") AS join_date')
// ->group('join_date')
// ->select();
// for ($time = $starttime; $time <= $endtime;) {
// $column[] = date("Y-m-d", $time);
// $time += 86400;
// }
// $userlist = array_fill_keys($column, 0);
// foreach ($joinlist as $k => $v) {
// $userlist[$v['join_date']] = $v['nums'];
// }
//
// $dbTableList = Db::query("SHOW TABLE STATUS");
// $addonList = get_addon_list();
// $totalworkingaddon = 0;
// $totaladdon = count($addonList);
// foreach ($addonList as $index => $item) {
// if ($item['state']) {
// $totalworkingaddon += 1;
// }
// }
// $this->view->assign([
// 'totaluser' => User::count(),
// 'totaladdon' => $totaladdon,
// 'totaladmin' => Admin::count(),
// 'totalcategory' => \app\common\model\Category::count(),
// 'todayusersignup' => User::whereTime('jointime', 'today')->count(),
// 'todayuserlogin' => User::whereTime('logintime', 'today')->count(),
// 'sevendau' => User::whereTime('jointime|logintime|prevtime', '-7 days')->count(),
// 'thirtydau' => User::whereTime('jointime|logintime|prevtime', '-30 days')->count(),
// 'threednu' => User::whereTime('jointime', '-3 days')->count(),
// 'sevendnu' => User::whereTime('jointime', '-7 days')->count(),
// 'dbtablenums' => count($dbTableList),
// 'dbsize' => array_sum(array_map(function ($item) {
// return $item['Data_length'] + $item['Index_length'];
// }, $dbTableList)),
// 'totalworkingaddon' => $totalworkingaddon,
// 'attachmentnums' => Attachment::count(),
// 'attachmentsize' => Attachment::sum('filesize'),
// 'picturenums' => Attachment::where('mimetype', 'like', 'image/%')->count(),
// 'picturesize' => Attachment::where('mimetype', 'like', 'image/%')->sum('filesize'),
// ]);
//
// $this->assignconfig('column', array_keys($userlist));
// $this->assignconfig('userdata', array_values($userlist));
//
// return $this->view->fetch();
// }
}