<?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.AdNewsGroupDaoImpl">
    <resultMap id="BaseResultMap" type="com.pcloud.book.adnews.entity.AdNewsGroup">
        <id column="id" property="id" jdbcType="BIGINT"/>
        <result column="classify_id" property="classifyId" jdbcType="BIGINT"/>
        <result column="book_group_id" property="bookGroupId" jdbcType="BIGINT"/>
        <result column="ad_news_set_id" property="adNewsSetId" jdbcType="BIGINT"/>
        <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, classify_id, book_group_id, ad_news_set_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 ad_news_group
        where id = #{id,jdbcType=BIGINT}
    </select>

    <insert id="insert" parameterType="com.pcloud.book.adnews.entity.AdNewsGroup" useGeneratedKeys="true" keyProperty="id">
        insert into ad_news_group
        <trim prefix="(" suffix=")" suffixOverrides=",">
            classify_id,
            book_group_id,
            ad_news_set_id,
            is_delete,
            create_user,
            create_time,
            update_user,
            update_time
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            #{classifyId,jdbcType=BIGINT},
            #{bookGroupId,jdbcType=BIGINT},
            #{adNewsSetId,jdbcType=BIGINT},
            #{isDelete,jdbcType=BOOLEAN},
            #{createUser,jdbcType=BIGINT},
            NOW(),
            #{updateUser,jdbcType=BIGINT},
            NOW(),
        </trim>
    </insert>

    <update id="update" parameterType="com.pcloud.book.adnews.entity.AdNewsGroup">
        update ad_news_group
        <set>
            <if test="classifyId != null">
                classify_id = #{classifyId,jdbcType=BIGINT},
            </if>
            <if test="bookGroupId != null">
                book_group_id = #{bookGroupId,jdbcType=BIGINT},
            </if>
            <if test="adNewsSetId != null">
                ad_news_set_id = #{adNewsSetId,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_group (
            classify_id,
            book_group_id,
            ad_news_set_id,
            is_delete,
            create_user,
            create_time,
            update_user,
            update_time
        )
        values
        <foreach collection="list" item="item" index="index" separator="," >
            (
            #{item.classifyId,jdbcType=BIGINT},
            #{item.bookGroupId,jdbcType=BIGINT},
            #{item.adNewsSetId,jdbcType=BIGINT},
            0,
            #{item.createUser,jdbcType=BIGINT},
            NOW(),
            #{item.updateUser,jdbcType=BIGINT},
            NOW()
            )
        </foreach>
    </insert>

    <!--根据早晚报设置id删除-->
    <update id="deleteByAdNewsSetId" parameterType="Long">
        update ad_news_group set
        is_delete=1,
        update_time=now()
        where ad_news_set_id=#{adNewsSetId}
    </update>

    <!--根据早晚报设置id查询-->
    <select id="getListByAdNewsSetId" parameterType="Long" resultType="com.pcloud.book.adnews.entity.AdNewsGroup">
        select
            a.id,
            a.classify_id classifyId,
            a.book_group_id bookGroupId,
            a.ad_news_set_id adNewsSetId,
            a.is_delete isDelete,
            a.create_user createUser,
            a.create_time createTime,
            a.update_user updateUser,
            a.update_time updateTime,
            (select classify from book_group_classify b where b.id=a.classify_id) classify
        from ad_news_group a
        where a.is_delete=0 and a.ad_news_set_id=#{adNewsSetId}
    </select>
</mapper>