package com.erp.invoice.dto;

import com.erp.invoice.entity.Invoice;
import lombok.Data;

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

/**
 * 发票响应DTO
 */
@Data
public class InvoiceResponse {
    
    // ==================== 主键和租户 ====================
    
    private Long id;
    private Long tenantId;
    
    // ==================== 发票基本信息 ====================
    
    private String invoiceNo;
    private String invoiceCode;
    private Integer invoiceType;
    private String invoiceTypeName;
    private Integer status;
    private String statusName;
    
    // ==================== 购方信息 ====================
    
    private String buyerName;
    private String buyerTaxNo;
    private String buyerAddressPhone;
    private String buyerBankAccount;
    
    // ==================== 销方信息 ====================
    
    private String sellerName;
    private String sellerTaxNo;
    private String sellerAddressPhone;
    private String sellerBankAccount;
    
    // ==================== 金额信息 ====================
    
    private BigDecimal totalAmount;
    private BigDecimal amount;
    private BigDecimal taxAmount;
    private BigDecimal taxRate;
    
    // ==================== 关联信息 ====================
    
    private Long orderId;
    private String orderNo;
    private Long customerId;
    private String customerName;
    
    // ==================== 发票内容 ====================
    
    private String invoiceContent;
    private String drawer;
    private String reviewer;
    private String payee;
    
    // ==================== 时间信息 ====================
    
    private LocalDate invoiceDate;
    private LocalDate applyDate;
    private LocalDate voidDate;
    
    // ==================== 原因和备注 ====================
    
    private String voidReason;
    private String remark;
    
    // ==================== 文件路径 ====================
    
    private String pdfPath;
    private String imagePath;
    
    // ==================== 操作人信息 ====================
    
    private Long applicantId;
    private String applicantName;
    private Long auditorId;
    private String auditorName;
    private Long issuerId;
    private String issuerName;
    
    // ==================== 时间戳 ====================
    
    private LocalDateTime createdAt;
    private LocalDateTime updatedAt;
    
    // ==================== 发票明细 ====================
    
    private List<InvoiceItemResponse> items;
    
    /**
     * 发票明细响应
     */
    @Data
    public static class InvoiceItemResponse {
        private Long id;
        private Long invoiceId;
        private String goodsName;
        private String spec;
        private String unit;
        private BigDecimal quantity;
        private BigDecimal unitPrice;
        private BigDecimal amount;
        private BigDecimal taxRate;
        private BigDecimal taxAmount;
        private BigDecimal totalAmount;
        private String goodsCode;
        private String taxClassificationCode;
        private Integer preferentialFlag;
        private Integer zeroTaxFlag;
    }
    
    /**
     * 从实体转换
     */
    public static InvoiceResponse fromEntity(Invoice invoice) {
        InvoiceResponse response = new InvoiceResponse();
        response.setId(invoice.getId());
        response.setTenantId(invoice.getTenantId());
        response.setInvoiceNo(invoice.getInvoiceNo());
        response.setInvoiceCode(invoice.getInvoiceCode());
        response.setInvoiceType(invoice.getInvoiceType());
        response.setInvoiceTypeName(invoice.getInvoiceTypeName());
        response.setStatus(invoice.getStatus());
        response.setStatusName(invoice.getStatusName());
        response.setBuyerName(invoice.getBuyerName());
        response.setBuyerTaxNo(invoice.getBuyerTaxNo());
        response.setBuyerAddressPhone(invoice.getBuyerAddressPhone());
        response.setBuyerBankAccount(invoice.getBuyerBankAccount());
        response.setSellerName(invoice.getSellerName());
        response.setSellerTaxNo(invoice.getSellerTaxNo());
        response.setSellerAddressPhone(invoice.getSellerAddressPhone());
        response.setSellerBankAccount(invoice.getSellerBankAccount());
        response.setTotalAmount(invoice.getTotalAmount());
        response.setAmount(invoice.getAmount());
        response.setTaxAmount(invoice.getTaxAmount());
        response.setTaxRate(invoice.getTaxRate());
        response.setOrderId(invoice.getOrderId());
        response.setOrderNo(invoice.getOrderNo());
        response.setCustomerId(invoice.getCustomerId());
        response.setCustomerName(invoice.getCustomerName());
        response.setInvoiceContent(invoice.getInvoiceContent());
        response.setDrawer(invoice.getDrawer());
        response.setReviewer(invoice.getReviewer());
        response.setPayee(invoice.getPayee());
        response.setInvoiceDate(invoice.getInvoiceDate());
        response.setApplyDate(invoice.getApplyDate());
        response.setVoidDate(invoice.getVoidDate());
        response.setVoidReason(invoice.getVoidReason());
        response.setRemark(invoice.getRemark());
        response.setPdfPath(invoice.getPdfPath());
        response.setImagePath(invoice.getImagePath());
        response.setApplicantId(invoice.getApplicantId());
        response.setApplicantName(invoice.getApplicantName());
        response.setAuditorId(invoice.getAuditorId());
        response.setAuditorName(invoice.getAuditorName());
        response.setIssuerId(invoice.getIssuerId());
        response.setIssuerName(invoice.getIssuerName());
        response.setCreatedAt(invoice.getCreatedAt());
        response.setUpdatedAt(invoice.getUpdatedAt());
        return response;
    }
}
