package com.ruoyi.cms.util;

import com.ruoyi.cms.domain.UserInfoDetail;
import com.ruoyi.common.core.domain.entity.AppUser;
import com.ruoyi.common.utils.StringUtils;

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

public class AppUserFieldCustomCopy {

    private boolean isNull(Object o){
        if(Objects.isNull(o)|| StringUtils.isEmpty(o.toString())){
            return true;
        }else{
            return false;
        }
    }

    public AppUser conditionalCopy(AppUser appUser, UserInfoDetail detail) throws IllegalAccessException {
        Field[] fields = appUser.getClass().getDeclaredFields();
        Field sourceField = null;
        Object sourceFieldValue = null;
        for (Field field : fields) {
            field.setAccessible(true);
            if (!isNull(field.get(appUser))){
                continue;
            }
            try {
                sourceField = detail.getClass().getDeclaredField(field.getName());
            }catch (Exception e){
                continue;
            }
            sourceField.setAccessible(true);
            sourceFieldValue = sourceField.get(detail);
            field.set(appUser, sourceFieldValue);
        }
        return appUser;
    }
}
