package com.test;

import cn.hutool.core.io.FileUtil;
import cn.hutool.core.lang.Console;
import cn.hutool.http.Header;
import cn.hutool.http.HttpRequest;
import cn.hutool.poi.excel.ExcelUtil;
import cn.hutool.poi.excel.ExcelWriter;
import com.alibaba.fastjson.JSONObject;

import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * @author sangkuofeng
 * @date 2024/12/6 16:45
 */
public class GetDictItem {

    public static final String URL = "http://49.77.204.37:39081/service/bsp/dict/queryDictItemsPaged";
    public static final String COOKIE = "idp_ls=U1FfWktfSkZERkNCX0FETUlO; idp_ls_un=U1FfWktfSkZERkNCX0FETUlO; ss_ctrl=ae1ad89381fc8701939b366c2103cd; sso_token=Ci6aWCN5Z1K-D68b9gmCXUNBnJ_MT17qUQcA; JSESSIONID=Ci6aWCN5Z1K-D68b9gmCXUNBnJ_MT17qUQcA";

    public static final String CATEGORY_DATA = "/Users/sangkuofeng/Documents/IdeaProjects/dict_get/src/main/resources/dictKey.json";

    public static final String TARGET_PATH = "/Users/sangkuofeng/Documents/IdeaProjects/dict_get/src/main/resources/";

    public static void main(String[] args) {
        // 加载字典
        KeyResult keyResult = JSONObject.parseObject(FileUtil.readString(CATEGORY_DATA, StandardCharsets.UTF_8), KeyResult.class);

        for (KeyResult.DataDto category : keyResult.getData()) {
            Console.log(category.getDictCode(), category.getDictName());
            getDictResult(category);
            break;
        }

    }

    private static void getDictResult(KeyResult.DataDto category) {
        HashMap<String, Object> paramMap = new HashMap<>();
        paramMap.put("dictCode", category.getDictCode());
        paramMap.put("parentCode", null);
        paramMap.put("start", 0);
        paramMap.put("limit", 50);
        paramMap.put("dataTotal", "true");
        paramMap.put("dictItemName", "");

        //链式构建请求
        String result2 = HttpRequest.post(URL)
                .header(Header.COOKIE, COOKIE)//头信息，多个头信息多次调用此方法即可
                .header(Header.CONTENT_TYPE, "application/json")
                .body(JSONObject.toJSONString(paramMap))
                .timeout(20000)//超时，毫秒
                .execute().body();
        // 解析字典结果
        ItemResult itemResult = JSONObject.parseObject(result2, ItemResult.class);
        // 导出Excel

        for (ItemResult.DataDto datum : itemResult.getData()) {
            Console.log(datum.getXh(), datum.getItemType(), datum.getItemValue());
        }
        writeExcel(category, itemResult.getData());
    }


    /**
     * 生成Excel
     * @param category
     * @param items
     */
    private static void writeExcel(KeyResult.DataDto category, List<ItemResult.DataDto> items) {
        // 通过工具类创建writer
        ExcelWriter writer = ExcelUtil.getWriter(TARGET_PATH + "excel/" + category.getDictCode() + "_" + category.getDictName() + ".xlsx");
        // 合并单元格后的标题行，使用默认标题样式
        writer.merge(4, category.getDictName());
        // 一次性写出内容，使用默认样式，强制输出标题
        writer.write(items, true);
        // 关闭writer，释放内存
        writer.close();
    }

    /**
     * 生成SQL
     * @param category
     * @param items
     */
    private static void writeSQL(KeyResult.DataDto category, List<ItemResult.DataDto> items) {
        List <String> list = new ArrayList<>();

        list.add("");

    }


}
