package com.erp.aftersale.entity;

import com.baomidou.mybatisplus.annotation.*;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.AllArgsConstructor;
import lombok.Builder;
import com.fasterxml.jackson.annotation.JsonFormat;
import com.fasterxml.jackson.annotation.JsonIgnore;
import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.List;

@Data
@NoArgsConstructor
@AllArgsConstructor
@Builder
@TableName("after_sales")
public class AfterSale {

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

    /** 售后单短ID，如 AS20240401ABC123 */
    private String shortId;

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

    /** 原订单短ID */
    private String orderShortId;

    /** 平台订单号 */
    private String platformOrderSn;

    /** 店铺ID */
    private Long shopId;

    /** 店铺名称 */
    private String shopName;

    /** 客户ID */
    private Long customerId;

    /** 客户姓名 */
    private String customerName;

    /** 客户电话 */
    private String customerPhone;

    // ===== 售后类型与原因 =====
    /** 售后类型: refund(仅退款)/return(退货退款)/exchange(换货)/repair(维修) */
    private String type;

    /** 售后原因 */
    private String reason;

    /** 详细原因 */
    private String reasonDetail;

    /** 客户描述 */
    private String description;

    // ===== 金额信息 =====
    /** 申请退款金额 */
    private BigDecimal applyAmount;

    /** 审核通过退款金额 */
    private BigDecimal approveAmount;

    /** 运费退款金额 */
    private BigDecimal freightRefund;

    /** 实际退款金额 */
    private BigDecimal actualRefund;

    // ===== 状态 =====
    /** 售后状态: pending/approved/rejected/processing/completed/closed */
    private String status;

    /** 审核状态: pending/approved/rejected */
    private String auditStatus;

    /** 上一状态（撤销用） */
    private String previousStatus;

    // ===== 审核信息 =====
    /** 审核备注 */
    private String auditComment;

    /** 审核人ID */
    private Long auditorId;

    /** 审核人姓名 */
    private String auditorName;

    /** 审核时间 */
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    private LocalDateTime auditTime;

    // ===== 退款/收货信息 =====
    /** 退款时间 */
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    private LocalDateTime refundTime;

    /** 退货快递单号 */
    private String returnTrackingNo;

    /** 退货快递公司 */
    private String returnExpressCompany;

    /** 收到退货时间 */
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    private LocalDateTime receiveTime;

    /** 收货备注 */
    private String receiveComment;

    // ===== 完成/关闭 =====
    /** 完成时间 */
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    private LocalDateTime completeTime;

    /** 关闭时间 */
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    private LocalDateTime closeTime;

    /** 关闭原因 */
    private String closeReason;

    // ===== 图片凭证 =====
    /** 图片JSON数组 */
    private String images;

    // ===== 关联单据 =====
    /** 换货新订单ID */
    private Long exchangeOrderId;

    /** 维修工单ID */
    private Long repairOrderId;

    /** 财务退款记录ID */
    private Long financeRefundId;

    // ===== 审计字段 =====
    @Version
    private Integer version;

    @TableField(fill = FieldFill.INSERT)
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    private LocalDateTime createdAt;

    @TableField(fill = FieldFill.INSERT_UPDATE)
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    private LocalDateTime updatedAt;

    @TableLogic
    @JsonIgnore
    private Integer deleted;

    // ===== 非持久化字段 =====
    @TableField(exist = false)
    private List<AfterSaleItem> items;

    /** 售后类型常量 */
    public static final String TYPE_REFUND = "refund";       // 仅退款
    public static final String TYPE_RETURN = "return";        // 退货退款
    public static final String TYPE_EXCHANGE = "exchange";   // 换货
    public static final String TYPE_REPAIR = "repair";        // 维修

    /** 状态常量 */
    public static final String STATUS_PENDING = "pending";       // 待审核
    public static final String STATUS_APPROVED = "approved";     // 已通过(待处理)
    public static final String STATUS_REJECTED = "rejected";     // 已驳回
    public static final String STATUS_PROCESSING = "processing"; // 处理中
    public static final String STATUS_COMPLETED = "completed";  // 已完成
    public static final String STATUS_CLOSED = "closed";       // 已关闭

    /** 审核状态常量 */
    public static final String AUDIT_PENDING = "pending";
    public static final String AUDIT_APPROVED = "approved";
    public static final String AUDIT_REJECTED = "rejected";
}
