package com.erp.tenant.entity.finance;

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;

/**
 * 收款/付款记录实体
 */
@Data
@EqualsAndHashCode(callSuper = false)
@TableName("finance_records")
public class FinanceRecord extends BaseEntity {

    // ==================== 记录类型 ====================
    /** 类型：收款 */
    public static final String TYPE_INCOME = "income";
    /** 类型：付款 */
    public static final String TYPE_EXPENSE = "expense";

    // ==================== 状态 ====================
    /** 状态：待确认 */
    public static final int STATUS_PENDING = 0;
    /** 状态：已确认 */
    public static final int STATUS_CONFIRMED = 1;
    /** 状态：已取消 */
    public static final int STATUS_CANCELLED = -1;

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

    // ==================== 基本信息 ====================
    /** 记录编号 */
    @TableField("record_no")
    private String recordNo;

    /** 类型：income-收款，expense-付款 */
    private String type;

    /** 关联单据类型：order-订单，purchase-采购单 */
    @TableField("source_type")
    private String sourceType;

    /** 关联单据ID */
    @TableField("source_id")
    private Long sourceId;

    /** 关联单据号 */
    @TableField("source_no")
    private String sourceNo;

    // ==================== 金额信息 ====================
    /** 金额 */
    private BigDecimal amount;

    /** 已核销金额 */
    @TableField("verified_amount")
    private BigDecimal verifiedAmount;

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

    // ==================== 对方信息 ====================
    /** 对方名称（客户/供应商） */
    @TableField("counterparty_name")
    private String counterpartyName;

    /** 对方账户 */
    @TableField("counterparty_account")
    private String counterpartyAccount;

    // ==================== 支付信息 ====================
    /** 支付方式 */
    @TableField("payment_method")
    private String paymentMethod;

    /** 支付流水号 */
    @TableField("payment_serial")
    private String paymentSerial;

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

    // ==================== 状态和时间 ====================
    /** 状态：0-待确认，1-已确认，-1-已取消 */
    private Integer status;

    /** 确认时间 */
    @TableField("confirmed_at")
    private LocalDateTime confirmedAt;

    /** 确认人 */
    @TableField("confirmed_by")
    private Long confirmedBy;

    // ==================== 备注 ====================
    /** 摘要/说明 */
    private String summary;

    /** 备注 */
    private String remark;

    // ==================== 辅助方法 ====================
    public String getStatusText() {
        return switch (status) {
            case STATUS_PENDING -> "待确认";
            case STATUS_CONFIRMED -> "已确认";
            case STATUS_CANCELLED -> "已取消";
            default -> "未知";
        };
    }

    public String getTypeText() {
        return TYPE_INCOME.equals(type) ? "收款" : "付款";
    }
}
