package com.erp.waybill.entity;

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

import java.time.LocalDateTime;

/**
 * 运单实体
 */
@Data
@EqualsAndHashCode(callSuper = false)
@TableName("waybills")
public class Waybill {

    @TableId(type = IdType.AUTO)
    private Long id;

    /**
     * 订单ID
     */
    private Long orderId;

    /**
     * 快递编码（如：SF、YTO、ZTO）
     */
    private String expressCode;

    /**
     * 运单号
     */
    private String waybillNo;

    /**
     * 电子面单打印内容（JSON格式）
     */
    private String printContent;

    /**
     * 状态：unused-未使用，used-已使用，cancelled-已取消，void-作废
     */
    private String status;

    /**
     * 使用时间
     */
    private LocalDateTime usedAt;

    /**
     * 取消时间
     */
    private LocalDateTime cancelledAt;

    /**
     * 取消原因
     */
    private String cancelReason;

    /**
     * 作废时间
     */
    private LocalDateTime voidedAt;

    /**
     * 作废原因
     */
    private String voidReason;

    /**
     * 租户ID
     */
    private Long tenantId;

    /**
     * 创建时间
     */
    @TableField(fill = FieldFill.INSERT)
    private LocalDateTime createdAt;

    /**
     * 更新时间
     */
    @TableField(fill = FieldFill.INSERT_UPDATE)
    private LocalDateTime updatedAt;

    /**
     * 删除时间
     */
    @TableLogic
    private LocalDateTime deletedAt;

    // ==================== 辅助方法 ====================

    public boolean isUnused() {
        return "unused".equals(this.status);
    }

    public boolean isUsed() {
        return "used".equals(this.status);
    }

    public boolean isCancelled() {
        return "cancelled".equals(this.status);
    }

    public boolean isVoided() {
        return "void".equals(this.status);
    }

    public void markAsUsed() {
        this.status = "used";
        this.usedAt = LocalDateTime.now();
    }

    public void markAsCancelled(String reason) {
        this.status = "cancelled";
        this.cancelledAt = LocalDateTime.now();
        this.cancelReason = reason;
    }

    public void markAsVoided(String reason) {
        this.status = "void";
        this.voidedAt = LocalDateTime.now();
        this.voidReason = reason;
    }
}
