package com.nokia.domainb.capability;

import cn.chinaunicom.open.nlgxptconnection.COMPConnectionContext;
import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import com.nokia.domainb.util.Utils;
import de.siegmar.fastcsv.writer.CsvWriter;
import lombok.extern.slf4j.Slf4j;
import org.junit.jupiter.api.Test;

import java.io.IOException;
import java.io.OutputStreamWriter;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;

/**
 * 已订购产品查询
 * json_microservice_users_foreignproduct
 */
@Slf4j
class ForeignproductTest {
    @Test
    void test() {
        Foreignproduct foreignproduct = new Foreignproduct();
        COMPConnectionContext result = foreignproduct.query("18633367653", "18", false);
        System.out.println(result.getResponseContext());
    }

    @Test
    void testQuery() {
        String filePath = "result/已订购产品查询.csv";
        try {
            Files.createDirectories(Paths.get("result"));
        } catch (IOException e) {
            e.printStackTrace();
            return;
        }
        Set<String> phones = Utils.readPhones();
        try (OutputStreamWriter osw = new OutputStreamWriter(Files.newOutputStream(Paths.get(filePath)), StandardCharsets.UTF_8);
             CsvWriter csv = CsvWriter.builder().build(osw);) {
            csv.writeRow("phone", "productType", "productName");
            for (String t : phones) {
                Map<String, JsonObject> map = new HashMap<>();
                String[] a = t.split(",");
                String phone = a[0];
                String provinceCode = a[1];
                Foreignproduct foreignproduct = new Foreignproduct();
                log.info("phone: {}", phone);
                COMPConnectionContext result = foreignproduct.query(phone, provinceCode, false);
                JsonObject jsonObject = JsonParser.parseString(result.getResponseContext()).getAsJsonObject();
                JsonObject rsp = jsonObject.getAsJsonObject("UNI_BSS_BODY")
                        .getAsJsonObject("FOREIGN_PRODUCT_RSP")
                        .getAsJsonObject("RSP");
                String rspCode = rsp.get("RSP_CODE").getAsString();
                if (!"0000".equals(rspCode)) {
                    continue;
                }
                JsonArray data = rsp.getAsJsonArray("DATA");
                for (JsonElement tt : data) {
                    JsonObject ttt = tt.getAsJsonObject();
                    if ("M".equals(ttt.get("productType").getAsString())) {
                        map.put(phone + ttt.get("productType").getAsString() + ttt.get("productName").getAsString(), ttt);
                    }
                }
                for (JsonObject value : map.values()) {
                    csv.writeRow(phone, value.get("productType").getAsString(), value.get("productName").getAsString());
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
