package com.ruoyi.aspect;

import cn.hutool.core.util.DesensitizedUtil;
import cn.hutool.core.util.ReflectUtil;
import cn.hutool.core.util.StrUtil;
import com.ruoyi.common.core.page.TableDataInfo;
import lombok.RequiredArgsConstructor;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.stereotype.Component;

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

@Aspect
@Component
@RequiredArgsConstructor
public class responseAspect {

//    @Around("execution(* com.ruoyi.business.controller.*.list(..))")
//    public TableDataInfo<Object> around(ProceedingJoinPoint joinPoint) throws Throwable {
//        Object proceed = joinPoint.proceed();
//        TableDataInfo<Object> tableDataInfo = (TableDataInfo<Object>) proceed;
//        List list = (List) tableDataInfo.getRows();
//
//        for (Object o : list) {
//            // 获取list表中类的类型
//            Class<?> aClass = o.getClass();
//
//            // 遍历实体类的字段
//            for (Field field : aClass.getDeclaredFields()) {
//                String fieldName = field.getName();
//                // 判断字段是否为电话号码或身份证号码，并对其进行脱敏
//                if (StrUtil.containsIgnoreCase(fieldName, "idcard")) {
//                    field.setAccessible(true);
//                    String value = String.valueOf(field.get(o));
//                    try {
//                        ReflectUtil.setFieldValue(o, fieldName, DesensitizedUtil.idCardNum(value, 4, 4));
//                    } catch (Exception ignored) {
//                    }
//                }
//
//                if (StrUtil.containsIgnoreCase(fieldName, "phone")) {
//                    field.setAccessible(true);
//                    String value = String.valueOf(field.get(o));
//                    try {
//                        ReflectUtil.setFieldValue(o, fieldName, DesensitizedUtil.mobilePhone(value));
//                    } catch (Exception ignored) {
//                    }
//                }
//            }
//        }
//        tableDataInfo.setRows(list);
//        return tableDataInfo;
//    }
}
