This commit is contained in:
chenbo 2024-02-19 14:55:08 +08:00
parent 31d7dbe201
commit 3ade7df922
1 changed files with 51 additions and 2 deletions

View File

@ -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'));
}
}