package com.goldenwater.slgc.controller.proxy;

import java.util.Collections;
import org.springframework.http.ResponseEntity;
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 org.springframework.web.client.RestTemplate;
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 = "30. 水量数据管理")
@RestController
@RequestMapping("/slgc/water")
public class WaterController extends BaseController {

    private final RestTemplate restTemplate;
    private final ApiProperties apiProperties;

    public WaterController(RestTemplate restTemplate, ApiProperties apiProperties) {
        this.restTemplate = restTemplate;
        this.apiProperties = apiProperties;
    }

    @Operation(summary = "查询闸坝水量数据")
    @GetMapping("/bz/list")
    public AjaxResult bzList(@RequestParam(defaultValue = "") String startDate,
                             @RequestParam(defaultValue = "") String bsnm,
                             @RequestParam(defaultValue = "") String frgrd) {
        try {
            String url = apiProperties.getYjzh() + "/SELECT_BZselect?StartDate=" + startDate
                    + "&BSNM=" + bsnm + "&FRGRD=" + frgrd;
            ResponseEntity<String> resp = restTemplate.getForEntity(url, String.class);
            return success(parseJsonFromSoap(resp.getBody()));
        } catch (Exception e) {
            return success(Collections.emptyList());
        }
    }

    @Operation(summary = "查询工程水量数据")
    @GetMapping("/gc/list")
    public AjaxResult gcList(@RequestParam(defaultValue = "") String startDate,
                             @RequestParam(defaultValue = "") String bsnm,
                             @RequestParam(defaultValue = "") String frgrd) {
        try {
            String url = apiProperties.getYjzh() + "/Select_GC?StartDate=" + startDate
                    + "&BSNM=" + bsnm + "&FRGRD=" + frgrd;
            ResponseEntity<String> resp = restTemplate.getForEntity(url, String.class);
            return success(parseJsonFromSoap(resp.getBody()));
        } catch (Exception e) {
            return success(Collections.emptyList());
        }
    }

    @SuppressWarnings("unchecked")
    private Object parseJsonFromSoap(String xml) {
        if (xml == null || xml.isEmpty()) return Collections.emptyList();
        try {
            javax.xml.parsers.DocumentBuilder db = javax.xml.parsers.DocumentBuilderFactory.newInstance().newDocumentBuilder();
            org.w3c.dom.Document doc = db.parse(new java.io.ByteArrayInputStream(xml.getBytes("UTF-8")));
            javax.xml.xpath.XPath xpath = javax.xml.xpath.XPathFactory.newInstance().newXPath();
            org.w3c.dom.NodeList nodes = (org.w3c.dom.NodeList) xpath.evaluate("//Table/string", doc, javax.xml.xpath.XPathConstants.NODESET);
            if (nodes.getLength() > 0) {
                String json = nodes.item(0).getTextContent().trim();
                com.alibaba.fastjson2.JSONArray arr = com.alibaba.fastjson2.JSONArray.parseArray(json);
                return arr != null ? arr : Collections.emptyList();
            }
        } catch (Exception ignored) {}
        return Collections.emptyList();
    }
}
