import DataUtils from "../utils/DataUtils";
import GeogUtils from "../utils/GeogUtils";
import VerifyUtils from "../utils/VerifyUtils";

import LayerProvider from "../geog/LayerProvider";
import FacilityProvider from "../facility/FacilityProvider";
import PlantPumpProvider from "../plant/PlantPumpProvider";
import PipenetProvider from "../pipeline/PipenetProvider";
import PipenetInProvider from "../pipeline/PipenetInProvider";
import DrainageEntityProvider from "../drainer/DrainageEntityProvider";

import LayerConfig from "../config/LayerConfig";
import PropertyConfig from "../config/PropertyConfig";

/**
 * 定义业务图层管理提供类
 *
 * @alias LayerManagerProvider
 *
 * @summary <用于管理管网业务各图层的显隐>
 */
export default class LayerManagerProvider {
  /**
   * 构造函数
   * @param {Object} options -图层选项
   */
  constructor(options) {
    /**
     * 地图对象
     * @type {Object}
     */
    this._map = options.map;

    /**
     * 最小缩放层级
     * @type {Number}
     */
    this._minzoom = DataUtils.default(options.minzoom, 10);

    /**
     * 是否切片的管网
     * @type {Boolean}
     */
    this._cachedPipe = DataUtils.default(options.cachedPipe, false);

    // 勾选的图层列表
    this._checkedLayers = [];

    /** 图层对象 **/
    // 厂、站、农污
    this._plantPumpProvider = null;
    // 管线、检查井
    this._pipenetProvider = null;
    // 排水单元内管线
    this._pipenetInProvider = null;
    // 管网设施
    this._facilityProvider = null;
    // 排水户
    this._drainageEntityProvider = null;

    this._connHoleData = options._connHoleData

    // 初始化各图层对象
    this._initProviders();
  }

  /** 静态方法 **/
  /**
   * 图层是否分层级显示
   * @param {String} category -图层类型
   *
   * @returns {Boolean}
   */
  static isLayerShowLevel(category) {
    return LayerConfig.Layers.some((layer) => {
      return (layer.code === category && layer.showLevel === true);
    });
  }

  /**
   * 是否管网(包括排水单元内)图层
   * @param {String} category -图层类型
   *
   * @returns {Boolean}
   */
  static isPipenetLayer(category) {
    return LayerConfig.Layers.some((layer) => {
      return (layer.code === category && (layer.parent === 'manhole' || layer.parent === 'pipeline'));
    });
  }

  /**
   * 获取厂站农污图层列表
   *
   * @returns {Array}
   */
  static getPlantPumpLayers() {
    return LayerConfig.Layers.filter((layer) => {
      return (layer.plantPump === true);
    });
  }

  /**
   * 获取设施图层列表
   *
   * @returns {Array}
   */
  static getFacilityLayers() {
    return LayerConfig.Layers.filter((layer) => {
      return (layer.facility && layer.facility === true);
    });
  }

  /**
   * 获取排水单元内管网图层列表
   *
   * @returns {Array}
   */
  static getPipeinLayers() {
    return LayerConfig.Layers.filter((layer) => {
      return (layer.pipeIn && layer.pipeIn === true);
    });
  }

  /**
   * 获取管网图层列表
   *
   * @returns {Array}
   */
  static getPipenetLayers() {
    return LayerConfig.Layers.filter((layer) => {
      return (
        !layer.pipeIn &&
        (layer.parent === 'manhole' || layer.parent === 'pipeline')
      );
    });
  }

  /**
   * 获取排水户图层列表
   *
   * @returns {Array}
   */
  static getDrainEntityLayers() {
    return LayerConfig.Layers.filter((layer) => {
      return (layer.drainEntity && layer.drainEntity === true);
    });
  }

  /**
   * 根据类型获取图层列表
   * @param {String} category -类别(parent)
   *
   * @returns {Array}
   */
  static getLayers(category) {
    return LayerConfig.Layers.filter((layer) => {
      return (layer.parent === category);
    });
  }

  /**
   * 获取图层
   * @param {String} code -编码
   *
   * @returns {Object}
   */
  static getLayer(code) {
    const layers = LayerConfig.Layers.filter((layer) => {
      return (layer.code === code);
    });

    return layers.length > 0 ? layers[0] : null;
  }

  /**
   * 获取图层类型
   * @param {String} category -图层类型
   *
   * @returns {String}
   */
  static getLayerType(category) {
    const layer = this.getLayer(category);
    if (!layer) return null;

    return layer.type === 'symbol' ? 'marker' : layer.type;
  }

