package com.erp.invoice.repository;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.erp.invoice.entity.Invoice;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.time.LocalDate;
import java.util.List;

/**
 * 发票数据访问层
 */
@Mapper
public interface InvoiceRepository extends BaseMapper<Invoice> {
    
    /**
     * 根据发票号码查询
     */
    @Select("SELECT * FROM invoices WHERE invoice_no = #{invoiceNo} AND deleted_at IS NULL LIMIT 1")
    Invoice findByInvoiceNo(@Param("invoiceNo") String invoiceNo);
    
    /**
     * 根据订单ID查询发票列表
     */
    @Select("SELECT * FROM invoices WHERE order_id = #{orderId} AND deleted_at IS NULL")
    List<Invoice> findByOrderId(@Param("orderId") Long orderId);
    
    /**
     * 根据客户ID查询发票列表
     */
    @Select("SELECT * FROM invoices WHERE customer_id = #{customerId} AND deleted_at IS NULL ORDER BY created_at DESC")
    List<Invoice> findByCustomerId(@Param("customerId") Long customerId);
    
    /**
     * 统计租户已开具发票金额
     */
    @Select("SELECT COALESCE(SUM(total_amount), 0) FROM invoices WHERE tenant_id = #{tenantId} AND status = 1 AND deleted_at IS NULL")
    java.math.BigDecimal sumIssuedAmountByTenant(@Param("tenantId") Long tenantId);
    
    /**
     * 统计日期范围内发票数量
     */
    @Select("SELECT COUNT(*) FROM invoices WHERE tenant_id = #{tenantId} AND invoice_date BETWEEN #{startDate} AND #{endDate} AND deleted_at IS NULL")
    Integer countByDateRange(@Param("tenantId") Long tenantId, @Param("startDate") LocalDate startDate, @Param("endDate") LocalDate endDate);
    
    /**
     * 更新发票状态
     */
    @Update("UPDATE invoices SET status = #{status}, updated_at = NOW() WHERE id = #{id}")
    int updateStatus(@Param("id") Long id, @Param("status") Integer status);
    
    /**
     * 作废发票
     */
    @Update("UPDATE invoices SET status = 2, void_date = NOW(), void_reason = #{reason}, updated_at = NOW() WHERE id = #{id} AND status = 1")
    int voidInvoice(@Param("id") Long id, @Param("reason") String reason);
    
    /**
     * 红冲发票
     */
    @Update("UPDATE invoices SET status = 3, void_date = NOW(), void_reason = #{reason}, updated_at = NOW() WHERE id = #{id} AND status = 1")
    int reverseInvoice(@Param("id") Long id, @Param("reason") String reason);
    
    /**
     * 开具发票（更新状态和发票号）
     */
    @Update("UPDATE invoices SET status = 1, invoice_no = #{invoiceNo}, invoice_code = #{invoiceCode}, " +
            "invoice_date = #{invoiceDate}, issuer_id = #{issuerId}, issuer_name = #{issuerName}, " +
            "updated_at = NOW() WHERE id = #{id} AND status = 0")
    int issueInvoice(@Param("id") Long id, 
                     @Param("invoiceNo") String invoiceNo,
                     @Param("invoiceCode") String invoiceCode,
                     @Param("invoiceDate") LocalDate invoiceDate,
                     @Param("issuerId") Long issuerId,
                     @Param("issuerName") String issuerName);
}
