package com.ningdatech.carapi.sys.convert;

import java.util.List;
import java.util.Objects;

import com.ningdatech.carapi.sys.entity.dto.UserQueryDTO;
import com.ningdatech.carapi.sys.entity.vo.UserInfoVO;
import com.ningdatech.carapi.user.entity.NdUserAuth;

import cn.hutool.core.collection.CollUtil;

/**
 * @Classname UserAuthConverter
 * @Description
 * @Date 2022/10/9 10:30
 * @Created by PoffyZhang
 */
public class UserAuthConverter {

    public static UserInfoVO convert(UserInfoVO user, List<NdUserAuth> auths) {
        if(Objects.isNull(user) || Objects.isNull(user.getId())){
            return user;
        }
        if(CollUtil.isNotEmpty(auths)){
            auths.forEach(auth -> {
                if(user.getId().equals(auth.getUserId()) && Boolean.TRUE.equals(auth.getIsAvailable())){
                    user.setAccount(auth.getIdentifier());
                }
            });
        }
        return user;
    }

    public static UserQueryDTO convert(UserQueryDTO user, List<NdUserAuth> auths) {
        if(Objects.isNull(user) || Objects.isNull(user.getId())){
            return user;
        }
        if(CollUtil.isNotEmpty(auths)){
            auths.forEach(auth -> {
                if(user.getId().equals(auth.getUserId())){
                    user.setAccount(auth.getIdentifier());
                    user.setPassword(auth.getCredential());
                }
            });
        }
        return user;
    }
}