  /**
   * 获取图层查询URL
   * @param {Object} map -地图对象
   * @param {String} category -图层类型
   * @param {String} value -ID值
   *
   * @returns {String}
   */
  static getLayerQueryUrl(map, category, value) {
    // 管线、检查井分级显示，需要处理
    switch (category) {
      case "manhole":
      case "manholerain":       // 雨水检查井
      case "manholewaste":      // 污水检查井
      case "manholeindustry": { // 工废检查井
        category = "manhole";
        break;
      }
      case "pipeline":
      case "pipelinerain":       // 雨水管线
      case "pipelinewaste":      // 污水管线
      case "pipelineindustry": { // 工废管线
        category = "pipeline";
        break;
      }
      case 'area': { // 片区
        category = 'patrolarea';
      }
      default:
        break;
    }
    // 1. 过滤基础查询图层
    let url = LayerProvider.getBaseLayerQueryUrl(map, category, value);
    if (url) {
      return url;
    }

    // 2. 过滤厂站、设施及相关图层
    let sourceId = "", where = "";
    const layers = LayerConfig.Layers.filter((layer) => {
      return layer.code === category;
    });

    if (layers && layers.length > 0) {
      sourceId = layers[0].id;
      where = "&cql_filter=" + layers[0].mainField + "='" + value.toString() + "'";
    }
    if (sourceId.length === 0 || where.length === 0) {
      return null;
    }

    const source = map.getSource(sourceId);
    if (!source) return null;

    return source._data + where;
  }

  /**
   * 获取图层属性配置
   * @param {String} category -图层类型
   *
   * @returns {Array}
   */
  static getLayerPropertyConfig(category) {
    switch (category) {
      case "poi": { // 地名点
        return PropertyConfig.POIConfig;
      }
      case "road": { // 道路
        return PropertyConfig.RoadConfig;
      }
      // 厂、站、农污
      case "plant":
      case "waterplant":
      case "industryplant":
      case 'otherplant': { // 水厂
        return PropertyConfig.PlantConfig;
      }
      case "pump":
      case "pumprain":
      case "pumpwaste":
      case "pumpindustry":
      case "pumpcombine": { // 泵站
        return PropertyConfig.PumpConfig;
      }
      case "rural":
      case "agricwaste": { // 农污
        return PropertyConfig.StationConfig;
      }
      case "drainer": { // 排水户(接口)
        return PropertyConfig.DrainentityConfig;
      }
      case "drainentity": { // 排水户
        return PropertyConfig.DrainageEntityConfig;
      }
      // 管网
      case "manhole":
      case "manholerain":
      case "manholewaste":
      case "manholeindustry":
      case "manholein":
      case "manholeinrain":
      case "manholeinwaste": { // 检查井
        return PropertyConfig.ManholeConfig;
      }
      case "pipeline":
      case "pipelinerain":
      case "pipelinewaste":
      case "pipelineindustry":
      case "pipelinein":
      case "pipelineinrain":
      case "pipelineinwaste": { // 管线
        return PropertyConfig.PipelineConfig;
      }
      // 设施
      case "ditch": { // 排水渠
        return PropertyConfig.DitchConfig;
      }
      case "interceptionsew": { // 截污设施
        return PropertyConfig.InterceptionSewConfig;
      }
      case "interception": { // 截流设施
        return PropertyConfig.InterceptionConfig;
      }
      case "raininlet": { // 雨水口
        return PropertyConfig.RaininletConfig;
      }
      case "storage": { // 调蓄设施
        return PropertyConfig.StorageConfig;
      }
      case "outfall": { // 排放口
        return PropertyConfig.FlapConfig;
      }
      case "flap": { // 排放口
        return PropertyConfig.FlapConfig;
      }
      case "gate": { // 闸门
        return PropertyConfig.GateConfig;
      }
      // 业务
      case "faci-alarm": { // 报警
        return PropertyConfig.AlarmConfig;
      }
      case "faci-order": { // 工单
        return PropertyConfig.OrderConfig;
      }
      case "faci-staff": { // 人员
        return PropertyConfig.StaffConfig;
      }
      case "faci-vehicle": { // 车辆
        return PropertyConfig.VehicleConfig;
      }
      case "buss-site": { // 工地
        return PropertyConfig.BuildingsiteConfig;
      }
      default:
        return null;
    }
  }

  /**
   * 获取图层属性
   * @param {String} category -图层类型
   *
   * @returns {Object}
   */
  static getLayerProperties(category) {
    // 获取属性配置
    let config = this.getLayerPropertyConfig(category);
    if (!config) return {};

    let properties = {};
    config.forEach((item) => {
      //properties[item.name] = item.default || undefined;
      properties[item.name] = VerifyUtils.isUndefined(item.default) ? undefined : item.default;
    });

    return properties;
  }

  /**
   * 获取图层主字段名
   * @param {String} category -图层类型
   *
   * @returns {String}
   */
  static getLayerFieldName(category) {
    if (category === 'poi' || category === 'road') {
      return 'name';
    }

    const layers = LayerConfig.Layers.filter((layer) => {
      return layer.code === category;
    });
    if (layers.length > 0) {
      return layers[0].mainField;
    }

    return null;
  }

