package com.yutou.common.databases;

import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import com.alibaba.fastjson2.JSONWriter;
import com.alibaba.fastjson2.annotation.JSONField;
import com.alibaba.fastjson2.annotation.JSONType;
import lombok.Data;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Set;
@JSONType(ignores  = {"fields", "tableName"})
@Data
public class AbsDatabasesBean {
    String tableName;
    @JSONField(name = "sql_time")
    public Date sql_time;

    private AbsDatabasesBean() {
    }

    public AbsDatabasesBean(String tableName,long saveTimer) {
        this.tableName = tableName;
        this.sql_time = new Date(saveTimer);
    }

    public JSONObject toJson() {
        JSONObject json = JSONObject.parseObject(JSONObject.toJSONString(this, JSONWriter.Feature.WriteMapNullValue));
        json.remove("tableName");
        json.remove("fields");
        json.put("id", "0");
        for (String key : json.keySet()) {
            if(json.get(key) instanceof JSONArray){
                json.put(key,json.getString(key));
            }
            if(json.get(key) instanceof String&& "null".equals(json.getString(key))){
                json.put(key,"");
            }
        }
        //Log.i("创建" + tableName + "表 json:" + json);
        return json;
    }

    public List<Field> getFields() {
        Field[] fields = getClass().getDeclaredFields();
        List<Field> list = new ArrayList<>();
        list.addAll(List.of(fields));
        list.addAll(List.of(getClass().getFields()));
        return list;
    }
}
