<?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.push.dao.impl.PushNewsRecordDaoImpl">
    <resultMap id="BaseResultMap" type="com.pcloud.book.push.entity.PushNewsRecord">
        <id column="id" property="id" jdbcType="BIGINT"/>
        <result column="weixin_group_id" property="weixinGroupId" jdbcType="VARCHAR"/>
        <result column="news_id" property="newsId" jdbcType="BIGINT"/>
        <result column="create_time" property="createTime" jdbcType="TIMESTAMP"/>
    </resultMap>

    <sql id="Base_Column_List">
        id, weixin_group_id, news_id, create_time
    </sql>

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

    <insert id="insert" parameterType="com.pcloud.book.push.entity.PushNewsRecord" useGeneratedKeys="true" keyProperty="id">
        insert into push_news_record
        <trim prefix="(" suffix=")" suffixOverrides=",">
            weixin_group_id,
            news_id,
            create_time
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            #{weixinGroupId,jdbcType=VARCHAR},
            #{newsId,jdbcType=BIGINT},
            NOW()
        </trim>
    </insert>

    <update id="update" parameterType="com.pcloud.book.push.entity.PushNewsRecord">
        update push_news_record
        <set>
            <if test="weixinGroupId != null">
                weixin_group_id = #{weixinGroupId,jdbcType=VARCHAR},
            </if>
            <if test="newsId != null">
                news_id = #{newsId,jdbcType=BIGINT},
            </if>
        </set>
        where id = #{id,jdbcType=BIGINT}
    </update>

    <!--根据微信群id集合查询发送记录-->
    <select id="getListByGroupIds" parameterType="map" resultMap="BaseResultMap">
        select <include refid="Base_Column_List"/> from push_news_record
        where weixin_group_id in
        <foreach collection="weixinGroupIds" index="index" item="item" open="(" close=")" separator=",">
            #{item}
        </foreach>
    </select>

    <!--批量新增-->
    <insert id="batchInsert"  parameterType="com.pcloud.book.push.entity.PushNewsRecord"
            useGeneratedKeys="true" keyProperty="id">
        insert into push_news_record (
        weixin_group_id,
        news_id,
        create_time
        ) values
        <foreach collection="list" item="item" index="index" separator=",">
            (
            #{item.weixinGroupId,jdbcType=VARCHAR},
            #{item.newsId,jdbcType=BIGINT},
            NOW()
            )
        </foreach>
    </insert>
</mapper>