  /**
   * 获取图层查询属性
   * @param {String} category -图层类型
   */
  static getLayerQueryProperty(category) {
    switch (category) {
      case "waterplant": { // 水质净化厂
        return {name: "category", value: "2"};
      }
      case "industryplant": { // 工废处理厂
        return {name: "category", value: "5"};
      }
      case 'otherplant': { // 其他水厂
        return {name: "category", value: "4"};
      }
      case "pumprain": { // 雨水泵站
        return {name: "pump_category", value: "1"};
      }
      case "pumpwaste":
      case "pumpsewage": { // 污水泵站
        return {name: "pump_category", value: "2"};
      }
      case "pumpindustry": { // 工废泵站
        return {name: "pump_category", value: "3"};
      }
      case "pumpcombine": { // 一体化泵站
        return {name: "pump_category", value: "4"};
      }
      case "manholerain":
      case "pipelinerain": { // 雨水管线/井
        return {name: "category", value: "1"};
      }
      case "manholewaste":
      case "manholesewage":
      case "pipelinewaste":
      case "pipelinesewage": { // 污水管线/井
        return {name: "category", value: "2"};
      }
      case "manholeindustry":
      case "pipelineindustry": { // 工废管线/井
        return {name: "category", value: "5"};
      }
      case "manholeinrain":
      case "pipelineinrain": { // 排水单元-雨水管线/井
        return {name: "category", value: "1"};
      }
      case "manholeinwaste":
      case "pipelineinwaste": { // 排水单元-污水管线/井
        return {name: "category", value: "2"};
      }
      default:
        return null;
    }
  }

  /**
   * 获取几何类型
   * @param {String} category -图层类型
   *
   * @returns {String}
   */
  static getGeometryType(category) {
    switch (category) {
      case 'customdot':
        return 'Dot';
      case 'poi':
      case 'drainentity':
      case 'waterplant':
      case 'otherplant':
      case 'industryplant':
      case 'pumprain':
      case 'pumpwaste':
      case 'pumpindustry':
      case 'pumpcombine':
      case 'manholerain':
      case 'manholewaste':
      case "manholeindustry":
      case 'manholeinrain':
      case 'manholeinwaste':
      case 'gate':
      case 'outfall':
        case 'flap':
      case 'storage':
      case 'raininlet':
      case 'interception':
      case 'interceptionsew':
        return 'Point';
      case 'road':
      case 'ditch':
      case 'pipelinerain':
      case 'pipelinewaste':
      case 'pipelineindustry':
      case 'pipelineinrain':
      case 'pipelineinwaste':
        return 'LineString';
      case 'waterlogging':
      case 'patrolarea':
        return 'Polygon';
      default:
        return null;
    }
  }

