Common.php 7.6 KB
<?php

namespace app\admin\controller\cloudapishow;

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

/**
 * 展示省、州、市县、水库树公共类
 * Class Common
 * @package app\admin\controller\cloudapishow
 */
class Common extends Backend
{
    //当前用户信息
    protected $apiUserInfo = [];
    //当前用户拥有的水库ID
    protected $reservoirIds = [];
    //当前查询权限条件
    protected $authWhere = [];
    public function _initialize()
    {
        parent::_initialize();

        //获取当前用户拥有的水库权限
        $apiUserInfo = Db::name('cloudapi_user')->where(['admin_id' => $this->auth->id])->find();
        if (!empty($apiUserInfo)){
            $this->apiUserInfo = $apiUserInfo;
            if (!empty($apiUserInfo['reservoir_ids'])){
                $this->reservoirIds = explode(',', $apiUserInfo['reservoir_ids']);
            }
        }
        $this->authWhere['reservoir_id'] = ['in', $this->reservoirIds];
        $reservoirList = Db::name('reservoir_list')
            ->alias('rl')
            ->field('rl.id,rl.name,rl.province_id,rl.state_id,rl.county_id,p.name as province_name,s.name as state_name,c.name as county_name')
            ->join('reservoir_province p','p.id=rl.province_id', 'LEFT')
            ->join('reservoir_state s','s.id=rl.state_id', 'LEFT')
            ->join('reservoir_county c','c.id=rl.county_id', 'LEFT')
            ->where([
                'rl.id' => ['in',$this->reservoirIds]
            ])
            ->select();

        //定义水库列表
        $reservoirLists = [];
        //定义树
        $treeData = [];
        //因为省州市县水库分别处理四张表中防止树中的id重复,所以全部加上前缀
        $provincePrefix = 'p_';
        $statePrefix = 's_';
        $countyPrefix = 'c_';
        $reservoirPrefix = 'r_';
        foreach ($reservoirList as $item){
            //整理水库列表
            $reservoirLists[$item['id']] = $item['name'];
            //省份
            $hasProvince = false;
            $hasState = false;
            $hasCounty = false;
            $hasReservoir = false;
            foreach ($treeData as $province){
                if (trim($province['id'], $provincePrefix) == $item['province_id']){
                    $hasProvince = true;
                }
                foreach ($province['children'] as $state){
                    if (trim($state['id'], $statePrefix) == $item['state_id']){
                        $hasState = true;
                    }
                    foreach ($state['children'] as $county){
                        if (trim($county['id'], $countyPrefix) == $item['county_id']){
                            $hasCounty = true;
                        }
                        foreach ($county['children'] as $reservoir){
                            if (trim($reservoir['id'], $reservoirPrefix) == $item['id']){
                                $hasReservoir = true;
                            }
                        }
                    }
                }
            }
            if ($hasProvince === false){
                $provinceData = [
                    'id' => $provincePrefix . $item['province_id'],
                    'level' => '1',
                    'real_id' => $item['province_id'],
                    'text' => $item['province_name'],
                    'type' => '1',
//                    'parent' => '#',
//                    'state' => [
//                        'opened' => false,  //是否展开
//                        'disabled' => false,//是否禁用
//                        'selected' => false,//是否被选中
//                    ]
                ];
                $treeData[] = $provinceData;
            }

            foreach ($treeData as &$province){
                if (trim($province['id'], $provincePrefix) == $item['province_id']){
                    if ($hasState === false){
                        $stateData = [
                            'id' => $statePrefix . $item['state_id'],
                            'level' => '2',
                            'real_id' => $item['state_id'],
                            'text' => $item['state_name'],
                            'type' => '1',
//                            'parent' => $provincePrefix . $item['province_id'],
//                            'state' => [
//                                'opened' => false,  //是否展开
//                                'disabled' => false,//是否禁用
//                                'selected' => false,//是否被选中
//                            ]
                        ];
                        $province['children'][] = $stateData;
                    }
                    foreach ($province['children'] as &$state){
                        if (trim($state['id'], $statePrefix) == $item['state_id']){
                            if ($hasCounty === false){
                                $countyData = [
                                    'id' => $countyPrefix . $item['county_id'],
                                    'level' => '3',
                                    'real_id' => $item['county_id'],
                                    'text' => $item['county_name'],
                                    'type' => '1',
//                                    'parent' => $statePrefix . $item['state_id'],
//                                    'state' => [
//                                        'opened' => false,  //是否展开
//                                        'disabled' => false,//是否禁用
//                                        'selected' => false,//是否被选中
//                                    ]
                                ];
                                $state['children'][] = $countyData;
                            }
                            foreach ($state['children'] as &$county){
                                if (trim($county['id'], $countyPrefix) == $item['county_id']){
                                    if ($hasReservoir === false){
                                        $reservoirData = [
                                            'id' => $reservoirPrefix . $item['id'],
                                            'level' => '4',
                                            'real_id' => $item['id'],
                                            'text' => $item['name'],
                                            'type' => '2',
//                                            'parent' => $countyPrefix . $item['county_id'],
//                                            'state' => [
//                                                'opened' => false,  //是否展开
//                                                'disabled' => false,//是否禁用
//                                                'selected' => false,//是否被选中
//                                            ]
                                        ];
                                        $county['children'][] = $reservoirData;
                                    }
                                }
                            }
                            unset($county);
                        }
                    }
                    unset($state);
                }
            }
            unset($province);
        }

        $this->assignconfig('treeList', $treeData);

        //查询设备列表
        $deviceList = Db::name('reservoir_equipment')->where(['reservoir_id' => ['in', $this->reservoirIds]])->column('deviceId');
        $deviceLists = [];
        foreach ($deviceList as $value){
            $deviceLists[$value] = $value;
        }

        $this->assignconfig('reservoirList',$reservoirLists);
        $this->assignconfig('deviceList',$deviceLists);
    }
}