Riverlist.php 5.7 KB
<?php

namespace app\admin\controller\river;

use app\common\controller\Backend;
use fast\Tree;
use think\Db;

/**
 * 河湖管理
 *
 * @icon fa fa-circle-o
 */
class Riverlist extends Backend
{

    /**
     * Riverlist模型对象
     * @var \app\admin\model\river\Riverlist
     */
    protected $model = null;

    public function _initialize()
    {
        parent::_initialize();
        $this->model = new \app\admin\model\river\Riverlist;
        //分类
        $catmodel = new \app\admin\model\river\Rivercat;

        $tree = Tree::instance();
        $tree->init(collection($catmodel->order('weigh asc,id asc')->select())->toArray(), 'pid');
        $this->categorylist = $tree->getTreeList($tree->getTreeArray(0), 'name');
        foreach ($this->categorylist as $k => $v) {
            $categorydata[$v['id']] = $v;
        }
        //上级河段
        $tree2 = Tree::instance();
        $tree2->init(collection($this->model->order('id asc')->select())->toArray(), 'river_pid');
        $plist = $tree2->getTreeList($tree2->getTreeArray(0), 'name');
        foreach ($plist as $kk => $vv) {
            $plistdata[$vv['id']] = $vv;
        }
        //部门选择
        $tree3 = Tree::instance();
        $departModel = new \app\admin\model\inspection\Depart();
        $tree3->init(collection($departModel->order('weigh asc,id asc')->select())->toArray(), 'pid');
        $departdata = ['' => ['depart_name' => '请选择部门']];
        foreach ($tree3->getTreeList($tree3->getTreeArray(0), 'depart_name') as $k => $v) {
            $departdata[$v['id']] = $v;
        }
        //var_dump($plistdata);
        $this->view->assign("catList", $categorydata);
        $this->view->assign("pList", $plistdata);
        $this->view->assign("departList", $departdata);
    }

    public function import()
    {
        parent::import();
    }


    /**
     * 查看
     */
    public function index()
    {
        //当前是否为关联查询
        $this->relationSearch = true;
        //设置过滤方法
        $this->request->filter(['strip_tags', 'trim']);
        if ($this->request->isAjax()) {
            //如果发送的来源是Selectpage,则转发到Selectpage
            if ($this->request->request('keyField')) {
                return $this->selectpage();
            }
            list($where, $sort, $order, $offset, $limit) = $this->buildparams();

            $list = $this->model
                ->with(['rivercat', 'reservoirprovince', 'reservoirstate', 'reservoircounty', 'reservoirstreet', 'reservoirvillage', 'inspectiondepart', 'inspectionstaff'])
                ->where($where)
                ->order($sort, $order)
                ->paginate($limit);

            foreach ($list as $row) {
                $row->visible(['id', 'river_code', 'name', 'cat_id', 'river_pid', 'thumbnail_images', 'start_address', 'end_address', 'banks_type', 'river_long', 'user_name', 'user_mobile', 'province_id', 'state_id', 'county_id', 'street_id', 'village_id', 'status', 'start_lat', 'start_lng', 'end_lat', 'createtime', 'end_lng', 'user_id', 'depart_id', 'staff_name']);
                $row->visible(['rivercat', 'reservoirprovince', 'reservoirstate', 'reservoircounty', 'reservoirstreet', 'reservoirvillage', 'inspectiondepart', 'inspectionstaff']);
                $row->getRelation('rivercat');
                $row->getRelation('reservoirprovince');
                $row->getRelation('reservoirstate');
                $row->getRelation('reservoircounty');
                $row->getRelation('reservoirstreet');
                $row->getRelation('reservoirvillage');
                $row->getRelation('inspectiondepart');
                $row->getRelation('inspectionstaff');
            }
            $rows = $list->items();
            $snararr = [];
            foreach ($rows as $kk => $vv) {
                $staffarr = explode(',', $vv['user_id']);

                if (!empty($staffarr)) {
                    foreach ($staffarr as $n => $j) {
                        $sinfo = Db::name('inspection_staff')
                            ->where(['id' => $j])
                            ->field('id,staff_name,mobile')
                            ->find();
                        $snararr[$n] = $sinfo['staff_name'];
                    }
                    $rows[$kk]['staff_name'] = implode(',', $snararr);
                } else {
                    $rows[$kk]['staff_name'] = '';
                }
            }

            $result = array("total" => $list->total(), "rows" => $rows);

            return json($result);
        }
        return $this->view->fetch();
    }

    /**
     * 指定河长
     *
     */
    public function setadmin()
    {
        if ($this->request->isAjax()) {
            //保存数据处理
            $river_id = $this->request->param('river_pid', 0);
            $user_id = $this->request->param('user_id', 0);
            //更新河长表的指定

            $uprid['user_id'] = $user_id;
            $uprid['updatetime'] = time();

            $res = Db::name('river_list')->where(['id' => $river_id])->update($uprid);
            //return Db::name('inspection_staff')->getLastSql();
            if ($res != false) {
                $rd['stu'] = 1;
                $rd['msg'] = '操作成功!';
                return $rd;
            } else {
                $rd['stu'] = 2;
                $rd['msg'] = '操作失败,请重试!';
                return $rd;
            }

        } else {
            //加载页面
            //获得河长列表
            $ids = $this->request->param('rid', 0);
            $row = Db::name('river_list')->where(['id' => $ids])->find();

            $this->view->assign("row", $row);

            return $this->view->fetch();
        }
    }

}