<?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.web.modules.trading.mapper.AppSupplierHomeMapper">

    <sql id="orderInRange">
        o.supplier_id = #{supplierId}
        and o.del_flag = '0'
        and o.create_time &gt;= #{rangeStart}
        and o.create_time &lt;= #{rangeEnd}
    </sql>

    <select id="selectTradeSummary"
            resultType="com.ruoyi.web.modules.trading.domain.dto.AppSupplierTradeSummaryDto">
        select count(*) as orderCount,
               coalesce(sum(o.total_amount), 0) as totalAmount,
               coalesce(sum(o.total_heads), 0) as totalHeads,
               coalesce(count(distinct if(o.order_status = '1', o.distributor_id, null)), 0) as payingBuyerCount
        from biz_trade_order o
        where <include refid="orderInRange"/>
    </select>

    <select id="selectSettlementSummary"
            resultType="com.ruoyi.web.modules.trading.domain.dto.AppSupplierSettlementSummaryDto">
        select coalesce(sum(s.payable_amount), 0) as payableAmount,
               coalesce(sum(s.service_fee_amount), 0) as serviceFeeAmount
        from biz_supplier_settlement s
        where s.supplier_id = #{supplierId}
          and s.del_flag = '0'
          and s.settlement_status = '1'
          and s.settle_finish_time &gt;= #{rangeStart}
          and s.settle_finish_time &lt;= #{rangeEnd}
    </select>

    <select id="selectHourlyAmountTrend"
            resultType="com.ruoyi.web.modules.trading.domain.dto.AppSupplierAmountBucketDto">
        select cast(hour(o.create_time) as char) as bucketKey,
               coalesce(sum(o.total_amount), 0) as amount
        from biz_trade_order o
        where <include refid="orderInRange"/>
        group by hour(o.create_time)
    </select>

    <select id="selectDailyAmountTrend"
            resultType="com.ruoyi.web.modules.trading.domain.dto.AppSupplierAmountBucketDto">
        select date_format(o.create_time, '%Y-%m-%d') as bucketKey,
               coalesce(sum(o.total_amount), 0) as amount
        from biz_trade_order o
        where <include refid="orderInRange"/>
        group by date_format(o.create_time, '%Y-%m-%d')
    </select>

    <select id="selectTodayTradedHeads" resultType="long">
        select coalesce(sum(o.total_heads), 0)
        from biz_trade_order o
        where o.supplier_id = #{supplierId}
          and o.del_flag = '0'
          and o.create_time &gt;= #{dayStart}
          and o.create_time &lt;= #{dayEnd}
    </select>

    <select id="selectPendingPayHeads" resultType="long">
        select coalesce(sum(o.total_heads), 0)
        from biz_trade_order o
        where o.supplier_id = #{supplierId}
          and o.del_flag = '0'
          and o.order_status = '0'
    </select>

    <select id="countTodayEntryEarTags" resultType="long">
        select count(distinct t.ear_tag)
        from biz_market_entry_ear_tag t
        inner join biz_market_entry e on e.id = t.entry_id and e.del_flag = '0'
        where e.supplier_id = #{supplierId}
          and e.entry_time &gt;= #{dayStart}
          and e.entry_time &lt;= #{dayEnd}
    </select>

    <select id="selectActiveIsolationEarTagRows" resultType="string">
        select e.ear_tags
        from biz_entry_isolation e
        where e.del_flag = '0'
          and e.supplier_id = #{supplierId}
          and date(e.entry_time) &lt;= curdate()
          and e.expected_release_date &gt;= curdate()
        union all
        select a.ear_tags
        from biz_entry_isolation_alloc a
        inner join biz_entry_isolation e on e.id = a.entry_id and e.del_flag = '0'
        where e.supplier_id = #{supplierId}
          and date(e.entry_time) &lt;= curdate()
          and e.expected_release_date &gt;= curdate()
    </select>

    <select id="selectSickDeadEarTagRowsBySupplier" resultType="string">
        select p.ear_tags
        from biz_isolation_patrol p
        inner join biz_entry_isolation_alloc a on a.pen_id = p.pen_id
        inner join biz_entry_isolation e on e.id = a.entry_id and e.del_flag = '0'
        where p.del_flag = '0'
          and e.supplier_id = #{supplierId}
          and ifnull(p.sick_dead_qty, 0) &gt; 0
          and ifnull(p.ear_tags, '') &lt;&gt; ''
    </select>

</mapper>
