<?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.advertising.dao.impl.AdvertisingLinkSendDaoImpl" >
  <resultMap id="BaseResultMap" type="com.pcloud.book.advertising.entity.AdvertisingLinkSend" >
    <id column="id" property="id" jdbcType="BIGINT" />
    <result column="link_id" property="linkId" jdbcType="BIGINT" />
    <result column="send_count" property="sendCount" jdbcType="INTEGER" />
    <result column="create_time" property="createTime" jdbcType="TIMESTAMP" />
    <result column="statistic_date" property="statisticDate" jdbcType="DATE" />
  </resultMap>

  <sql id="Base_Column_List" >
    id, link_id, send_count, create_time, statistic_date
  </sql>

  <insert id="insert" parameterType="com.pcloud.book.advertising.entity.AdvertisingLinkSend" useGeneratedKeys="true" keyProperty="id">
    insert into advertising_link_send (id, link_id, send_count, 
      create_time, statistic_date)
    values (#{id,jdbcType=BIGINT}, #{linkId,jdbcType=BIGINT}, #{sendCount,jdbcType=INTEGER}, 
      #{createTime,jdbcType=TIMESTAMP}, #{statisticDate,jdbcType=DATE})
  </insert>

  <insert id="batchInsert"  parameterType="com.pcloud.book.advertising.entity.AdvertisingLinkSend" useGeneratedKeys="true" keyProperty="id">
    insert into advertising_link_send (
    link_id,
    send_count,
    create_time,
    statistic_date) values
    <foreach collection="list" item="item" index="index" separator=",">
      (#{item.linkId,jdbcType=BIGINT},
      #{item.sendCount,jdbcType=INTEGER},
      #{item.createTime,jdbcType=TIMESTAMP},
      #{item.statisticDate,jdbcType=DATE}
    )
    </foreach>
  </insert>

  <select id="mapSendCount" parameterType="map" resultType="com.pcloud.book.advertising.dto.LinkRevertRecordDTO">
    SELECT
	    link_id id,
        SUM(IFNULL(send_count, 0)) sendCount
    FROM
        advertising_link_send
    WHERE
        link_id IN
    <foreach collection="linkIds" index="index" item="item" open="(" separator="," close=")">
      #{item}
    </foreach>
    GROUP BY
        link_id
  </select>

</mapper>