<?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.AppletBookClassifyAgentDaoImpl">

    <resultMap type="com.pcloud.book.applet.entity.AppletBookClassifyAgent" id="AppletBookClassifyAgentMap">
        <result property="id" column="id" jdbcType="INTEGER"/>
        <result property="appletBookClassifyId" column="applet_book_classify_id" jdbcType="INTEGER"/>
        <result property="agentId" column="agent_id" jdbcType="INTEGER"/>
        <result property="showState" column="show_state" jdbcType="BOOLEAN"/>
        <result property="createTime" column="create_time" jdbcType="TIMESTAMP"/>
        <result property="updateTime" column="update_time" jdbcType="TIMESTAMP"/>
        <result property="classifyName" column="classify_name" jdbcType="VARCHAR"/>
        <result property="seq" column="seq" jdbcType="INTEGER"/>
        <result property="pic" column="pic" jdbcType="VARCHAR"/>
    </resultMap>

    <sql id="Base_Column_List" >
       id, applet_book_classify_id, agent_id, show_state, create_time, update_time, classify_name,seq,pic
    </sql>

    <!--新增所有列-->
    <insert id="insert" keyProperty="id" useGeneratedKeys="true">
        insert into book.applet_book_classify_agent(applet_book_classify_id, agent_id, show_state, create_time, update_time, classify_name,seq, pic)
        values (#{appletBookClassifyId}, #{agentId}, #{showState}, NOW(), NOW(), #{classifyName}, #{seq}, #{pic})
    </insert>

    <!--批量新增-->
    <insert id="batchInsert" useGeneratedKeys="true" parameterType="java.util.List">
        insert into book.applet_book_classify_agent(applet_book_classify_id, agent_id, show_state, create_time, update_time, classify_name, seq, pic)
        values
        <foreach collection="list" item="item" index="index" separator="," >
            (
            #{item.appletBookClassifyId,jdbcType=INTEGER},
            #{item.agentId,jdbcType=INTEGER},
            #{item.showState},
            NOW(),
            NOW(),
            #{item.classifyName},
            #{item.seq},
            #{item.pic}
            )
        </foreach>
    </insert>

       <!--通过主键修改数据-->
    <update id="updateBaseInfoByAgent" parameterType="com.pcloud.book.applet.entity.AppletBookClassifyAgent">
        update book.applet_book_classify_agent
        <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
        applet_book_classify_id = #{appletBookClassifyId}
        and  agent_id = #{agentId}
    </update>

    <!--通过主键删除-->
    <delete id="deleteById">
        delete from book.applet_book_classify_agent where id = #{id}
    </delete>

    <select id="getByAgent" parameterType="long" resultMap="AppletBookClassifyAgentMap">
        SELECT <include refid="Base_Column_List"/>
        FROM applet_book_classify_agent
        WHERE agent_id = #{agentId}
    </select>

    <select id="listClassifyByAgent" parameterType="map" resultType="com.pcloud.book.applet.dto.AppletBookClassifyDTO">
        select
        c.id, c.classify, a.classify_name classifyName, a.seq, a.pic, a.show_state showState
        from applet_book_classify c
        LEFT JOIN applet_book_classify_agent a ON c.id = a.applet_book_classify_id
        WHERE
         a.agent_id = #{agentId}
        <if test="showState != null">
        AND a.show_state = #{showState}
        </if>
        order by a.seq asc, a.id asc
    </select>

</mapper>