package com.goldenwater.slgc.controller.proxy;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import com.goldenwater.common.core.controller.BaseController;
import com.goldenwater.common.core.domain.AjaxResult;
import com.goldenwater.slgc.config.ApiProperties;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;

@Tag(name = "23. 水文信息")
@RestController
@RequestMapping("/slgc/hydrology")
public class HydrologyController extends BaseController {

    private final ApiProperties apiProperties;

    public HydrologyController(ApiProperties apiProperties) {
        this.apiProperties = apiProperties;
    }

    @Operation(summary = "23.01 查询站点列表")
    @GetMapping("/stations")
    public AjaxResult stations() {
        return success(ProxyHelper.fetchJson(apiProperties.getHydrology() + "/getStation"));
    }

    @Operation(summary = "23.02 查询水位数据")
    @GetMapping("/level")
    public AjaxResult level(@RequestParam(defaultValue = "") String rtucd,
                            @RequestParam(defaultValue = "") String startTime,
                            @RequestParam(defaultValue = "") String endTime) {
        return success(ProxyHelper.fetchJson(apiProperties.getHydrology() + "/c0d1/list?rtucd=" + rtucd + "&startTime=" + startTime + "&endTime=" + endTime));
    }

    @Operation(summary = "23.03 查询流量数据")
    @GetMapping("/flow")
    public AjaxResult flow(@RequestParam(defaultValue = "") String rtucd,
                           @RequestParam(defaultValue = "") String startTime,
                           @RequestParam(defaultValue = "") String endTime) {
        return success(ProxyHelper.fetchJson(apiProperties.getHydrology() + "/c0d2/list?rtucd=" + rtucd + "&startTime=" + startTime + "&endTime=" + endTime));
    }

    @Operation(summary = "23.04 查询实时监测数据")
    @GetMapping("/realtime")
    public AjaxResult realtime(@RequestParam(defaultValue = "") String rtucd,
                               @RequestParam(defaultValue = "") String startTime,
                               @RequestParam(defaultValue = "") String endTime) {
        // 支持按站码 rtucd 过滤，与前端 handleStationDetail() 传参一致
        String url = apiProperties.getHydrology() + "/swflow/listForTable";
        if (!rtucd.isEmpty()) {
            url = apiProperties.getHydrology() + "/swflow/historyList?rtucd=" + rtucd
                    + "&startTime=" + startTime + "&endTime=" + endTime;
        }
        return success(ProxyHelper.fetchJson(url));
    }

    @Operation(summary = "23.05 查询水文综合列表数据")
    @GetMapping("/list")
    public AjaxResult list(@RequestParam(defaultValue = "") String rtucd,
                           @RequestParam(defaultValue = "") String dataType,
                           @RequestParam(defaultValue = "") String startTime,
                           @RequestParam(defaultValue = "") String endTime) {
        return success(ProxyHelper.fetchJson(apiProperties.getHydrology() + "/list?rtucd=" + rtucd + "&dataType=" + dataType + "&startTime=" + startTime + "&endTime=" + endTime));
    }

    @Operation(summary = "23.06 查询水质数据")
    @GetMapping("/waterQuality")
    public AjaxResult waterQuality(@RequestParam(defaultValue = "") String rtucd,
                                   @RequestParam(defaultValue = "") String startTime,
                                   @RequestParam(defaultValue = "") String endTime) {
        return success(ProxyHelper.fetchJson(apiProperties.getHydrology() + "/waterQuality/list?rtucd=" + rtucd + "&startTime=" + startTime + "&endTime=" + endTime));
    }

    @Operation(summary = "23.07 查询工情数据")
    @GetMapping("/engineering")
    public AjaxResult engineering(@RequestParam(defaultValue = "") String type,
                                  @RequestParam(defaultValue = "") String startTime,
                                  @RequestParam(defaultValue = "") String endTime) {
        return success(ProxyHelper.fetchJson(apiProperties.getHydrology() + "/engineering/list?type=" + type + "&startTime=" + startTime + "&endTime=" + endTime));
    }
}
