Area.php备份231205 6.6 KB
<?php

namespace app\admin\controller\inspection;

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

/**
 * 巡检区域管理
 *
 * @icon fa fa-circle-o
 */
class Area extends Backend
{
    
    /**
     * Area模型对象
     * @var \app\admin\model\inspection\Area
     */
    protected $model = null;
    protected $relationSearch = true;
    protected $noNeedRight = ['tree','dynamicselect','cxselect'];
    public function _initialize()
    {
        parent::_initialize();
        $this->model = new \app\admin\model\inspection\Area();

        $tree = Tree::instance();
        $tree->init(collection($this->model->order('weigh asc,id asc')->select())->toArray(), 'pid');
        $this->categorylist = $tree->getTreeList($tree->getTreeArray(0), 'area_name');
        $categorydata = [0 => ['area_name' => __('None')]];
        foreach ($this->categorylist as $k => $v) {
            $categorydata[$v['id']] = $v;
        }

        $this->view->assign("parentList", $categorydata);
        $this->view->assign("reservoirList",\app\admin\model\reservoir\Reservoirlist::field('id,name')->select());
    }

    public function dynamicselect(){

        $list = $this->model->order("id desc")->select();
        foreach($list as $k=>$v){
            $agentJson[$v['id']] = $v['area_name'];
        }

        echo json_encode($agentJson,JSON_FORCE_OBJECT);
    }


    /**
     * 联动搜索
     */
    public function cxselect()
    {
        $list = $this->model->field('id as value, area_name as name')->select();
        $this->success('', null, $list);
    }

    /**
     * 查看
     */
    public function index()
    {
        //设置过滤方法
        $this->request->filter(['strip_tags']);
        if ($this->request->isAjax()) {
            $search = $this->request->request("search");
            $type = $this->request->request("type");

            //构造父类select列表选项数据
            $list = [];

            foreach ($this->categorylist as $k => $v) {
                if ($search) {
                    if ($v['type'] == $type && stripos($v['name'], $search) !== false || stripos($v['nickname'], $search) !== false) {
                        if ($type == "all" || $type == null) {
                            $list = $this->categorylist;
                        } else {
                            $list[] = $v;
                        }
                    }
                } else {
                    if ($type == "all" || $type == null) {
                        $list = $this->categorylist;
                    } elseif ($v['type'] == $type) {
                        $list[] = $v;
                    }
                }
            }

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

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

    /**
     * 编辑
     */
    public function edit($ids = null)
    {
        $row = $this->model->get($ids);
        if (!$row) {
            $this->error(__('No Results were found'));
        }
        $adminIds = $this->getDataLimitAdminIds();
        if (is_array($adminIds)) {
            if (!in_array($row[$this->dataLimitField], $adminIds)) {
                $this->error(__('You have no permission'));
            }
        }
        if ($this->request->isPost()) {
            $this->token();
            $params = $this->request->post("row/a");
            if ($params) {
                $params = $this->preExcludeFields($params);

                if ($params['pid'] != $row['pid']) {
                    $childrenIds = Tree::instance()->init(collection( \app\admin\model\inspection\Area::select())->toArray())->getChildrenIds($row['id'], true);
                    if (in_array($params['pid'], $childrenIds)) {
                        $this->error(__('上级部门不能是它的子部门或它自己'));
                    }
                }

                try {
                    //是否采用模型验证
                    if ($this->modelValidate) {
                        $name = str_replace("\\model\\", "\\validate\\", get_class($this->model));
                        $validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.edit' : $name) : $this->modelValidate;
                        $row->validate($validate);
                    }
                    $result = $row->allowField(true)->save($params);
                    if ($result !== false) {
                        $this->success();
                    } else {
                        $this->error($row->getError());
                    }
                } catch (\think\exception\PDOException $e) {
                    $this->error($e->getMessage());
                } catch (\think\Exception $e) {
                    $this->error($e->getMessage());
                }
            }
            $this->error(__('Parameter %s can not be empty', ''));
        }
        $this->view->assign("row", $row);
        return $this->view->fetch();
    }

    public function tree(){

        $id = $this->request->get('id');


        $areaModel = new \app\admin\model\inspection\Area();
        $departList = $areaModel->where(['pid'=>($id=='#'?0:$id)])->select();


        $chanelList = [];
        if($id=='#'){
            $text = "全部";
            $chanelList[-1] = ['id'=>-1,'type'=>'total','text'=>$text,'parent'=>"#"];
        }

        foreach($departList as $k=>$v){
            $areaModel =  new \app\admin\model\inspection\Area();
            if($areaModel->where(['pid'=>$v->id])->find()){
                $children = true;
            }else{
                $children = false;
            }

            $chanelList[$v->id] = ['id'=>$v->id,'text'=>$v->area_name,'type'=>"folder",'children'=>$children,'parent'=>empty($v->pid)?"#":$v->pid,'state'=>['opened'=>false]];
        }

        $chanelList = array_values($chanelList);

        return json($chanelList);
    }
    
    /**
     * 查看
     */
    public function indexlist()
    {
        //设置过滤方法
        $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
                ->where($where)
                ->order($sort, $order)
                ->paginate($limit);

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

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

}