<?php

namespace app\api\controller\dataview;

use app\api\controller\BaseApiController;
use app\common\logic\RemoteRequestLogic;
use app\common\model\device\Device;
use app\common\model\device\MonitorAlarm;
use app\common\model\farm\Farm;
use app\common\model\fence_house\FenceHouse;
use app\common\model\land\LandPlant;
use app\common\model\land\Product;
use app\common\model\product\ProductDevice;
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', 'deviceDesc'];

    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()
    {
        $deviceList = 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);
            }
        })->select()->toArray();

        // 请求mqtt接口
        $list = RemoteRequestLogic::getAlarmData($deviceList);

        return $this->success('成功', compact('list'));
    }

    // 监测设备数量统计
    public function  deviceCount()
    {
        $query = 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);
                }
            });
        $deviceList = $query->select()->toArray();
        $total = $query->count();

        $online = $query->where('d.is_online', 1)->count();

        $offline = $query->where('d.is_online', 2)->count();


        // 请求mqtt
        [$alarmCount,$todayAlarmCount] = RemoteRequestLogic::getAlarmCount($deviceList);

        return $this->success('成功', compact('total', 'online', 'offline', 'alarmCount', 'todayAlarmCount'));

    }

    public function deviceDesc()
    {
        $data = [
            [
                'name'=>'项圈中转站',
                'desc'=>'4G中转站设备',
                'icon'=>'https://lihai001.oss-cn-chengdu.aliyuncs.com/def/15fc3202402211610238016.png'
            ],
            [
                'name'=>'噪音监测',
                'desc'=>'噪音监测设备',
                'icon'=>'https://lihai001.oss-cn-chengdu.aliyuncs.com/def/15fc3202402211610238016.png'
            ],
            [
                'name'=>'温湿度监测',
                'desc'=>'温湿度监测设备',
                'icon'=>'https://lihai001.oss-cn-chengdu.aliyuncs.com/def/15fc3202402211610238016.png'
            ],
            [
                'name'=>'烟雾监测',
                'desc'=>'烟雾监测设备',
                'icon'=>'https://lihai001.oss-cn-chengdu.aliyuncs.com/def/15fc3202402211610238016.png'
            ],
            [
                'name'=>'甲烷监测',
                'desc'=>'甲烷监测设备',
                'icon'=>'https://lihai001.oss-cn-chengdu.aliyuncs.com/def/15fc3202402211610238016.png'
            ],
            [
                'name'=>'氮气监测',
                'desc'=>'氮气监测设备',
                'icon'=>'https://lihai001.oss-cn-chengdu.aliyuncs.com/def/15fc3202402211610238016.png'
            ]
        ];
        return $this->success('成功', $data);
    }
}