<?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.pcloud.book.applet.dao.impl.AppletBookClassifyDaoImpl">

    <resultMap type="com.pcloud.book.applet.entity.AppletBookClassify" id="AppletBookClassifyMap">
        <result property="id" column="id" jdbcType="INTEGER"/>
        <result property="classify" column="classify" jdbcType="VARCHAR"/>
        <result property="classifyName" column="classify_name" jdbcType="VARCHAR"/>
        <result property="seq" column="seq" jdbcType="INTEGER"/>
        <result property="pic" column="pic" jdbcType="VARCHAR"/>
        <result property="showState" column="show_state" jdbcType="BOOLEAN"/>
        <result property="createTime" column="create_time" jdbcType="TIMESTAMP"/>
        <result property="updateTime" column="update_time" jdbcType="TIMESTAMP"/>
    </resultMap>

    <!--查询单个-->
    <select id="queryById" resultMap="AppletBookClassifyMap">
        select
          id, classify, classify_name, seq, pic, show_state, create_time, update_time
        from book.applet_book_classify
        where id = #{id}
    </select>


    <!--通过实体作为筛选条件查询-->
    <select id="queryAll" parameterType="map" resultType="com.pcloud.book.applet.dto.AppletBookClassifyDTO">
        select
          id, classify, classify_name classifyName, seq, pic, show_state showState
        from book.applet_book_classify
        WHERE 1=1
            <if test="showState != null">
                and show_state = #{showState}
            </if>
        order by seq asc, id asc
    </select>


    <!--通过主键修改数据-->
    <update id="update">
        update book.applet_book_classify
        <set>
            <if test="classifyName != null and classifyName != ''">
                classify_name = #{classifyName},
            </if>
            <if test="seq != null">
                seq = #{seq},
            </if>
            <if test="pic != null and pic != ''">
                pic = #{pic},
            </if>
            <if test="showState != null">
                show_state = #{showState},
            </if>
             update_time = now(),
        </set>
        where id = #{id}
    </update>

    <insert id="addBookClassifyRelation" parameterType="list" useGeneratedKeys="true" keyProperty="id">
        insert into book.applet_book_classify_relation (
        applet_book_classify_id, book_templet_id, create_time)
        values
        <foreach collection="list" item="item" index="index" separator=",">
            ( #{item.appletBookClassifyId}, #{item.bookTempletId}, NOW() )
        </foreach>
    </insert>

    <select id="getRelationBookTempletByBookClassify" parameterType="long" resultType="long">
        SELECT
        book_templet_id bookTempletId
        FROM book.applet_book_classify_relation
        WHERE applet_book_classify_id = #{bookClassifyId}
    </select>

    <select id="getAppletBookClassifyId" parameterType="long" resultType="long">
        select
        applet_book_classify_id
        from
        applet_book_classify_relation
        where
        book_templet_id = #{firstClassifyId}
    </select>

    <select id="getBookClassifyByIds" parameterType="list" resultType="com.pcloud.book.applet.dto.AppletBookClassifyDTO">
        select
        id, classify, classify_name classifyName, seq, pic, show_state showState
        from book.applet_book_classify
        <where>
            <if test="list != null">
                id in
                <foreach collection="list" index="index" item="item" open="(" close=")" separator=",">
                    #{item}
                </foreach>
            </if>
        </where>
        order by seq asc, id asc
    </select>

    <select id="getClassifyNames" parameterType="list" resultType="com.pcloud.book.applet.dto.AppletBookClassifyDTO">
        select
        book_templet_id templetId,
        classify_name classifyName
        from book.applet_book_classify a
        left join
        applet_book_classify_relation b
        on a.id = b.applet_book_classify_id
        <where>
            <if test="list != null">
                book_templet_id in
                <foreach collection="list" index="index" item="item" open="(" close=")" separator=",">
                    #{item}
                </foreach>
            </if>
        </where>
        group by book_templet_id
    </select>

</mapper>