<?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.sckw.order.dao.KwoTradeOrderMapper">


    <select id="getMaxOrderNoByDate" resultType="java.lang.String">
        SELECT MAX(t_order_no)
        FROM kwo_trade_order
        WHERE create_time &gt;= #{date};
    </select>

    <select id="getOrderNumByTopEntId" resultType="com.sckw.order.api.model.TradeOrderCountStatisticsDTO">
        SELECT u.unit_type AS orderType,
               COUNT(*)    AS num
        FROM kwo_trade_order o
                 LEFT JOIN kwo_trade_order_unit u
                           ON o.id = u.t_order_id
        <where>
            o.del_flag = 0 AND o.status NOT IN (0, 2) AND u.del_flag = 0
            <if test="topEntId != null ">
                and u.top_ent_id = #{topEntId}
            </if>
        </where>
        GROUP BY u.unit_type
    </select>

    <select id="workbenchExcel" resultType="com.sckw.order.model.dto.WbTOrderExcelStsResDTO">
        SELECT DATE_FORMAT(create_time, #{item.dateFormat}) AS formatDate,
               SUM(CASE
                       WHEN #{item.stsFactorsOne} = 1 THEN amount
                       WHEN #{item.stsFactorsOne} = 2 THEN 1
                       ELSE 0
                   END)                                     AS stsFactorsOneValue,
               SUM(CASE
                       WHEN #{item.stsFactorsTwo} = 1 THEN amount
                       WHEN #{item.stsFactorsTwo} = 2 THEN 1
                       ELSE 0
                   END)                                     AS stsFactorsTwoValue
        FROM kwo_trade_order
        WHERE
          id IN (SELECT t_order_id
                FROM kwo_trade_order_unit
                WHERE del_flag = 0
                AND unit_type = #{item.unitType}
                AND top_ent_id = #{item.topEntId})
          AND create_time >= #{item.startTime}
          AND status NOT IN (0, 2)
          AND del_flag = 0
        GROUP BY DATE_FORMAT(create_time, #{item.dateFormat});
    </select>
</mapper>