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){ // 档案数量 $fenceHouseIds = FenceHouse::where('farm_id', $farm['id'])->column('id'); $farm['animal_count'] = AnimalInfo::whereIn('fence_house_id', $fenceHouseIds)->count(); $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')); } 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')); } public function centralCount() { $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'); // 养殖种类 $breedCount = $query->group('breed_type')->count(); return $this->success('成功', compact('farmCount', 'totalScale', 'breedCount')); } 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']; return $this->success('成功', compact('farmInfo')); } }