package com.dayou.report.format;

import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.dayou.report.calculation.cityPrice.bo.BusinessCenterBaseBO;
import com.dayou.report.calculation.cityPrice.bo.BusinessCenterIndexWeightBO;
import com.dayou.report.dto.*;
import com.dayou.report.enums.TableNoEnum;
import com.dayou.report.html.DataToHtmlBO;
import com.google.common.collect.Lists;
import org.apache.poi.ss.formula.functions.T;

import java.util.List;

/**
 * 类说明：
 *
 * @author: wucl
 * @since: 2023/9/25
 * created with IntelliJ IDEA.
 */
public class CalculateDataFormatUtil {

    public static InputData calculateJsonStringFormat(String dataString){
        JSONObject jsonObject = JSONObject.parseObject(dataString, JSONObject.class);
        JSONArray tables = (JSONArray) jsonObject.get("inputTableData");
        JSONArray jsonArray = jsonObject.getJSONArray("params");
        List<Params> params = jsonArray.toJavaList(Params.class);
        Long sectionId = Long.valueOf(String.valueOf(jsonObject.get("sectionId")));
        InputData inputData = InputData.builder().params(params).sectionId(sectionId).build();
        List<InputTableData> tableDataList = Lists.newArrayList();
        try {
            for (Object data : tables){
                InputTableData tableData = new InputTableData();
                JSONObject d = (JSONObject) data;
                String tableNo = (String) d.get("tableNo");
                String name = TableNoEnum.getClassByTableNo(tableNo);
                List<? extends DataToHtmlBO> data1 = (List<? extends DataToHtmlBO>) d.getJSONArray("tableData").toJavaList(Class.forName(name));
                List<TableHead> tableHeads = (List<TableHead>) d.get("tableHead");
                tableData.setTableNo(tableNo);
                tableData.setTableData(data1);
                tableData.setTableHead(tableHeads);
                tableData.setTableName((String) d.get("tableName"));
                tableDataList.add(tableData);
            }
            inputData.setInputTableData(tableDataList);
        } catch (ClassNotFoundException e) {
            throw new RuntimeException(e);
        }
        return inputData;
    }
}
