package com.yutou.biliapi.bean.login;

import com.alibaba.fastjson2.JSONObject;
import com.alibaba.fastjson2.util.DateUtils;
import com.yutou.biliapi.bean.user.UserInfoBean;
import com.yutou.common.databases.AbsDatabasesBean;
import lombok.Data;
import lombok.EqualsAndHashCode;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;

@EqualsAndHashCode(callSuper = true)
@Data
public class LoginUserDatabaseBean extends AbsDatabasesBean {
    UserInfoBean userInfo;

    public LoginUserDatabaseBean(UserInfoBean userInfo) {
        super("login_user",System.currentTimeMillis());
        this.userInfo=userInfo;
    }

    @Override
    public JSONObject toJson() {
        if(userInfo==null){
            return new JSONObject();
        }
        JSONObject json= JSONObject.parseObject(JSONObject.toJSONString(userInfo));
        json.put("sql_time", DateUtils.format(getSql_time(), "yyyy-MM-dd HH:mm:ss.SSS"));
        return json;
    }

    @Override
    public List<Field> getFields() {
        List<Field> fields = new ArrayList<>(List.of(UserInfoBean.class.getDeclaredFields()));
        for (Field field : super.getFields()) {
            if(!field.getName().contains("userInfo")){
                fields.add(field);
            }
        }
        return fields;
    }
}
