package com.zksy.web.controller.WaterSupply.WaterSupplyGisMap;

import com.zksy.WaterSupply.WaterSupplyGisMap.service.WaterSupplyGisMapService;
import com.zksy.common.core.domain.AjaxResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.Collections;
import java.util.Set;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/waterSupply/gis/map")
@Api(tags = "供水管网-供水设施GIS一张图")
public class WaterSupplyGisMapController {
    @Resource private WaterSupplyGisMapService gisMapService;

    @GetMapping("/features")
    @ApiOperation(value = "供水管网-供水设施GIS一张图-查询 GeoJSON 图层",
            notes = "响应 data 为 GeoJSON FeatureCollection。点设施 geometry 为 Point，管网 geometry 为 LineString；坐标顺序固定为 [longitude, latitude]，缺少完整坐标的记录不返回。")
    @PreAuthorize("@ss.hasPermi('waterSupply:gis:map')")
    public AjaxResult features(
            @ApiParam(value = "设施类型，逗号分隔：source、plant、pumpStation、pipe、user；不传返回全部图层", example = "plant,pipe")
            @RequestParam(required = false) String types) {
        try {
            return AjaxResult.success(gisMapService.getFeatures(parseTypes(types)));
        } catch (IllegalArgumentException e) {
            return AjaxResult.error(e.getMessage());
        }
    }

    @GetMapping("/detail/{facilityType}/{id}")
    @ApiOperation(value = "供水管网-供水设施GIS一张图-查询设施详情",
            notes = "facilityType 仅支持 source、plant、pumpStation、pipe、user。响应 data 为对应设施实体，包含新增 WGS84 坐标字段；详情查询不要求坐标完整。")
    @PreAuthorize("@ss.hasPermi('waterSupply:gis:map')")
    public AjaxResult detail(
            @ApiParam(value = "设施类型：source、plant、pumpStation、pipe、user", required = true, example = "plant")
            @PathVariable String facilityType,
            @ApiParam(value = "设施主键 ID", required = true, example = "12")
            @PathVariable Long id) {
        try {
            Object detail = gisMapService.getDetail(facilityType, id);
            return detail == null ? AjaxResult.error("设施不存在") : AjaxResult.success(detail);
        } catch (IllegalArgumentException e) {
            return AjaxResult.error(e.getMessage());
        }
    }

    @GetMapping("/overview")
    @ApiOperation(value = "供水管网-供水设施GIS一张图-查询坐标完整性概览",
            notes = "响应 data 为数组，按设施类型返回 totalCount、geoCompleteCount、geoMissingCount，并追加 facilityType=total 的合计项。")
    @PreAuthorize("@ss.hasPermi('waterSupply:gis:map')")
    public AjaxResult overview() {
        return AjaxResult.success(gisMapService.getOverview());
    }

    private Set<String> parseTypes(String text) {
        if (text == null || text.trim().isEmpty()) return Collections.emptySet();
        return Arrays.stream(text.split(",")).map(String::trim).filter(s -> !s.isEmpty()).collect(Collectors.toSet());
    }
}
