2023-12-14 15:00:45 +08:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace app\api\controller\dataview;
|
|
|
|
|
|
|
|
use app\api\controller\BaseApiController;
|
|
|
|
use app\common\model\device\Device;
|
|
|
|
use app\common\model\device\MonitorAlarm;
|
2023-12-14 16:33:06 +08:00
|
|
|
use app\common\model\land\LandPlant;
|
2023-12-14 15:00:45 +08:00
|
|
|
use think\App;
|
|
|
|
use think\Exception;
|
|
|
|
use think\exception\ErrorException;
|
|
|
|
use think\exception\HttpResponseException;
|
|
|
|
use think\exception\ValidateException;
|
|
|
|
|
|
|
|
class DeviceController extends BaseApiController
|
|
|
|
{
|
2023-12-14 16:33:06 +08:00
|
|
|
public array $notNeedLogin = ['deviceAlarmCount', 'deviceCount'];
|
2023-12-14 15:00:45 +08:00
|
|
|
|
|
|
|
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('未获取到位置信息');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-12-14 16:33:06 +08:00
|
|
|
// 设备告警统计
|
2023-12-14 15:00:45 +08:00
|
|
|
public function deviceAlarmCount()
|
|
|
|
{
|
2023-12-15 18:24:24 +08:00
|
|
|
$landId = $this->request->get('land_id', 0);
|
2023-12-19 18:03:06 +08:00
|
|
|
$list = MonitorAlarm::alias('ma')->field('ma.*,d.code AS device_code,d.name AS device_name,d.image')
|
2023-12-14 15:00:45 +08:00
|
|
|
->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')
|
2023-12-15 18:24:24 +08:00
|
|
|
->where(function ($query) use ($landId) {
|
|
|
|
if ($landId != 0) {
|
|
|
|
$query->where('ma.land_id', $landId);
|
2023-12-14 15:00:45 +08:00
|
|
|
} else {
|
2023-12-15 18:24:24 +08:00
|
|
|
if ($this->streetCode != '') {
|
|
|
|
$query->where('l.town_code', $this->streetCode);
|
|
|
|
} else {
|
|
|
|
$query->where('l.county_code', $this->areaCode);
|
|
|
|
}
|
2023-12-14 15:00:45 +08:00
|
|
|
}
|
|
|
|
})
|
|
|
|
->whereWeek('ma.create_time')
|
2024-01-05 14:33:31 +08:00
|
|
|
->order('ma.id', 'desc')
|
|
|
|
->limit(100)
|
2023-12-14 15:00:45 +08:00
|
|
|
->select();
|
|
|
|
return $this->success('成功', compact('list'));
|
|
|
|
}
|
|
|
|
|
2023-12-16 09:35:47 +08:00
|
|
|
// 监测设备数量统计
|
2023-12-14 16:33:06 +08:00
|
|
|
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);
|
|
|
|
}
|
2023-12-15 11:36:49 +08:00
|
|
|
})->where('d.is_online', 2)->count();
|
2023-12-14 16:33:06 +08:00
|
|
|
|
|
|
|
return $this->success('成功', compact('total', 'online', 'offline'));
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2023-12-14 15:00:45 +08:00
|
|
|
}
|