<?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.cf.imes.module.system.dal.mysql.funds.purchase.PurchaseRecordMapper">
    <select id="getInvoicingAmount" resultType="java.math.BigDecimal">
        select sum(account_balance_spent) from purchase_record
        where organ_id = #{organId} and is_invocing = '${@com.cf.imes.module.system.enums.pay.InvoiceStatusEnum@INVOICABLE.code}';
    </select>

    <!--    查询机构下开票金额统计：可开票、开票中、已开票-->
    <select id="getOrgInvoiceAmountStatistics" resultType="com.cf.imes.module.system.controller.admin.funds.invoice.vo.InvoiceAmountRespVO">
        select sum(case when is_invocing = '${@com.cf.imes.module.system.enums.pay.InvoiceStatusEnum@INVOICABLE.code}' then account_balance_spent else 0 end) as invocable,
               sum(case when is_invocing = '${@com.cf.imes.module.system.enums.pay.InvoiceStatusEnum@PENDINGINVOICING.code}' then account_balance_spent else 0 end) as inTheInvoice,
               sum(case when is_invocing = '${@com.cf.imes.module.system.enums.pay.InvoiceStatusEnum@INVOICINGSUCCESSFUL.code}' then account_balance_spent else 0 end) as invoiced
        from purchase_record
        where organ_id = #{organId}
    </select>

    <select id="getInvoicableAmount" resultType="java.math.BigDecimal">
        select sum(account_balance_spent) from purchase_record
        where organ_id = #{organId} and is_invocing = '${@com.cf.imes.module.system.enums.pay.InvoiceStatusEnum@INVOICABLE.code}'
        and id in
        <foreach collection="ids" item="id" open="(" close=")" separator=",">
            #{id}
        </foreach>
    </select>
</mapper>