2024-02-03 16:01:29 +08:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace app\api\controller\dataview;
|
|
|
|
|
|
|
|
use app\api\controller\BaseApiController;
|
2024-02-19 14:55:08 +08:00
|
|
|
use app\common\logic\RemoteRequestLogic;
|
2024-02-03 17:30:58 +08:00
|
|
|
use app\common\model\animal_info\AnimalInfo;
|
2024-02-03 16:01:29 +08:00
|
|
|
use app\common\model\device\Device;
|
2024-02-03 17:30:58 +08:00
|
|
|
use app\common\model\dict\DictData;
|
2024-02-03 16:01:29 +08:00
|
|
|
use app\common\model\farm\Farm;
|
|
|
|
use app\common\model\fence_house\FenceHouse;
|
|
|
|
use app\common\model\land\Land;
|
|
|
|
use app\common\model\land\Product;
|
|
|
|
use app\common\model\product\ProductDevice;
|
|
|
|
use think\exception\ValidateException;
|
|
|
|
|
|
|
|
class FarmController extends BaseApiController
|
|
|
|
{
|
|
|
|
public array $notNeedLogin = [
|
2024-02-20 10:02:07 +08:00
|
|
|
'farmCount','breedTypeCount','farmInfo','deviceAlarmCount','deviceCount','alarmRangeList'
|
2024-02-03 16:01:29 +08:00
|
|
|
];
|
|
|
|
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 farmCount()
|
|
|
|
{
|
|
|
|
$query = Farm::alias('f')->where(function ($query) {
|
|
|
|
if ($this->streetCode != '') {
|
|
|
|
$query->where('f.street', $this->streetCode);
|
|
|
|
} else {
|
|
|
|
$query->where('f.area', $this->areaCode);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
// 地块数量
|
|
|
|
$farmCount = $query->count();
|
|
|
|
// 养殖规模
|
|
|
|
$totalScale = $query->sum('form_scale');
|
|
|
|
// 基地列表
|
|
|
|
$farmList = $query->select()->each(function ($farm){
|
2024-02-04 09:58:55 +08:00
|
|
|
// 档案数量
|
|
|
|
$fenceHouseIds = FenceHouse::where('farm_id', $farm['id'])->column('id');
|
|
|
|
$farm['animal_count'] = AnimalInfo::whereIn('fence_house_id', $fenceHouseIds)->count();
|
2024-02-03 16:01:29 +08:00
|
|
|
$fenceHouse = FenceHouse::where('farm_id', $farm['id'])->order('id desc')->findOrEmpty()->toArray();
|
|
|
|
if(empty($fenceHouse)){
|
|
|
|
$farm['video_url'] = '';
|
|
|
|
$farm['device_id'] = 0;
|
|
|
|
return $farm;
|
|
|
|
}
|
|
|
|
$product = Product::where('fence_house_id', $fenceHouse['id'])->findOrEmpty()->toArray();
|
|
|
|
if(empty($product)){
|
|
|
|
$farm['video_url'] = '';
|
|
|
|
$farm['device_id'] = 0;
|
|
|
|
return $farm;
|
|
|
|
}
|
|
|
|
$deviceIds = ProductDevice::where('product_id', $product['id'])->column('device_id');
|
|
|
|
$device = Device::whereIn('id', $deviceIds)->where('type', 2)->findOrEmpty();
|
|
|
|
if(empty($device)){
|
|
|
|
$farm['video_url'] = '';
|
|
|
|
$farm['device_id'] = 0;
|
|
|
|
return $farm;
|
|
|
|
}
|
|
|
|
$farm['video_url'] = $device['video_url'];
|
|
|
|
$farm['device_id'] = $device['id'];
|
|
|
|
return $farm;
|
|
|
|
})->toArray();
|
|
|
|
return $this->success('成功', compact('farmCount', 'totalScale', 'farmList'));
|
|
|
|
}
|
2024-02-03 17:30:58 +08:00
|
|
|
|
|
|
|
public function breedTypeCount()
|
|
|
|
{
|
|
|
|
$breedTypeRows = DictData::where('type_value', 'breed_type')->field('name,value')->select()->each(function($breedType){
|
|
|
|
$breedType['animalCount'] = Farm::alias('f')->join('fence_house fh', 'f.id=fh.farm_id')->where(function ($query) {
|
|
|
|
if ($this->streetCode != '') {
|
|
|
|
$query->where('f.street', $this->streetCode);
|
|
|
|
} else {
|
|
|
|
$query->where('f.area', $this->areaCode);
|
|
|
|
}
|
|
|
|
})->where('fh.animal_type', $breedType['value'])->count();
|
|
|
|
})->toArray();
|
|
|
|
|
|
|
|
$fenceHouseIds = Farm::alias('f')->join('fence_house fh', 'f.id=fh.farm_id')->where(function ($query) {
|
|
|
|
if ($this->streetCode != '') {
|
|
|
|
$query->where('f.street', $this->streetCode);
|
|
|
|
} else {
|
|
|
|
$query->where('f.area', $this->areaCode);
|
|
|
|
}
|
|
|
|
})->column('fh.id');
|
|
|
|
|
|
|
|
$animalList = AnimalInfo::whereIn('fence_house_id', $fenceHouseIds)->select();
|
|
|
|
return $this->success('成功', compact('breedTypeRows', 'animalList'));
|
|
|
|
}
|
|
|
|
|
2024-02-03 16:01:29 +08:00
|
|
|
public function centralCount()
|
|
|
|
{
|
|
|
|
|
|
|
|
$query = Farm::alias('f')->where(function ($query) {
|
|
|
|
if ($this->streetCode != '') {
|
2024-02-03 17:30:58 +08:00
|
|
|
$query->where('f.street', $this->streetCode);
|
2024-02-03 16:01:29 +08:00
|
|
|
} else {
|
2024-02-03 17:30:58 +08:00
|
|
|
$query->where('f.area', $this->areaCode);
|
2024-02-03 16:01:29 +08:00
|
|
|
}
|
|
|
|
});
|
|
|
|
// 地块数量
|
|
|
|
$farmCount = $query->count();
|
|
|
|
// 养殖规模
|
|
|
|
$totalScale = $query->sum('form_scale');
|
|
|
|
// 养殖种类
|
|
|
|
$breedCount = $query->group('breed_type')->count();
|
|
|
|
|
|
|
|
return $this->success('成功', compact('farmCount', 'totalScale', 'breedCount'));
|
|
|
|
}
|
2024-02-19 13:45:28 +08:00
|
|
|
|
|
|
|
public function farmInfo()
|
|
|
|
{
|
|
|
|
// 获取栏舍信息
|
|
|
|
$params = $this->request->param();
|
|
|
|
$farmInfo = Farm::where('id', $params['id'])->findOrEmpty()->toArray();
|
|
|
|
$data = FenceHouse::where('farm_id',$params['id'])->findOrEmpty()->toArray();
|
|
|
|
$product = Product::where('fence_house_id', $data['id'])->findOrEmpty()->toArray();
|
|
|
|
if(empty($product)){
|
|
|
|
return $this->fail('栏舍暂未绑定设备',[]);
|
|
|
|
}
|
|
|
|
$deviceIds = ProductDevice::where('product_id', $product['id'])->column('device_id');
|
|
|
|
$device = Device::whereIn('id', $deviceIds)->where('type', 2)->findOrEmpty();
|
|
|
|
$farmInfo['video_url'] = $device['video_url'];
|
|
|
|
$farmInfo['device_id'] = $device['id'];
|
2024-02-19 14:55:08 +08:00
|
|
|
|
|
|
|
$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);
|
2024-03-06 21:44:02 +08:00
|
|
|
foreach($list as $k=>$v){
|
|
|
|
if(time() - strtotime($v['alarm_time']) > 604800){
|
|
|
|
unset($list[$k]);
|
|
|
|
}
|
|
|
|
}
|
2024-02-19 14:55:08 +08:00
|
|
|
|
|
|
|
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);
|
|
|
|
});
|
2024-02-20 13:54:08 +08:00
|
|
|
$deviceList = $query->select()->toArray();
|
2024-02-19 14:55:08 +08:00
|
|
|
$total = $query->count();
|
|
|
|
|
|
|
|
$online = $query->where('d.is_online', 1)->count();
|
|
|
|
|
|
|
|
$offline = $query->where('d.is_online', 2)->count();
|
|
|
|
|
2024-02-20 13:54:08 +08:00
|
|
|
|
2024-02-19 14:55:08 +08:00
|
|
|
// 请求mqtt
|
|
|
|
[$alarmCount,$todayAlarmCount] = RemoteRequestLogic::getAlarmCount($deviceList);
|
|
|
|
|
|
|
|
return $this->success('成功', compact('total', 'online', 'offline', 'alarmCount', 'todayAlarmCount'));
|
|
|
|
|
2024-02-19 13:45:28 +08:00
|
|
|
}
|
2024-02-20 10:02:07 +08:00
|
|
|
|
|
|
|
public function alarmRangeList()
|
|
|
|
{
|
|
|
|
$params = $this->request->param();
|
|
|
|
$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');
|
|
|
|
$list = RemoteRequestLogic::getRangeMonitorData($deviceIds);
|
|
|
|
return $this->success('成功', $list);
|
|
|
|
}
|
2024-02-03 16:01:29 +08:00
|
|
|
}
|