package com.erp.tenant.entity.order;

import com.baomidou.mybatisplus.annotation.*;
import lombok.Data;
import lombok.EqualsAndHashCode;
import com.erp.tenant.entity.common.BaseEntity;

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

/**
 * 订单实体
 */
@Data
@EqualsAndHashCode(callSuper = false)
@TableName("orders")
public class Order extends BaseEntity {

    // ==================== 平台常量 ====================
    public static final String PLATFORM_TAOBAO = "taobao";
    public static final String PLATFORM_JD = "jd";
    public static final String PLATFORM_PDD = "pdd";
    public static final String PLATFORM_DOUYIN = "douyin";
    public static final String PLATFORM_KUAISHOU = "kuaishou";

    // ==================== 订单状态常量 ====================
    /** ERP状态：待审核 */
    public static final String ERP_STATUS_PENDING = "pending";
    /** ERP状态：已审核 */
    public static final String ERP_STATUS_APPROVED = "approved";
    /** ERP状态：已驳回 */
    public static final String ERP_STATUS_REJECTED = "rejected";
    /** ERP状态：已发货 */
    public static final String ERP_STATUS_SHIPPED = "shipped";
    /** ERP状态：已完成 */
    public static final String ERP_STATUS_COMPLETED = "completed";
    /** ERP状态：已取消 */
    public static final String ERP_STATUS_CANCELLED = "cancelled";

    /** 订单状态：待付款 */
    public static final String ORDER_STATUS_PENDING = "pending";
    /** 订单状态：已付款 */
    public static final String ORDER_STATUS_PAID = "paid";
    /** 订单状态：已完成 */
    public static final String ORDER_STATUS_COMPLETED = "completed";
    /** 订单状态：已取消 */
    public static final String ORDER_STATUS_CANCELLED = "cancelled";

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

    // ==================== 订单基本信息 ====================
    /** 订单编号 */
    @TableField("order_sn")
    private String orderSn;

    /** 平台订单号 */
    @TableField("platform_order_sn")
    private String platformOrderSn;

    /** 平台 */
    private String platform;

    /** 店铺ID */
    @TableField("shop_id")
    private Long shopId;

    /** 店铺名称 */
    @TableField("shop_name")
    private String shopName;

    // ==================== 金额信息 ====================
    /** 订单总金额 */
    @TableField("total_amount")
    private BigDecimal totalAmount;

    /** 商品金额 */
    @TableField("goods_amount")
    private BigDecimal goodsAmount;

    /** 运费 */
    @TableField("freight")
    private BigDecimal freight;

    /** 优惠金额 */
    @TableField("discount_amount")
    private BigDecimal discountAmount;

    // ==================== 收件人信息 ====================
    /** 收件人姓名 */
    @TableField("receiver_name")
    private String receiverName;

    /** 收件人手机号 */
    @TableField("receiver_phone")
    private String receiverPhone;

    /** 收件人省份 */
    @TableField("receiver_province")
    private String receiverProvince;

    /** 收件人城市 */
    @TableField("receiver_city")
    private String receiverCity;

    /** 收件人区县 */
    @TableField("receiver_district")
    private String receiverDistrict;

    /** 收件人详细地址 */
    @TableField("receiver_address")
    private String receiverAddress;

    // ==================== 仓库和物流 ====================
    /** 仓库ID */
    @TableField("warehouse_id")
    private Long warehouseId;

    /** 仓库名称 */
    @TableField("warehouse_name")
    private String warehouseName;

    /** 物流公司 */
    @TableField("logistics_company")
    private String logisticsCompany;

    /** 物流单号 */
    @TableField("logistics_no")
    private String logisticsNo;

    // ==================== 状态字段 ====================
    /** ERP状态 */
    @TableField("erp_status")
    private String erpStatus;

    /** 平台订单状态 */
    @TableField("order_status")
    private String orderStatus;

    /** 发货状态 */
    @TableField("delivery_status")
    private String deliveryStatus;

    // ==================== 时间字段 ====================
    /** 支付时间 */
    @TableField("payment_time")
    private LocalDateTime paymentTime;

    /** 发货时间 */
    @TableField("shipped_at")
    private LocalDateTime shippedAt;

    /** 审核时间 */
    @TableField("approved_at")
    private LocalDateTime approvedAt;

    /** 审核人ID */
    @TableField("approved_by")
    private Long approvedBy;

    /** 驳回原因 */
    @TableField("reject_reason")
    private String rejectReason;

    /** 驳回时间 */
    @TableField("rejected_at")
    private LocalDateTime rejectedAt;

    /** 驳回人ID */
    @TableField("rejected_by")
    private Long rejectedBy;

    // ==================== 备注 ====================
    /** 买家备注 */
    private String remark;

    /** 管理员备注 */
    @TableField("admin_remark")
    private String adminRemark;

    // ==================== 非数据库字段 ====================
    @TableField(exist = false)
    private List<OrderItem> items;

    @TableField(exist = false)
    private String warehouseName_;

    // ==================== 辅助方法 ====================
    public String getErpStatusText() {
        return switch (erpStatus) {
            case ERP_STATUS_PENDING -> "待审核";
            case ERP_STATUS_APPROVED -> "已审核";
            case ERP_STATUS_REJECTED -> "已驳回";
            case ERP_STATUS_SHIPPED -> "已发货";
            case ERP_STATUS_COMPLETED -> "已完成";
            case ERP_STATUS_CANCELLED -> "已取消";
            default -> "未知";
        };
    }
}
