<?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">
        SELECT date_series.date AS date,
               COALESCE(COUNT(s.id), 0) AS scanCount,
               COALESCE(COUNT(br.id), 0) AS orderCount,
               COALESCE(SUM(br.order_price), 0) AS totalOrderPrice,
               COALESCE(SUM(br.broker_price), 0) AS totalBrokerPrice
        FROM (
            SELECT DATE_ADD(#{startTime}, INTERVAL (units.i + tens.i * 10 + hundreds.i * 100) DAY) AS date
            FROM (SELECT 0 AS i UNION ALL SELECT 1 UNION ALL SELECT 2 UNION ALL SELECT 3 UNION ALL SELECT 4 UNION ALL SELECT 5 UNION ALL SELECT 6 UNION ALL SELECT 7 UNION ALL SELECT 8 UNION ALL SELECT 9) units
            CROSS JOIN (SELECT 0 AS i UNION ALL SELECT 1 UNION ALL SELECT 2 UNION ALL SELECT 3 UNION ALL SELECT 4 UNION ALL SELECT 5 UNION ALL SELECT 6 UNION ALL SELECT 7 UNION ALL SELECT 8 UNION ALL SELECT 9) tens
            CROSS JOIN (SELECT 0 AS i UNION ALL SELECT 1 UNION ALL SELECT 2 UNION ALL SELECT 3 UNION ALL SELECT 4 UNION ALL SELECT 5 UNION ALL SELECT 6 UNION ALL SELECT 7 UNION ALL SELECT 8 UNION ALL SELECT 9) hundreds
            WHERE DATE_ADD(#{startTime}, INTERVAL (units.i + tens.i * 10 + hundreds.i * 100) DAY) &lt;= #{endTime}
        ) date_series
        LEFT JOIN broker_record br ON DATE(br.create_time) = date_series.date AND br.broker_id = #{brokerId}
        LEFT JOIN statistics s ON s.type = 20 and DATE(s.create_time) = br.create_time and s.morph_id = br.broker_id
        GROUP BY date_series.date
        ORDER BY date_series.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>