From 3ade7df9220868416837fea85ba9c49193d9566b Mon Sep 17 00:00:00 2001 From: chenbo <709206448@qq.com> Date: Mon, 19 Feb 2024 14:55:08 +0800 Subject: [PATCH] update --- .../controller/dataview/FarmController.php | 53 ++++++++++++++++++- 1 file changed, 51 insertions(+), 2 deletions(-) diff --git a/app/api/controller/dataview/FarmController.php b/app/api/controller/dataview/FarmController.php index 5f4e2b4..524b603 100644 --- a/app/api/controller/dataview/FarmController.php +++ b/app/api/controller/dataview/FarmController.php @@ -3,6 +3,7 @@ namespace app\api\controller\dataview; use app\api\controller\BaseApiController; +use app\common\logic\RemoteRequestLogic; use app\common\model\animal_info\AnimalInfo; use app\common\model\device\Device; use app\common\model\dict\DictData; @@ -16,7 +17,7 @@ use think\exception\ValidateException; class FarmController extends BaseApiController { public array $notNeedLogin = [ - 'farmCount','breedTypeCount','farmInfo' + 'farmCount','breedTypeCount','farmInfo','deviceAlarmCount','deviceCount' ]; public function initialize() { @@ -130,6 +131,54 @@ class FarmController extends BaseApiController $device = Device::whereIn('id', $deviceIds)->where('type', 2)->findOrEmpty(); $farmInfo['video_url'] = $device['video_url']; $farmInfo['device_id'] = $device['id']; - return $this->success('成功', compact('farmInfo')); + + $breedTypeRows = DictData::where('type_value', 'breed_type')->field('name,value')->select()->each(function($breedType) use($params){ + $breedType['animalCount'] = Farm::alias('f')->join('fence_house fh', 'f.id=fh.farm_id')->where('f.id', $params['id'])->where('fh.animal_type', $breedType['value'])->count(); + })->toArray(); + return $this->success('成功', compact('farmInfo', 'breedTypeRows')); + } + + // 设备告警统计 + public function deviceAlarmCount() + { + $params = $this->request->param(); + $deviceList = Device::alias('d')->where(function ($query) use($params) { + $farmIds = [$params['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); + })->select()->toArray(); + + // 请求mqtt接口 + $list = RemoteRequestLogic::getAlarmData($deviceList); + + return $this->success('成功', compact('list')); + } + + // 监测设备数量统计 + public function deviceCount() + { + $params = $this->request->param(); + $query = Device::alias('d')->where(function ($query) use ($params){ + $farmIds = [$params['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); + }); + + $total = $query->count(); + + $online = $query->where('d.is_online', 1)->count(); + + $offline = $query->where('d.is_online', 2)->count(); + + $deviceList = $query->select()->toArray(); + // 请求mqtt + [$alarmCount,$todayAlarmCount] = RemoteRequestLogic::getAlarmCount($deviceList); + + return $this->success('成功', compact('total', 'online', 'offline', 'alarmCount', 'todayAlarmCount')); + } } \ No newline at end of file