<?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.textmessage.TextMessageMapper">

    <resultMap type="TextMessage" id="TextMessageResult">
        <result property="id" column="id"/>
        <result property="departmentId" column="department_id"/>
        <result property="departmentName" column="department_name"/>
        <result property="diseaseTypeId" column="disease_type_id"/>
        <result property="diseaseTypeName" column="disease_type_name"/>
        <result property="textMessageName" column="text_message_name"/>
        <result property="textMessageId" column="text_message_id"/>
        <result property="textMessageContent" column="text_message_content"/>
        <result property="textMessageChannel" column="text_message_channel"/>
        <result property="textMessageStatus" column="text_message_status"/>
        <result property="textMessageSort" column="text_message_sort"/>
        <result property="textMessageRemark" column="text_message_remark"/>
	    <result property="sourceTemplateId" column="source_template_id"/>
        <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>

    <resultMap id="TextMessageVOResultMap" type="com.xinelu.manage.vo.textmessage.TextMessageVO">
        <id property="id" column="id"/>
        <result column="department_id" property="departmentId"/>
        <result column="department_name" property="departmentName"/>
        <result column="disease_type_id" property="diseaseTypeId"/>
        <result column="disease_type_name" property="diseaseTypeName"/>
        <result column="text_message_name" property="textMessageName"/>
        <result column="text_message_id" property="textMessageId"/>
        <result column="text_message_content" property="textMessageContent"/>
        <result column="text_message_channel" property="textMessageChannel"/>
        <result column="text_message_status" property="textMessageStatus"/>
        <result column="text_message_sort" property="textMessageSort"/>
        <result column="text_message_remark" property="textMessageRemark"/>
	    <result property="sourceTemplateId" column="source_template_id"/>
        <collection property="suitTaskList" ofType="com.xinelu.manage.domain.textmessagesuittask.TextMessageSuitTask">
            <id column="taskId" property="id"/>
            <result column="message_id" property="messageId"/>
            <result column="suit_task_type_id" property="suitTaskTypeId"/>
            <result column="suit_task_type_name" property="suitTaskTypeName"/>
        </collection>
    </resultMap>

    <sql id="selectTextMessageVo">
        select id,
               department_id,
               department_name,
               disease_type_id,
               disease_type_name,
               text_message_name,
               text_message_id,
               text_message_content,
               text_message_channel,
               text_message_status,
               text_message_sort,
               text_message_remark,
	           source_template_id,
               create_by,
               create_time,
               update_by,
               update_time
        from text_message
    </sql>

    <select id="selectTextMessageById" parameterType="Long"
            resultMap="TextMessageResult">
        <include refid="selectTextMessageVo"/>
        where id = #{id}
    </select>


    <select id="selectTextMessageInfoById" resultMap="TextMessageVOResultMap">
        select tm.id,
               tm.department_id,
               tm.department_name,
               tm.disease_type_id,
               tm.disease_type_name,
               tm.text_message_name,
               tm.text_message_id,
               tm.text_message_content,
               tm.text_message_channel,
               tm.text_message_status,
               tm.text_message_sort,
               tm.text_message_remark,
               tmst.id AS taskId,
               tmst.message_id,
               tmst.suit_task_type_id,
               tmst.suit_task_type_name
        from text_message tm
                 left join text_message_suit_task tmst on
            tm.id = tmst.message_id
        where tm.id = #{id}
    </select>

    <select id="selectTextMessageList" resultMap="TextMessageVOResultMap">
        select
        tm.id,
        tm.department_id,
        tm.department_name,
        tm.disease_type_id,
        tm.disease_type_name,
        tm.text_message_name,
        tm.text_message_id,
        tm.text_message_content,
        tm.text_message_channel,
        tm.text_message_status,
        tm.text_message_sort,
        tm.text_message_remark,
        tm.source_template_id,
        tmst.id AS taskId,
        tmst.message_id,
        tmst.suit_task_type_id,
        tmst.suit_task_type_name
        from text_message tm
        left join text_message_suit_task tmst on tm.id = tmst.message_id
        left join department d on d.id = tm.department_id
        <where>
	        <if test="hospitalAgencyId != null">
		        and d.hospital_agency_id = #{hospitalAgencyId}
	        </if>
            <if test="departmentId != null">
                AND tm.department_id =
                #{departmentId}
            </if>
            <if test="textMessageName != null and textMessageName != ''">
                AND tm.text_message_name LIKE CONCAT('%',
                #{textMessageName},
                '%'
                )
            </if>
            <if test="textMessageId != null and textMessageId != ''">
                AND tm.text_message_id LIKE CONCAT('%',
                #{textMessageId},
                '%'
                )
            </if>
            <if test="textMessageStatus != null">
                AND tm.text_message_status =
                #{textMessageStatus}
            </if>
            <!-- 数据范围过滤 -->
            ${params.dataScope}
        </where>
        order by tm.text_message_sort, tm.create_time DESC ,tmst.create_time DESC
    </select>

    <select id="countByTextMessageTaskDTO" resultType="java.lang.Integer"
            parameterType="com.xinelu.manage.dto.textmessage.TextMessageTaskDTO">
        select count(*)
        from text_message
        where text_message_id = #{textMessageId}
    </select>

    <select id="countByTextMessageNameExcludingId" resultType="java.lang.Integer">
        select count(1)
        from text_message
        where department_id = #{departmentId}
          and text_message_name = #{textMessageName}
          and id != #{id}
    </select>


    <insert id="insertTextMessage" parameterType="TextMessage" useGeneratedKeys="true"
            keyProperty="id">
        insert into text_message
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="departmentId != null">department_id,
            </if>
            <if test="departmentName != null">department_name,
            </if>
            <if test="diseaseTypeId != null">disease_type_id,
            </if>
            <if test="diseaseTypeName != null">disease_type_name,
            </if>
            <if test="textMessageName != null">text_message_name,
            </if>
            <if test="textMessageId != null">text_message_id,
            </if>
            <if test="textMessageContent != null">text_message_content,
            </if>
            <if test="textMessageChannel != null">text_message_channel,
            </if>
            <if test="textMessageStatus != null">text_message_status,
            </if>
            <if test="textMessageSort != null">text_message_sort,
            </if>
            <if test="textMessageRemark != null">text_message_remark,
            </if>
	        <if test="sourceTemplateId != null">source_template_id,
	        </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="departmentId != null">#{departmentId},
            </if>
            <if test="departmentName != null">#{departmentName},
            </if>
            <if test="diseaseTypeId != null">#{diseaseTypeId},
            </if>
            <if test="diseaseTypeName != null">#{diseaseTypeName},
            </if>
            <if test="textMessageName != null">#{textMessageName},
            </if>
            <if test="textMessageId != null">#{textMessageId},
            </if>
            <if test="textMessageContent != null">#{textMessageContent},
            </if>
            <if test="textMessageChannel != null">#{textMessageChannel},
            </if>
            <if test="textMessageStatus != null">#{textMessageStatus},
            </if>
            <if test="textMessageSort != null">#{textMessageSort},
            </if>
            <if test="textMessageRemark != null">#{textMessageRemark},
            </if>
	        <if test="sourceTemplateId != null">#{sourceTemplateId},
	        </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>

    <insert id="insertTextMessageSuitTasks" parameterType="java.util.List">
        INSERT INTO text_message_suit_task
        (message_id, suit_task_type_id, suit_task_type_name, create_by, create_time)
        VALUES
        <foreach collection="list" item="task" index="index" separator=",">
            (#{task.messageId}, #{task.suitTaskTypeId}, #{task.suitTaskTypeName}, #{task.createBy},
            #{task.createTime})
        </foreach>
    </insert>

	<select id="selectSuitTaskList" resultType="com.xinelu.manage.domain.textmessagesuittask.TextMessageSuitTask">
		select *
		from text_message_suit_task
		<where>
			<if test="messageId != null">
			and message_id = #{messageId}
			</if>
		</where>
	</select>

    <update id="updateTextMessage" parameterType="TextMessage">
        update text_message
        <trim prefix="SET" suffixOverrides=",">
            <if test="departmentId != null">department_id =
            #{departmentId},
            </if>
            <if test="departmentName != null">department_name =
            #{departmentName},
            </if>
            disease_type_id = #{diseaseTypeId},
            disease_type_name = #{diseaseTypeName},
            <if test="textMessageName != null">text_message_name =
            #{textMessageName},
            </if>
            <if test="textMessageId != null">text_message_id =
            #{textMessageId},
            </if>
            <if test="textMessageContent != null">text_message_content =
            #{textMessageContent},
            </if>
            <if test="textMessageChannel != null">text_message_channel =
            #{textMessageChannel},
            </if>
            <if test="textMessageStatus != null">text_message_status =
            #{textMessageStatus},
            </if>
            <if test="textMessageSort != null">text_message_sort =
            #{textMessageSort},
            </if>
            <if test="textMessageRemark != null">text_message_remark =
            #{textMessageRemark},
            </if>
	        <if test="sourceTemplateId != null">source_template_id =
		        #{sourceTemplateId},
	        </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>

    <update id="updateTextMessageSuitTask">
        update text_message_suit_task
        <trim prefix="SET" suffixOverrides=",">
            <if test="task.suitTaskTypeName != null">suit_task_type_name =
            #{task.suitTaskTypeName},
            </if>
            <if test="task.suitTaskTypeId != null">suit_task_type_id =
            #{task.suitTaskTypeId},
            </if>
            <if test="task.updateBy != null">update_by =
            #{task.updateBy},
            </if>
            <if test="task.updateTime != null">update_time =
            #{task.updateTime},
            </if>
        </trim>
        where id = #{task.id}
    </update>

    <delete id="deleteTextMessageById" parameterType="Long">
        delete
        from text_message
        where id = #{id}
    </delete>

    <delete id="deleteTextMessageByIds" parameterType="String">
        delete from text_message where id in
        <foreach item="id" collection="array" open="(" separator="," close=")">
            #{id}
        </foreach>
    </delete>
    <delete id="deleteTextMessageSuitTasks">
        delete from text_message_suit_task
        where message_id in
        <foreach collection="array" item="id" open="(" separator="," close=")">
            #{id}
        </foreach>
    </delete>
    <delete id="deleteTextMessageSuitTask">
        delete
        from text_message_suit_task
        where message_id = #{id}
    </delete>
</mapper>