package com.nokia.sms;

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.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.util.CollectionUtils;

import java.io.OutputStreamWriter;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import java.util.stream.Stream;

@Slf4j
@SpringBootTest
class SyncTest {
    @Autowired
    private JdbcTemplate jdbcTemplate;

    @Test
    void test() {
        Map<String, Map<String, String>> m1 = new HashMap<>();
        Map<String, Map<String, String>> m2 = new HashMap<>();
        String p1 = "z:/blacklist_202208231737.csv";
        String p2 = "z:/data1.csv";
        String result = "z:/result2.csv";
        try {
            try (CsvReader csvReader = CsvReader.builder().build(Paths.get(p2), StandardCharsets.UTF_8)) {
                for (CsvRow row : csvReader) {
                    m1.put(row.getField(0), new HashMap<String, String>() {{
                        put("phone", row.getField(0));
                        put("reason", row.getField(1));
                        put("suggestion", row.getField(2));
                    }});
                }
            }
            try (CsvReader csvReader = CsvReader.builder().build(Paths.get(p1), StandardCharsets.UTF_8)) {
                for (CsvRow row : csvReader) {
                    m2.put(row.getField(0), new HashMap<String, String>() {{
                        put("phone", row.getField(0));
                        put("reason", row.getField(1));
                        put("start_time", row.getField(2));
                        put("end_time", row.getField(3));
                        put("hit_desc", row.getField(4));
                        put("operation_type", row.getField(5));
                        put("suggestion", row.getField(6));
                    }});
                }
            }
            try (OutputStreamWriter osw = new OutputStreamWriter(Files.newOutputStream(Paths.get(result)),
                    StandardCharsets.UTF_8);
                 CsvWriter csvWriter = CsvWriter.builder().build(osw);) {
                csvWriter.writeRow("黑名单号码", "入黑名单原因", "解黑建议", "是否一致", "入黑名单原因", "解黑建议", "开始时间", "结束时间", "命中策略说明", "操作类型");
                for (Map<String, String> v2 : m2.values()) {
                    String phone = v2.get("phone");
                    Map<String, String> v1 = m1.getOrDefault(phone, new HashMap<>());
                    String flag = "是";
                    if (!v2.get("reason").equals(v1.get("reason")) || !v2.get("suggestion").equals(v1.get("suggestion"))) {
                        flag = "否";
                        if (CollectionUtils.isEmpty(v1) && v2.get("operation_type").equals("1")) {
                            System.out.println(">>>>>>>>>>" + phone);
                        }
                    }
                    csvWriter.writeRow(phone, v1.get("reason"), v1.get("suggestion"), flag, v2.get("reason"), v2.get("suggestion"), v2.get("start_time"), v2.get("end_time"), v2.get("hit_desc"), v2.get("operation_type"));
                }
//                for (Map<String, String> v1 : m1.values()) {
//                    String phone = v1.get("phone");
//                    Map<String, String> v2 = m2.getOrDefault(phone, new HashMap<>());
//                    String flag = "是";
//                    if (!v1.get("reason").equals(v2.get("reason")) || !v1.get("suggestion").equals(v2.get("suggestion"))) {
//                        flag = "否";
//                    }
//                    csvWriter.writeRow(phone, v1.get("reason"), v1.get("suggestion"), flag, v2.get("reason"), v2.get("suggestion"), v2.get("start_time"), v2.get("end_time"), v2.get("hit_desc"), v2.get("operation_type"));
//                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 测试导入数据
     */
    @Test
    void testImport() {
        String path = "z:/download";
        // 读取目标文件夹下的文件
        try (Stream<Path> stream = Files.list(Paths.get(path))) {
            List<Path> pathList = stream.collect(Collectors.toList());
            log.info("pathList: {}", pathList);
            for (Path t : pathList) {
                List<Object[]> l1 = new ArrayList<>();
                List<Object[]> l2 = new ArrayList<>();
                log.info("file: {}", t.getFileName().toString());
                try (CsvReader csvReader = CsvReader.builder().build(t, StandardCharsets.UTF_8)) {
                    for (CsvRow row : csvReader) {
                        Object[] o1 = new Object[] {
                                row.getField(0),
                                Integer.valueOf(row.getField(1)),
                                row.getField(2),
                                row.getField(3),
                                row.getField(4),
                                Integer.valueOf(row.getField(5)),
                                Integer.valueOf(row.getField(6)),
                        };
                        l1.add(o1);
                        Object[] o2 = new Object[] {
                                row.getField(0),
                                Integer.valueOf(row.getField(1)),
                                row.getField(2),
                                row.getField(3),
                                row.getField(4),
                                Integer.valueOf(row.getField(5)),
                                Integer.valueOf(row.getField(6)),
                                t.getFileName().toString()
                        };
                        l2.add(o2);
                    }
                }
                log.info("blacklist");
                String sql = "insert into sms_blk.blacklist " +
                        "(phone, reason, start_time, end_time, hit_desc, operation_type, suggestion) " +
                        "values (?, ?, ?, ?, ?, ?, ?) " +
                        "on conflict (phone) " +
                        "do update set (reason, start_time, end_time, hit_desc, operation_type, suggestion) = " +
                        "(excluded.reason, excluded.start_time, excluded.end_time, excluded.hit_desc, " +
                        "excluded.operation_type, excluded.suggestion)";
                jdbcTemplate.batchUpdate(sql, l1);
                log.info("blacklist_file");
                sql = "insert into sms_blk.blacklist_file " +
                        "(phone, reason, start_time, end_time, hit_desc, operation_type, suggestion, filename) " +
                        "values (?, ?, ?, ?, ?, ?, ?, ?)";
                jdbcTemplate.batchUpdate(sql, l2);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
