<?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 COALESCE(SUM(total_amount) ,0) AS total_spent
        FROM purchase_record
        where organ_id = #{organId} and is_invocing = '${@com.cf.imes.module.system.enums.pay.InvoiceStatusEnum@INVOICABLE.code}' and deleted = false and initial != true;
    </select>

    <!--    查询机构下开票金额统计：可开票、开票中、已开票-->
    <select id="getOrgInvoiceAmountStatistics" resultType="com.cf.imes.module.system.controller.admin.funds.invoice.vo.InvoiceAmountRespVO">
        SELECT
            COALESCE(SUM(
                    CASE
                        WHEN is_invocing = '${@com.cf.imes.module.system.enums.pay.InvoiceStatusEnum@INVOICABLE.code}'
                            THEN external_spent
                        ELSE 0
                        END
            ) ,0) AS invocable,

            COALESCE(SUM(
                    CASE
                        WHEN is_invocing = '${@com.cf.imes.module.system.enums.pay.InvoiceStatusEnum@PENDINGINVOICING.code}'
                            THEN external_spent
                        ELSE 0
                        END
            ) ,0) AS inTheInvoice,

            COALESCE(SUM(
                    CASE
                        WHEN is_invocing = '${@com.cf.imes.module.system.enums.pay.InvoiceStatusEnum@INVOICINGSUCCESSFUL.code}'
                            THEN external_spent
                        ELSE 0
                        END
            ) ,0) AS invoiced
        from purchase_record
        where organ_id = #{organId} and deleted = false and initial != true;
    </select>

    <select id="getInvoicableAmount" resultType="java.math.BigDecimal">
        select COALESCE(sum(external_spent) ,0) 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>
        and deleted = false;
    </select>

    <select id="getTotalSales"
            resultType="com.cf.imes.module.system.controller.admin.funds.organamount.vo.TotalSalesAmountRespVO">
        select
            COALESCE(sum(total_amount) ,0) as amount
        from purchase_record
        where deleted = false and initial != true;
    </select>

    <select id="getTodaySales"
            resultType="java.math.BigDecimal">
        select
            COALESCE(sum(total_amount) ,0) as amount
        from purchase_record
        where deleted = false and initial != true
        and create_time &gt;= CURDATE()
        and create_time &lt; #{endTime};
    </select>

    <sql id="dateCreate">
        <if test="req.unit != null and req.unit == @com.cf.imes.framework.common.enums.StatisticsUnit@QUARTER.getValue()">
            ,CONCAT(YEAR(o.create_time), '-', QUARTER(o.create_time)) as date
        </if>
        <if test="req.unit != null and req.unit == @com.cf.imes.framework.common.enums.StatisticsUnit@MONTH.getValue()">
            ,CONCAT(YEAR(o.create_time), '-', MONTH(o.create_time)) as date
        </if>
        <if test="req.unit != null and req.unit == @com.cf.imes.framework.common.enums.StatisticsUnit@WEEK.getValue()">
            ,CONCAT(YEAR(o.create_time), '-', MONTH(o.create_time), '-', FLOOR((DayOfMonth(o.create_time)-1)/7)+1) AS
            date
        </if>
        <if test="req.unit != null and req.unit == @com.cf.imes.framework.common.enums.StatisticsUnit@DAY.getValue()">
            ,CONCAT(YEAR(o.create_time), '-', MONTH(o.create_time), '-', DAY(o.create_time)) as date
        </if>
    </sql>

    <delete id="physicalDeleteByIds">
        DELETE FROM purchase_record
        WHERE organ_id = #{organId}
        AND id IN
        <foreach collection="ids"
                 item="id"
                 open="("
                 separator=","
                 close=")">
            #{id}
        </foreach>
    </delete>
</mapper>