package com.zbf.generator.util;

import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;
import com.zbf.common.annotation.Excel;

import java.lang.reflect.Field;

/**
 * 动态加载列表和中文名称
 */
public class ColumnUtils {

    public static JSONArray getColumnsJson(Class<?> clazz) {
        JSONArray columns = new JSONArray();
        Field[] fields = clazz.getDeclaredFields();
        int key = 0;

        for (Field field : fields) {
            Excel excelAnnotation = field.getAnnotation(Excel.class);
            if (excelAnnotation != null) {
                JSONObject column = new JSONObject();
                column.put("seq", key++);
                column.put("key", field.getName());
                column.put("label", excelAnnotation.name());
                column.put("visible", true);  // 假设所有列默认都是可见的
                column.put("columnName", field.getName());  // 设置列ID
                columns.put(column);
            }
        }
        Class<?> superclass = clazz.getSuperclass();
        Field[] declaredFields = superclass.getDeclaredFields();
        for (Field declaredField : declaredFields) {
            Excel excelAnnotation = declaredField.getAnnotation(Excel.class);
            if (excelAnnotation != null) {
                JSONObject column = new JSONObject();
                column.put("seq", key++);
                column.put("key", declaredField.getName());
                column.put("label", excelAnnotation.name());
                column.put("visible", true);  // 假设所有列默认都是可见的
                column.put("columnName", declaredField.getName());  // 设置列ID
                columns.put(column);
            }
        }
        return columns;
    }
}
