<?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="cn.roadm.mapper.SmsSignAutoExtractMapper">

    <resultMap id="SmsSignAutoExtractResultMap" type="cn.roadm.bo.SmsSignAutoExtract">
        <id column="id" property="id" jdbcType="BIGINT"/>
        <result column="sp_id" property="spId" jdbcType="VARCHAR"/>
        <result column="sign" property="sign" jdbcType="VARCHAR"/>
        <result column="channel_id" property="channelId" jdbcType="VARCHAR"/>
        <result column="channel_name" property="channelName" jdbcType="VARCHAR"/>
        <result column="type" property="type" jdbcType="VARCHAR"/>
        <result column="type_value" property="typeValue" jdbcType="VARCHAR"/>
        <result column="send_count" property="sendCount" jdbcType="BIGINT"/>
        <result column="create_time" property="createTime" jdbcType="TIMESTAMP"/>
        <result column="summary_date" property="summaryDate" jdbcType="VARCHAR"/>
        <result column="content" property="content" jdbcType="LONGVARCHAR"/>
        <result column="sp_name" property="spName" jdbcType="VARCHAR"/>
    </resultMap>

    <select id="listPageSmsSignAutoExtract" parameterType="cn.roadm.bo.SmsSignAutoExtract"
            resultMap="SmsSignAutoExtractResultMap">
        SELECT
            se.id,
            se.sp_id,
            se.sign,
            se.channel_id,
            sc.channel_name,
            se.type,
            se.type_value,
            se.send_count,
            se.create_time,
            se.summary_date,
            se.content,
            sp.sp_name
        FROM sms_sign_auto_extract se
        LEFT JOIN sms_sp_info sp ON se.sp_id = sp.id
        LEFT JOIN sms_send_channel_info sc ON se.channel_id = sc.id
        <where>
        <if test="spId != null and spId != ''">
            se.sp_id = #{spId}
        </if>
        <if test="sign != null and sign != ''">
            AND se.sign LIKE CONCAT('%', #{sign}, '%')
        </if>
        <if test="channelId != null and channelId != ''">
            AND se.channel_id = #{channelId}
        </if>
        <if test="type != null and type != ''">
            AND se.type = #{type}
        </if>
        <if test="typeValue != null and typeValue != ''">
            AND se.type_value LIKE CONCAT('%', #{typeValue}, '%')
        </if>
        <if test="summaryDateStart != null and summaryDateStart != ''">
            AND se.summary_date >= #{summaryDateStart}
        </if>
        <if test="summaryDateEnd != null and summaryDateEnd != ''">
            AND se.summary_date &lt;= #{summaryDateEnd}
        </if>
        <if test="createTimeStart != null and createTimeStart != ''">
            AND se.create_time &gt;=str_to_date(#{createTimeStart},'%Y-%m-%d')
        </if>
        <if test="createTimeEnd != null and createTimeEnd != ''">
            AND se.create_time &lt;DATE_ADD(str_to_date(#{createTimeEnd},'%Y-%m-%d'),INTERVAL 1 DAY)
        </if>
        <if test="spName != null and spName != ''">
            AND sp.sp_name LIKE CONCAT('%', #{spName}, '%')
        </if>
            <if test="sendCount != null">
                AND se.send_count &gt;= #{sendCount}
            </if>
        </where>
        ORDER BY se.create_time DESC
    </select>

    <select id="getSmsSignAutoExtractById" parameterType="java.lang.Long"
            resultMap="SmsSignAutoExtractResultMap">
        SELECT
            se.id,
            se.sp_id,
            se.sign,
            se.channel_id,
            sc.channel_name,
            se.type,
            se.type_value,
            se.send_count,
            se.create_time,
            se.summary_date,
            se.content,
            sp.sp_name
        FROM sms_sign_auto_extract se
        LEFT JOIN sms_sp_info sp ON se.sp_id = sp.id
        LEFT JOIN sms_send_channel_info sc ON se.channel_id = sc.id
        WHERE se.id = #{id}
    </select>

    <insert id="insertSmsSignAutoExtract" parameterType="cn.roadm.bo.SmsSignAutoExtract"
            useGeneratedKeys="true" keyProperty="id">
        INSERT INTO sms_sign_auto_extract (
            sp_id, sign, channel_id, type, type_value, send_count, summary_date, content, create_time
        ) VALUES (
            #{spId}, #{sign}, #{channelId}, #{type}, #{typeValue}, #{sendCount}, #{summaryDate}, #{content}, NOW()
        )
    </insert>

    <insert id="batchInsertSmsSignAutoExtract" parameterType="java.util.List">
        INSERT INTO sms_sign_auto_extract (
            sp_id, sign, channel_id, type, type_value, send_count, summary_date, content, create_time
        ) VALUES
        <foreach collection="list" item="item" separator=",">
            (#{item.spId}, #{item.sign}, #{item.channelId}, #{item.type}, #{item.typeValue}, #{item.sendCount}, #{item.summaryDate}, #{item.content}, NOW())
        </foreach>
    </insert>

    <select id="listForExport" parameterType="cn.roadm.bo.SmsSignAutoExtract"
            resultMap="SmsSignAutoExtractResultMap">
        SELECT
            se.id,
            se.sp_id,
            se.sign,
            se.channel_id,
            se.type,
            se.type_value,
            se.send_count,
            se.create_time,
            se.summary_date,
            se.content,
            sp.sp_name
        FROM sms_sign_auto_extract se
        LEFT JOIN sms_sp_info sp ON se.sp_id = sp.id
        <where>
            <if test="spId != null and spId != ''">
                se.sp_id = #{spId}
            </if>
            <if test="sign != null and sign != ''">
                AND se.sign LIKE CONCAT('%', #{sign}, '%')
            </if>
            <if test="channelId != null and channelId != ''">
                AND se.channel_id = #{channelId}
            </if>
            <if test="type != null and type != ''">
                AND se.type = #{type}
            </if>
            <if test="typeValue != null and typeValue != ''">
                AND se.type_value LIKE CONCAT('%', #{typeValue}, '%')
            </if>
            <if test="summaryDateStart != null and summaryDateStart != ''">
                AND se.summary_date >= #{summaryDateStart}
            </if>
            <if test="summaryDateEnd != null and summaryDateEnd != ''">
                AND se.summary_date &lt;= #{summaryDateEnd}
            </if>
            <if test="createTimeStart != null and createTimeStart != ''">
                AND se.create_time &gt;=str_to_date(#{createTimeStart},'%Y-%m-%d')
            </if>
            <if test="createTimeEnd != null and createTimeEnd != ''">
                AND se.create_time &lt;DATE_ADD(str_to_date(#{createTimeEnd},'%Y-%m-%d'),INTERVAL 1 DAY)
            </if>
            <if test="spName != null and spName != ''">
                AND sp.sp_name LIKE CONCAT('%', #{spName}, '%')
            </if>
            <if test="endId != null and endId != 0">
                AND se.id &lt; #{endId}
            </if>
            <if test="sendCount != null">
                AND se.send_count &gt;= #{sendCount}
            </if>
            ORDER BY se.id DESC
            <if test="exportExcelSize != null">
                LIMIT #{exportExcelSize}
            </if>
        </where>
    </select>

</mapper>