package com.cxyxiaomo.epp.common.vo;

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

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

// 数据库关系映射

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

    private Integer id;

    private Integer userId;

    private String name;

    private String address;

    private Long timestamp;

    private Integer temperature;

    public static ReportVO convertFrom(Report report) {
        if (report == null) {
            return null;
        }
        ReportVO reportVO = new ReportVO();
        BeanUtils.copyProperties(report, reportVO);
        Date time = report.getTime();
        reportVO.setTimestamp(time.getTime());
        return reportVO;
    }

    public static List<ReportVO> convertFrom(List<Report> reportList) {
        if (reportList == null) {
            return null;
        }
        List<ReportVO> reportVOList = reportList.stream()
                .map(ReportVO::convertFrom).collect(Collectors.toList());
        return reportVOList;
    }

    public static Report convertTo(ReportVO reportVO) {
        if (reportVO == null) {
            return null;
        }
        Report report = new Report();
        BeanUtils.copyProperties(reportVO, report);
        Long timestamp = reportVO.getTimestamp();
        if (timestamp != null) {
            Date date = new Date(timestamp);
            report.setTime(date);
        }
        return report;
    }
}
