package com.cxyxiaomo.epp.common.vo;

import com.cxyxiaomo.epp.common.pojo.User;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.experimental.Accessors;
import org.springframework.beans.BeanUtils;

import java.io.Serializable;
import java.time.LocalDateTime;
import java.util.List;
import java.util.stream.Collectors;

// 数据库关系映射

@Data
@NoArgsConstructor
@Accessors(chain = true) // 链式写法
// 微服务必须要实现Serializable
public class UserVO implements Serializable {
    private Integer id;
    private String username;
    private String realname;
    private String idNumber;
    private String phoneNumber;
    private Integer roleId;
    private String buildingId;
    private String doorplate;
    private String permission;
    private LocalDateTime permissionTime;

    public static UserVO convertFrom(User user) {
        if (user == null) {
            return null;
        }
        UserVO userVO = new UserVO();
        BeanUtils.copyProperties(user, userVO);
        return userVO;
    }

    public static List<UserVO> convertFrom(List<User> userList) {
        if (userList == null) {
            return null;
        }
        List<UserVO> userVOList = userList.stream().map(UserVO::convertFrom).collect(Collectors.toList());
        return userVOList;
    }

    public static User convertTo(UserVO userVO) {
        if (userVO == null) {
            return null;
        }
        User user = new User();
        BeanUtils.copyProperties(userVO, user);
        return user;
    }
}
