<?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.yc.ship.module.trade.dal.mysql.smsSendconfig.SmsSendconfigMapper">

    <!--
        一般情况下，尽可能使用 Mapper 进行 CRUD 增删改查即可。
        无法满足的场景，例如说多表关联查询，才使用 XML 编写 SQL。
        代码生成器暂时只生成 Mapper XML 文件本身，更多推荐 MybatisX 快速开发插件来生成查询。
        文档可见：https://www.iocoder.cn/MyBatis/x-plugins/
     -->
    <select id="querySendList" resultType="com.yc.ship.module.trade.controller.admin.smsSendconfig.vo.SmsSendconfigQueryRespVO">
        select * from (select t7.*,t1.group_no groupNo,t2.product_id,t2.product_name,t66.name voyageName,t66.start_time,t66.leave_time,t77.name shipName,t67.name routeName,t1.order_no,t1.seller_id,t2.use_date,count(distinct t2.id) num,t1.source_name,t1.link_man contact_name,t1.link_mobile mobile,t1.group_no,t6.params templateParams,t6.api_template_id,t8.api_key,t8.api_secret,t8.api_url,t8.signature,t8.code channelCode from
        trade_order t1
        inner join trade_detail t2 on t1.id = t2.order_id
        inner join trade_order_user t3 on t3.order_id = t1.id
        inner join product_voyage t66 on t66.id = t1.voyage_id
        inner join resource_route t67 on t66.route_id = t67.id
        inner join resource_ship t77 on t77.id = t1.ship_id
        inner join system_sms_product t5 on (t67.id=t5.product_id )
        inner join system_sms_sendconfig t7 on t7.id = t5.sms_template_id
        inner join system_sms_template t6 on t7.sms_template_id = t6.id
        inner join system_sms_channel t8 on t8.id = t6.channel_id
        where t2.use_date = DATE_SUB(CURDATE(), INTERVAL (t7.send_date_type-1) DAY)
        and t2.voucher_status in (1,2,6)
        and t1.deleted = 0
        and t2.deleted = 0
        and t7.deleted = 0
        and t1.is_use = 1
        <if test="id != null">
            AND t1.id = #{id}
        </if>
        <if test="type != null and type == 0">
            AND t1.send_type = 2
        </if>
        and t6.send_type = 1
        GROUP BY t1.id,t4.id
        ) m	where  not EXISTS (
            select p1.order_no from system_sms_send_log p1 where status = 1 and m.order_no=p1.order_no and p1.sms_config_id = m.id
        )
    </select>

    <select id="querySendLogList" resultType="com.yc.ship.module.trade.controller.admin.smsSendconfig.vo.SmsSendconfigQueryRespVO">
        select t1.*,t2.source_name,t5.contact_name from
        system_sms_send_log t1
        inner join trade_order t2 on t1.order_no = t2.order_no
        inner join trade_order_user t5 on t2.id=t5.order_id
        where 1=1
        <if test="param.smsConfigId != null">
            AND t1.sms_config_id = #{param.smsConfigId}
        </if>
        <if test="param.contact != null and param.contact!=''">
            AND (t5.contact_name = #{param.contact} or t1.mobile = #{param.contact})
        </if>
        GROUP BY t1.id
        order by t1.create_time desc
    </select>

</mapper>