package com.zksy.web.controller.gasbasic;

import com.zksy.base.domain.EquipmentBase;
import com.zksy.base.domain.ManholeBase;
import com.zksy.base.domain.PipeNetworkBase;
import com.zksy.base.service.EquipmentBaseService;
import com.zksy.base.service.ManholeBaseService;
import com.zksy.base.service.PipeNetworkBaseService;
import com.zksy.common.annotation.Anonymous;
import com.zksy.common.core.domain.AjaxResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.*;

@Slf4j
@RestController
@RequestMapping("/api/gis")
@Api(tags = "GIS一张图")
public class GisController {

    @Autowired
    private PipeNetworkBaseService pipeNetworkService;

    @Autowired
    private ManholeBaseService manholeService;

    @Autowired
    private EquipmentBaseService equipmentService;

    @GetMapping("/pipe-lines")
    @ApiOperation("获取管网GeoJSON数据")
    @Anonymous
    public AjaxResult getPipeLines(@RequestParam(required = false) String networkType,
                                    @RequestParam(required = false) String status) {
        List<PipeNetworkBase> list = pipeNetworkService.list();
        List<Map<String, Object>> features = new ArrayList<>();
        for (PipeNetworkBase pipe : list) {
            if (pipe.getLongitude() == null || pipe.getLatitude() == null) continue;
            Map<String, Object> feature = new HashMap<>();
            feature.put("type", "Feature");
            Map<String, Object> props = new HashMap<>();
            props.put("networkId", pipe.getNetworkId());
            props.put("networkName", pipe.getNetworkName());
            props.put("networkType", pipe.getNetworkType());
            props.put("networkCode", pipe.getNetworkCode());
            props.put("status", pipe.getStatus());
            props.put("material", pipe.getMaterial());
            feature.put("properties", props);
            Map<String, Object> geometry = new HashMap<>();
            geometry.put("type", "Point");
            geometry.put("coordinates", new double[]{pipe.getLongitude().doubleValue(), pipe.getLatitude().doubleValue()});
            feature.put("geometry", geometry);
            features.add(feature);
        }
        Map<String, Object> geojson = new HashMap<>();
        geojson.put("type", "FeatureCollection");
        geojson.put("features", features);
        return AjaxResult.success(geojson);
    }

    @GetMapping("/manholes")
    @ApiOperation("获取窨井点位")
    @Anonymous
    public AjaxResult getManholes() {
        List<ManholeBase> list = manholeService.list();
        List<Map<String, Object>> features = new ArrayList<>();
        for (ManholeBase m : list) {
            if (m.getLongitude() == null || m.getLatitude() == null) continue;
            Map<String, Object> feature = new HashMap<>();
            feature.put("type", "Feature");
            Map<String, Object> props = new HashMap<>();
            props.put("manholeId", m.getManholeId());
            props.put("manholeName", m.getManholeName());
            props.put("manholeCode", m.getManholeCode());
            props.put("status", m.getStatus());
            feature.put("properties", props);
            Map<String, Object> geometry = new HashMap<>();
            geometry.put("type", "Point");
            geometry.put("coordinates", new double[]{m.getLongitude().doubleValue(), m.getLatitude().doubleValue()});
            feature.put("geometry", geometry);
            features.add(feature);
        }
        Map<String, Object> geojson = new HashMap<>();
        geojson.put("type", "FeatureCollection");
        geojson.put("features", features);
        return AjaxResult.success(geojson);
    }

    @GetMapping("/equipments")
    @ApiOperation("获取设备点位")
    @Anonymous
    public AjaxResult getEquipments(@RequestParam(required = false) String equipmentTypeId) {
        List<EquipmentBase> list = equipmentService.list();
        List<Map<String, Object>> features = new ArrayList<>();
        for (EquipmentBase e : list) {
            if (e.getLongitude() == null || e.getLatitude() == null) continue;
            Map<String, Object> feature = new HashMap<>();
            feature.put("type", "Feature");
            Map<String, Object> props = new HashMap<>();
            props.put("equipmentId", e.getEquipmentId());
            props.put("equipmentName", e.getEquipmentName());
            props.put("equipmentCode", e.getEquipmentCode());
            props.put("equipmentTypeId", e.getEquipmentTypeId());
            feature.put("properties", props);
            Map<String, Object> geometry = new HashMap<>();
            geometry.put("type", "Point");
            geometry.put("coordinates", new double[]{e.getLongitude().doubleValue(), e.getLatitude().doubleValue()});
            feature.put("geometry", geometry);
            features.add(feature);
        }
        Map<String, Object> geojson = new HashMap<>();
        geojson.put("type", "FeatureCollection");
        geojson.put("features", features);
        return AjaxResult.success(geojson);
    }
}
