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

110 行
4.1 KiB
PHP

<?php
namespace app\admin\controller;
use app\common\controller\GranaryBackend;
use app\common\library\GranarySite;
use app\common\library\GrainForecast;
use think\Db;
class Forecast extends GranaryBackend
{
public function index()
{
if (!$this->request->isAjax()) return $this->fetch();
$gid = $this->request->get('gid/d', 0);
$page = max(1, $this->request->get('page/d', 1));
$query = $this->scopeRecords(Db::name('grain_forecast'));
if ($gid) $query->where('gid', $gid);
$total = $query->count();
$query = $this->scopeRecords(Db::name('grain_forecast'));
if ($gid) $query->where('gid', $gid);
$list = $query->field('request_key', true)->order('id DESC')->page($page, 15)->select();
$points = $list ? Db::name('grain_forecast_point')->where('forecast_id', 'in', array_column($list, 'id'))->select() : [];
foreach ($list as &$row) {
$row['measured'] = 0;
$row['point_count'] = 0;
foreach ($points as $point) {
if ($point['forecast_id'] != $row['id']) continue;
$row['point_count']++;
$actual = json_decode($point['actual'], true) ?: [];
$filled = count(array_filter($actual, function ($v) { return $v !== null && $v !== ''; }));
if ($filled === count(GrainForecast::metrics($row['grain_type']))) $row['measured']++;
}
}
unset($row);
$granaries = array_values(array_filter(GranarySite::granaries(->filterSites), function () { return in_array(['varieties'], ['小麦', '稻谷'], true); }));
if (!empty()) {
= array_column(, 'id');
= Db::name('grain_forecast')->where('gid', 'in', )
->field('gid,tm_i,hm_i,tm_o,hm_o,observed_date')->order('id DESC')->select();
= [];
foreach ( as ) {
if (!isset([['gid']])) {
[['gid']] = [
'tm_i' => (float)['tm_i'],
'hm_i' => (float)['hm_i'],
'tm_o' => (float)['tm_o'],
'hm_o' => (float)['hm_o'],
'observed_date' => ['observed_date']
];
}
}
foreach ( as &) {
['last_env'] = isset([['id']]) ? [['id']] : null;
}
unset();
}
return json(['code' => 200, 'list' => , 'total' => , 'granaries' => ]);
}
public function detail()
{
try {
return json(['code' => 200, 'data' => (new GrainForecast())->detail($this->request->get('id/d', 0))]);
} catch (\InvalidArgumentException $e) {
return json(['code' => 400, 'msg' => $e->getMessage()]);
}
}
public function add()
{
return $this->write(function ($service) {
$id = $service->create($this->request->post(), $this->auth->id);
$service->generate($id);
return $id;
});
}
public function retry()
{
return $this->write(function ($service) {
$id = $this->request->post('id/d', 0);
$service->generate($id);
return $id;
});
}
public function actual()
{
return $this->write(function ($service) {
return $service->saveActual($this->request->post(), $this->auth->id);
});
}
protected function write($action)
{
if (!$this->request->isPost()) return json(['code' => 405, 'msg' => '请使用POST提交']);
try {
$service = new GrainForecast();
$id = $action($service);
return json(['code' => 200, 'msg' => '保存成功', 'data' => $service->detail($id)]);
} catch (\InvalidArgumentException $e) {
return json(['code' => 400, 'msg' => $e->getMessage()]);
} catch (\Exception $e) {
\think\Log::record('Grain forecast: ' . $e->getMessage(), 'error');
return json(['code' => 500, 'msg' => '保存失败,请刷新查看记录后重试']);
}
}
}