<?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.screen.mapper.TradeSalesScreenMapper">

    <sql id="completedOrderInYear">
        o.del_flag = '0'
        and o.order_status = '1'
        and o.finish_time &gt;= #{yearStart}
        and o.finish_time &lt;= #{yearEnd}
    </sql>

    <select id="selectTradeOverview" resultType="com.ruoyi.web.modules.screen.domain.dto.TradeSalesOverviewDto">
        select count(*) as orderCount,
               coalesce(sum(total_heads), 0) as tradeHeads,
               coalesce(sum(total_amount), 0) as tradeAmount
        from biz_trade_order o
        where <include refid="completedOrderInYear"/>
    </select>

    <select id="selectMonthlyTradeByYear"
            resultType="com.ruoyi.web.modules.screen.domain.dto.TradeSalesMonthTradeDto">
        select year(o.finish_time) as statYear,
               month(o.finish_time) as month,
               coalesce(sum(o.total_heads), 0) as tradeHeads,
               coalesce(sum(o.total_amount), 0) as tradeAmount
        from biz_trade_order o
        where <include refid="completedOrderInYear"/>
        group by year(o.finish_time), month(o.finish_time)
    </select>

    <select id="selectSalesDestinationByYear"
            resultType="com.ruoyi.web.modules.screen.domain.dto.TradeSalesDestinationCountDto">
        select d.sales_destination as salesDestination,
               coalesce(sum(o.total_heads), 0) as tradeHeads
        from biz_trade_order o
        inner join biz_distributor d on d.id = o.distributor_id and d.del_flag = '0'
        where <include refid="completedOrderInYear"/>
        group by d.sales_destination
    </select>

    <select id="selectQualityGradeByYear"
            resultType="com.ruoyi.web.modules.screen.domain.dto.TradeSalesGradeCountDto">
        select l.grade_code as gradeCode,
               max(g.grade_name) as gradeName,
               count(*) as tradeHeads
        from biz_trade_order_line l
        inner join biz_trade_order o on o.id = l.order_id
        left join biz_grade_weight_config g on g.grade_code = l.grade_code
        where <include refid="completedOrderInYear"/>
          and l.grade_code is not null
          and l.grade_code != ''
        group by l.grade_code
    </select>

    <select id="countGradedLinesInYear" resultType="long">
        select count(*)
        from biz_trade_order_line l
        inner join biz_trade_order o on o.id = l.order_id
        where <include refid="completedOrderInYear"/>
          and l.grade_code is not null
          and l.grade_code != ''
    </select>

    <select id="selectQuotesInWindow"
            resultType="com.ruoyi.web.modules.screen.domain.dto.TradeSalesQuoteRawDto">
        select quote_date as quoteDate,
               price_min as priceMin,
               price_max as priceMax,
               avg_price as avgPrice,
               price_unit as priceUnit,
               create_time as createTime
        from biz_market_daily_quote
        where del_flag = '0'
          and trade_market_id = #{tradeMarketId}
          and quote_date &gt;= #{startDate}
          and quote_date &lt;= #{endDate}
    </select>

    <select id="selectTradeMarketIdByName" resultType="long">
        select id
        from biz_trade_market
        where del_flag = '0'
          and market_name = #{marketName}
        limit 1
    </select>

    <select id="selectDistinctFinishYears" resultType="int">
        select distinct year(finish_time)
        from biz_trade_order
        where del_flag = '0'
          and order_status = '1'
          and finish_time is not null
        order by year(finish_time) desc
    </select>

</mapper>
