package com.table.transfer.module.enpty;

import com.baomidou.mybatisplus.annotation.*;
import lombok.Data;

import java.math.BigDecimal;
import java.time.LocalDateTime;

@Data
@TableName("t_transfer_records")
public class TransferRecord {
    @TableId(type = IdType.AUTO)
    private Long id;
    
    private String requestId;
    
    private String fromAddress;
    
    private String toAddress;
    
    private BigDecimal amount;

    private String transactionHash;

    private String status; // SUCCESS, FAILED, PENDING
    
    private String Message;
    
    @TableField(fill = FieldFill.INSERT)
    private LocalDateTime createTime;
    
    @TableField(fill = FieldFill.INSERT_UPDATE)
    private LocalDateTime updateTime;

    private boolean deleted;
    public static TransferRecord create(TransferRequest request,TransferResponse response){
        TransferRecord record =new TransferRecord();
        record.setRequestId(request.getRequestId());
        record.setAmount(new BigDecimal(request.getAmount()));
        record.setFromAddress(request.getFromAddress());
        record.setToAddress(request.getToAddress());

        LocalDateTime now = LocalDateTime.now();
        record.setCreateTime(now);
        record.setUpdateTime(now);

        record.setStatus(response.isSuccess()?"SUCCESS":"FAILED");
        record.setTransactionHash(response.getTransactionHash());
        record.setMessage(response.getMessage());
        return record;
    };
}