<?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.personsubscribemessage.PersonSubscribeMessageMapper">

    <resultMap type="PersonSubscribeMessage" id="PersonSubscribeMessageResult">
        <result property="id" column="id"/>
        <result property="nurseStationPersonId" column="nurse_station_person_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="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="selectPersonSubscribeMessageVo">
        select id,
               nurse_station_person_id,
               unionid,
               openid,
               applet_id,
               template_id,
               message_type,
               subscribe_time,
               subscribe_status,
               create_by,
               create_time,
               update_by,
               update_time
        from person_subscribe_message
    </sql>

    <select id="selectPersonSubscribeMessageList" parameterType="PersonSubscribeMessage"
            resultMap="PersonSubscribeMessageResult">
        <include refid="selectPersonSubscribeMessageVo"/>
        <where>
            <if test="nurseStationPersonId != null ">
                and nurse_station_person_id = #{nurseStationPersonId}
            </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="subscribeTime != null ">
                and subscribe_time = #{subscribeTime}
            </if>
            <if test="subscribeStatus != null  and subscribeStatus != ''">
                and subscribe_status = #{subscribeStatus}
            </if>
        </where>
    </select>

    <select id="selectPersonSubscribeMessageById" parameterType="Long"
            resultMap="PersonSubscribeMessageResult">
        <include refid="selectPersonSubscribeMessageVo"/>
        where id = #{id}
    </select>

    <insert id="insertPersonSubscribeMessage" parameterType="PersonSubscribeMessage" useGeneratedKeys="true"
            keyProperty="id">
        insert into person_subscribe_message
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="nurseStationPersonId != null">nurse_station_person_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="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="nurseStationPersonId != null">#{nurseStationPersonId},
            </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="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="updatePersonSubscribeMessage" parameterType="PersonSubscribeMessage">
        update person_subscribe_message
        <trim prefix="SET" suffixOverrides=",">
            <if test="nurseStationPersonId != null">nurse_station_person_id =
                #{nurseStationPersonId},
            </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="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="deletePersonSubscribeMessageById" parameterType="Long">
        delete
        from person_subscribe_message
        where id = #{id}
    </delete>

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

    <select id="selectPersonSubscribeMessageByPatientId" parameterType="PersonSubscribeMessage"
            resultMap="PersonSubscribeMessageResult">
        <include refid="selectPersonSubscribeMessageVo"/>
        <where>
            <if test="nurseStationPersonId != null ">
                and nurse_station_person_id = #{nurseStationPersonId}
            </if>
            <if test="openid != null  and openid != ''">
                and openid = #{openid}
            </if>
            <if test="templateId != null  and templateId != ''">
                and template_id = #{templateId}
            </if>
        </where>
    </select>


    <select id="selectPersonSubscribeMessageByCount"
            resultType="Integer">
        select COUNT(1)
        from person_subscribe_message
        <where>
            <if test="nurseStationPersonId != null ">
                and nurse_station_person_id = #{nurseStationPersonId}
            </if>
            <if test="unionid != null  and unionid != ''">
                and unionid = #{unionid}
            </if>
            <if test="openid != null  and openid != ''">
                and openid = #{openid}
            </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>
        </where>
    </select>

    <select id="selectPersonSubscribeList"
            resultType="PersonSubscribeMessage">
        select id,
        nurse_station_person_id,
        unionid,
        openid,
        template_id,
        message_type,
        subscribe_time,
        applet_id,
        subscribe_status
        from person_subscribe_message
        where patient_id in
        <foreach item="list" collection="list" open="(" separator="," close=")">
            #{list}
        </foreach>
        and subscribe_status =#{subscribeStatus}
        and message_type =#{messageType}
    </select>

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

    <update id="updatePersonSubscribeMessageTemplateId">
        UPDATE person_subscribe_message
        SET nurse_station_person_id = #{nurseStationPersonId},
            subscribe_status        = #{subscribeStatus},
            update_time             = now()
        WHERE openid = #{openId}
          AND template_id = #{templateId}
    </update>

    <select id="selectPersonSubscribeMessageByPersonWechatId" parameterType="PersonSubscribeMessage"
            resultType="com.xinelu.manage.vo.personsubscribemessage.PersonSubscribeMessageVO">
        SELECT
        psm.id,
        psm.nurse_station_person_id,
        psm.unionid,
        psm.openid,
        psm.applet_id,
        psm.template_id,
        psm.message_type,
        psm.subscribe_time,
        psm.subscribe_status,
        psm.create_by,
        psm.create_time,
        psm.update_by,
        psm.update_time,
        nspw.nurse_station_id,
        nspw.user_id,
        nspw.phone
        FROM
        person_subscribe_message psm
        INNER JOIN nurse_manage_person_wechat_info nspw ON psm.openid = nspw.openid
        <where>
            <if test="nurseStationId != null ">
                FIND_IN_SET(#{nurseStationId},nspw.nurse_station_id)
            </if>
            <if test="nurseStationPersonId != null ">
                and psm.nurse_station_person_id = #{nurseStationPersonId}
            </if>
            <if test="openid != null  and openid != ''">
                and psm.openid = #{openid}
            </if>
            <if test="templateId != null  and templateId != ''">
                and psm.template_id = #{templateId}
            </if>
        </where>
    </select>

    <select id="selectPersonSubscribeMessageListByIds"
            resultType="com.xinelu.manage.domain.personsubscribemessage.PersonSubscribeMessage">
        select id,
        nurse_station_person_id,
        unionid,
        openid,
        template_id,
        message_type,
        subscribe_time,
        applet_id,
        subscribe_status
        from person_subscribe_message
        where nurse_station_person_id in
        <foreach item="list" collection="list" open="(" separator="," close=")">
            #{list}
        </foreach>
        and subscribe_status =#{subscribeStatus}
        and message_type =#{messageType}
    </select>
</mapper>
