Hardware.php 2.5 KB
<?php

namespace app\api\controller\reservoir\hkws;

use app\common\controller\Api;
use think\Request;
use think\Db;

/**
 * Class Displacement
 * @package app\api\controller
 */
class Hardware extends Api
{
    protected $noNeedLogin = '*';
    protected $noNeedRight = '*';

    public function _initialize()
    {
        parent::_initialize();
        $this->model = new \app\admin\model\reservoir\hkws\Hardware;
    }

    public function getByType()
    {
        $type = input('get.type');
        $id = input('get.id');
        $list = Db::name('reservoir_hkws_hardware')->alias('h')->join('reservoir_list l', 'h.reservoir_id = l.id')->field('l.id,h.indexCode,l.name')->where('h.' . $type . '_id', $id)->select();
        foreach ($list as $k => &$v) {
            $v['rsvr'] = $this->getRsvrById($v['id']);
        }
        $this->success('', $list);
    }

    //根据水库id获取水位信息
    public function getRsvrById($id)
    {
        $info = Db::name('st_rsvr_r')->where('RESERVOIR_ID', $id)->order('TM desc')->limit(1)->find();
        return $info;
    }

    //根据县市id获取cameraindexcode
    public function getListByCounty($id)
    {
        $reservoirids = Db::name('reservoir_list')->where('county_id', $id)->field('id')->select();
        $ids = [];
        foreach ($reservoirids as $k => $v) {
            $ids[] = $v['id'];
        }
        $list = Db::name('reservoir_hkws_hardware')->field('id,name,indexCode')->where('reservoir_id', 'in', $ids)->select();
        $this->success('', $list);
    }

    //根据水库id获取cameraindexcode
    public function getListByReservoir()
    {
        $id=40;
        $config = get_addon_config('qiniu');
        $list = Db::name('reservoir_hkws_hardware')->field('id,play_type,haikang_code,type_child_name,name,indexCode,scode,channel_no,image')->where('reservoir_id', $id)->select();
        foreach ($list as &$v) {
            $v['image'] = $config['cdnurl'] . $v['image'];
            $v['reservoir_name'] = Db::name("reservoir_list")->where("id", $id)->value("name");
            //计算当前设备未读入侵条数
            $monitoringCount = Db::name('reservoir_hkws_alarm_monitoring')
                ->where(['cameraIndexCode' => $v['indexCode'], 'status' => '0'])
                ->count();
            $v['monitor_num'] = $monitoringCount ? $monitoringCount : 0;
        }
        usort($list, function ($a, $b) {
            return $b['monitor_num'] - $a['monitor_num'];
        });

        $this->success('获取成功', $list);
    }


}