<?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.advertising.dao.impl.AdvertisingPlanGroupDaoImpl">
    <resultMap id="BaseResultMap" type="com.pcloud.book.advertising.entity.AdvertisingPlanGroup">
        <id column="id" property="id" jdbcType="BIGINT"/>
        <result column="advertising_plan_id" property="advertisingPlanId" jdbcType="BIGINT"/>
        <result column="book_id" property="bookId" jdbcType="BIGINT"/>
        <result column="book_group_id" property="bookGroupId" jdbcType="BIGINT"/>
        <result column="classify_id" property="classifyId" jdbcType="BIGINT"/>
        <result column="qrcode_id" property="qrcodeId" jdbcType="BIGINT"/>
        <result column="weixin_group_id" property="weixinGroupId" jdbcType="VARCHAR"/>
        <result column="is_delete" property="isDelete" jdbcType="BOOLEAN"/>
        <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, advertising_plan_id, book_id, book_group_id, classify_id, qrcode_id,
        weixin_group_id, is_delete, create_user, create_time, update_user, update_time
    </sql>

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

    <update id="deleteById" parameterType="java.lang.Long">
        update advertising_plan_group
        set is_delete=1
        where id = #{id}
    </update>
    
    <insert id="insert" parameterType="com.pcloud.book.advertising.entity.AdvertisingPlanGroup"
            useGeneratedKeys="true" keyProperty="id">
        insert into advertising_plan_group
        <trim prefix="(" suffix=")" suffixOverrides=",">
            advertising_plan_id,
            book_id,
            book_group_id,
            classify_id,
            qrcode_id,
            weixin_group_id,
            is_delete,
            create_user,
            create_time,
            update_user,
            update_time
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            #{advertisingPlanId,jdbcType=BIGINT},
            #{bookId,jdbcType=BIGINT},
            #{bookGroupId,jdbcType=BIGINT},
            #{classifyId,jdbcType=BIGINT},
            #{qrcodeId,jdbcType=BIGINT},
            #{weixinGroupId,jdbcType=VARCHAR},
            0,
            #{createUser,jdbcType=BIGINT},
            NOW(),
            #{updateUser,jdbcType=BIGINT},
            NOW(),
        </trim>
    </insert>

    <update id="update" parameterType="com.pcloud.book.advertising.entity.AdvertisingPlanGroup">
        update advertising_plan_group
        <set>
            <if test="advertisingPlanId != null">
                advertising_plan_id = #{advertisingPlanId,jdbcType=BIGINT},
            </if>
            <if test="bookId != null">
                book_id = #{bookId,jdbcType=BIGINT},
            </if>
            <if test="bookGroupId != null">
                book_group_id = #{bookGroupId,jdbcType=INTEGER},
            </if>
            <if test="classifyId != null">
                classify_id = #{classifyId,jdbcType=VARCHAR},
            </if>
            <if test="qrcodeId != null">
                qrcode_id = #{qrcodeId,jdbcType=TIMESTAMP},
            </if>
            <if test="weixinGroupId != null">
                weixin_group_id = #{weixinGroupId,jdbcType=TIMESTAMP},
            </if>
            <if test="isDelete != null">
                is_delete = #{isDelete,jdbcType=BOOLEAN},
            </if>
            <if test="updateUser != null">
                update_user = #{updateUser,jdbcType=BIGINT},
            </if>
                update_time=now()
        </set>
        where id = #{id}
    </update>

    <!--批量插入-->
    <insert id="batchInsert"  parameterType="com.pcloud.book.advertising.entity.AdvertisingPlanGroup" useGeneratedKeys="true" keyProperty="id">
        insert into advertising_plan_group (
            advertising_plan_id,
            book_id,
            book_group_id,
            classify_id,
            qrcode_id,
            weixin_group_id,
            is_delete,
            create_user,
            create_time,
            update_user,
            update_time
        ) values
        <foreach collection="list" item="item" index="index" separator=",">
            (
            #{item.advertisingPlanId,jdbcType=BIGINT},
            #{item.bookId,jdbcType=BIGINT},
            #{item.bookGroupId,jdbcType=BIGINT},
            #{item.classifyId,jdbcType=BIGINT},
            #{item.qrcodeId,jdbcType=BIGINT},
            #{item.weixinGroupId,jdbcType=VARCHAR},
            0,
            #{item.createUser,jdbcType=BIGINT},
            NOW(),
            #{item.updateUser,jdbcType=BIGINT},
            NOW()
            )
        </foreach>
    </insert>

    <!--根据计划id查询    -->
    <select id="getListByPlanId" parameterType="long" resultMap="BaseResultMap">
        select <include refid="Base_Column_List"/> from advertising_plan_group
        where is_delete=0 and  advertising_plan_id=#{planId}
    </select>

    <!--批量删除-->
    <update id="deleteByIds" parameterType="Long">
        UPDATE
        advertising_plan_group
        SET is_delete = 1,
        update_user = #{partyId},
        update_time = now()
        WHERE id in
        <foreach collection="ids" index="index" item="item" open="(" separator="," close=")">
            ${item}
        </foreach>
    </update>

    <!--根据计划id删除-->
    <update id="deleteByPlanId" parameterType="map">
        UPDATE
        advertising_plan_group
        SET is_delete = 1,
        update_user = #{partyId},
        update_time = now()
        WHERE advertising_plan_id=#{planId}
    </update>

    <!--根据计划id集合查询-->
    <select id="getListByPlanIds" parameterType="map" resultMap="BaseResultMap">
        select <include refid="Base_Column_List"/> from advertising_plan_group t
        where t.is_delete=0 and advertising_plan_id in
        <foreach collection="planIds" item="item" open="(" separator="," close=")">
            ${item}
        </foreach>
    </select>
</mapper>