<?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.xinelu.manage.mapper.subscribemessagerecord.SubscribeMessageRecordMapper">

    <resultMap type="SubscribeMessageRecord" id="SubscribeMessageRecordResult">
        <result property="id" column="id"/>
        <result property="patientId" column="patient_id"/>
        <result property="manageRouteNodeId" column="manage_route_node_id"/>
        <result property="unionid" column="unionid"/>
        <result property="openid" column="openid"/>
        <result property="appletId" column="applet_id"/>
        <result property="templateId" column="template_id"/>
        <result property="messageType" column="message_type"/>
        <result property="subscribeCount" column="subscribe_count"/>
        <result property="subscribeTime" column="subscribe_time"/>
        <result property="subscribeStatus" column="subscribe_status"/>
        <result property="createBy" column="create_by"/>
        <result property="createTime" column="create_time"/>
        <result property="updateBy" column="update_by"/>
        <result property="updateTime" column="update_time"/>
    </resultMap>

    <sql id="selectSubscribeMessageRecordVo">
        select id, patient_id, manage_route_node_id, unionid, openid, applet_id, template_id, message_type, subscribe_count, subscribe_time, subscribe_status, create_by, create_time, update_by, update_time from subscribe_message_record
    </sql>

    <select id="selectSubscribeMessageRecordList" parameterType="SubscribeMessageRecord"
            resultMap="SubscribeMessageRecordResult">
        <include refid="selectSubscribeMessageRecordVo"/>
        <where>
            <if test="patientId != null ">
                and patient_id = #{patientId}
            </if>
            <if test="manageRouteNodeId != null ">
                and manage_route_node_id = #{manageRouteNodeId}
            </if>
            <if test="unionid != null  and unionid != ''">
                and unionid = #{unionid}
            </if>
            <if test="openid != null  and openid != ''">
                and openid = #{openid}
            </if>
            <if test="appletId != null  and appletId != ''">
                and applet_id = #{appletId}
            </if>
            <if test="templateId != null  and templateId != ''">
                and template_id = #{templateId}
            </if>
            <if test="messageType != null  and messageType != ''">
                and message_type = #{messageType}
            </if>
            <if test="subscribeCount != null ">
                and subscribe_count = #{subscribeCount}
            </if>
            <if test="subscribeTime != null ">
                and subscribe_time = #{subscribeTime}
            </if>
            <if test="subscribeStatus != null  and subscribeStatus != ''">
                and subscribe_status = #{subscribeStatus}
            </if>
        </where>
    </select>

    <select id="selectSubscribeMessageRecordById" parameterType="Long"
            resultMap="SubscribeMessageRecordResult">
        <include refid="selectSubscribeMessageRecordVo"/>
        where id = #{id}
    </select>

    <insert id="insertSubscribeMessageRecord" parameterType="SubscribeMessageRecord" useGeneratedKeys="true"
            keyProperty="id">
        insert into subscribe_message_record
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="patientId != null">patient_id,
            </if>
            <if test="manageRouteNodeId != null">manage_route_node_id,
            </if>
            <if test="unionid != null">unionid,
            </if>
            <if test="openid != null">openid,
            </if>
            <if test="appletId != null">applet_id,
            </if>
            <if test="templateId != null">template_id,
            </if>
            <if test="messageType != null">message_type,
            </if>
            <if test="subscribeCount != null">subscribe_count,
            </if>
            <if test="subscribeTime != null">subscribe_time,
            </if>
            <if test="subscribeStatus != null">subscribe_status,
            </if>
            <if test="createBy != null">create_by,
            </if>
            <if test="createTime != null">create_time,
            </if>
            <if test="updateBy != null">update_by,
            </if>
            <if test="updateTime != null">update_time,
            </if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="patientId != null">#{patientId},
            </if>
            <if test="manageRouteNodeId != null">#{manageRouteNodeId},
            </if>
            <if test="unionid != null">#{unionid},
            </if>
            <if test="openid != null">#{openid},
            </if>
            <if test="appletId != null">#{appletId},
            </if>
            <if test="templateId != null">#{templateId},
            </if>
            <if test="messageType != null">#{messageType},
            </if>
            <if test="subscribeCount != null">#{subscribeCount},
            </if>
            <if test="subscribeTime != null">#{subscribeTime},
            </if>
            <if test="subscribeStatus != null">#{subscribeStatus},
            </if>
            <if test="createBy != null">#{createBy},
            </if>
            <if test="createTime != null">#{createTime},
            </if>
            <if test="updateBy != null">#{updateBy},
            </if>
            <if test="updateTime != null">#{updateTime},
            </if>
        </trim>
    </insert>

    <update id="updateSubscribeMessageRecord" parameterType="SubscribeMessageRecord">
        update subscribe_message_record
        <trim prefix="SET" suffixOverrides=",">
            <if test="patientId != null">patient_id =
                #{patientId},
            </if>
            <if test="manageRouteNodeId != null">manage_route_node_id =
                #{manageRouteNodeId},
            </if>
            <if test="unionid != null">unionid =
                #{unionid},
            </if>
            <if test="openid != null">openid =
                #{openid},
            </if>
            <if test="appletId != null">applet_id =
                #{appletId},
            </if>
            <if test="templateId != null">template_id =
                #{templateId},
            </if>
            <if test="messageType != null">message_type =
                #{messageType},
            </if>
            <if test="subscribeCount != null">subscribe_count =
                #{subscribeCount},
            </if>
            <if test="subscribeTime != null">subscribe_time =
                #{subscribeTime},
            </if>
            <if test="subscribeStatus != null">subscribe_status =
                #{subscribeStatus},
            </if>
            <if test="createBy != null">create_by =
                #{createBy},
            </if>
            <if test="createTime != null">create_time =
                #{createTime},
            </if>
            <if test="updateBy != null">update_by =
                #{updateBy},
            </if>
            <if test="updateTime != null">update_time =
                #{updateTime},
            </if>
        </trim>
        where id = #{id}
    </update>

    <delete id="deleteSubscribeMessageRecordById" parameterType="Long">
        delete from subscribe_message_record where id = #{id}
    </delete>

    <delete id="deleteSubscribeMessageRecordByIds" parameterType="String">
        delete from subscribe_message_record where id in
        <foreach item="id" collection="array" open="(" separator="," close=")">
            #{id}
        </foreach>
    </delete>

    <insert id="insertSubscribeMessageRecordList" parameterType="java.util.List">
        insert into subscribe_message_record(
        patient_id,
        unionid,
        openid,
        template_id,
        message_type,
        subscribe_count,
        subscribe_time,
        applet_id,
        subscribe_status,
        create_by,
        create_time,
        update_by,
        update_time
        ) values
        <foreach item="subscribeMessageRecordList" index="index" collection="list" separator=",">
            (
            #{subscribeMessageRecordList.patientId},
            #{subscribeMessageRecordList.unionid},
            #{subscribeMessageRecordList.openid},
            #{subscribeMessageRecordList.templateId},
            #{subscribeMessageRecordList.messageType},
            #{subscribeMessageRecordList.subscribeCount},
            #{subscribeMessageRecordList.subscribeTime},
            #{subscribeMessageRecordList.appletId},
            #{subscribeMessageRecordList.subscribeStatus},
            #{subscribeMessageRecordList.createBy},
            #{subscribeMessageRecordList.createTime},
            #{subscribeMessageRecordList.updateBy},
            #{subscribeMessageRecordList.updateTime}
            )
        </foreach>
    </insert>

    <update id="updateSubscribeMessageTemplateId">
        update subscribe_message_record
        set patient_id = #{patientId}, subscribe_status = #{subscribeStatus}, update_time = now()
        where openid = #{openId} and template_id = #{templateId}
    </update>
</mapper>