package co.yixiang.yshop.framework.desensitize.core.util;

import cn.hutool.core.util.ReflectUtil;
import co.yixiang.yshop.framework.desensitize.core.slider.annotation.MobileDesensitize;
import co.yixiang.yshop.framework.desensitize.core.slider.handler.MobileDesensitization;

import java.lang.annotation.Annotation;
import java.util.List;

/**
 * @author pepis
 * @apiNote
 **/
public class DesensitizeUtil {
    public static <T> void mobileDesensitize(List<T> voList, String fieldName) {
        MobileDesensitization mobileDesensitize = new MobileDesensitization();

        MobileDesensitize desensitize = new MobileDesensitize() {

            @Override
            public Class<? extends Annotation> annotationType() {
                return MobileDesensitize.class;
            }

            @Override
            public int prefixKeep() {
                return 3;
            }

            @Override
            public int suffixKeep() {
                return 4;
            }

            @Override
            public String replacer() {
                return "*";
            }
        };
        voList.forEach(vo -> {
            Object value = ReflectUtil.getFieldValue(vo, fieldName);
            if (value != null) {
                String desensitized = mobileDesensitize.desensitize((String) value, desensitize);
                ReflectUtil.setFieldValue(vo, fieldName, desensitized);
            }
        });
    }
}
