<?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.AdNewsWechatChooseDaoImpl">
    <resultMap id="BaseResultMap" type="com.pcloud.book.adnews.entity.AdNewsWechatChoose">
        <id column="id" property="id" jdbcType="BIGINT"/>
        <result column="adviser_id" property="adviserId" jdbcType="BIGINT"/>
        <result column="ad_news_wechat_id" property="adNewsWechatId" 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_wechat_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_wechat_choose
        where id = #{id,jdbcType=BIGINT}
    </select>

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

    <update id="update" parameterType="com.pcloud.book.adnews.entity.AdNewsWechatChoose">
        update ad_news_wechat_choose
        <set>
            <if test="adviserId != null">
                adviser_id = #{adviserId,jdbcType=BIGINT},
            </if>
            <if test="adNewsWechatId != null">
                ad_news_wechat_id = #{adNewsWechatId,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_wechat_choose (
        adviser_id,
        ad_news_wechat_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.adNewsWechatId,jdbcType=BIGINT},
            0,
            #{item.createUser,jdbcType=BIGINT},
            NOW(),
            #{item.updateUser,jdbcType=BIGINT},
            NOW()
            )
        </foreach>
    </insert>

    <!--根据id删除-->
    <update id="deleteAdNewsWechatChooseById" parameterType="map">
        update ad_news_wechat_choose set
        is_delete=1,
        update_user=#{partyId},
        update_time=now()
        where ad_news_wechat_id=#{adNewsWechatId}
    </update>

    <!--根据编辑id删除-->
    <update id="deleteAdNewsWechatChooseByPartyId" parameterType="map">
        update ad_news_wechat_choose set
        is_delete=1,
        update_user=#{partyId},
        update_time=now()
        where adviser_id=#{partyId}
    </update>

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

</mapper>