<?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.ycwl.basic.mapper.BrokerRecordMapper">
    <select id="list" resultType="com.ycwl.basic.model.pc.broker.resp.BrokerRecordRespVO">
        select id, broker_id, order_id, order_price, broker_rate, broker_price, reason, create_time
        from broker_record
        <where>
            <if test="brokerId != null">
                and broker_id = #{brokerId}
            </if>
            <if test="orderId != null">
                and order_id = #{orderId}
            </if>
            <if test="startTime != null">
                and create_time &gt;= #{startTime}
            </if>
            <if test="endTime != null">
                and create_time &lt;= #{endTime}
            </if>
        </where>
        order by create_time desc
    </select>

    <select id="getById" resultType="com.ycwl.basic.model.pc.broker.resp.BrokerRecordRespVO">
        select id, broker_id, order_id, order_price, broker_rate, broker_price, reason, create_time
        from broker_record
        where id = #{id}
    </select>

    <select id="getDailySummaryByBrokerId" resultType="com.ycwl.basic.model.pc.broker.resp.DailySummaryRespVO">
        WITH RECURSIVE
            date_series AS (SELECT DATE(#{startTime}) AS date
                            UNION ALL
                            SELECT DATE_ADD(date, INTERVAL 1 DAY)
                            FROM date_series
                            WHERE date &lt; DATE(#{endTime})),
            scan_stats AS (SELECT DATE(create_time)  AS date,
                                  COUNT(DISTINCT id) AS scanCount
                           FROM t_stats_record
                           WHERE action = 'CODE_SCAN'
                             AND identifier = #{brokerId}
                             AND DATE(create_time) BETWEEN
                               DATE(#{startTime}) AND DATE(#{endTime})
                           GROUP BY DATE(create_time)),
            order_stats AS (SELECT DATE(create_time)              AS date,
                                   COUNT(DISTINCT id)             AS orderCount,
                                   COALESCE(SUM(order_price), 0)  AS
                                                                     totalOrderPrice,
                                   COALESCE(SUM(broker_price), 0) AS
                                                                     totalBrokerPrice
                            FROM broker_record
                            WHERE broker_id = #{brokerId}
                              AND DATE(create_time) BETWEEN
                                DATE(#{startTime}) AND DATE(#{endTime})
                            GROUP BY DATE(create_time))
        SELECT ds.date,
               COALESCE(ss.scanCount, 0)        AS scanCount,
               COALESCE(os.orderCount, 0)       AS orderCount,
               COALESCE(os.totalOrderPrice, 0)  AS
                                                   totalOrderPrice,
               COALESCE(os.totalBrokerPrice, 0) AS
                                                   totalBrokerPrice
        FROM date_series ds
                 LEFT JOIN scan_stats ss ON ds.date = ss.date
                 LEFT JOIN order_stats os ON ds.date = os.date
        ORDER BY ds.date

    </select>

    <insert id="add">
        insert into broker_record(broker_id, order_id, order_price, broker_rate, broker_price, direct_price, reason, create_time)
        values (#{brokerId}, #{orderId}, #{orderPrice}, #{brokerRate}, #{brokerPrice}, #{directPrice}, #{reason}, now())
    </insert>

    <delete id="deleteById">
        delete from broker_record
        where id = #{id}
    </delete>
    <delete id="deleteByOrderId">
        delete from broker_record
        where order_id = #{orderId}
    </delete>

    <update id="update">
        update broker_record
        <set>
            <if test="brokerId != null">
                broker_id = #{brokerId},
            </if>
            <if test="orderId != null">
                order_id = #{orderId},
            </if>
            <if test="orderPrice != null">
                order_price = #{orderPrice},
            </if>
            <if test="brokerRate != null">
                broker_rate = #{brokerRate},
            </if>
            <if test="brokerPrice != null">
                broker_price = #{brokerPrice},
            </if>
            <if test="reason != null">
                reason = #{reason},
            </if>
        </set>
        where id = #{id}
    </update>
</mapper>