<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">

<mapper namespace="com.java2nb.books.dao.BookContentDao">

	<select id="get" resultType="com.java2nb.books.domain.BookContentDO">
		select `id`,`book_id`,`index_id`,`index_num`,`content` from book_content where id = #{value}
	</select>

	<select id="list" resultType="com.java2nb.books.domain.BookContentDO">
		select `id`,`book_id`,`index_id`,`index_num`,`content` from book_content
        <where>  
		  		  <if test="id != null and id != ''"> and id = #{id} </if>
		  		  <if test="bookId != null and bookId != ''"> and book_id = #{bookId} </if>
		  		  <if test="indexId != null and indexId != ''"> and index_id = #{indexId} </if>
		  		  <if test="indexNum != null and indexNum != ''"> and index_num = #{indexNum} </if>
		  		  <if test="content != null and content != ''"> and content = #{content} </if>
		  		</where>
        <choose>
            <when test="sort != null and sort.trim() != ''">
                order by ${sort} ${order}
            </when>
			<otherwise>
                order by id desc
			</otherwise>
        </choose>
		<if test="offset != null and limit != null">
			limit #{offset}, #{limit}
		</if>
	</select>
	
 	<select id="count" resultType="int">
		select count(*) from book_content
		 <where>  
		  		  <if test="id != null and id != ''"> and id = #{id} </if>
		  		  <if test="bookId != null and bookId != ''"> and book_id = #{bookId} </if>
		  		  <if test="indexId != null and indexId != ''"> and index_id = #{indexId} </if>
		  		  <if test="indexNum != null and indexNum != ''"> and index_num = #{indexNum} </if>
		  		  <if test="content != null and content != ''"> and content = #{content} </if>
		  		</where>
	</select>
	 
	<insert id="save" parameterType="com.java2nb.books.domain.BookContentDO" useGeneratedKeys="true" keyProperty="id">
		insert into book_content
		(
			`id`, 
			`book_id`, 
			`index_id`, 
			`index_num`, 
			`content`
		)
		values
		(
			#{id}, 
			#{bookId}, 
			#{indexId}, 
			#{indexNum}, 
			#{content}
		)
	</insert>
	 
	<update id="update" parameterType="com.java2nb.books.domain.BookContentDO">
		update book_content 
		<set>
			<if test="bookId != null">`book_id` = #{bookId}, </if>
			<if test="indexId != null">`index_id` = #{indexId}, </if>
			<if test="indexNum != null">`index_num` = #{indexNum}, </if>
			<if test="content != null">`content` = #{content}</if>
		</set>
		where id = #{id}
	</update>
	
	<delete id="remove">
		delete from book_content where id = #{value}
	</delete>
	
	<delete id="batchRemove">
		delete from book_content where id in 
		<foreach item="id" collection="array" open="(" separator="," close=")">
			#{id}
		</foreach>
	</delete>

	<insert id="insertBatch" parameterType="java.util.List">
		insert into book_content (book_id, index_num, content)
		values
		<foreach collection="list" item="item" index="index"
				 separator=",">
			<trim prefix="(" suffix=")" suffixOverrides=",">
				#{item.bookId,jdbcType=VARCHAR},
				#{item.indexNum,jdbcType=VARCHAR},
				#{item.content,jdbcType=VARCHAR},
			</trim>
		</foreach>
	</insert>


	<delete id="removeByBookIds" parameterType="string">
		delete from book_content where book_id in (${bookIds});
	</delete>

</mapper>