<?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.tmerclub.cloud.biz.mapper.NotifyTemplateRemindMapper">
  <resultMap id="notifyTemplateRemindMap" type="com.tmerclub.cloud.biz.model.NotifyTemplateRemind">
    <id column="notify_template_remind_id" property="notifyTemplateRemindId" />
    <result column="template_id" property="templateId"/>
    <result column="shop_id" property="shopId"/>
    <result column="is_remind" property="isRemind"/>
    <result column="sys_type" property="sysType"/>
  </resultMap>
  <sql id="Vo_Column_List">
    `notify_template_remind_id`,`template_id`,`shop_id`,`is_remind`, `sys_type`
  </sql>

  <select id="getByNotifyTemplateRemindId" resultMap="notifyTemplateRemindMap">
    select <include refid="Vo_Column_List"/> from notify_template_remind where notify_template_remind_id = #{notifyTemplateRemindId}
  </select>
  <select id="getByTemplateIdAndShopIdAndSysType" resultType="com.tmerclub.cloud.biz.vo.NotifyTemplateRemindVO">
    select <include refid="Vo_Column_List"/> from notify_template_remind where template_id = #{templateId} and shop_id = #{shopId} and sys_type=#{sysType}
  </select>
  <select id="list" resultType="com.tmerclub.cloud.biz.vo.NotifyTemplateRemindVO">
    select nt.`template_id`, nt.`send_type`, nt.`message`, ntr.is_remind, ntr.notify_template_remind_id, ntr.is_remind, ntr.shop_id, ntr.sys_type  from notify_template nt
    left join notify_template_remind ntr on nt.template_id = ntr.template_id and ntr.sys_type=#{notifyTemplateRemindDTO.sysType} and ntr.shop_id = #{notifyTemplateRemindDTO.shopId}
    <where>
       nt.status > 0
        <!-- 商家端 -->
        <if test="notifyTemplateRemindDTO.sysType != null and notifyTemplateRemindDTO.sysType == 1">
            and nt.send_type <![CDATA[>]]> 100 and nt.send_type <![CDATA[<]]> 1000
        </if>
     <!-- 供应商端过滤掉营销活动相关提醒 -->
     <if test="notifyTemplateRemindDTO.sysType != null and notifyTemplateRemindDTO.sysType == 3">
       and nt.send_type <![CDATA[>]]> 1000
     </if>
   </where>

 </select>
 <insert id="save">
   insert into notify_template_remind (`template_id`,`shop_id`,`is_remind`, `sys_type`)
   values (#{notifyTemplateRemind.templateId},#{notifyTemplateRemind.shopId},#{notifyTemplateRemind.isRemind}, #{notifyTemplateRemind.sysType});
 </insert>
 <update id="update">
   update notify_template_remind
   <set>
     <if test="notifyTemplateRemind.templateId != null">
       `template_id` = #{notifyTemplateRemind.templateId},
     </if>
     <if test="notifyTemplateRemind.shopId != null">
       `shop_id` = #{notifyTemplateRemind.shopId},
     </if>
     <if test="notifyTemplateRemind.isRemind != null">
       `is_remind` = #{notifyTemplateRemind.isRemind},
     </if>
   </set>
   where notify_template_remind_id = #{notifyTemplateRemind.notifyTemplateRemindId}
 </update>
 <delete id="deleteById">
   delete from notify_template_remind where notify_template_remind_id = #{notifyTemplateRemindId}
 </delete>

</mapper>
