package com.cxyxiaomo.epp.common.vo;

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

import java.io.Serializable;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

// 数据库关系映射

@Data
@NoArgsConstructor
@Accessors(chain = true) // 链式写法
// 微服务必须要实现Serializable
public class GateVO implements Serializable {

    private String id;
    private String name;
    private Boolean open;

    public static GateVO convertFrom(Gate gate) {
        if (gate == null) {
            return null;
        }
        GateVO gateVO = new GateVO();
        BeanUtils.copyProperties(gate, gateVO);
        if (Objects.nonNull(gate.getId())) {
            gateVO.setId(String.valueOf(gate.getId()));
        }
        return gateVO;
    }

    public static List<GateVO> convertFrom(List<Gate> gateList) {
        if (gateList == null) {
            return null;
        }
        List<GateVO> gateVOList = gateList.stream()
                .map(GateVO::convertFrom).collect(Collectors.toList());
        return gateVOList;
    }

    public static Gate convertTo(GateVO gateVO) {
        if (gateVO == null) {
            return null;
        }
        Gate gate = new Gate();
        BeanUtils.copyProperties(gateVO, gate);
        try {
            if (Objects.nonNull(gateVO.getId())) {
                Long gateId = Long.valueOf(gateVO.getId());
                gate.setId(gateId);
            } else {
                gate.setId(null);
            }
        } catch (Exception e) {
            gate.setId(null);
        }
        return gate;
    }
}
