<?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.sale.mapper.contract.ContractInvoiceDetailsMapper">
    <select id="getPage" resultType="com.fjhx.sale.entity.contract.vo.ContractInvoiceDetailsVo">
        select cid.id,
               cid.invoice_id,
               cid.contract_id,
               cid.money,
               cid.remark,
               cid.create_user,
               cid.create_time,
               cid.update_user,
               cid.update_time
        from contract_invoice_details cid
            ${ew.customSqlSegment}
    </select>

    <select id="getDetail" resultType="com.fjhx.sale.entity.contract.vo.ContractInvoiceDetailsVo">
        SELECT t1.*,
               t2.`code` AS contractCode,
               t2.amount AS contractAmount
        FROM contract_invoice_details t1
                 LEFT JOIN contract t2 ON t1.contract_id = t2.id
        WHERE invoice_id = #{invoiceId}
    </select>

    <select id="getSumMoneyByContractIds" resultType="com.fjhx.sale.entity.contract.vo.ContractInvoiceDetailsVo">
        SELECT
        contract_id,
        IFNULL(SUM(money),0) AS sumMoney
        FROM
        contract_invoice_details
        <where>
            <if test="contractIds neq null and contractIds.size() > 0">
                <foreach collection="contractIds" item="contractId" open="contract_id IN (" separator="," close=")">
                    #{contractId}
                </foreach>
            </if>
        </where>
        GROUP BY
        contract_id
    </select>
</mapper>