  /**
   * 获取查询结果属性列表
   * @param {Array} features -GeoJSON要素列表
   *
   * @return {Array}
   */
  static getResultProperties(features) {
    if (!features || features.length === 0) {
      return [];
    }

    const resultProps = [];
    for (let i = 0; i < features.length; i++) {
      const feature = features[i];
      const fid = feature.id;
      const geometry = feature.geometry;
      const properties = feature.properties;

      // 计算中点
      const coordinates = GeogUtils.calcCenter(feature);
      if (!coordinates) {
        coordinates = [null, null];
      }

      const layerCode = properties.layercode || properties.layer_code;
      const layerName = properties.layername  || properties.layer_name;
      switch (layerCode) {
        case 'customdot': {
          resultProps.push({ // 自定义点
            fid: fid,
            id: properties.id ? properties.id : fid,
            rangeId: null,
            geomType: 'Dot',
            category: layerCode,
            type: layerCode,
            typeName: '自定义点',
            name: null,
            town: null,
            roadName: null,
            address: null,
            len: null,
            diameter: null,
            caliber: null,
            wellId: null,
            plantId: null,
            pumpId: null,
            geom: geometry,
            lon: coordinates[0].toFixed(6),
            lat: coordinates[1].toFixed(6)
          });
          break;
        }
        case 'poi': { // 地名点
          resultProps.push({
            fid: fid,
            id: properties.name,
            rangeId: properties.rangeId,
            geomType: 'Point',
            category: layerCode,
            type: properties.type,
            typeName: '地名点',
            name: properties.name,
            town: null,
            roadName: null,
            address: null,
            len: null,
            diameter: null,
            caliber: null,
            wellId: null,
            plantId: null,
            pumpId: null,
            geom: geometry,
            lon: coordinates[0].toFixed(6),
            lat: coordinates[1].toFixed(6)
          });
          break;
        }
        case "road": { // 道路
          resultProps.push({
            fid: fid,
            id: properties.name,
            rangeId: properties.rangeId,
            geomType: 'LineString',
            category: layerCode,
            type: layerCode,
            typeName: '道路',
            name: properties.name,
            town: '',
            roadName: properties.name,
            address: properties.name,
            len: null,
            diameter: null,
            caliber: null,
            wellId: null,
            plantId: null,
            pumpId: null,
            geom: geometry,
            lon: coordinates[0].toFixed(6),
            lat: coordinates[1].toFixed(6)
          });
          break;
        }
        case 'drainentity': { // 排水户
          resultProps.push({
            fid: fid,
            id: properties.drainersCode || properties.drain_code,
            rangeId: properties.rangeId,
            geomType: 'Point',
            category: layerCode,
            type: layerCode,
            typeName: '排水户',
            name: properties.drainersName || properties.drain_name,
            town: properties.drainersRegion || properties.town,
            roadName: null,
            address: null,
            len: null,
            diameter: null,
            caliber: null,
            wellId: null,
            plantId: (properties.plant_id && properties.plant_id !== 'null') ? properties.plant_id : null,
            pumpId: (properties.pump_id && properties.pump_id !== 'null') ? properties.pump_id : null,
            geom: geometry,
            lon: coordinates[0].toFixed(6),
            lat: coordinates[1].toFixed(6)
          });
          break;
        }
        case 'waterplant':
        case "industryplant":
        case 'otherplant': { // 水厂
          resultProps.push({
            fid: fid,
            id: properties.plant_id,
            rangeId: properties.rangeId,
            geomType: 'Point',
            category: layerCode,
            type: layerCode,
            typeName: layerName || '水质净化厂',
            name: properties.name,
            town: properties.town,
            roadName: null,
            address: properties.address,
            len: null,
            diameter: null,
            caliber: null,
            wellId: null,
            plantId: (properties.plant_id && properties.plant_id !== 'null') ? properties.plant_id : null,
            pumpId: null,
            geom: geometry,
            lon: coordinates[0].toFixed(6),
            lat: coordinates[1].toFixed(6)
          });
          break;
        }
        case "pumprain":
        case "pumpwaste":
        case "pumpindustry":
        case "pumpcombine": { // 泵站
          resultProps.push({
            fid: fid,
            id: properties.pump_id,
            rangeId: properties.rangeId,
            geomType: 'Point',
            category: layerCode,
            type: layerCode,
            typeName: layerName || '泵站',
            name: properties.pump_name,
            town: properties.town,
            roadName: null,
            address: properties.address,
            len: null,
            diameter: null,
            caliber: null,
            wellId: null,
            plantId: (properties.plant_id && properties.plant_id !== 'null') ? properties.plant_id : null,
            pumpId: (properties.pump_id && properties.pump_id !== 'null') ? properties.pump_id : null,
            geom: geometry,
            lon: coordinates[0].toFixed(6),
            lat: coordinates[1].toFixed(6)
          });
          break;
        }
        case 'manholerain':
        case 'manholewaste':
        case "manholeindustry": { // 检查井
          resultProps.push({
            fid: fid,
            id: properties.node_id,
            rangeId: properties.rangeId,
            geomType: 'Point',
            category: layerCode,
            type: layerCode,
            typeName: layerName || '检查井',
            //name: properties.node_id,
            name: properties.nodeid, // 设施(业务)ID
            town: properties.town,
            roadName: properties.road_name,
            address: null,
            len: null,
            diameter: null,
            caliber: null,
            wellId: properties.node_id || '-',
            plantId: (properties.plant_id && properties.plant_id !== 'null') ? properties.plant_id : null,
            pumpId: (properties.pump_id && properties.pump_id !== 'null') ? properties.pump_id : null,
            geom: geometry,
            lon: coordinates[0].toFixed(6),
            lat: coordinates[1].toFixed(6)
          });
          break;
        }
        case "pipelinerain":
        case "pipelinewaste":
        case "pipelineindustry": { // 管线
          resultProps.push({
            fid: fid,
            id: properties.pipe_id,
            rangeId: properties.rangeId,
            geomType: 'LineString',
            category: layerCode,
            type: layerCode,
            typeName: layerName || '管线',
            //name: properties.pipe_id,
            name: properties.pipeid, // 设施(业务)ID
            town: properties.town,
            roadName: properties.road_name,
            address: null,
            len: properties.pipe_length,   // 管长
            diameter: properties.diameter, // 管径
            caliber: properties.diameter,
            wellId: (properties.in_nodeid || '-') + ',' + (properties.out_nodeid || '-'),
            plantId: (properties.plant_id && properties.plant_id !== 'null') ? properties.plant_id : null,
            pumpId: (properties.pump_id && properties.pump_id !== 'null') ? properties.pump_id : null,
            geom: geometry,
            lon: coordinates[0].toFixed(6),
            lat: coordinates[1].toFixed(6)
          });
          break;
        }
        case "ditch": { // 排水渠
          resultProps.push({
            fid: fid,
            id: properties.ditch_id,
            rangeId: properties.rangeId,
            geomType: 'LineString',
            category: layerCode,
            type: properties.ditch_type,//设置类型ID
            typeName: '排水渠',
            //name: properties.ditch_id,
            name: properties.ditchid, // 设施(业务)ID
            town: properties.town,
            roadName: null,
            address: null,
            len: properties.ditch_length,  // 管长
            diameter: properties.diameter, // 管径
            caliber: properties.diameter,
            wellId: (properties.in_nodeid || '-') + ',' + (properties.out_nodeid || '-'),
            plantId: (properties.plant_id && properties.plant_id !== 'null') ? properties.plant_id : null,
            pumpId: (properties.pump_id && properties.pump_id !== 'null') ? properties.pump_id : null,
            geom: geometry,
            lon: coordinates[0].toFixed(6),
            lat: coordinates[1].toFixed(6)
          });
          break;
        }
        case 'interceptionsew': { // 截污设施
          resultProps.push({
            fid: fid,
            id: properties.inter_id,
            rangeId: properties.rangeId,
            geomType: 'Point',
            category: layerCode,
            type: layerCode,
            typeName: '截污设施',
            //name: properties.inter_name,
            name: properties.interid, // 设施(业务)ID
            town: properties.town,
            roadName: null,
            address: null,
            len: null,
            diameter: null,
            caliber: null,
            wellId: null,
            plantId: (properties.plant_id && properties.plant_id !== 'null') ? properties.plant_id : null,
            pumpId: (properties.pump_id && properties.pump_id !== 'null') ? properties.pump_id : null,
            geom: geometry,
            lon: coordinates[0].toFixed(6),
            lat: coordinates[1].toFixed(6)
          });
          break;
        }
        case 'interception': { // 截流设施
          resultProps.push({
            fid: fid,
            id: properties.inter_id,
            rangeId: properties.rangeId,
            geomType: 'Point',
            category: layerCode,
            type: layerCode,
            typeName: '截流设施',
            //name: properties.inter_id,
            name: properties.interid, // 设施(业务)ID
            town: properties.town,
            roadName: null,
            address: null,
            len: null,
            diameter: null,
            caliber: null,
            wellId: null,
            plantId: (properties.plant_id && properties.plant_id !== 'null') ? properties.plant_id : null,
            pumpId: (properties.pump_id && properties.pump_id !== 'null') ? properties.pump_id : null,
            geom: geometry,
            lon: coordinates[0].toFixed(6),
            lat: coordinates[1].toFixed(6)
          });
          break;
        }
        case 'raininlet': { // 雨水口
          resultProps.push({
            fid: fid,
            id: properties.inlet_id,
            rangeId: properties.rangeId,
            geomType: 'Point',
            category: layerCode,
            type: layerCode,
            typeName: '雨水口',
            //name: properties.inlet_name,
            name: properties.inletid, // 设施(业务)ID
            town: properties.town,
            roadName: null,
            address: null,
            len: null,
            diameter: null,
            caliber: null,
            wellId: null,
            plantId: (properties.plant_id && properties.plant_id !== 'null') ? properties.plant_id : null,
            pumpId: (properties.pump_id && properties.pump_id !== 'null') ? properties.pump_id : null,
            geom: geometry,
            lon: coordinates[0].toFixed(6),
            lat: coordinates[1].toFixed(6)
          });
          break;
        }
        case 'storage': { // 调蓄设施
          resultProps.push({
            fid: fid,
            id: properties.storage_id,
            rangeId: properties.rangeId,
            geomType: 'Point',
            category: layerCode,
            type: layerCode,
            typeName: '调蓄设施',
            //name: properties.storage_name,
            name: properties.storageid, // 设施(业务)ID
            town: properties.town,
            roadName: null,
            address: null,
            len: null,
            diameter: null,
            caliber: null,
            wellId: null,
            plantId: (properties.plant_id && properties.plant_id !== 'null') ? properties.plant_id : null,
            pumpId: (properties.pump_id && properties.pump_id !== 'null') ? properties.pump_id : null,
            geom: geometry,
            lon: coordinates[0].toFixed(6),
            lat: coordinates[1].toFixed(6)
          });
          break;
        }
        case 'outfall': { // 排放口
          resultProps.push({
            fid: fid,
            id: properties.outfall_id,
            rangeId: properties.rangeId,
            geomType: 'Point',
            category: layerCode,
            type: layerCode,
            typeName: '排放口',
            //name: properties.outfall_id,
            name: properties.outfallid, // 设施(业务)ID
            town: properties.town,
            roadName: null,
            address: null,
            len: null,
            diameter: null,
            caliber: null,
            wellId: null,
            plantId: (properties.plant_id && properties.plant_id !== 'null') ? properties.plant_id : null,
            pumpId: (properties.pump_id && properties.pump_id !== 'null') ? properties.pump_id : null,
            geom: geometry,
            lon: coordinates[0].toFixed(6),
            lat: coordinates[1].toFixed(6)
          });
          break;
        }
        case 'flap': { // 拍门
          resultProps.push({
            fid: fid,
            id: properties.outfall_id,
            rangeId: properties.rangeId,
            geomType: 'Point',
            category: layerCode,
            type: layerCode,
            typeName: '拍门',
            //name: properties.outfall_id,
            name: properties.outfallid, // 设施(业务)ID
            town: properties.town,
            roadName: null,
            address: null,
            len: null,
            diameter: null,
            caliber: null,
            wellId: null,
            plantId: (properties.plant_id && properties.plant_id !== 'null') ? properties.plant_id : null,
            pumpId: (properties.pump_id && properties.pump_id !== 'null') ? properties.pump_id : null,
            geom: geometry,
            lon: coordinates[0].toFixed(6),
            lat: coordinates[1].toFixed(6)
          });
          break;
        }
        case 'gate': { // 闸门
          resultProps.push({
            fid: fid,
            id: properties.gate_id,
            rangeId: properties.rangeId,
            geomType: 'Point',
            category: layerCode,
            type: layerCode,
            typeName: '闸门',
            //name: properties.gate_name,
            name: properties.gateid, // 设施(业务)ID
            town: properties.town,
            roadName: null,
            address: null,
            len: null,
            diameter: null,
            caliber: null,
            wellId: null,
            plantId: (properties.plant_id && properties.plant_id !== 'null') ? properties.plant_id : null,
            pumpId: (properties.pump_id && properties.pump_id !== 'null') ? properties.pump_id : null,
            geom: geometry,
            lon: coordinates[0].toFixed(6),
            lat: coordinates[1].toFixed(6)
          });
          break;
        }
        case "agricwaste": { // 农污装置
          resultProps.push({
            fid: fid,
            id: properties.station_id,
            rangeId: properties.rangeId,
            geomType: 'Point',
            category: layerCode,
            type: layerCode,
            typeName: layerName || '农污装置',
            //name: properties.node_id,
            name: properties.station_name,
            town: properties.town,
            roadName: null,
            address: properties.address,
            len: null,
            diameter: null,
            caliber: null,
            wellId: properties.station_id || '-',
            plantId: (properties.plant_id && properties.plant_id !== 'null') ? properties.plant_id : null,
            pumpId: (properties.pump_id && properties.pump_id !== 'null') ? properties.pump_id : null,
            geom: geometry,
            lon: coordinates[0].toFixed(6),
            lat: coordinates[1].toFixed(6)
          });
          break;
        }
        default:
          break;
      }
    } // end of for (...)

    return resultProps;
  }
  /** **/

