<?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.da.dangan.mapper.DaBookshelfMapper">
    
    <resultMap type="DaBookshelf" id="DaBookshelfResult">
        <result property="shelfId"    column="shelf_id"    />
        <result property="name"    column="name"    />
        <result property="pid"    column="pid"    />
        <result property="ancestors"    column="ancestors"    />
        <result property="orderNum"    column="order_num"    />
        <result property="remark"    column="remark"    />
        <result property="createBy"    column="create_by"    />
        <result property="createTime"    column="create_time"    />
        <result property="updateBy"    column="update_by"    />
        <result property="updateTime"    column="update_time"    />
    </resultMap>

    <sql id="selectDaBookshelfVo">
        select shelf_id, name, pid, ancestors, order_num, remark, create_by, create_time, update_by, update_time from da_bookshelf
    </sql>

    <select id="selectDaBookshelfList" parameterType="DaBookshelf" resultMap="DaBookshelfResult">
        <include refid="selectDaBookshelfVo"/>
        <where>  
            <if test="name != null  and name != ''"> and name like concat('%', #{name}, '%')</if>
            <if test="pid != null "> and pid = #{pid}</if>
            <if test="ancestors != null  and ancestors != ''"> and ancestors = #{ancestors}</if>
            <if test="orderNum != null "> and order_num = #{orderNum}</if>
        </where>
    </select>
    
    <select id="selectDaBookshelfByShelfId" parameterType="Long" resultMap="DaBookshelfResult">
        <include refid="selectDaBookshelfVo"/>
        where shelf_id = #{shelfId}
    </select>
        
    <insert id="insertDaBookshelf" parameterType="DaBookshelf" useGeneratedKeys="true" keyProperty="shelfId">
        insert into da_bookshelf
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="name != null and name != ''">name,</if>
            <if test="pid != null">pid,</if>
            <if test="ancestors != null">ancestors,</if>
            <if test="orderNum != null">order_num,</if>
            <if test="remark != null">remark,</if>
            <if test="createBy != null">create_by,</if>
            <if test="createTime != null">create_time,</if>
            <if test="updateBy != null">update_by,</if>
            <if test="updateTime != null">update_time,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="name != null and name != ''">#{name},</if>
            <if test="pid != null">#{pid},</if>
            <if test="ancestors != null">#{ancestors},</if>
            <if test="orderNum != null">#{orderNum},</if>
            <if test="remark != null">#{remark},</if>
            <if test="createBy != null">#{createBy},</if>
            <if test="createTime != null">#{createTime},</if>
            <if test="updateBy != null">#{updateBy},</if>
            <if test="updateTime != null">#{updateTime},</if>
         </trim>
    </insert>

    <update id="updateDaBookshelf" parameterType="DaBookshelf">
        update da_bookshelf
        <trim prefix="SET" suffixOverrides=",">
            <if test="name != null and name != ''">name = #{name},</if>
            <if test="pid != null">pid = #{pid},</if>
            <if test="ancestors != null">ancestors = #{ancestors},</if>
            <if test="orderNum != null">order_num = #{orderNum},</if>
            <if test="remark != null">remark = #{remark},</if>
            <if test="createBy != null">create_by = #{createBy},</if>
            <if test="createTime != null">create_time = #{createTime},</if>
            <if test="updateBy != null">update_by = #{updateBy},</if>
            <if test="updateTime != null">update_time = #{updateTime},</if>
        </trim>
        where shelf_id = #{shelfId}
    </update>

    <delete id="deleteDaBookshelfByShelfId" parameterType="Long">
        delete from da_bookshelf where shelf_id = #{shelfId}
    </delete>

    <delete id="deleteDaBookshelfByShelfIds" parameterType="String">
        delete from da_bookshelf where shelf_id in 
        <foreach item="shelfId" collection="array" open="(" separator="," close=")">
            #{shelfId}
        </foreach>
    </delete>
</mapper>