Raingauge.php 3.5 KB
<?php

namespace app\equipment\controller;

use app\common\controller\Frontend;

class Raingauge extends Frontend
{

    protected $noNeedLogin = [];
    protected $noNeedRight = '*';
    protected $layout = '';

    public function _initialize()
    {
        parent::_initialize();

        $this->model = new \app\admin\model\reservoir\Equipment;
    }

    public function list1()
    {
        $list = $this->model
            ->alias('e')
            ->join('reservoir_list r','r.id=e.reservoir_id','LEFT')
            ->field('e.*,r.name as reservoir,case e.type when 1 then "水位计" when 2 then "渗流计" when 3 then "渗压计" when 4 then "GNSS" else "雨量计" end as typename')
            ->where('e.type','5')
            ->order('id desc')
            ->paginate(10,'',['query'=>$this->request->param()]);
        $this->view->assign(['list'=>$list,'reservoir_list' => \think\Db::name('reservoir_list')->field('id,name')->where('county_id',1)->select()]);
        return $this->view->fetch();
    }
    public function add()
    {
        if($this->request->isPost()){
            $params = $this->request->post('row/a');
            if($params){
                if (!$params['deviceId']) {
                    $this->error('设备全局id为空');
                }
                if($this->model->get(['deviceId'=>$params['deviceId']])){
                    $this->error('设备全局id已存在');
                }
                if (!$params['deviceId']) {
                    $this->error('设备全局id为空');
                }
                if (!$params['apiKey']) {
                     $this->error('设备apiKey为空');
                }
                if (!$params['name']) {
                    $this->error('设备名称为空');
                }
                $params['createtime']  =  time();
                $result = $this->model->allowField(true)->save($params);
                if(!$result){
                     $this->error('网络繁忙');
                }
                $this->success('添加成功');
            }
            
        }    
    }
    public function edit($ids=null)
    {
        $row = $this->model->get($ids);
        if (!$row) {
            $this->error('数据不存在');
        }
        if ($this->request->isPost()) {
            $params = $this->request->post('row/a');
            if (!$params['deviceId']) {
                $this->error('设备全局id为空');
            }
            if($this->model->where('id','<>',$ids)->where('deviceId',$params['deviceId'])->value('id') ){
                $this->error('设备全局id已存在');
            }
            if (!$params['deviceId']) {
                $this->error('设备全局id为空');
            }
            if (!$params['apiKey']) {
                $this->error('设备apiKey为空');
            }
            if($this->model->where('id','<>',$ids)->where('apiKey',$params['apiKey'])->value('id') ){
                $this->error('设备apiKey已存在');
            }
            if (!$params['name']) {
                $this->error('设备名称为空');
            }    
            $result = $row->allowField(true)->save($params);
            if (!$result) {
                $this->error('网络繁忙');
            }
            $this->success('更新成功');
            
        }
        return json(['code'=>1,'data'=>$row]);
    }
    public function del($ids=null)
    {
        $m = $this->model->get($ids);
        $res = $m->delete(true);
        if(!$res){
            $this->error('网络繁忙');
        }
        $this->success('删除成功');
    }
}