<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "mybatis-3-mapper.dtd" >
<mapper namespace="com.pcloud.book.group.dao.impl.PushBookGroupUpdateDaoImpl">
    <resultMap id="BaseResultMap" type="com.pcloud.book.group.entity.PushBookGroupUpdate">
        <id column="id" property="id" jdbcType="BIGINT"/>
        <result column="book_group_id" property="bookGroupId" jdbcType="BIGINT"/>
        <result column="wx_user_id" property="wxUserId" jdbcType="VARCHAR"/>
        <result column="alt_id" property="altId" jdbcType="VARCHAR"/>
        <result column="is_push" property="isPush" jdbcType="BOOLEAN"/>
        <result column="create_time" property="createTime" jdbcType="TIMESTAMP"/>
        <result column="update_time" property="updateTime" jdbcType="TIMESTAMP"/>
    </resultMap>

    <sql id="Base_Column_List">
        id, book_group_id,wx_user_id,alt_id,is_push,create_time,update_time
    </sql>

    <select id="getById" resultMap="BaseResultMap" parameterType="java.lang.Long">
        select
        <include refid="Base_Column_List"/>
        from push_book_group_update
        where id = #{id,jdbcType=BIGINT}
    </select>

    <!--批量插入-->
    <insert id="batchInsert"  parameterType="com.pcloud.book.group.entity.PushBookGroupUpdate" useGeneratedKeys="true" keyProperty="id">
        insert into push_book_group_update (
            book_group_id,
            wx_user_id,
            alt_id,
            is_push,
            create_time,
            update_time
        ) values
        <foreach collection="list" item="item" index="index" separator=",">
         (
            #{item.bookGroupId,jdbcType=BIGINT},
            #{item.wxUserId,jdbcType=VARCHAR},
            #{item.altId,jdbcType=VARCHAR},
            #{item.isPush,jdbcType=BOOLEAN},
            NOW(),
            NOW()
          )
        </foreach>
    </insert>

    <!--获取最早的更新推送-->
    <select id="getEarliestPushBookGroupUpdate" resultMap="BaseResultMap">
        select <include refid="Base_Column_List"/>
        from push_book_group_update
        where is_push=0
        order by create_time asc
        limit 1
    </select>

    <!--更新是否推送-->
    <update id="updateIsPushTrue" parameterType="map">
        update push_book_group_update
        set is_push=1,
        update_time=now()
        where id=#{id}
        and is_push=0
    </update>
</mapper>