package com.example.demo.expand.excel;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.ExcelWriter;
import com.alibaba.excel.support.ExcelTypeEnum;
import com.alibaba.excel.write.merge.AbstractMergeStrategy;
import com.alibaba.excel.write.metadata.WriteSheet;
import com.alibaba.fastjson.JSONObject;
import com.example.demo.SmartBowBusinessServerApplication;
import com.example.demo.common.GlobalConfig;
import com.example.demo.common.GlobalRoot;
import com.example.demo.expand.location.LocationParser;
import pers.jc.util.JCFileTool;

import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

public class ExcelTester {
    public static void main(String[] args) throws Exception {
        GlobalConfig.init();
        GlobalRoot.init();
        new ExcelTester().writeExcel();
    }

    public static List<ExcelUserGameRecord> listUserGameRecordList() throws Exception {
        JCFileTool fileTool = new JCFileTool();
        List<JSONObject> list = GlobalRoot.db.select(fileTool.readStr(new File(getSqlPath())));
        List<ExcelUserGameRecord> exportData = new LinkedList<>();

        String xmlFilePath = new File("").getCanonicalPath()
                + File.separator + "res"
                + File.separator + "location"
                + File.separator + "LocList.xml";
        LocationParser locationParser = new LocationParser(xmlFilePath);

        Map<Integer, String> gameTypeNameMap = new HashMap<>();
        gameTypeNameMap.put(1, "静止靶 (单人)");
        gameTypeNameMap.put(2, "静止靶 (本地PK)");
        gameTypeNameMap.put(3, "兔子关卡 (单人)");
        gameTypeNameMap.put(4, "野鸡关卡 (单人)");
        gameTypeNameMap.put(5, "野狼关卡 (单人)");
        gameTypeNameMap.put(6, "兔子关卡 (本地PK)");
        gameTypeNameMap.put(7, "野鸡关卡 (本地PK)");
        gameTypeNameMap.put(8, "野狼关卡 (本地PK)");
        gameTypeNameMap.put(9, "静止靶 (联机PK)");
        gameTypeNameMap.put(10, "兔子关卡 (联机PK)");
        gameTypeNameMap.put(11, "野鸡关卡 (联机PK)");
        gameTypeNameMap.put(12, "野狼关卡 (联机PK)");
        gameTypeNameMap.put(13, "野鸭关卡 (单人)");
        gameTypeNameMap.put(14, "荒野射击 (单人)");
        gameTypeNameMap.put(15, "水果达人 (单人)");

        list.forEach(jo -> {
//            String area = locationParser.parseNameByCode(
//                    jo.getString("country"), jo.getString("state"), jo.getString("city"));
            ExcelUserGameRecord uo = jo.toJavaObject(ExcelUserGameRecord.class);
//            uo.setArea(area);
            uo.setArea(jo.getString("country") + " " + jo.getString("state") + " " + jo.getString("city"));
            uo.setGameType(gameTypeNameMap.get(Integer.parseInt(uo.getGameType())));
            exportData.add(uo);
        });
        return exportData;
    }

    public void writeExcel() throws Exception {
        String fileName = getPath();
        ExcelWriter excelWriter = EasyExcel.write(fileName).excelType(ExcelTypeEnum.XLSX).build();

        List<ExcelUserGameRecord> dataList = listUserGameRecordList();
        AbstractMergeStrategy ms1 = new MergeStrategyJoinTable<>(
                dataList, 0, 13,
                (now, last) -> {
                    if (now.getId() == last.getId()) {
                        return 1;
                    }
                    return 0;
                }
        );
        AbstractMergeStrategy ms2 = new MergeStrategyJoinTable<>(
                dataList, 13, 22,
                (now, last) -> {
                    if (now.getId() == last.getId() && now.getDate().equals(last.getDate())) {
                        return 1;
                    }
                    return 0;
                }
        );

        WriteSheet writeSheet = EasyExcel.writerSheet("用户游戏记录").head(ExcelUserGameRecord.class)
                .registerWriteHandler(ms1)
                .registerWriteHandler(ms2)
                .build();
        excelWriter.write(dataList, writeSheet);
        excelWriter.finish();
    }

    private String getPath() throws IOException {
        return new File("").getCanonicalPath()
                + File.separator + "TestTable.xlsx";
    }

    private static String getSqlPath() throws IOException {
        return new File("").getCanonicalPath()
                + File.separator + "res"
                + File.separator + "sql"
                + File.separator + "获取用户游戏记录.sql";
    }
}


