package com.common.base.common.utils;


import com.common.base.module.data.module.DataMap;
import org.springframework.util.StringUtils;

import javax.persistence.Id;
import javax.persistence.Table;
import java.lang.reflect.Field;
import java.util.HashMap;
import java.util.Map;

public class ObjectUtil {

    private static final String SERIAL_VERSION_UID = "serialVersionUID";

    public static Map<String, Object> objToMap(Object obj) {
        Map<String, Object> map = new HashMap<String, Object>();
        Class<?> clazz = obj.getClass();
        for (Field field : clazz.getDeclaredFields()) {
            field.setAccessible(true);
            String fieldName = field.getName();
            try {
                if (!StringUtils.isEmpty(field.get(obj)) && !fieldName.equals(SERIAL_VERSION_UID)) {
                    Object value = field.get(obj);
                    map.put(fieldName, value);
                }
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            }
        }
        return map;
    }

    public static<T> void objToSqlMap(Object obj, DataMap<T> dataMap) {
        Map<String, Object> map = new HashMap<String, Object>();
        Class<?> clazz = obj.getClass();
        if (clazz.getAnnotation(Table.class) != null) {
            Table table = clazz.getAnnotation(Table.class);
            dataMap = dataMap.toBuilder().tableName(table.name()).build();
        }
        for (Field field : clazz.getDeclaredFields()) {
            field.setAccessible(true);
            String fieldName = field.getName();
            if (field.getAnnotation(Id.class) != null) {
                dataMap = dataMap.toBuilder().pkName(com.common.base.common.utils.StringUtils.toUnderScoreUpperCase(fieldName)).build();
            }
            try {
                if (!StringUtils.isEmpty(field.get(obj)) && !fieldName.equals(SERIAL_VERSION_UID)) {
                    Object value = field.get(obj);
                    map.put(com.common.base.common.utils.StringUtils.toUnderScoreUpperCase(fieldName), value);
                }
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            }
        }
        dataMap = dataMap.toBuilder().data(map).build();
    }

    public static<T> void objToSqlMap(Object obj, DataMap.DataMapBuilder<T> dataMap) {
        Map<String, Object> map = new HashMap<String, Object>();
        Class<?> clazz = obj.getClass();
        if (clazz.getAnnotation(Table.class) != null) {
            Table table = clazz.getAnnotation(Table.class);
            dataMap = dataMap.tableName(table.name());
        }
        for (Field field : clazz.getDeclaredFields()) {
            field.setAccessible(true);
            String fieldName = field.getName();
            if (field.getAnnotation(Id.class) != null) {
                dataMap = dataMap.pkName(com.common.base.common.utils.StringUtils.toUnderScoreUpperCase(fieldName));
            }
            try {
                if (!StringUtils.isEmpty(field.get(obj)) && !fieldName.equals(SERIAL_VERSION_UID)) {
                    Object value = field.get(obj);
                    map.put(com.common.base.common.utils.StringUtils.toUnderScoreUpperCase(fieldName), value);
                }
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            }
        }
        dataMap.data(map);
    }
}
