package com.wzj.soopin.order.task;

import java.util.concurrent.Delayed;
import java.util.concurrent.TimeUnit;

/**
 * 订单延迟取消任务
 */
public class OrderCancelTask implements Delayed {
    private final String orderSn;         // 订单编号
    private final long executeTime;       // 执行时间（毫秒）

    public OrderCancelTask(String orderSn, long delayTime) {
        this.orderSn = orderSn;
        this.executeTime = System.currentTimeMillis() + delayTime;
    }

    @Override
    public long getDelay(TimeUnit unit) {
        return unit.convert(executeTime - System.currentTimeMillis(), TimeUnit.MILLISECONDS);
    }

    @Override
    public int compareTo(Delayed other) {
        return Long.compare(this.executeTime, ((OrderCancelTask) other).executeTime);
    }

    public String getOrderSn() {
        return orderSn;
    }
}
