<?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.flossom.common.core.mapper.WxUserScriptLogMapper">
    
    <resultMap type="WxUserScriptLog" id="WxUserScriptLogResult">
        <result property="id"    column="id"    />
        <result property="wxUserId"    column="wx_user_id"    />
        <result property="messageType"    column="message_type"   />
        <result property="isRead"    column="is_read"    />
        <result property="isCustom"    column="is_custom"    />
        <result property="scriptTemplateId"    column="script_template_id"    />
        <result property="scriptName"    column="script_name"    />
        <result property="titile"    column="titile"    />
        <result property="content"    column="content"    />
        <result property="tagType"    column="tag_type"    />
        <result property="type"    column="type"    />
        <result property="link"    column="link"    />
        <result property="linkParams"    column="link_params"    />
        <result property="redirectAppid"    column="redirect_appid"    />
        <result property="redirectUrl"    column="redirect_url"    />
        <result property="videoNo"    column="video_no"    />
        <result property="feedId"    column="feed_id"    />
        <result property="tagNames"    column="tag_names"    />
        <result property="tagIds"    column="tag_ids"    />
        <result property="status"    column="status"    />
        <result property="createBy"    column="create_by"    />
        <result property="createTime"    column="create_time"    />
    </resultMap>

    <sql id="selectWxUserScriptLogVo">
        select id, wx_user_id, message_type, is_read, is_custom, script_template_id, script_name, titile, content, tag_type, type, link, link_params, redirect_appid, redirect_url, video_no, feed_id, tag_names, tag_ids, status, create_by, create_time from wx_user_script_log
    </sql>

    <select id="selectWxUserScriptLogList" parameterType="WxUserScriptLog" resultMap="WxUserScriptLogResult">
        <include refid="selectWxUserScriptLogVo"/>
        <where>  
            <if test="wxUserId != null "> and wx_user_id = #{wxUserId}</if>
            <if test="isRead != null "> and is_read = #{isRead}</if>
            <if test="messageType != null "> and message_type = #{messageType}</if>
            <if test="isCustom != null "> and is_custom = #{isCustom}</if>
            <if test="scriptTemplateId != null "> and script_template_id = #{scriptTemplateId}</if>
            <if test="scriptName != null  and scriptName != ''"> and script_name like concat('%', #{scriptName}, '%')</if>
            <if test="titile != null  and titile != ''"> and titile = #{titile}</if>
            <if test="content != null  and content != ''"> and content = #{content}</if>
            <if test="tagType != null "> and tag_type = #{tagType}</if>
            <if test="type != null "> and type = #{type}</if>
            <if test="link != null  and link != ''"> and link = #{link}</if>
            <if test="linkParams != null  and linkParams != ''"> and link_params = #{linkParams}</if>
            <if test="redirectAppid != null  and redirectAppid != ''"> and redirect_appid = #{redirectAppid}</if>
            <if test="redirectUrl != null  and redirectUrl != ''"> and redirect_url = #{redirectUrl}</if>
            <if test="videoNo != null  and videoNo != ''"> and video_no = #{videoNo}</if>
            <if test="feedId != null  and feedId != ''"> and feed_id = #{feedId}</if>
            <if test="tagNames != null  and tagNames != ''"> and tag_names = #{tagNames}</if>
            <if test="tagIds != null  and tagIds != ''"> and tag_ids = #{tagIds}</if>
            <if test="status != null "> and status = #{status}</if>
        </where>
        order by create_time desc
    </select>
    
    <select id="selectWxUserScriptLogById" parameterType="Long" resultMap="WxUserScriptLogResult">
        <include refid="selectWxUserScriptLogVo"/>
        where id = #{id}
    </select>

    <select id="getNoReadMessageNum" resultType="java.lang.Integer">
        select count(1) from wx_user_script_log
        <where>
            <if test="wxUserId != null "> and wx_user_id = #{wxUserId}</if>
            <if test="isRead != null "> and is_read = #{isRead}</if>
            <if test="status != null "> and status = #{status}</if>
        </where>
    </select>

    <insert id="insertWxUserScriptLog" parameterType="WxUserScriptLog" useGeneratedKeys="true" keyProperty="id">
        insert into wx_user_script_log
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="wxUserId != null">wx_user_id,</if>
            <if test="isRead != null">is_read,</if>
            <if test="messageType != null">message_type,</if>
            <if test="isCustom != null">is_custom,</if>
            <if test="scriptTemplateId != null">script_template_id,</if>
            <if test="scriptName != null">script_name,</if>
            <if test="titile != null">titile,</if>
            <if test="content != null">content,</if>
            <if test="tagType != null">tag_type,</if>
            <if test="type != null">type,</if>
            <if test="link != null">link,</if>
            <if test="linkParams != null">link_params,</if>
            <if test="redirectAppid != null">redirect_appid,</if>
            <if test="redirectUrl != null">redirect_url,</if>
            <if test="videoNo != null">video_no,</if>
            <if test="feedId != null">feed_id,</if>
            <if test="tagNames != null">tag_names,</if>
            <if test="tagIds != null">tag_ids,</if>
            <if test="status != null">status,</if>
            <if test="createBy != null">create_by,</if>
            <if test="createTime != null">create_time,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="wxUserId != null">#{wxUserId},</if>
            <if test="isRead != null">#{isRead},</if>
            <if test="messageType != null">#{messageType},</if>
            <if test="isCustom != null">#{isCustom},</if>
            <if test="scriptTemplateId != null">#{scriptTemplateId},</if>
            <if test="scriptName != null">#{scriptName},</if>
            <if test="titile != null">#{titile},</if>
            <if test="content != null">#{content},</if>
            <if test="tagType != null">#{tagType},</if>
            <if test="type != null">#{type},</if>
            <if test="link != null">#{link},</if>
            <if test="linkParams != null">#{linkParams},</if>
            <if test="redirectAppid != null">#{redirectAppid},</if>
            <if test="redirectUrl != null">#{redirectUrl},</if>
            <if test="videoNo != null">#{videoNo},</if>
            <if test="feedId != null">#{feedId},</if>
            <if test="tagNames != null">#{tagNames},</if>
            <if test="tagIds != null">#{tagIds},</if>
            <if test="status != null">#{status},</if>
            <if test="createBy != null">#{createBy},</if>
            <if test="createTime != null">#{createTime},</if>
         </trim>
    </insert>

    <update id="updateWxUserScriptLog" parameterType="WxUserScriptLog">
        update wx_user_script_log
        <trim prefix="SET" suffixOverrides=",">
            <if test="wxUserId != null">wx_user_id = #{wxUserId},</if>
            <if test="messageType != null">message_type = #{messageType},</if>
            <if test="isRead != null">is_read = #{isRead},</if>
            <if test="isCustom != null">is_custom = #{isCustom},</if>
            <if test="scriptTemplateId != null">script_template_id = #{scriptTemplateId},</if>
            <if test="scriptName != null">script_name = #{scriptName},</if>
            <if test="titile != null">titile = #{titile},</if>
            <if test="content != null">content = #{content},</if>
            <if test="tagType != null">tag_type = #{tagType},</if>
            <if test="type != null">type = #{type},</if>
            <if test="link != null">link = #{link},</if>
            <if test="linkParams != null">link_params = #{linkParams},</if>
            <if test="redirectAppid != null">redirect_appid = #{redirectAppid},</if>
            <if test="redirectUrl != null">redirect_url = #{redirectUrl},</if>
            <if test="videoNo != null">video_no = #{videoNo},</if>
            <if test="feedId != null">feed_id = #{feedId},</if>
            <if test="tagNames != null">tag_names = #{tagNames},</if>
            <if test="tagIds != null">tag_ids = #{tagIds},</if>
            <if test="status != null">status = #{status},</if>
            <if test="createBy != null">create_by = #{createBy},</if>
            <if test="createTime != null">create_time = #{createTime},</if>
        </trim>
        where id = #{id}
    </update>

    <update id="hasBeenRead">
        update wx_user_script_log set
        `is_read` = 1
        <where>
            <if test="wxUserId != null "> and wx_user_id = #{wxUserId}</if>
            <if test="isRead != null "> and is_read = #{isRead}</if>
            <if test="status != null "> and status = #{status}</if>
        </where>
    </update>

    <delete id="deleteWxUserScriptLogById" parameterType="Long">
        delete from wx_user_script_log where id = #{id}
    </delete>

    <delete id="deleteWxUserScriptLogByIds" parameterType="String">
        delete from wx_user_script_log where id in 
        <foreach item="id" collection="array" open="(" separator="," close=")">
            #{id}
        </foreach>
    </delete>
    <delete id="deleteWxUserScriptLogByWxUserId" parameterType="Long">
        delete from wx_user_script_log where wx_user_id = #{userId}
    </delete>


</mapper>