<?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.fastbee.notify.mapper.NotifyTemplateMapper">

    <resultMap type="com.fastbee.notify.domain.NotifyTemplate" id="NotifyTemplateResult">
        <result property="id"    column="id"    />
        <result property="serviceCode"    column="service_code"    />
        <result property="msgParams" column="msg_params"/>
        <result property="status" column="status"    />
        <result property="name"    column="name"    />
        <result property="channelId"    column="channel_id"    />
        <result property="channelType"    column="channel_type"    />
        <result property="provider"    column="provider"    />
        <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="delFlag"    column="del_flag"    />
        <result property="tenantId"    column="tenant_id"    />
        <result property="tenantName"    column="tenant_name"    />
    </resultMap>

    <sql id="selectNotifyTemplateVo">
        select id, service_code,msg_params,status, name, channel_id, channel_type, provider,  create_by, create_time, update_by, update_time, del_flag, tenant_id, tenant_name from notify_template
    </sql>

    <delete id="deleteAlertNotifyTemplateByNotifyTemplateIds">
        delete from iot_alert_notify_template where notify_template_id in
        <foreach item="notifyTemplateId" collection="array" open="(" separator="," close=")">
            #{notifyTemplateId}
        </foreach>
    </delete>

    <select id="selectEnableNotifyTemplateCount" parameterType="NotifyTemplate" resultType="java.lang.Integer">
        select count(*) from notify_template t
        where t.service_code = #{serviceCode}
              and t.status = #{status} and t.id != #{id}
              and t.channel_type = #{channelType}
              and t.tenant_id = #{tenantId}
              <if test="provider != null and provider != ''">
                  and t.provider = #{provider}
              </if>
              and t.del_flag = 0
    </select>

    <select id="selectOnlyEnable" parameterType="NotifyTemplate" resultType="com.fastbee.notify.domain.NotifyTemplate">
        <include refid="selectNotifyTemplateVo"/>
        where service_code = #{serviceCode}
        and status = 1
        and channel_type = #{channelType}
        and tenant_id = #{tenantId}
        <if test="provider != null and provider != ''">
            and provider = #{provider}
        </if>
        and del_flag = 0
    </select>

    <update id="updateNotifyBatch" >
        update notify_template
        <trim prefix="SET" suffixOverrides=",">
            <if test="status != null">status = #{status},</if>
        </trim>
        where id in
        <foreach collection="ids" item="item" open="(" separator="," close=")">
            #{item}
        </foreach>
    </update>

</mapper>
