suyuan-breed/app/api/controller/dataview/DeviceController.php

102 lines
3.6 KiB
PHP

<?php
namespace app\api\controller\dataview;
use app\api\controller\BaseApiController;
use app\common\model\device\Device;
use app\common\model\device\MonitorAlarm;
use app\common\model\land\LandPlant;
use think\App;
use think\Exception;
use think\exception\ErrorException;
use think\exception\HttpResponseException;
use think\exception\ValidateException;
class DeviceController extends BaseApiController
{
public array $notNeedLogin = ['deviceAlarmCount', 'deviceCount'];
public $areaCode;
public $streetCode;
public function initialize()
{
parent::initialize();
$this->areaCode = $this->request->param('areaCode', '');
$this->streetCode = $this->request->param('streetCode', '');
if ($this->areaCode == '') {
throw new ValidateException('未获取到位置信息');
}
}
// 设备告警统计
public function deviceAlarmCount()
{
$landId = $this->request->get('land_id', 0);
$list = MonitorAlarm::alias('ma')->field('ma.*,d.code AS device_code,d.name AS device_name,d.image')
->join('device d', 'ma.device_id=d.id')
->join('product_device pd', 'd.id=pd.device_id')
->join('land_product lp', 'pd.product_id=lp.product_id')
->join('land l', 'l.id=lp.land_id')
->where(function ($query) use ($landId) {
if ($landId != 0) {
$query->where('ma.land_id', $landId);
} else {
if ($this->streetCode != '') {
$query->where('l.town_code', $this->streetCode);
} else {
$query->where('l.county_code', $this->areaCode);
}
}
})
->whereWeek('ma.create_time')
->order('ma.id', 'desc')
->limit(100)
->select();
return $this->success('成功', compact('list'));
}
// 监测设备数量统计
public function deviceCount()
{
$total = Device::alias('d')
->join('product_device pd', 'd.id=pd.device_id')
->join('land_product lp', 'pd.product_id=lp.product_id')
->join('land l', 'l.id=lp.land_id')
->where(function ($query) {
if ($this->streetCode != '') {
$query->where('l.town_code', $this->streetCode);
} else {
$query->where('l.county_code', $this->areaCode);
}
})
->count();
$online = Device::alias('d')
->join('product_device pd', 'd.id=pd.device_id')
->join('land_product lp', 'pd.product_id=lp.product_id')
->join('land l', 'l.id=lp.land_id')
->where(function ($query) {
if ($this->streetCode != '') {
$query->where('l.town_code', $this->streetCode);
} else {
$query->where('l.county_code', $this->areaCode);
}
})->where('d.is_online', 1)->count();
$offline = Device::alias('d')
->join('product_device pd', 'd.id=pd.device_id')
->join('land_product lp', 'pd.product_id=lp.product_id')
->join('land l', 'l.id=lp.land_id')
->where(function ($query) {
if ($this->streetCode != '') {
$query->where('l.town_code', $this->streetCode);
} else {
$query->where('l.county_code', $this->areaCode);
}
})->where('d.is_online', 2)->count();
return $this->success('成功', compact('total', 'online', 'offline'));
}
}