package com.yc.ship.module.trade.framework.common;

import com.yc.ship.framework.common.exception.ServiceException;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.springframework.util.ReflectionUtils;

import java.util.HashMap;
import java.util.Map;

/**
 * &#064;Description ：TODO
 * &#064;Author ：qsl
 * &#064;Date ：2025/1/31 12:49
 */
@Slf4j
public class ExceptionUtils {

    public static void log(String message,Exception e){
        if(e instanceof ServiceException){
            log.error("[{}],code:{},message:{}",message,((ServiceException) e).getCode(),e.getMessage());
        }else {
            log.error(message, e);
        }
    }

    public static void log(Logger logger, String message,Exception e){
        if(e instanceof ServiceException){
            logger.error("[{}],code:{},message:{}",message,((ServiceException) e).getCode(),e.getMessage());
        }else {
            logger.error(message, e);
        }
    }

    public static Map<String, Object> bean2Map(Object object) {
        Map<String, Object> map = new HashMap<>();
        ReflectionUtils.doWithFields(object.getClass(), field -> {
            field.setAccessible(true);
            Object value = ReflectionUtils.getField(field, object);
            if (value != null) {
                map.put(field.getName(), value);
            }
        });
        return map;
    }
}
