<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.ruoyi.system.mapper.PosOrderInvoiceMapper">

    <!-- 订单发票列表：列名别名对齐 VO 属性，resultType 自动映射 -->
    <select id="selectInvoiceList" parameterType="com.ruoyi.system.domain.vo.PosOrderInvoiceVo"
            resultType="com.ruoyi.system.domain.vo.PosOrderInvoiceVo">
        SELECT
            i.id,
            i.order_id         AS orderId,
            i.order_no         AS orderNo,
            i.store_id         AS storeId,
            s.pos_name         AS posName,
            i.invoice_category AS invoiceCategory,
            i.buyer_name       AS buyerName,
            i.buyer_ubn        AS buyerUbn,
            i.invoice_number   AS invoiceNumber,
            i.invoice_status   AS invoiceStatus,
            i.total_amt        AS totalAmt,
            i.sales_amt        AS salesAmt,
            i.tax_amt          AS taxAmt,
            i.fail_reason      AS failReason,
            i.apply_time       AS applyTime,
            i.issue_time       AS issueTime,
            i.invalid_time     AS invalidTime,
            i.create_time      AS createTime
        FROM pos_order_invoice i
        LEFT JOIN pos_store s ON s.id = i.store_id
        <where>
            <if test="orderNo != null and orderNo != ''"> AND i.order_no = #{orderNo} </if>
            <if test="storeId != null"> AND i.store_id = #{storeId} </if>
            <if test="invoiceStatus != null"> AND i.invoice_status = #{invoiceStatus} </if>
            <if test="invoiceCategory != null and invoiceCategory != ''"> AND i.invoice_category = #{invoiceCategory} </if>
            <if test="posNameLike != null and posNameLike != ''"> AND s.pos_name LIKE concat('%', #{posNameLike}, '%') </if>
            <if test="cretimStart != null and cretimStart != ''"> AND i.create_time &gt;= #{cretimStart} </if>
            <if test="cretimEnd != null and cretimEnd != ''"> AND i.create_time &lt;= #{cretimEnd} </if>
        </where>
        ORDER BY i.id DESC
    </select>

    <!-- 订单发票详情（含买方/载具/金额/凭证完整字段） -->
    <select id="selectInvoiceDetail" parameterType="Long"
            resultType="com.ruoyi.system.domain.vo.PosOrderInvoiceVo">
        SELECT
            i.id,
            i.order_id         AS orderId,
            i.order_no         AS orderNo,
            i.store_id         AS storeId,
            s.pos_name         AS posName,
            i.invoice_category AS invoiceCategory,
            i.buyer_name       AS buyerName,
            i.buyer_ubn        AS buyerUbn,
            i.buyer_email      AS buyerEmail,
            i.carrier_type     AS carrierType,
            i.carrier_num      AS carrierNum,
            i.invoice_number   AS invoiceNumber,
            i.random_num       AS randomNum,
            i.invoice_status   AS invoiceStatus,
            i.total_amt        AS totalAmt,
            i.sales_amt        AS salesAmt,
            i.tax_amt          AS taxAmt,
            i.fail_reason      AS failReason,
            i.invoice_trans_no AS invoiceTransNo,
            i.invoice_bar_code AS invoiceBarCode,
            i.invoice_qrcode_l AS invoiceQrcodeL,
            i.invoice_qrcode_r AS invoiceQrcodeR,
            i.apply_time       AS applyTime,
            i.issue_time       AS issueTime,
            i.invalid_time     AS invalidTime,
            i.create_time      AS createTime
        FROM pos_order_invoice i
        LEFT JOIN pos_store s ON s.id = i.store_id
        WHERE i.order_id = #{orderId}
    </select>

</mapper>
