package com.jeequan.jeepay.core.annotate.kits;

import cn.hutool.core.bean.BeanUtil;
import com.jeequan.jeepay.core.annotate.ChannelMchId;
import com.jeequan.jeepay.core.constants.ChannelMchIdTypeEnum;
import lombok.extern.slf4j.Slf4j;

import java.lang.reflect.Field;

/**
 * @description: 工具类
 * @author: zx
 * @date: 2023/04/07 12:25
 */
@Slf4j
public class ChannelMchIdKit {

    public static String getChannelMchNo(Object javaBean, ChannelMchIdTypeEnum channelMchIdTypeEnum) {
        if (null == javaBean) {
            return null;
        }

        final String[] channelMchIdArr = { null };

        Class<?> beanClass = javaBean.getClass();
        BeanUtil.descForEach(beanClass, i -> {
            Field field = i.getField();
            Object value = i.getValue(javaBean);
            if (null == value) {
                return;
            }

            ChannelMchId annotation = field.getAnnotation(ChannelMchId.class);
            if (null != annotation && annotation.type() == channelMchIdTypeEnum) {
                channelMchIdArr[0] = (String) value;
            }
        });

        return channelMchIdArr[0];
    }

}