  /** 公有方法 **/
  /**
   * 设置勾选的图层列表
   * @param {Array} layers -图层列表
   */
  setLayers(layers) {
    this._checkedLayers = layers;
  }

  /**
   * 判断图层是否勾选
   * @param {array} code -图层编码
   *
   * @returns {Boolean}
   */
  isLayerChecked(code) {
    return this._checkedLayers.some((layer) => {
      return layer.layercode === code;
    });
  }

  /**
   * 设置绘制属性
   * @param {String} layerCode -图层编码
   * @param {String} name -属性名称
   * @param {String|Object} value -属性值
   */
  setPaintProperty(layerCode, name, value) {
    switch (layerCode) {
      case 'pipeline':
      case 'pipelinerain':
      case 'pipelinewaste':
      case 'pipelineindustry':
      case 'manhole':
      case 'manholerain':
      case 'manholewaste':
      case "manholeindustry": {
        // 管线、检查井
        this._pipenetProvider.setPaintProperty(layerCode, name, value);
        break;
      }
    }
  }

  /**
   * 设置布局属性
   * @param {String} layerCode -图层编码
   * @param {String} name -属性名称
   * @param {String|Object} value -属性值
   */
  setLayoutProperty(layerCode, name, value) {
    switch (layerCode) {
      case 'pipeline':
      case 'pipelinerain':
      case 'pipelinewaste':
      case 'pipelineindustry':
      case 'manhole':
      case 'manholerain':
      case 'manholewaste':
      case "manholeindustry": {
        // 管线、检查井
        this._pipenetProvider.setLayoutProperty(layerCode, name, value);
        break;
      }
    }
  }

