<?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.sqx.modules.errand.dao.TbIndentSmsSendLogDao">
    <select id="logPageByRider" resultType="com.sqx.modules.errand.IndentSmsSendLogVO">
        select
               issl.*, o.delivery_imgs as imgs, u.phone as sendFromPhone
        from
            tb_indent_sms_send_log issl
        left join tb_indent i on i.indent_id = issl.order_id
        left join tb_order o on o.order_id = i.order_id
        left join tb_user u on u.user_id = issl.send_from_id
        <where>
                and issl.send_source = '1'
            <if test="query.orderId != null">
                and issl.order_id = #{query.orderId}
            </if>
            <if test="query.successFlag != null and query.successFlag != ''">
                and issl.success_flag = #{query.successFlag}
            </if>
            <if test="query.fromTo != null and query.fromTo != ''">
                and u.phone = #{query.fromTo}
            </if>
            <if test="query.sendTo != null and query.sendTo != ''">
                and issl.send_to = #{query.sendTo}
            </if>
            <if test="query.startTime != null">
                and issl.send_time >= #{query.startTime}
            </if>
            <if test="query.endTime != null">
                and issl.send_time <![CDATA[<=]]> concat(#{query.endTime},' 23:59:59')
            </if>
        </where>
        order by issl.send_time desc
    </select>

    <select id="logPageByShop" resultType="com.sqx.modules.errand.IndentSmsSendLogVO">
        select
        issl.*, o.delivery_imgs as imgs, gs.shop_name as sendFromShopName
        from
        tb_indent_sms_send_log issl
        left join tb_indent i on i.indent_id = issl.order_id
        left join tb_order o on o.order_id = i.order_id
        left join goods_shop gs on gs.shop_id = issl.send_from_id
        <where>
            and issl.send_source = '2'
            <if test="query.orderId != null">
                and issl.order_id = #{query.orderId}
            </if>
            <if test="query.successFlag != null and query.successFlag != ''">
                and issl.success_flag = #{query.successFlag}
            </if>
            <if test="query.shopId != null">
                and o.shop_id = #{query.shopId}
            </if>
            <if test="query.sendTo != null and query.sendTo != ''">
                and issl.send_to = #{query.sendTo}
            </if>
            <if test="query.startTime != null">
                and issl.send_time >= #{query.startTime}
            </if>
            <if test="query.endTime != null">
                and issl.send_time <![CDATA[<=]]> concat(#{query.endTime},' 23:59:59')
            </if>
        </where>
        order by issl.send_time desc
    </select>

    <select id="countOfSendSuccess" resultType="java.lang.Integer">
        select
               count(1)
        from
            tb_indent_sms_send_log issl
        left join tb_user u on u.user_id = issl.send_from_id
        where
            issl.success_flag = '1'
            <if test="query.fromTo != null and query.fromTo != ''">
                and u.phone = #{query.fromTo}
            </if>
            <if test="query.startTime != null">
                and issl.send_time >= #{query.startTime}
            </if>
            <if test="query.endTime != null">
                and issl.send_time <![CDATA[<=]]> #{query.endTime}
            </if>
    </select>
</mapper>