<?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.subscribemessagesendrecord.SubscribeMessageSendRecordMapper">

    <resultMap type="SubscribeMessageSendRecord" id="SubscribeMessageSendRecordResult">
        <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="subscribeTime" column="subscribe_time"/>
        <result property="templateId" column="template_id"/>
        <result property="msgId" column="msg_id"/>
        <result property="messageType" column="message_type"/>
        <result property="errorCode" column="error_code"/>
        <result property="errorStatus" column="error_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="selectSubscribeMessageSendRecordVo">
        select id, patient_id, manage_route_node_id, unionid, openid, applet_id, subscribe_time, template_id, msg_id, message_type, error_code, error_status, create_by, create_time, update_by, update_time from subscribe_message_send_record
    </sql>

    <select id="selectSubscribeMessageSendRecordList" parameterType="SubscribeMessageSendRecord"
            resultMap="SubscribeMessageSendRecordResult">
        <include refid="selectSubscribeMessageSendRecordVo"/>
        <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="subscribeTime != null ">
                and subscribe_time = #{subscribeTime}
            </if>
            <if test="templateId != null  and templateId != ''">
                and template_id = #{templateId}
            </if>
            <if test="msgId != null  and msgId != ''">
                and msg_id = #{msgId}
            </if>
            <if test="messageType != null  and messageType != ''">
                and message_type = #{messageType}
            </if>
            <if test="errorCode != null ">
                and error_code = #{errorCode}
            </if>
            <if test="errorStatus != null  and errorStatus != ''">
                and error_status = #{errorStatus}
            </if>
        </where>
    </select>

    <select id="selectSubscribeMessageSendRecordById" parameterType="Long"
            resultMap="SubscribeMessageSendRecordResult">
        <include refid="selectSubscribeMessageSendRecordVo"/>
        where id = #{id}
    </select>

    <insert id="insertSubscribeMessageSendRecord" parameterType="SubscribeMessageSendRecord" useGeneratedKeys="true"
            keyProperty="id">
        insert into subscribe_message_send_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="subscribeTime != null">subscribe_time,
            </if>
            <if test="templateId != null">template_id,
            </if>
            <if test="msgId != null">msg_id,
            </if>
            <if test="messageType != null">message_type,
            </if>
            <if test="errorCode != null">error_code,
            </if>
            <if test="errorStatus != null">error_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="subscribeTime != null">#{subscribeTime},
            </if>
            <if test="templateId != null">#{templateId},
            </if>
            <if test="msgId != null">#{msgId},
            </if>
            <if test="messageType != null">#{messageType},
            </if>
            <if test="errorCode != null">#{errorCode},
            </if>
            <if test="errorStatus != null">#{errorStatus},
            </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="updateSubscribeMessageSendRecord" parameterType="SubscribeMessageSendRecord">
        update subscribe_message_send_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="subscribeTime != null">subscribe_time =
                #{subscribeTime},
            </if>
            <if test="templateId != null">template_id =
                #{templateId},
            </if>
            <if test="msgId != null">msg_id =
                #{msgId},
            </if>
            <if test="messageType != null">message_type =
                #{messageType},
            </if>
            <if test="errorCode != null">error_code =
                #{errorCode},
            </if>
            <if test="errorStatus != null">error_status =
                #{errorStatus},
            </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="deleteSubscribeMessageSendRecordById" parameterType="Long">
        delete from subscribe_message_send_record where id = #{id}
    </delete>

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

    <insert id="insertSubscribeMessageSendRecordList" parameterType="java.util.List">
        insert into subscribe_message_send_record(
        openid,
        applet_id,
        subscribe_time,
        template_id,
        msg_id,
        message_type,
        error_code,
        error_status,
        create_time
        ) values
        <foreach item="sendRecord" index="index" collection="list" separator=",">
            (
            #{sendRecord.openid},
            #{sendRecord.appletId},
            #{sendRecord.subscribeTime},
            #{sendRecord.templateId},
            #{sendRecord.msgId},
            #{sendRecord.messageType},
            #{sendRecord.errorCode},
            #{sendRecord.errorStatus},
            #{sendRecord.createTime}
            )
        </foreach>
    </insert>

    <select id="selectRecordCountByManageRouteNodeIds"
            resultType="com.xinelu.manage.vo.subscribemessagesendrecord.RecordNum">
        select
        spmrn.id manageRouteNodeId,
        COUNT(smsr.id) num
        FROM
        sign_patient_manage_route_node spmrn
        LEFT JOIN subscribe_message_send_record smsr ON spmrn.id = smsr.manage_route_node_id
        where spmrn.id IN
        <foreach item="manageRouteNodeIds" collection="manageRouteNodeIds" open="(" separator="," close=")">
            #{manageRouteNodeIds}
        </foreach>
    </select>

	<select id="getStatisticNum" parameterType="com.xinelu.manage.dto.statistics.FollowUpRateDto"
		resultType="com.xinelu.manage.vo.statistics.MessageSendVo">
		select
		record.patient_id,
		date_format(record.subscribe_time, '%Y-%m') as sendTimeMonthStr
		from subscribe_message_send_record record left join patient_info patient on patient.id = record.patient_id
		<where>
			<if test="hospitalAgencyId != null">
				and patient.hospital_agency_id = #{hospitalAgencyId}
			</if>
			<if test="campusAgencyId != null">
				and patient.campus_agency_id = #{campusAgencyId}
			</if>
			<if test="departmentId != null">
				and patient.department_id = #{departmentId}
			</if>
			<if test="wardId != null">
				and patient.ward_id = #{wardId}
			</if>
			<if test="startDate != null">
				and date_format(record.subscribe_time,'%y%m') &gt;= date_format(#{startDate},'%y%m')
			</if>
			<if test="endDate != null">
				and date_format(record.subscribe_time,'%y%m') &lt;= date_format(#{endDate},'%y%m')
			</if>
		</where>
	</select>
</mapper>