  /**
   * 重置管线宽度
   * @param {String} layerCode -类型
   */
  resetPipelineWidth(layerCode) {
    this._pipenetProvider.resetPipelineWidth(layerCode);
  }

  /**
   * 过滤图层
   * @param {String} layerCode -图层树节点
   * @param {Array} filter -过滤器
   */
  setFilter(layerCode, filter) {
    switch (layerCode) {
      case 'waterplant':
      case 'industryplant':
      case 'otherplant':
      case 'pumpwaste':
      case 'pumpindustry':
      case 'pumprain':
      case 'pumpcombine':
      case 'agricwaste':
      case 'servicerange': // 服务范围
      case 'servicerange-plant':
      case 'servicerange-pump': {
        // 厂、站、农污，服务范围
        if (!this._plantPumpProvider.hasLayer(layerCode)) {
          this._plantPumpProvider.loadLayer(layerCode);
        }

        this._plantPumpProvider.setFilter(layerCode, filter);
        break;
      }
      case 'pipeline':
      case 'pipelinerain':
      case 'pipelinewaste':
      case 'pipelineindustry':
      case 'manhole':
      case 'manholerain':
      case 'manholewaste':
      case "manholeindustry": {
        // 管线、检查井
        if (!this._pipenetProvider.hasLayer(layerCode)) {
          this._pipenetProvider.loadLayer(layerCode);
        }

        this._pipenetProvider.setFilter(layerCode, filter);
        break;
      }
      case 'pipelinein':
      case 'pipelineinrain':
      case 'pipelineinwaste':
      case 'manholein':
      case 'manholeinrain':
      case 'manholeinwaste': {
        // 排水单元内管线、检查井
        if (!this._pipenetInProvider.hasLayer(layerCode)) {
          this._pipenetInProvider.loadLayer(layerCode);
        }

        this._pipenetInProvider.setFilter(layerCode, filter);
        break;
      }
      case 'gate':
      case 'outfall':
      case 'flap':
      case 'storage':
      case 'raininlet':
      case 'interception':
      case 'interceptionsew':
      case 'ditch':
      case 'patrolarea': {
        // 设施
        if (!this._facilityProvider.hasLayer(layerCode)) {
          this._facilityProvider.loadLayer(layerCode);
        }
        this._facilityProvider.setFilter(layerCode, filter);
        break;
      }
      default:
        break;
    }
  }

