<?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.adnews.dao.impl.AdNewsChooseDaoImpl">
    <resultMap id="BaseResultMap" type="com.pcloud.book.adnews.entity.AdNewsChoose">
        <id column="id" property="id" jdbcType="BIGINT"/>
        <result column="adviser_id" property="adviserId" jdbcType="BIGINT"/>
        <result column="ad_news_id" property="adNewsId" jdbcType="BIGINT"/>
        <result column="is_delete" property="isDelete" jdbcType="BOOLEAN"/>
        <result column="create_time" property="createTime" jdbcType="TIMESTAMP"/>
        <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"/>
    </resultMap>

    <sql id="Base_Column_List">
        id, adviser_id, ad_news_id, is_delete, create_time, create_user, update_time, update_user
    </sql>

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

    <insert id="insert" parameterType="com.pcloud.book.adnews.entity.AdNewsChoose" useGeneratedKeys="true" keyProperty="id">
        insert into ad_news_choose
        <trim prefix="(" suffix=")" suffixOverrides=",">
            adviser_id,
            ad_news_id,
            is_delete,
            create_user,
            create_time,
            update_user,
            update_time
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            #{adviserId,jdbcType=BIGINT},
            #{adNewsId,jdbcType=BIGINT},
            0,
            #{createUser,jdbcType=BIGINT},
            NOW(),
            #{updateUser,jdbcType=BIGINT},
            NOW(),
        </trim>
    </insert>

    <update id="update" parameterType="com.pcloud.book.adnews.entity.AdNewsChoose">
        update ad_news_choose
        <set>
            <if test="adviserId != null">
                adviser_id = #{adviserId,jdbcType=BIGINT},
            </if>
            <if test="adNewsId != null">
                ad_news_id = #{adNewsId,jdbcType=BIGINT},
            </if>
            <if test="isDelete != null">
                is_delete = #{isDelete,jdbcType=BOOLEAN},
            </if>
            <if test="updateUser != null">
                update_user = #{updateUser,jdbcType=VARCHAR},
            </if>
                update_time=now()
        </set>
        where id = #{id,jdbcType=BIGINT}
    </update>

    <!--批量新增-->
    <insert id="batchInsert" useGeneratedKeys="true" parameterType="java.util.List">
        insert into ad_news_choose (
            adviser_id,
            ad_news_id,
            is_delete,
            create_user,
            create_time,
            update_user,
            update_time
        )
        values
        <foreach collection="list" item="item" index="index" separator="," >
            (
            #{item.adviserId,jdbcType=BIGINT},
            #{item.adNewsId,jdbcType=BIGINT},
            0,
            #{item.createUser,jdbcType=BIGINT},
            NOW(),
            #{item.updateUser,jdbcType=BIGINT},
            NOW()
            )
        </foreach>
    </insert>

    <!--根据id删除-->
    <update id="deleteAdNewsChooseById" parameterType="map">
        update ad_news_choose set
        is_delete=1,
        update_user=#{partyId},
        update_time=now()
        where ad_news_id=#{adNewsId}
    </update>

    <!--根据条件获取数量-->
    <select id="getCountByAdNewsIdsAndPartyId" parameterType="map" resultType="Integer">
        select count(1) from ad_news_choose where
        is_delete=0
        and adviser_id=#{partyId}
        and ad_news_id in
        <foreach collection="adNewsIds" item="item" index="index" separator="," open="(" close=")">
            #{item}
        </foreach>
    </select>
</mapper>