<?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.fjhx.purchase.mapper.invoice.InvoiceDetailsMapper">
    <select id="getPage" resultType="com.fjhx.purchase.entity.invoice.vo.InvoiceDetailsVo">
        SELECT
            t1.*,
            t2.supply_id AS supplyId,
            t2.type  AS invoiceType,
            t3.`code` AS purchaseCode
        FROM
            invoice_details t1
        LEFT JOIN invoice t2 ON t1.invoice_id = t2.id
        LEFT JOIN purchase t3 ON t3.id = t1.purchase_id
            ${ew.customSqlSegment}
    </select>

    <select id="getSumMoneyByPurchaseIds" resultType="com.fjhx.purchase.entity.invoice.vo.InvoiceDetailsVo">
        SELECT
            purchase_id,
            IFNULL(SUM(money),0) AS sumMoney
        FROM
            invoice_details
        <where>
            <if test="purchaseIds neq null and purchaseIds.size() > 0">
                <foreach collection="purchaseIds" item="purchaseId" open="purchase_id IN (" separator="," close=")">
                    #{purchaseId}
                </foreach>
            </if>
        </where>
        GROUP BY
            purchase_id
    </select>

    <select id="getDetail" resultType="com.fjhx.purchase.entity.invoice.vo.InvoiceDetailsVo">
        SELECT
            t1.*,
            t2.`code` AS purchaseCode,
            t2.amount AS purchaseAmount
        FROM
            invoice_details t1
            LEFT JOIN purchase t2 ON t1.purchase_id = t2.id
        WHERE invoice_id = #{invoiceId}
    </select>
</mapper>
