package com.ytpm.middle.util;


import cn.hutool.core.util.StrUtil;
import com.ytpm.annotation.BeanChange;
import lombok.extern.slf4j.Slf4j;

import java.lang.reflect.Field;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Objects;

/**
 * @author lih
 * @date 2025-11-03 15:55
 */
@Slf4j
public class BeanChangeUtil<T> {


    /**
     * 比较两个实体对象中被@BeanChange注解标记的属性变化
     *
     * @param oldBean 旧对象
     * @param newBean 新对象
     * @return 变化描述字符串
     */
    public static <T> String compareBeans(T oldBean, T newBean) {
        if (oldBean == null || newBean == null) {
            return "";
        }
        if (!oldBean.getClass().equals(newBean.getClass())) {
            throw new IllegalArgumentException("两个对象类型不一致");
        }
        StringBuilder changeLog = new StringBuilder();
        Class<?> clazz = oldBean.getClass();
        Field[] fields = clazz.getDeclaredFields();

        // 遍历所有字段
        for (Field field : fields) {
            BeanChange annotation = field.getAnnotation(BeanChange.class);
            if (annotation == null) {
                continue;
            }
            field.setAccessible(true);
            try {
                Object oldValue = field.get(oldBean);
                Object newValue = field.get(newBean);

                // 比较值是否发生变化
                if (!Objects.equals(oldValue, newValue)) {
                    String fieldName = annotation.name();
                    String formattedOldValue = formatValue(oldValue, annotation);
                    String formattedNewValue = formatValue(newValue, annotation);

                    // 拼接变化描述
                    if (changeLog.length() > 0) {
                        changeLog.append(" ");
                    }
                    changeLog.append(StrUtil.format("{}由'{}'修改为'{}';", fieldName, formattedOldValue, formattedNewValue));
                }
            } catch (IllegalAccessException e) {
                // 记录访问异常，继续处理其他字段
                System.err.println("访问字段失败: " + field.getName() + ", 错误: " + e.getMessage());
            }
        }

        return changeLog.toString();
    }


    /**
     * 格式化属性值
     *
     * @param value      属性值
     * @return 格式化后的字符串
     */
    private static String formatValue(Object value, BeanChange annotation) {
        if (value == null) {
            return "空";
        }
        // 处理Date类型
        if (value instanceof Date && StrUtil.isNotEmpty(annotation.dateFormat())) {
            try {
                SimpleDateFormat sdf = new SimpleDateFormat(annotation.dateFormat());
                return sdf.format((Date) value);
            } catch (Exception e) {
                return value.toString(); // 格式化失败返回原始字符串
            }
        }
        if (value instanceof Integer && annotation.ifYesNo()) {
            switch ((Integer)value) {
                case 0:
                    return "否";
                case 1:
                    return "是";
                default:
                    return value.toString();
            }
        }
        return value.toString();
    }
}
