2024-01-09 15:59:23 +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;
|
2024-02-04 15:25:26 +08:00
|
|
|
use app\common\model\farm\Farm;
|
|
|
|
use app\common\model\fence_house\FenceHouse;
|
2024-01-09 15:59:23 +08:00
|
|
|
use app\common\model\land\LandPlant;
|
2024-02-04 15:25:26 +08:00
|
|
|
use app\common\model\land\Product;
|
|
|
|
use app\common\model\product\ProductDevice;
|
2024-01-09 15:59:23 +08:00
|
|
|
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()
|
|
|
|
{
|
2024-02-04 15:25:26 +08:00
|
|
|
$total = Device::alias('d')->where(function ($query) {
|
2024-01-09 15:59:23 +08:00
|
|
|
if ($this->streetCode != '') {
|
2024-02-04 15:25:26 +08:00
|
|
|
$farmIds = Farm::where('street', $this->streetCode)->column('id');
|
|
|
|
$fenceHouseIds = FenceHouse::whereIn('farm_id', $farmIds)->column('id');
|
|
|
|
$productIds = Product::whereIn('fence_house_id', $fenceHouseIds)->column('id');
|
|
|
|
$deviceIds = ProductDevice::whereIn('product_id', $productIds)->column('device_id');
|
|
|
|
$query->whereIn('d.id', $deviceIds);
|
2024-01-09 15:59:23 +08:00
|
|
|
} else {
|
2024-02-04 15:25:26 +08:00
|
|
|
$farmIds = Farm::where('area', $this->areaCode)->column('id');
|
|
|
|
$fenceHouseIds = FenceHouse::whereIn('farm_id', $farmIds)->column('id');
|
|
|
|
$productIds = Product::whereIn('fence_house_id', $fenceHouseIds)->column('id');
|
|
|
|
$deviceIds = ProductDevice::whereIn('product_id', $productIds)->column('device_id');
|
|
|
|
$query->whereIn('d.id', $deviceIds);
|
2024-01-09 15:59:23 +08:00
|
|
|
}
|
|
|
|
})
|
|
|
|
->count();
|
|
|
|
|
2024-02-04 15:25:26 +08:00
|
|
|
$online = Device::alias('d')->where(function ($query) {
|
|
|
|
if ($this->streetCode != '') {
|
|
|
|
$farmIds = Farm::where('street', $this->streetCode)->column('id');
|
|
|
|
$fenceHouseIds = FenceHouse::whereIn('farm_id', $farmIds)->column('id');
|
|
|
|
$productIds = Product::whereIn('fence_house_id', $fenceHouseIds)->column('id');
|
|
|
|
$deviceIds = ProductDevice::whereIn('product_id', $productIds)->column('device_id');
|
|
|
|
$query->whereIn('d.id', $deviceIds);
|
|
|
|
} else {
|
|
|
|
$farmIds = Farm::where('area', $this->areaCode)->column('id');
|
|
|
|
$fenceHouseIds = FenceHouse::whereIn('farm_id', $farmIds)->column('id');
|
|
|
|
$productIds = Product::whereIn('fence_house_id', $fenceHouseIds)->column('id');
|
|
|
|
$deviceIds = ProductDevice::whereIn('product_id', $productIds)->column('device_id');
|
|
|
|
$query->whereIn('d.id', $deviceIds);
|
|
|
|
}
|
|
|
|
})->where('d.is_online', 1)->count();
|
|
|
|
|
|
|
|
$offline = Device::alias('d')->where(function ($query) {
|
|
|
|
if ($this->streetCode != '') {
|
|
|
|
$farmIds = Farm::where('street', $this->streetCode)->column('id');
|
|
|
|
$fenceHouseIds = FenceHouse::whereIn('farm_id', $farmIds)->column('id');
|
|
|
|
$productIds = Product::whereIn('fence_house_id', $fenceHouseIds)->column('id');
|
|
|
|
$deviceIds = ProductDevice::whereIn('product_id', $productIds)->column('device_id');
|
|
|
|
$query->whereIn('d.id', $deviceIds);
|
|
|
|
} else {
|
|
|
|
$farmIds = Farm::where('area', $this->areaCode)->column('id');
|
|
|
|
$fenceHouseIds = FenceHouse::whereIn('farm_id', $farmIds)->column('id');
|
|
|
|
$productIds = Product::whereIn('fence_house_id', $fenceHouseIds)->column('id');
|
|
|
|
$deviceIds = ProductDevice::whereIn('product_id', $productIds)->column('device_id');
|
|
|
|
$query->whereIn('d.id', $deviceIds);
|
|
|
|
}
|
|
|
|
})->where('d.is_online', 2)->count();
|
|
|
|
|
|
|
|
$alarmCount = 0;
|
|
|
|
$todayAlarmCount = 0;
|
2024-01-09 15:59:23 +08:00
|
|
|
|
2024-02-04 15:25:26 +08:00
|
|
|
return $this->success('成功', compact('total', 'online', 'offline', 'alarmCount', 'todayAlarmCount'));
|
2024-01-09 15:59:23 +08:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|