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 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 = "24. 工程概化图")
@RestController
@RequestMapping("/slgc/ght")
public class GhtController extends BaseController {

    private final RestTemplate restTemplate;
    private final ApiProperties apiProperties;

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

    @Operation(summary = "24.01 查询概化图数据")
    @GetMapping("/list")
    public AjaxResult list(@RequestParam(defaultValue = "") String name) {
        try {
            String xml = restTemplate.getForObject(apiProperties.getYjzh() + "/SELECT_GCGHT?name=" + name, String.class);
            org.w3c.dom.Document doc = javax.xml.parsers.DocumentBuilderFactory.newInstance()
                    .newDocumentBuilder().parse(new java.io.ByteArrayInputStream(xml.getBytes("UTF-8")));
            org.w3c.dom.NodeList nodes = (org.w3c.dom.NodeList)
                    javax.xml.xpath.XPathFactory.newInstance().newXPath()
                    .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 success(arr != null ? arr : java.util.Collections.emptyList());
            }
        } catch (Exception ignored) {}
        return success(java.util.Collections.emptyList());
    }
}
