package com.nokia.domainb.capability;

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

import java.io.IOException;
import java.io.OutputStreamWriter;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.time.LocalDateTime;
import java.util.Set;

public class QryUserProdMsgTest {
    @Test
    void testQuery() {
        QryUserProdMsg qryUserProdMsg = new QryUserProdMsg();
//        COMPConnectionContext result = qryUserProdMsg.query("18503369666", "18", "0335", false);
//        System.out.println(result.getResponseContext());
        Set<String> phones = Utils.readPhones();
//        String filePath = "result/查询用户产品信息样本.csv";
        String filePath = "result/查询用户产品信息.csv";
        try (OutputStreamWriter osw = new OutputStreamWriter(Files.newOutputStream(Paths.get(filePath)), "GBK");
             CsvWriter csv = CsvWriter.builder().build(osw);) {
            csv.writeRow("手机号", "时间", "调用时长", "结果");
            phones.forEach(t -> {
                String[] a = t.split(",");
                String phone = a[0];
                String provinceCode = a[1];
                String eparchyCode = a[2];
                LocalDateTime startTime = LocalDateTime.now();
                long startTimestamp = System.currentTimeMillis();
                COMPConnectionContext result = qryUserProdMsg.query(phone, provinceCode, eparchyCode, false);
                long endTimestamp = System.currentTimeMillis();
                long cost = endTimestamp - startTimestamp;
                JsonObject jsonObject = JsonParser.parseString(result.getResponseContext()).getAsJsonObject();
                JsonArray data = jsonObject.getAsJsonObject("UNI_BSS_BODY")
                        .getAsJsonObject("QRYUSERPRODMSG_RSP")
                        .getAsJsonObject("RSP")
                        .getAsJsonArray("DATA");
                csv.writeRow(phone, startTime.toString(), String.valueOf(cost), data.toString());
            });
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
