<?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.pcloud.book.cultivate.dao.impl.CultivateNotifySendDaoImpl" >
  <resultMap id="BaseResultMap" type="com.pcloud.book.cultivate.entity.CultivateNotifySend" >
    <id column="id" property="id" jdbcType="BIGINT" />
    <result column="send_type" property="sendType" jdbcType="INTEGER" />
    <result column="wx_user_id" property="wxUserId" jdbcType="VARCHAR" />
    <result column="wechat_user_id" property="wechatUserId" jdbcType="BIGINT" />
    <result column="message_id" property="messageId" jdbcType="BIGINT" />
    <result column="message_type" property="messageType" jdbcType="INTEGER" />
    <result column="is_read" property="isRead" jdbcType="INTEGER" />
    <result column="read_time" property="createTime" jdbcType="TIMESTAMP" />
    <result column="create_time" property="createTime" jdbcType="TIMESTAMP" />
    <result column="update_time" property="updateTime" jdbcType="TIMESTAMP" />
  </resultMap>

  <sql id="Base_Column_List" >
    id,wx_user_id, wechat_user_id, send_type,message_id,message_type,is_read,read_time, create_time,update_time
  </sql>

  <insert id="insert" parameterType="com.pcloud.book.cultivate.entity.CultivateNotifySend" useGeneratedKeys="true" keyProperty="id">
      insert into cultivate_notify_send
        (
        wx_user_id,
        wechat_user_id,
        send_type,
        message_id,
        message_type,
        is_read,
        show_home,
          create_time,
          update_time
          )
    values
     (#{wxUserId},
      #{wechatUserId},
      #{sendType},
      #{messageId},
      #{messageType},
      0,
     #{showHome},
      NOW(),
      NOW()
      )
  </insert>

  <insert id="batchInsert" parameterType="com.pcloud.book.cultivate.entity.CultivateNotifySend" useGeneratedKeys="true" keyProperty="id">
    insert into cultivate_notify_send
    (
    wx_user_id,
    wechat_user_id,
    send_type,
    message_id,
    message_type,
    is_read,
    show_home,
      create_time,
      update_time
      )
    values
    <foreach collection="list" item="item" index="index" separator=",">
    (
      #{item.wxUserId},
      #{item.wechatUserId},
      #{item.sendType},
      #{item.messageId},
      #{item.messageType},
      0,
      #{item.showHome},
      NOW(),
      NOW()
      )
    </foreach>
  </insert>

  <select id="getFirstNoReadNotify" parameterType="Long" resultType="Long">
    select message_id  messageId from cultivate_notify_send
    where wechat_user_id=#{wechatUserId}
    and is_read=0 AND show_home=1
    order by create_time desc limit 1
  </select>

  <update id="update" parameterType="com.pcloud.book.cultivate.entity.CultivateNotifySend">
    update cultivate_notify_send
    <set>
      <if test="messageType != null">
        message_type = #{messageType},
      </if>
      <if test="messageId != null">
        message_id = #{messageId},
      </if>
      <if test="isRead != null">
        is_read = #{isRead},
      </if>
      <if test="readTime != null">
        read_time = #{readTime},
      </if>
      update_time=NOW()
    </set>
    where id = #{id}
  </update>

  <update id="updateIsRead" parameterType="map">
    update cultivate_notify_send set is_read=1,read_time=now(),update_time=now()
    where wechat_user_id=#{wechatUserId}
    and message_id=#{messageId}
  </update>

  <update id="updateShowHome" parameterType="map">
    update cultivate_notify_send set show_home=0,update_time=now()
    where wechat_user_id=#{wechatUserId} AND show_home=1
  </update>

  <select id="getNotify4Type" resultType="com.pcloud.book.cultivate.dto.CultivateNotifyTypeDTO">
    select message_type messageType ,MAX(create_time) firstNoReadDate ,count(1) noReadCounts from cultivate_notify_send
    where is_read=0
    and wechat_user_id=#{wechatUserId}
    group by message_type
  </select>

  <select id="getNotifyListByType" parameterType="map" resultType="com.pcloud.book.cultivate.dto.CultivateNotifyDTO">
    select a.message_id messageId ,a.title,a.message_type messageType ,c.create_time sendTime from cultivate_notify a, cultivate_notify_send c
    where a.message_id=c.message_id
    and c.wechat_user_id=#{wechatUserId}
    <if test="messageType != null">
      and c.message_type=#{messageType}
    </if>
    order by c.create_time desc
  </select>

  <select id="getNotifySendsByType" parameterType="map" resultMap="BaseResultMap">
    select <include refid="Base_Column_List"/> from  cultivate_notify_send
    where wechat_user_id=#{wechatUserId}
    and message_type=#{messageType}
    order by create_time desc
  </select>

  <select id="listPage4CultivateNotify" parameterType="map" resultType="com.pcloud.book.cultivate.dto.CultivateNotifyPageDTO">
    select
    a.send_type sendType,
    a.message_type messageType,
    a.create_time createTime,
    a.message_id messageId,
    a.create_time sendTime,
    a.title title,
    a.show_home showHome,
    a.square_img squareImg
    from cultivate_notify a
    <if test="keyword !=null">
      LEFT JOIN cultivate_notify_item b on a.message_id=b.message_id
    </if>
    where
    1=1
    <if test="sendType!=null">
      and a.send_type=#{sendType}
    </if>
    <if test="messageType !=null">
    and a.message_type=#{messageType}
    </if>
    <if test="keyword !=null">
      and b.content like concat('%',#{keyword},'%')
    </if>
    <choose>
      <when test="agentId != null">
        and a.agent_id = #{agentId}
      </when>
      <otherwise>
        and a.agent_id = 0
      </otherwise>
    </choose>
    order by a.create_time DESC
  </select>

  <select id="getSendUserIds" parameterType="map" resultType="Long">
    select distinct wechat_user_id
    from cultivate_notify_send
    where message_id=#{messageId}
    order by id
    limit #{pageNum}, #{numPerPage}
  </select>

  <select id="getSendUserCount" parameterType="map" resultType="Integer">
    select ifnull(count(1),0)
    from cultivate_notify_send
    where message_id=#{messageId}
  </select>

  <delete id="clearExpireNotifies">
    DELETE
    FROM
    `cultivate_notify_send`
    WHERE
    message_type = 6
    AND DATE( create_time ) &lt; DATE(SUBDATE( NOW(), INTERVAL 1 MONTH ))
  </delete>

  <select id="getLastNoReadNotify" parameterType="long" resultType="long">
    SELECT
    message_id
    FROM
    cultivate_notify_send
    WHERE
    wechat_user_id = #{wechatUserId}
    AND is_read = 0
    AND show_home = 1
    ORDER BY
    create_time DESC
    LIMIT 3
  </select>

  <update id="batchUpdateIsRead" parameterType="map">
    UPDATE cultivate_notify_send
    SET is_read = 1,
    read_time = now(),
    update_time = now()
    WHERE
	wechat_user_id = #{wechatUserId}
    AND message_id  in
    <foreach collection="messageIds" index="index" item="item" open="(" close=")" separator=",">
      #{item}
    </foreach>
  </update>
</mapper>