package com.ycwl.basic.order.exception;

import com.ycwl.basic.exception.BaseException;
import com.ycwl.basic.pricing.enums.ProductType;

/**
 * 重复购买异常
 * 当用户尝试购买已经购买过的内容时抛出此异常
 */
public class DuplicatePurchaseException extends BaseException {
    
    private final Long existingOrderId;
    private final String existingOrderNo;
    private final ProductType productType;
    private final String productId;
    
    public DuplicatePurchaseException(String message, Long existingOrderId, String existingOrderNo, 
                                     ProductType productType, String productId) {
        super(message);
        this.existingOrderId = existingOrderId;
        this.existingOrderNo = existingOrderNo;
        this.productType = productType;
        this.productId = productId;
    }
    
    public DuplicatePurchaseException(String message, Long existingOrderId, String existingOrderNo, 
                                     ProductType productType) {
        this(message, existingOrderId, existingOrderNo, productType, null);
    }
    
    public Long getExistingOrderId() {
        return existingOrderId;
    }
    
    public String getExistingOrderNo() {
        return existingOrderNo;
    }
    
    public ProductType getProductType() {
        return productType;
    }
    
    public String getProductId() {
        return productId;
    }
    
    /**
     * 获取友好的错误消息
     */
    public String getFriendlyMessage() {
        String productDesc = productType != null ? productType.getDescription() : "商品";
        if (productId != null) {
            return String.format("您已购买过该%s（商品ID：%s），订单号：%s", productDesc, productId, existingOrderNo);
        } else {
            return String.format("您已购买过%s，订单号：%s", productDesc, existingOrderNo);
        }
    }
}