<?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.phonedialrecord.PhoneDialRecordMapper">

    <resultMap type="PhoneDialRecord" id="PhoneDialRecordResult">
        <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="dialTime" column="dial_time"/>
        <result property="phoneTemplateId" column="phone_template_id"/>
        <result property="phoneTemplateName" column="phone_template_name"/>
        <result property="messageNodeContent" column="message_node_content"/>
        <result property="phoneDialMethod" column="phone_dial_method"/>
        <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="ctUuid" column="ct_uuid"/>
	    <result property="phoneDialRecordVideo" column="phone_dial_record_video"/>

    </resultMap>

    <sql id="selectPhoneDialRecordVo">
        select id,
               patient_id,
               manage_route_node_id,
               patient_phone,
               dial_time,
               phone_template_id,
               phone_template_name,
               message_node_content,
               phone_dial_method,
               error_code,
               error_status,
               create_by,
               create_time,
               update_by,
               update_time,
               ct_uuid,
               phone_dial_record_video
        from phone_dial_record
    </sql>

    <select id="selectPhoneDialRecordList" parameterType="PhoneDialRecord" resultMap="PhoneDialRecordResult">
        <include refid="selectPhoneDialRecordVo"/>
        <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="dialTime != null ">
                and dial_time = #{dialTime}
            </if>
            <if test="phoneTemplateId != null  and phoneTemplateId != ''">
                and phone_template_id = #{phoneTemplateId}
            </if>
            <if test="phoneTemplateName != null  and phoneTemplateName != ''">
                and phone_template_name like concat('%', #{phoneTemplateName}, '%')
            </if>
            <if test="messageNodeContent != null  and messageNodeContent != ''">
                and message_node_content = #{messageNodeContent}
            </if>
            <if test="phoneDialMethod != null  and phoneDialMethod != ''">
                and phone_dial_method = #{phoneDialMethod}
            </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="selectPhoneDialRecordById" parameterType="Long"
            resultMap="PhoneDialRecordResult">
        <include refid="selectPhoneDialRecordVo"/>
        where id = #{id}
    </select>

    <insert id="insertPhoneDialRecord" parameterType="PhoneDialRecord" useGeneratedKeys="true"
            keyProperty="id">
        insert into phone_dial_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="dialTime != null">dial_time,
            </if>
            <if test="phoneTemplateId != null">phone_template_id,
            </if>
            <if test="phoneTemplateName != null">phone_template_name,
            </if>
            <if test="messageNodeContent != null">message_node_content,
            </if>
            <if test="phoneDialMethod != null">phone_dial_method,
            </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="ctUuid != null">ct_uuid,
	        </if>
	        <if test="phoneDialRecordVideo != null">phone_dial_record_video,
	        </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="dialTime != null">#{dialTime},
            </if>
            <if test="phoneTemplateId != null">#{phoneTemplateId},
            </if>
            <if test="phoneTemplateName != null">#{phoneTemplateName},
            </if>
            <if test="messageNodeContent != null">#{messageNodeContent},
            </if>
            <if test="phoneDialMethod != null">#{phoneDialMethod},
            </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="ctUuid != null">#{ctUuid},
	        </if>
	        <if test="phoneDialRecordVideo != null">#{phoneDialRecordVideo},
	        </if>
        </trim>
    </insert>

    <update id="updatePhoneDialRecord" parameterType="PhoneDialRecord">
        update phone_dial_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="dialTime != null">dial_time =
                #{dialTime},
            </if>
            <if test="phoneTemplateId != null">phone_template_id =
                #{phoneTemplateId},
            </if>
            <if test="phoneTemplateName != null">phone_template_name =
                #{phoneTemplateName},
            </if>
            <if test="messageNodeContent != null">message_node_content =
                #{messageNodeContent},
            </if>
            <if test="phoneDialMethod != null">phone_dial_method =
                #{phoneDialMethod},
            </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="ctUuid != null">ct_uuid =
            	#{ctUuid},
	        </if>
	        <if test="phoneDialRecordVideo != null">phone_dial_record_video =
            	#{phoneDialRecordVideo},
	        </if>
        </trim>
        where id = #{id}
    </update>

    <delete id="deletePhoneDialRecordById" parameterType="Long">
        delete
        from phone_dial_record
        where id = #{id}
    </delete>

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

	<select id="getPhoneDialStatistic" parameterType="com.xinelu.manage.dto.statistics.FollowUpRateDto"
		resultType="com.xinelu.manage.vo.phonedialrecord.PhoneDialRecordVo">
		select record.patient_id,
		       date_format(record.dial_time, '%Y-%m') as dialTimeMonth,
		       record.phone_dial_method,
		       record.error_code,
		       patient.hospital_agency_id,
		       patient.campus_agency_id,
		       patient.department_id,
		       patient.ward_id,
		       count(record.id) as count
		from phone_dial_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.dial_time,'%y%m') &gt;= date_format(#{startDate},'%y%m')
			</if>
			<if test="endDate != null">
				and date_format(record.dial_time,'%y%m') &lt;= date_format(#{endDate},'%y%m')
			</if>
		</where>
	</select>

	<select id="getLastRecord" parameterType="java.lang.Long" resultMap="PhoneDialRecordResult">
		<include refid="selectPhoneDialRecordVo"/>
		where manage_route_node_id = #{manageRouteNodeId}
		order by dial_time desc limit 1
	</select>

	<select id="getNoVideoList" parameterType="com.xinelu.manage.dto.phonedialrecord.PhoneDialRecordDto" resultMap="PhoneDialRecordResult">
		<include refid="selectPhoneDialRecordVo"/>
		<where>
			error_code = 0 and (phone_dial_record_video is null or phone_dial_record_video = '')
			<if test="dialTime != null">
				and date_format(dial_time, '%y%M%d%H%i') &lt;= date_format(#{dialTime}, '%y%M%d%H%i')
			</if>
			<if test="phoneDialMethod != null">
				and phone_dial_method = #{phoneDialMethod}
			</if>
		</where>
	</select>
</mapper>