  /**
   * 过滤图层
   * @param {Object} node -图层树节点
   * @param {Array} filter -过滤器
   */
  filterLayer(node, filter) {
    // 设置筛选器
    this.setFilter(node.layercode, filter);
  }

  /**
   * 显示/隐藏图层
   * @param {Object} node -图层树节点
   */
  showLayer(node) {
    const layerCode = node.layercode;
    const checked = node.layercheck;
    switch (layerCode) {
      case 'waterplant':
      case 'industryplant':
      case 'otherplant':
      case 'pumpwaste':
      case 'pumpindustry':
      case 'pumprain':
      case 'pumpcombine':
      case 'agricwaste':
      case 'servicerange': // 服务范围
      case 'servicerange-plant':
      case 'servicerange-pump': {
        // 厂、站、农污
        const provider = this._plantPumpProvider;
        /*
        if (!provider.hasLayer(layerCode)) {
          provider.loadLayer(layerCode);
        }

        provider.showLayer(layerCode, checked);
        */
        if (checked) {
          if (!provider.hasLayer(layerCode)) {
            provider.loadLayer(layerCode);
          }
          provider.showLayer(layerCode, true);
        } else {
          provider.removeLayer(layerCode);
        }
        break;
      }
      case 'pipelinerain':
      case 'pipelinewaste':
      case 'pipelineindustry':
      case 'pipeline':
      case 'manholerain':
      case 'manholewaste':
      case "manholeindustry":
      case "manhole": {
        // 管线、检查井
        const provider = this._pipenetProvider;
        if (this._cachedPipe) {
          provider.showLayer(layerCode, checked);
        } else {
          if (checked) {
            if (!provider.hasLayer(layerCode)) {
              provider.loadLayer(layerCode);
            }
            provider.showLayer(layerCode, true);
          } else {
            provider.removeLayer(layerCode);
          }
        }
        break;
      }
      case 'pipelinein':
      case 'pipelineinrain':
      case 'pipelineinwaste':
      case 'manholein':
      case 'manholeinrain':
      case 'manholeinwaste': {
        // 排水单元内管线、检查井
        const provider = this._pipenetInProvider;

        if (checked) {
          if (!provider.hasLayer(layerCode)) {
            provider.loadLayer(layerCode);
          }
          provider.showLayer(layerCode, true);
        } else {
          provider.removeLayer(layerCode);
        }
        break;
      }
      case 'gate':
      case 'outfall':
      case 'flap':
      case 'storage':
      case 'raininlet':
      case 'interception':
      case 'interceptionsew':
      case 'ditch':
      case 'patrolarea': {
        // 设施
        const provider = this._facilityProvider;
        if (checked) {
          if (!provider.hasLayer(layerCode)) {
            provider.loadLayer(layerCode);
          }
          provider.showLayer(layerCode, true);
        } else {
          provider.removeLayer(layerCode);
        }
        break;
      }
      case 'drainunit':
      case 'drainentity': {
        // 排水户
        const provider = this._drainageEntityProvider;

        if (checked) {
          if (!provider.hasLayer(layerCode)) {
            provider.loadLayer(layerCode);
          }
          provider.showLayer(layerCode, true);
        } else {
          provider.removeLayer(layerCode);
        }
        break;
      }
      case 'village': {
        // 行政区划
        LayerProvider.showLayer(this._map, node.layerid, checked);
        LayerProvider.showLayer(this._map, node.layerid + '-line', checked);
        break;
      }
      default: {
        LayerProvider.showLayer(this._map, node.layerid, checked);
        break;
      }
    }
  }

  /**
   * 显示/隐藏检查井标注图层
   * @param {Boolean} isShow -是否显示图层
   */
  showManholeLabelLayer(isShow) {
    const provider = this._pipenetProvider;

    if (isShow) {
      provider.loadManholeLabelLayer();
    } else {
      provider.removeManholeLabelLayer();
    }
  }

  /**
   * 移动图层
   * @param {String} id -目标图层Id
   * @param {String} ibeforeId -被插入的图层Id
   */
  moveLayer(id, beforeId) {
    if (!LayerProvider.hasLayer(this._map, id)) {
      return;
    }

    // 移动图层
    LayerProvider.moveLayer(this._map, id, beforeId);
  }

