<?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.push.dao.impl.SelfPushItemDaoImpl" >
  <resultMap id="BaseResultMap" type="com.pcloud.book.push.entity.SelfPushItem" >
    <id column="id" property="id" jdbcType="BIGINT" />
    <result column="push_id" property="pushId" jdbcType="BIGINT" />
    <result column="item_type" property="itemType" jdbcType="INTEGER" />
    <result column="text_content" property="textContent" jdbcType="VARCHAR" />
    <result column="link_url" property="linkUrl" jdbcType="VARCHAR" />
    <result column="link_title" property="linkTitle" jdbcType="VARCHAR" />
    <result column="link_description" property="linkDescription" jdbcType="VARCHAR" />
    <result column="link_image_url" property="linkImageUrl" jdbcType="VARCHAR" />
    <result column="image_url" property="imageUrl" jdbcType="VARCHAR" />
    <result column="app_id" property="appId" jdbcType="BIGINT" />
    <result column="app_url" property="appUrl" jdbcType="VARCHAR" />
    <result column="product_id" property="productId" jdbcType="BIGINT" />
    <result column="product_url" property="productUrl" jdbcType="VARCHAR" />
    <result column="push_status" property="pushStatus" jdbcType="INTEGER" />
    <result column="create_user" property="createUser" jdbcType="BIGINT" />
    <result column="create_time" property="createTime" jdbcType="TIMESTAMP" />
    <result column="update_user" property="updateUser" jdbcType="BIGINT" />
    <result column="update_time" property="updateTime" jdbcType="TIMESTAMP" />
    <result column="send_time" property="sendTime" jdbcType="TIMESTAMP" />
    <result column="resource_id" property="resourceId" jdbcType="BIGINT"/>
  </resultMap>

  <sql id="Base_Column_List" >
    id, push_id, item_type, text_content, link_url, link_title, link_description,
    link_image_url, image_url, app_id, app_url, product_id, product_url, push_status,
    create_user, create_time, update_user, update_time, send_time, resource_id
  </sql>

  <insert id="insert" parameterType="com.pcloud.book.push.entity.SelfPushItem" useGeneratedKeys="true" keyProperty="id">
    insert into self_push_item (push_id, item_type,
    text_content, link_url, link_title,
    link_description, link_image_url, image_url,
    app_id, app_url, product_id,
    product_url, push_status, create_user,
    create_time, update_user, update_time, send_time, resource_id
    )
    values (#{pushId,jdbcType=BIGINT}, #{itemType,jdbcType=INTEGER},
    #{textContent,jdbcType=VARCHAR}, #{linkUrl,jdbcType=VARCHAR}, #{linkTitle,jdbcType=VARCHAR},
    #{linkDescription,jdbcType=VARCHAR}, #{linkImageUrl,jdbcType=VARCHAR}, #{imageUrl,jdbcType=VARCHAR},
    #{appId,jdbcType=BIGINT}, #{appUrl,jdbcType=VARCHAR}, #{productId,jdbcType=BIGINT},
    #{productUrl,jdbcType=VARCHAR}, #{pushStatus,jdbcType=INTEGER}, #{createUser,jdbcType=BIGINT},
    now(), #{updateUser,jdbcType=BIGINT}, now(), #{sendTime}, #{resourceId}
    )
  </insert>

  <!--批量插入-->
  <insert id="batchInsert"  parameterType="com.pcloud.book.push.entity.SelfPushItem" useGeneratedKeys="true" keyProperty="id">
    insert into self_push_item (
    push_id,
    item_type,
    text_content,
    link_url,
    link_title,
    link_description,
    link_image_url,
    image_url,
    app_id,
    app_url,
    product_id,
    product_url,
    push_status,
    create_user,
    create_time,
    update_user,
    update_time,
    send_time,
    resource_id
    ) values
    <foreach collection="list" item="item" index="index" separator=",">
      (
      #{item.pushId,jdbcType=BIGINT},
      #{item.itemType,jdbcType=INTEGER},
      #{item.textContent,jdbcType=VARCHAR},
      #{item.linkUrl,jdbcType=VARCHAR},
      #{item.linkTitle,jdbcType=VARCHAR},
      #{item.linkDescription,jdbcType=VARCHAR},
      #{item.linkImageUrl,jdbcType=VARCHAR},
      #{item.imageUrl,jdbcType=VARCHAR},
      #{item.appId,jdbcType=BIGINT},
      #{item.appUrl,jdbcType=VARCHAR},
      #{item.productId,jdbcType=BIGINT},
      #{item.productUrl,jdbcType=VARCHAR},
      #{item.pushStatus,jdbcType=INTEGER},
      #{item.createUser,jdbcType=BIGINT},
      NOW(),
      #{item.updateUser,jdbcType=BIGINT},
      NOW(),
      #{item.sendTime},
      #{item.resourceId}
      )
    </foreach>
  </insert>


  <select id="listSelfPushRecord" parameterType="map" resultType="com.pcloud.book.push.dto.SelfPushRecordDTO">
    SELECT
    p.alt_id altId,
    p.book_group_id bookGroupId,
    p.id pushId,
    i.id pushItemId,
    i.item_type itemType,
    i.text_content textContent,
    i.link_url linkUrl,
    i.link_title linkTitle,
    i.link_description linkDescription,
    i.link_image_url linkImageUrl,
    i.image_url imageUrl,
    i.app_id appId,
    i.app_url appUrl,
    i.product_id productId,
    i.product_url productUrl,
    i.push_status pushStatus,
    i.create_time createTime,
    min(i.send_time) sendTime,
    p.send_all sendAll,
    p.create_user createUser,
    p.start_time startTime,
    p.end_time endTime,
    p.push_send_time pushSendTime,
    p.week_days weekDays,
    p.push_type pushType,
    i.resource_id resourceId
    FROM
    self_push_item i
    LEFT JOIN self_push p ON i.push_id = p.id
    WHERE
    1 = 1
    <if test="startTime != null and endTime != null">
      AND (
      (i.send_time BETWEEN #{startTime} AND #{endTime} )
      OR (p.start_time >= #{startTime} AND p.end_time &lt;= #{endTime})
      )
    </if>
    <if test="pushStatus != null">
      AND i.push_status = #{pushStatus}
    </if>
    <choose>
      <when test="partyId == 0">
        AND p.alt_id IS NOT NULL AND p.alt_id !=""
      </when>
      <otherwise>
        AND p.create_user = #{partyId}
      </otherwise>
    </choose>
    group by push_id
    ORDER BY
    i.create_time DESC, i.id DESC
  </select>

  <update id="updatePushStatusByPushId" parameterType="map">
    UPDATE self_push_item
    SET push_status = #{pushStatus}
    WHERE push_id = #{pushId}
  </update>

  <select id="getByPushId" parameterType="long" resultMap="BaseResultMap">
    SELECT <include refid="Base_Column_List"/>
    FROM self_push_item
    WHERE push_id = #{pushId}
  </select>

  <delete id="deleteByPushId" parameterType="long">
    delete from
    self_push_item
    WHERE  push_id = #{pushId}
  </delete>

  <select id="getStatusByPushId" parameterType="map" resultType="integer">
    select push_status from self_push_item
    where push_id=#{pushId}
    group by push_id
  </select>

  <select id="getByPushIds" parameterType="map" resultMap="BaseResultMap">
    SELECT <include refid="Base_Column_List"/>
    FROM self_push_item
    WHERE push_id in
    <foreach collection="pushIds" item="item" index="index" open="(" separator="," close=")">
      ${item}
    </foreach>
  </select>

</mapper>