<?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.wechattemplate.WechatTemplateMapper">

    <resultMap type="WechatTemplate" id="WechatTemplateResult">
        <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="templateId" column="template_id"/>
        <result property="templateContent" column="template_content"/>
        <result property="templateSource" column="template_source"/>
        <result property="templateSort" column="template_sort"/>
        <result property="templateRemark" column="template_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="WechatTemplateVOResultMap" type="com.xinelu.manage.vo.wechattemplate.WechatTemplateVO">
        <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="wechat_template_name" property="wechatTemplateName"/>
        <result column="template_id" property="templateId"/>
        <result column="template_content" property="templateContent"/>
        <result column="template_source" property="templateSource"/>
        <result column="template_sort" property="templateSort"/>
        <result column="template_remark" property="templateRemark"/>
        <collection property="suitTaskList"
                    ofType="com.xinelu.manage.domain.wechattemplatesuittask.WechatTemplateSuitTask">
            <id column="taskId" property="id"/>
            <result column="wechat_template_id" property="wechatTemplateId"/>
            <result column="suit_task_type_id" property="suitTaskTypeId"/>
            <result column="suit_task_type_name" property="suitTaskTypeName"/>
        </collection>
    </resultMap>

    <sql id="selectWechatTemplateVo">
        select id,
               department_id,
               department_name,
               disease_type_id,
               disease_type_name,
               wechat_template_name,
               template_id,
               template_content,
               template_source,
               template_sort,
               template_remark,
	    	   source_template_id,
               create_by,
               create_time,
               update_by,
               update_time
        from wechat_template
    </sql>

    <select id="selectWechatTemplateList" parameterType="WechatTemplate" resultMap="WechatTemplateResult">
        <include refid="selectWechatTemplateVo"/>
        <where>
            <if test="departmentId != null ">
                and department_id =
                #{departmentId}
            </if>
            <if test="departmentName != null  and departmentName != ''">
                and department_name like concat('%',
                #{departmentName},
                '%'
                )
            </if>
            <if test="diseaseTypeId != null ">
                and disease_type_id =
                #{diseaseTypeId}
            </if>
            <if test="diseaseTypeName != null  and diseaseTypeName != ''">
                and disease_type_name like concat('%',
                #{diseaseTypeName},
                '%'
                )
            </if>
            <if test="templateId != null  and templateId != ''">
                and template_id =
                #{templateId}
            </if>
            <if test="templateContent != null  and templateContent != ''">
                and template_content =
                #{templateContent}
            </if>
            <if test="templateSource != null  and templateSource != ''">
                and template_source =
                #{templateSource}
            </if>
            <if test="templateSort != null ">
                and template_sort =
                #{templateSort}
            </if>
            <if test="templateRemark != null  and templateRemark != ''">
                and template_remark =
                #{templateRemark}
            </if>
        </where>
    </select>

    <select id="selectWechatTemplateById" parameterType="Long"
            resultMap="WechatTemplateResult">
        <include refid="selectWechatTemplateVo"/>
        where id = #{id}
    </select>

    <select id="selectWechatTemplateDtoList" resultType="com.xinelu.manage.vo.wechattemplate.WechatTemplateTaskVO">
        select wt.id ,
        wt.department_id,
        wt.department_name ,
        wt.disease_type_id ,
        wt.disease_type_name,
        wt.wechat_template_name,
        wt.template_id,
        wt.template_content,
        wt.template_source,
        wt.template_sort,
        wt.template_remark,
	    wt.source_template_id,
        IFNULL(MAX(wtst.id), 0) AS taskId,
        GROUP_CONCAT(wtst.suit_task_type_name SEPARATOR ',') as suitTaskTypeName
        from wechat_template wt
        left join wechat_template_suit_task wtst on wt.id = wtst.wechat_template_id
        left join department d on d.id = wt.department_id
        <where>
            <if test="hospitalAgencyId != null">
                and d.hospital_agency_id = #{hospitalAgencyId}
            </if>
            <if test="departmentId != null ">
                and wt.department_id =
                #{departmentId}
            </if>
            <if test="wechatTemplateName != null  and wechatTemplateName != ''">
                and wt.wechat_template_name like concat('%',
                #{wechatTemplateName},
                '%'
                )
            </if>
            <if test="templateId != null  and templateId != ''">
                and wt.template_id like concat('%',
                #{templateId},
                '%'
                )
            </if>
            <if test="templateSource != null  and templateSource != ''">
                and wt.template_source =
                #{templateSource}
            </if>
            <!-- 数据范围过滤 -->
            ${params.dataScope}
        </where>
        group by wt.id order by wt.template_sort, wt.create_time DESC,wtst.create_time DESC
    </select>
    <select id="selectWechatTemplateDtoById" resultMap="WechatTemplateVOResultMap">
        select wt.id,
               wt.department_id,
               wt.department_name,
               wt.disease_type_id,
               wt.disease_type_name,
               wt.wechat_template_name,
               wt.template_id,
               wt.template_content,
               wt.template_source,
               wt.template_sort,
               wt.template_remark,
               wtst.id AS taskId,
               wtst.wechat_template_id,
               wtst.suit_task_type_id,
               wtst.suit_task_type_name
        from wechat_template wt
                 left join wechat_template_suit_task wtst on wt.id = wtst.wechat_template_id
        where wt.id = #{id}
    </select>

    <select id="countByWechatTemplateTaskDTO" resultType="java.lang.Integer"
            parameterType="com.xinelu.manage.dto.wechattemplate.WechatTemplateTaskDTO">
        select count(1)
        from wechat_template wt
        where department_id = #{departmentId}
          and wechat_template_name = #{wechatTemplateName}
          and template_source = #{templateSource}
    </select>

    <select id="countByWechatTemplateNameExcludingId" resultType="java.lang.Integer">
        select count(1)
        from wechat_template
        where department_id = #{departmentId}
          and wechat_template_name = #{wechatTemplateName}
          and template_source = #{templateSource}
          and id != #{id}
    </select>

    <insert id="insertWechatTemplate" parameterType="WechatTemplate" useGeneratedKeys="true"
            keyProperty="id">
        insert into wechat_template
        <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="wechatTemplateName != null">wechat_template_name,
            </if>
            <if test="templateId != null">template_id,
            </if>
            <if test="templateContent != null">template_content,
            </if>
            <if test="templateSource != null">template_source,
            </if>
            <if test="templateSort != null">template_sort,
            </if>
            <if test="templateRemark != null">template_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="wechatTemplateName != null">#{wechatTemplateName},
            </if>
            <if test="templateId != null">#{templateId},
            </if>
            <if test="templateContent != null">#{templateContent},
            </if>
            <if test="templateSource != null">#{templateSource},
            </if>
            <if test="templateSort != null">#{templateSort},
            </if>
            <if test="templateRemark != null">#{templateRemark},
            </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="insertWechatTemplateSuitTask" parameterType="java.util.List">
        INSERT INTO wechat_template_suit_task
        (wechat_template_id, suit_task_type_id, suit_task_type_name,create_by, create_time)
        VALUES
        <foreach collection="list" item="task" index="index" separator=",">
            (#{task.wechatTemplateId}, #{task.suitTaskTypeId}, #{task.suitTaskTypeName}, #{task.createBy},
            #{task.createTime})
        </foreach>
    </insert>

    <update id="updateWechatTemplate" parameterType="WechatTemplate">
        update wechat_template
        <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="wechatTemplateName != null">wechat_template_name =
            #{wechatTemplateName},
            </if>
            <if test="templateId != null">template_id =
            #{templateId},
            </if>
            <if test="templateContent != null">template_content =
            #{templateContent},
            </if>
            <if test="templateSource != null">template_source =
            #{templateSource},
            </if>
            <if test="templateSort != null">template_sort =
            #{templateSort},
            </if>
            <if test="templateRemark != null">template_remark =
            #{templateRemark},
            </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="updateWechatTemplateSuitTask">
        <foreach collection="list" item="task" separator=";">
            UPDATE wechat_template_suit_task
            SET suit_task_type_name = #{task.suitTaskTypeName},
            suit_task_type_id = #{task.suitTaskTypeId},
            update_by = #{task.updateBy},
            update_time = #{task.updateTime}
            WHERE wechat_template_id= #{task.wechatTemplateId}
        </foreach>
    </update>

    <delete id="deleteWechatTemplateById" parameterType="Long">
        delete
        from wechat_template
        where id = #{id}
    </delete>

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

    <delete id="deleteWechatTemplateSuitTaskByIds">
        delete
        from wechat_template_suit_task
        where wechat_template_id in
        <foreach collection="array" item="id" open="(" separator="," close=")">
            #{id}
        </foreach>
    </delete>
    <delete id="deleteWechatTemplateSuitTaskById">
        delete
        from wechat_template_suit_task
        where wechat_template_id = #{id}
    </delete>
</mapper>