<?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.SelfPushUserDaoImpl" >
  <resultMap id="BaseResultMap" type="com.pcloud.book.push.entity.SelfPushUser" >
    <id column="id" property="id" jdbcType="BIGINT" />
    <result column="push_id" property="pushId" jdbcType="BIGINT" />
    <result column="wx_user_id" property="wxUserId" jdbcType="VARCHAR" />
    <result column="create_time" property="createTime" jdbcType="TIMESTAMP" />
    <result column="create_user" property="createUser" jdbcType="BIGINT" />
  </resultMap>

  <sql id="Base_Column_List" >
    id, push_id, wx_user_id, create_time, create_user
  </sql>

  <insert id="insert" parameterType="com.pcloud.book.push.entity.SelfPushUser" useGeneratedKeys="true" keyProperty="id">
    insert into self_push_user (push_id, wx_user_id,
      create_time, create_user)
    values (#{pushId,jdbcType=BIGINT}, #{wxUserId,jdbcType=VARCHAR},
      now(), #{createUser,jdbcType=BIGINT})
  </insert>

  <insert id="batchInsert" parameterType="com.pcloud.book.push.entity.SelfPushUser" useGeneratedKeys="true" keyProperty="id">
    insert into self_push_user
    (push_id,
    wx_user_id,
    create_time,
    create_user
    ) values
    <foreach collection="list" item="item" index="index" separator=",">
      (
      #{item.pushId,jdbcType=BIGINT},
      #{item.wxUserId,jdbcType=VARCHAR},
      now(),
      #{item.createUser,jdbcType=BIGINT}
      )
    </foreach>
  </insert>

  <select id="listByPushId" parameterType="map" resultType="com.pcloud.book.group.vo.UserBookInfoVO">
    SELECT
        u.wx_user_id wxUserId,
        p.alt_id altId
    FROM
        self_push_user u
    LEFT JOIN self_push p ON u.push_id = p.id
    WHERE
        u.push_id = #{pushId}
  </select>

  <select id="getCountByPushId" resultType="integer" parameterType="long">
    SELECT
        COUNT(1)
    FROM
        self_push_user
    WHERE
        push_id = #{pushId}
  </select>

  <select id="getUserIdListByPushId" parameterType="long" resultType="string">
     SELECT
        DISTINCT wx_user_id
    FROM
        self_push_user
    WHERE
        push_id = #{pushId}
  </select>

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

  <select id="getCountByPushIds" resultType="com.pcloud.book.push.dto.UserCountAndPushDTO" parameterType="map">
    SELECT
        COUNT(1) userCount,
        push_id pushId
    FROM
        self_push_user
    WHERE
        push_id in
    <foreach collection="pushIds" item="item" index="index" open="(" separator="," close=")">
      ${item}
    </foreach>
    group by push_id
  </select>

</mapper>