package com.nokia.domainb.capability;

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.file.Files;
import java.nio.file.Paths;
import java.time.LocalDateTime;
import java.util.Set;

@Slf4j
class QryOweFeeTest {
    @Test
    void testQuery() {
        QryOweFee qryOweFee = new QryOweFee();
        String result = qryOweFee.query("18622305641", "13");
        JsonObject jsonObject = JsonParser.parseString(result).getAsJsonObject();
        JsonObject rsp = jsonObject.getAsJsonObject("UNI_BSS_BODY")
                .getAsJsonObject("QRY_OWE_FEE_RSP")
                .getAsJsonObject("RSP");
//        String rspCode = rsp.get("RSP_CODE").getAsString();
//        if (!"0000".equals(rspCode)) {
//            String rspDesc = rsp.get("RSP_DESC").getAsString();
//            return R.error().message(rspDesc);
//        }

        JsonObject data = rsp.getAsJsonArray("DATA").get(0).getAsJsonObject();
        JsonObject oweFeeInfo = data.getAsJsonObject("OWE_FEE_INFO");
//        if (oweFeeInfo == null) {
//            String detail = data.get("detail").getAsString();
//            return R.error().message(detail);
//        }
        String payFee = oweFeeInfo.get("PAY_FEE").getAsString();
        String balanceFee = oweFeeInfo.get("BALANCE_FEE").getAsString();
        String realFee = oweFeeInfo.get("REAL_FEE").getAsString();
        System.out.println(payFee);
        System.out.println(balanceFee);
        System.out.println(realFee);
    }

    @Test
    void test() {
        QryOweFee qryOweFee = new QryOweFee();
        Set<String> phones = Utils.readPhones();
        String filePath = "result/欠费查询接口1000条数据测试结果.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];
                LocalDateTime startTime = LocalDateTime.now();
                long startTimestamp = System.currentTimeMillis();
                String result = qryOweFee.query(phone, provinceCode);
                long endTimestamp = System.currentTimeMillis();
                long cost = endTimestamp - startTimestamp;
                csv.writeRow(phone, startTime.toString(), String.valueOf(cost), result);
            });
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
