<?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.NotifyLogMapper">
  <resultMap id="notifyLogMap" type="com.tmerclub.cloud.biz.model.NotifyLog">
    <id column="log_id" property="logId" />
    <result column="create_time" property="createTime"/>
    <result column="update_time" property="updateTime"/>
    <result column="nick_name" property="nickName"/>
    <result column="user_mobile" property="userMobile"/>
    <result column="remind_id" property="remindId"/>
    <result column="shop_id" property="shopId"/>
    <result column="remind_type" property="remindType"/>
    <result column="template_id" property="templateId"/>
    <result column="send_type" property="sendType"/>
    <result column="message" property="message"/>
    <result column="status" property="status"/>
    <result column="spu_id" property="spuId"/>
    <result column="activity_id" property="activityId"/>
  </resultMap>
  <sql id="Vo_Column_List">
    `log_id`,`create_time`,`update_time`,`nick_name`,`user_mobile`,`remind_id`,`shop_id`,`shop_name`,`remind_type`,`template_id`,`send_type`,`message`,`status`, `spu_id`, `activity_id`
  </sql>

  <select id="listBySendTypeAndRemindType" resultType="com.tmerclub.cloud.biz.vo.NotifyLogVO">
    select  <include refid="Vo_Column_List"/> from notify_log
    where

    remind_id = #{userId} and send_type <![CDATA[<]]> 100 and remind_type = 3
    <if test="status != null">
        and status = #{status}
    </if>
    ORDER BY create_time desc
  </select>

  <insert id="saveBatch">
    insert into notify_log (`nick_name`, `user_mobile`, `remind_id`, `shop_id`, `shop_name`, `remind_type`, `template_id`, `send_type`,`message`, `status`, biz_id, spu_id, activity_id) values
    <foreach collection="notifyLogs" item="notifyLog" separator=",">
      (#{notifyLog.nickName}, #{notifyLog.userMobile}, #{notifyLog.remindId}, #{notifyLog.shopId}, #{notifyLog.shopName}, #{notifyLog.remindType},
       #{notifyLog.templateId}, #{notifyLog.sendType}, #{notifyLog.message},#{notifyLog.status}, #{notifyLog.bizId}, #{notifyLog.spuId}, #{notifyLog.activityId})
    </foreach>
  </insert>

  <update id="updateBatchById">
    <foreach collection="notifyLogList" item="notifyLog" >
      update notify_log
      <set>
        <if test="notifyLog.nickName != null">
          `nick_name` = #{notifyLog.nickName},
        </if>
        <if test="notifyLog.userMobile != null">
          `user_mobile` = #{notifyLog.userMobile},
        </if>
        <if test="notifyLog.remindId != null">
          `remind_id` = #{notifyLog.remindId},
        </if>
        <if test="notifyLog.shopId != null">
          `shop_id` = #{notifyLog.shopId},
        </if>
        <if test="notifyLog.remindType != null">
          `remind_type` = #{notifyLog.remindType},
        </if>
        <if test="notifyLog.templateId != null">
          `template_id` = #{notifyLog.templateId},
        </if>
        <if test="notifyLog.sendType != null">
          `send_type` = #{notifyLog.sendType},
        </if>
        <if test="notifyLog.message != null">
          `message` = #{notifyLog.message},
        </if>
        <if test="notifyLog.status != null">
          `status` = #{notifyLog.status},
        </if>
      </set>
      where log_id = #{notifyLog.logId};
    </foreach>
  </update>
  <update id="updateStatus">
    update notify_log set status = 1 where log_id = #{logId}
  </update>

  <update id="isReadByIds">
    <foreach collection="logIds" item="logId" >
    update notify_log
    set status = 1
    where log_id = #{logId};
    </foreach>
  </update>
  <update id="isReadByShopId">
    update notify_log set status =1 where shop_id = #{shopId}
  </update>

  <select id="list" resultType="com.tmerclub.cloud.biz.vo.NotifyLogVO">
    select <include refid="Vo_Column_List"/> from notify_log
    <where>
      <if test="notifyLog.startTime != null">
        `create_time` <![CDATA[>]]>  #{notifyLog.startTime}
      </if>
      <if test="notifyLog.endTime != null">
        and `create_time` <![CDATA[<]]> #{notifyLog.endTime}
      </if>
      <if test="notifyLog.shopId != null">
        and shop_id = #{notifyLog.shopId}
      </if>
      <if test="notifyLog.sysType != null  and notifyLog.sysType == 1">
        and send_type <![CDATA[>]]> 100 and send_type <![CDATA[<]]> 1000
      </if>
      <if test="notifyLog.sysType != null  and notifyLog.sysType == 3">
        and send_type <![CDATA[>]]> 1000
      </if>
      <if test="notifyLog.message != null and notifyLog.message != ''">
        and message  like concat('%',#{notifyLog.message},'%')
      </if>
      <if test="notifyLog.status != null">
        and status = #{notifyLog.status}
      </if>
      <if test="notifyLog.sendType != null ">
        and send_type = #{notifyLog.sendType}
      </if>
      <if test="notifyLog.remindType != null ">
        and remind_type = #{notifyLog.remindType}
      </if>
    </where>
    order by `create_time` desc
  </select>

  <select id="countNotifyByConditions" resultType="int">
    SELECT ifnull(count(*),0) FROM `notify_log`
    WHERE send_type = #{sendType}
    <if test="orderId != null">
      AND biz_id = #{orderId}
    </if>
    <if test="userId != null">
      AND remind_id = #{userId}
    </if>
    <if test="level != null">
      AND biz_id = #{level}
    </if>
  </select>
  <select id="getByLogId" resultType="com.tmerclub.cloud.biz.vo.NotifyLogVO">
    select <include refid="Vo_Column_List"/> from notify_log where log_id = #{logId}
  </select>

</mapper>
