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

import com.zksy.base.domain.vo.DeviceDetailVO;
import com.zksy.base.domain.vo.DeviceGisVO;
import com.zksy.base.service.WaterSupplyDeviceService;
import com.zksy.common.core.domain.AjaxResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Slf4j
@RestController
@RequestMapping("/waterSupply/device/gis")
@Api(tags = "供水设备-GIS一张图")
public class DeviceGisController {

    @Autowired
    private WaterSupplyDeviceService waterSupplyDeviceService;

    @GetMapping("/allPoints")
    @ApiOperation("获取所有设备点位（GIS一张图用）")
    @PreAuthorize("@ss.hasPermi('waterSupply:device:gis')")
    public AjaxResult getAllDevicePoints(
            @ApiParam("设备类型ID") @RequestParam(required = false) String equipmentTypeId) {
        List<DeviceGisVO> list = waterSupplyDeviceService.getAllDeviceGisPoints(equipmentTypeId);
        Map<String, Object> result = new HashMap<>();
        result.put("type", "FeatureCollection");
        result.put("features", list);
        return AjaxResult.success(result);
    }

    @GetMapping("/abnormalPoints")
    @ApiOperation("获取异常设备点位（GIS高亮用）")
    @PreAuthorize("@ss.hasPermi('waterSupply:device:gis')")
    public AjaxResult getAbnormalPoints(
            @ApiParam("设备类型ID") @RequestParam(required = false) String equipmentTypeId) {
        List<?> list = waterSupplyDeviceService.getAbnormalDeviceGisPoints(equipmentTypeId);
        return AjaxResult.success(list);
    }

    @GetMapping("/detail/{equipmentId}")
    @ApiOperation("获取设备详情（GIS弹窗用，含实时数据+维修记录）")
    @PreAuthorize("@ss.hasPermi('waterSupply:device:gis')")
    public AjaxResult getDeviceDetail(@PathVariable String equipmentId) {
        DeviceDetailVO detail = waterSupplyDeviceService.getDeviceDetail(equipmentId);
        if (detail == null) {
            return AjaxResult.error("设备不存在");
        }
        return AjaxResult.success(detail);
    }

    @GetMapping("/statistics")
    @ApiOperation("获取GIS概览统计数据")
    @PreAuthorize("@ss.hasPermi('waterSupply:device:gis')")
    public AjaxResult getGisStatistics() {
        List<?> typeStats = waterSupplyDeviceService.getDeviceTypeStatistics();
        List<?> abnormalList = waterSupplyDeviceService.getAbnormalDeviceGisPoints(null);
        Map<String, Object> result = new HashMap<>();
        result.put("typeStatistics", typeStats);
        result.put("abnormalCount", abnormalList != null ? abnormalList.size() : 0);
        return AjaxResult.success(result);
    }
}
