package com.nokia.domainb.capability;

import com.alibaba.fastjson2.JSON;
import com.nokia.domainb.util.AESUtil;
import de.siegmar.fastcsv.reader.CsvReader;
import de.siegmar.fastcsv.reader.CsvRow;
import de.siegmar.fastcsv.writer.CsvWriter;
import lombok.extern.slf4j.Slf4j;
import org.junit.jupiter.api.Test;
import org.springframework.util.StopWatch;

import java.io.OutputStreamWriter;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.HashMap;
import java.util.Map;

@Slf4j
class UsercomplaintQuerysTest {
    public static final String TEST_DIR = "z:/";

    @Test
    void test() {
        String d1 = TEST_DIR + "103.csv";
        String d2 = TEST_DIR + "101.csv";
        String result1 = TEST_DIR + "无法发送测试结果.csv";
        String result2 = TEST_DIR + "无法接收测试结果.csv";
        try (CsvReader r1 = CsvReader.builder().build(Paths.get(d1), StandardCharsets.UTF_8);
                CsvReader r2 = CsvReader.builder().build(Paths.get(d2), StandardCharsets.UTF_8);
                OutputStreamWriter osw1 = new OutputStreamWriter(Files.newOutputStream(Paths.get(result1)),
                        StandardCharsets.UTF_8);
                CsvWriter w1 = CsvWriter.builder().build(osw1);
                OutputStreamWriter osw2 = new OutputStreamWriter(Files.newOutputStream(Paths.get(result2)),
                        StandardCharsets.UTF_8);
                CsvWriter w2 = CsvWriter.builder().build(osw2);) {
            w1.writeRow("入参", "时间", "调用时长", "结果");
            for (CsvRow row : r1) {
                if (row.getOriginalLineNumber() == 1) {
                    continue;
                }

                String mobile = row.getField(1);
                LocalDateTime beginTimeLocalDateTime = getLocalDateTime(row.getField(2));
                String beginTime = getTimeString(beginTimeLocalDateTime);
                // String endTime = "";
                String endTime = getTimeString(beginTimeLocalDateTime.plusDays(3));
                String cKey = "qg4uY8nV7fPhvUXt";
                String cSrc = getTimeString() + "_" + "test";
                String token = AESUtil.Encrypt(cSrc, cKey);
                String complaintType = "1";
                String isMobile = "";
                String flowNo = "103";
                String destAddr = "";
                Map<String, Object> map = new HashMap<>();
                map.put("mobile", mobile);
                map.put("destAddr", destAddr);
                map.put("complaintType", complaintType);
                map.put("isMobile", isMobile);
                map.put("flowNo", flowNo);
                map.put("beginTime", beginTime);
                map.put("endTime", endTime);
                map.put("token", token);
                UsercomplaintQuerys usercomplaintQuerys = new UsercomplaintQuerys();
                String result;
                String now = LocalDateTime.now().toString();
                StopWatch stopWatch = new StopWatch();
                stopWatch.start();
                try {
                    result = usercomplaintQuerys.query(mobile, destAddr, complaintType, isMobile, flowNo, beginTime,
                            endTime, token, false);
                } catch (Exception e) {
                    result = e.getMessage();
                }
                stopWatch.stop();
                log.info("{} | {} ms | {}", mobile, stopWatch.getTotalTimeMillis(), result);
                w1.writeRow(JSON.toJSONString(map), now, String.valueOf(stopWatch.getTotalTimeMillis()),
                        JSON.toJSONString(result));
            }

            w2.writeRow("入参", "时间", "调用时长", "结果");
            for (CsvRow row : r2) {
                if (row.getOriginalLineNumber() == 1) {
                    continue;
                }

                String mobile = row.getField(1);
                LocalDateTime beginTimeLocalDateTime = getLocalDateTime(row.getField(2));
                String beginTime = getTimeString(beginTimeLocalDateTime);
                // String endTime = "";
                String endTime = getTimeString(beginTimeLocalDateTime.plusDays(3));
                String cKey = "qg4uY8nV7fPhvUXt";
                String cSrc = getTimeString() + "_" + "test";
                String token = AESUtil.Encrypt(cSrc, cKey);
                String complaintType = "2";
                String isMobile = "";
                String flowNo = "101";
                String destAddr = "";
                Map<String, Object> map = new HashMap<>();
                map.put("mobile", mobile);
                map.put("destAddr", destAddr);
                map.put("complaintType", complaintType);
                map.put("isMobile", isMobile);
                map.put("flowNo", flowNo);
                map.put("beginTime", beginTime);
                map.put("endTime", endTime);
                map.put("token", token);
                UsercomplaintQuerys usercomplaintQuerys = new UsercomplaintQuerys();
                String result;
                String now = LocalDateTime.now().toString();
                StopWatch stopWatch = new StopWatch();
                stopWatch.start();
                try {
                    result = usercomplaintQuerys.query(mobile, destAddr, complaintType, isMobile, flowNo, beginTime,
                            endTime, token, false);
                } catch (Exception e) {
                    result = e.getMessage();
                }
                stopWatch.stop();
                log.info("{} | {} ms | {}", mobile, stopWatch.getTotalTimeMillis(), result);
                w2.writeRow(JSON.toJSONString(map), now, String.valueOf(stopWatch.getTotalTimeMillis()),
                        JSON.toJSONString(result));
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    // 单例测试
    @Test
    void testQuery() throws Exception {
        // 统一使用，不需要修改
        String cKey = "qg4uY8nV7fPhvUXt";
        String cSrc = getTimeString() + "_" + "test";
        String token = AESUtil.Encrypt(cSrc, cKey);
        // 单例测试时修改参数
        String mobile = "15132136436";
        String destAddr = "18631612746";
        // 所有接收101 指定接收102 所有发送 103 指定发送104
        String flowNo = "103";
        String beginTime = "20220927000000";
        String endTime = "20220930235959";
        String complaintType = "1";
        // 可以统一传空
        String isMobile = "";

        UsercomplaintQuerys usercomplaintQuerys = new UsercomplaintQuerys();
        StopWatch stopWatch = new StopWatch();
        stopWatch.start();
        String result = usercomplaintQuerys.query(mobile, destAddr, complaintType, isMobile, flowNo, beginTime,
                endTime, token, false);
        stopWatch.stop();
        log.info("{} | {} ms | {}", mobile, stopWatch.getTotalTimeMillis(), result);
    }

    private String getTimeString(LocalDateTime time) {
        return time.format(DateTimeFormatter.ofPattern("yyyyMMddHHmmss"));
    }

    private String getTimeString() {
        return LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyyMMddHHmmss"));
    }

    private LocalDateTime getLocalDateTime(String time) {
        return LocalDateTime.parse(time, DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
    }
}
