package com.izouma.nineth.dto;

import com.alibaba.excel.annotation.ExcelProperty;
import com.izouma.nineth.annotations.Searchable;
import com.izouma.nineth.domain.MintMaterial;
import com.izouma.nineth.domain.MintOrder;
import io.swagger.annotations.ApiModel;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.beans.BeanUtils;

import javax.persistence.Column;
import javax.persistence.EnumType;
import javax.persistence.Enumerated;
import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Optional;
import java.util.stream.Collectors;

@Data
@AllArgsConstructor
@NoArgsConstructor
@Builder
@ApiModel("铸造订单")
public class MintOrderDTO {

    @ExcelProperty("ID")
    private Long id;

    @ExcelProperty("用户ID")
    private Long userId;

    @ExcelProperty("手机号")
    private String phone;

    @ExcelProperty("下单时间")
    private LocalDateTime createdAt;

    @ExcelProperty("活动ID")
    private Long mintActivityId;

    @ExcelProperty("活动名称")
    private String mintActivity;

    @ExcelProperty("铸造材料")
    private String material;

    @ExcelProperty("是否消耗藏品")
    private String consume;

    @ExcelProperty("gas费")
    @Column(precision = 10, scale = 2)
    private BigDecimal gasPrice;

    @ExcelProperty("支付方式")
    @Enumerated(EnumType.STRING)
    private String payMethod;

    @ExcelProperty("交易ID")
    @Searchable
    private String transactionId;

    @ExcelProperty("支付时间")
    private LocalDateTime payAt;

    @ExcelProperty("铸造类型")
    private String airDrop;

    @ExcelProperty("快递单号")
    private String courierId;

    @ExcelProperty("收货人")
    private String contactName;

    @ExcelProperty("收货电话")
    private String contactPhone;

    @ExcelProperty("收货地址")
    private String address;

    @ExcelProperty("取消时间")
    private LocalDateTime cancelTime;

    @ExcelProperty("状态")
    private String status;

    public MintOrderDTO(MintOrder mintOrder) {
        BeanUtils.copyProperties(mintOrder, this);
        this.status = mintOrder.getStatus().getDescription();
        this.consume = mintOrder.isConsume() ? "是" : "否";
        this.material = Optional.ofNullable(mintOrder.getMaterial()).orElse(new ArrayList<>())
                .stream().map(MintMaterial::getName).collect(Collectors.joining(","));
        this.airDrop = mintOrder.isAirDrop() ? "空投" : "实物";
        this.payMethod = mintOrder.getPayMethod() == null ? "" : mintOrder.getPayMethod().getDescription();
    }

}