  /**
   * 绑定单击事件
   * @param {String} code -图层编码
   * @param {Function} event -事件函数
   */
  bindClickEvent(code, event) {
    switch (code) {
      case 'plant':
      case 'pump':
      case 'waterplant':
      case 'industryplant':
      case 'otherplant':
      case 'pumpwaste':
      case 'pumpindustry':
      case 'pumprain':
      case 'pumpcombine':
      case 'rural':
      case 'agricwaste': {
        // 厂、站、农污
        this._plantPumpProvider.bindClickEvent(code, event);
        break;
      }
      case 'pipeline':
      case 'pipelinerain':
      case 'pipelinewaste':
      case 'pipelineindustry':
      case 'manhole':
      case 'manholerain':
      case 'manholewaste':
      case "manholeindustry": {
        // 管线、检查井
        this._pipenetProvider.bindClickEvent(code, event);
        break;
      }
      case 'pipelinein':
      case 'pipelineinrain':
      case 'pipelineinwaste':
      case 'manholein':
      case 'manholeinrain':
      case 'manholeinwaste': {
        // 排水单元内管线、检查井
        this._pipenetInProvider.bindClickEvent(code, event);
        break;
      }
      case 'gate':
      case 'outfall':
      case 'flap':
      case 'storage':
      case 'raininlet':
      case 'interception':
      case 'interceptionsew':
      case 'ditch':
      case 'patrolarea': {
        // 设施
        this._facilityProvider.bindClickEvent(code, event);
        break;
      }
      case 'drainunit':
      case 'drainentity': {
        // 排水户
        this._drainageEntityProvider.bindClickEvent(code, event);
        break;
      }
      default:
        break;
    }
  }

  /**
   * 绑定事件
   * @param {String} code -图层编码
   * @param {String} name -事件名称
   * @param {Function} event -事件函数
   */
  bindEvent(code, name, event) {
    switch (code) {
      case 'plant':
      case 'pump':
      case 'waterplant':
      case 'industryplant':
      case 'otherplant':
      case 'pumpwaste':
      case 'pumpindustry':
      case 'pumprain':
      case 'pumpcombine':
      case 'rural':
      case 'agricwaste': {
        // 厂、站、农污
        this._plantPumpProvider.bindEvent(code, name, event);
        break;
      }
      case 'pipeline':
      case 'pipelinerain':
      case 'pipelinewaste':
      case 'pipelineindustry':
      case 'manhole':
      case 'manholerain':
      case 'manholewaste':
      case "manholeindustry": {
        // 管线、检查井
        this._pipenetProvider.bindEvent(code, name, event);
        break;
      }
      case 'pipelinein':
      case 'pipelineinrain':
      case 'pipelineinwaste':
      case 'manholein':
      case 'manholeinrain':
      case 'manholeinwaste': {
        // 排水单元内管线、检查井
        this._pipenetInProvider.bindEvent(code, name, event);
        break;
      }
      case 'gate':
      case 'outfall':
        case 'flap':
      case 'storage':
      case 'raininlet':
      case 'interception':
      case 'interceptionsew':
      case 'ditch':
      case 'patrolarea': {
        // 设施
        this._facilityProvider.bindEvent(code, name, event);
        break;
      }
      case 'drainunit':
      case 'drainentity': {
        // 排水户
        this._drainageEntityProvider.bindEvent(code, name, event);
        break;
      }
      default:
        break;
    }
  }

  /**
   * 销毁各图层对象
   */
  destroyProviders() {
    // 厂站
    if (this._plantPumpProvider) {
      this._plantPumpProvider.removeAllLayers();
    }

    // 管网
    if (this._pipenetProvider) {
      this._pipenetProvider.removeAllLayers();
    }
    // 排水单元内管网
    if (this._pipenetInProvider) {
      this._pipenetInProvider.removeAllLayers();
    }

    // 设施
    if (this._facilityProvider) {
      this._facilityProvider.removeAllLayers();
    }

    // 排水户
    if (this._drainageEntityProvider) {
      this._drainageEntityProvider.removeAllLayers();
    }
  }

  /** 私有方法 **/
  /**
   * 初始化各图层对象
   */
  _initProviders() {
    const map = this._map;
    const minZoom = this._minzoom;

    // 厂站农污
    this._plantPumpProvider = new PlantPumpProvider({
      map: map,
      minzoom: minZoom
    });

    // 管网
    this._pipenetProvider = new PipenetProvider({
      map: map,
      minzoom: minZoom,
      cachedPipe: this._cachedPipe
    });
    // 排水单元内管网
    this._pipenetInProvider = new PipenetInProvider({
      map: map,
      minzoom: minZoom + 3.5,
      _connHoleData:this._connHoleData
    });

    // 设施
    this._facilityProvider = new FacilityProvider({
      map: map,
      minzoom: minZoom + 1.5
    });

    // 排水户
    this._drainageEntityProvider = new DrainageEntityProvider({
      map: map,
      minzoom: minZoom
    });
  }
}
