<?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.shortmessagesendrecord.ShortMessageSendRecordMapper">

    <resultMap type="ShortMessageSendRecord" id="ShortMessageSendRecordResult">
        <result property="id" column="id"/>
        <result property="patientId" column="patient_id"/>
        <result property="manageRouteNodeId" column="manage_route_node_id"/>
        <result property="patientPhone" column="patient_phone"/>
        <result property="sendTime" column="send_time"/>
        <result property="messageTemplateId" column="message_template_id"/>
        <result property="messageNodeContent" column="message_node_content"/>
        <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"/>
        <result property="messageType" column="message_type"/>
    </resultMap>

    <sql id="selectShortMessageSendRecordVo">
        select id,
               patient_id,
               manage_route_node_id,
               patient_phone,
               send_time,
               message_template_id,
               message_node_content,
               error_code,
               error_status,
               create_by,
               create_time,
               update_by,
               update_time,
               message_type
        from short_message_send_record
    </sql>

    <select id="selectShortMessageSendRecordList" parameterType="ShortMessageSendRecord"
            resultMap="ShortMessageSendRecordResult">
        <include refid="selectShortMessageSendRecordVo"/>
        <where>
            <if test="patientId != null ">
                and patient_id = #{patientId}
            </if>
            <if test="manageRouteNodeId != null ">
                and manage_route_node_id = #{manageRouteNodeId}
            </if>
            <if test="patientPhone != null  and patientPhone != ''">
                and patient_phone = #{patientPhone}
            </if>
            <if test="sendTime != null ">
                and send_time = #{sendTime}
            </if>
            <if test="messageTemplateId != null  and messageTemplateId != ''">
                and message_template_id = #{messageTemplateId}
            </if>
            <if test="messageNodeContent != null  and messageNodeContent != ''">
                and message_node_content = #{messageNodeContent}
            </if>
            <if test="errorCode != null ">
                and error_code = #{errorCode}
            </if>
            <if test="errorStatus != null  and errorStatus != ''">
                and error_status = #{errorStatus}
            </if>
            <if test="messageType != null  and messageType != ''">
                and message_type = #{messageType}
            </if>
        </where>
    </select>

    <select id="selectShortMessageSendRecordById" parameterType="Long"
            resultMap="ShortMessageSendRecordResult">
        <include refid="selectShortMessageSendRecordVo"/>
        where id = #{id}
    </select>

    <insert id="insertShortMessageSendRecord" parameterType="ShortMessageSendRecord" useGeneratedKeys="true"
            keyProperty="id">
        insert into short_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="patientPhone != null">patient_phone,
            </if>
            <if test="sendTime != null">send_time,
            </if>
            <if test="messageTemplateId != null">message_template_id,
            </if>
            <if test="messageNodeContent != null">message_node_content,
            </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>
            <if test="messageType != null">message_type,
            </if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="patientId != null">#{patientId},
            </if>
            <if test="manageRouteNodeId != null">#{manageRouteNodeId},
            </if>
            <if test="patientPhone != null">#{patientPhone},
            </if>
            <if test="sendTime != null">#{sendTime},
            </if>
            <if test="messageTemplateId != null">#{messageTemplateId},
            </if>
            <if test="messageNodeContent != null">#{messageNodeContent},
            </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>
            <if test="messageType != null">#{messageType},
            </if>
        </trim>
    </insert>

    <update id="updateShortMessageSendRecord" parameterType="ShortMessageSendRecord">
        update short_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="patientPhone != null">patient_phone =
                #{patientPhone},
            </if>
            <if test="sendTime != null">send_time =
                #{sendTime},
            </if>
            <if test="messageTemplateId != null">message_template_id =
                #{messageTemplateId},
            </if>
            <if test="messageNodeContent != null">message_node_content =
                #{messageNodeContent},
            </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>
            <if test="messageType != null">message_type =
                #{messageType},
            </if>
        </trim>
        where id = #{id}
    </update>

    <delete id="deleteShortMessageSendRecordById" parameterType="Long">
        delete
        from short_message_send_record
        where id = #{id}
    </delete>

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

    <select id="selectShortMessageRecordCountByManageRouteNodeIds"
            resultType="com.xinelu.manage.vo.subscribemessagesendrecord.RecordNum">
        select
        manage_route_node_id,
        COUNT(1) num
        from short_message_send_record
        where manage_route_node_id IN
        <foreach item="manageRouteNodeId" collection="manageRouteNodeId" open="(" separator="," close=")">
            #{manageRouteNodeId}
        </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.send_time, '%Y-%m') as sendTimeMonthStr
		from short_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.send_time,'%y%m') &gt;= date_format(#{startDate},'%y%m')
			</if>
			<if test="endDate != null">
				and date_format(record.send_time,'%y%m') &lt;= date_format(#{endDate},'%y%m')
			</if>
		</where>
	</select>
</mapper>