package com.cxyxiaomo.epp.common.vo;

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

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

// 数据库关系映射

@Data
@NoArgsConstructor
@Accessors(chain = true) // 链式写法
// 微服务必须要实现Serializable
public class OrderVO implements Serializable {
    private String id;
    private Integer userId;
    private LocalDateTime orderDate;
    private String orderStatus;
    private String orderStatusCode;
    private String orderPrice;
    private LocalDateTime payDate;

    public static OrderVO convertFrom(Order order) {
        if (order == null) {
            return null;
        }
        OrderVO orderVO = new OrderVO();
        BeanUtils.copyProperties(order, orderVO);

        orderVO.setId(String.valueOf(order.getId())); // 转成字符串 避免前端丢失精度
        OrderStatus orderStatus = OrderStatus.get(order.getOrderStatus());
        orderVO.setOrderStatus(orderStatus.toString());
        orderVO.setOrderStatusCode(order.getOrderStatus());
        String price = order.getOrderPrice().setScale(2, RoundingMode.FLOOR).toPlainString();
        orderVO.setOrderPrice(price);
        return orderVO;
    }

    public static List<OrderVO> convertFrom(List<Order> orderList) {
        if (orderList == null) {
            return null;
        }
        List<OrderVO> goodsOrdersVOList = orderList.stream().map(OrderVO::convertFrom).collect(Collectors.toList());
        return goodsOrdersVOList;
    }
}
