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

    <resultMap type="com.pcloud.book.applet.entity.AppletOuterBook" id="AppletOuterBookMap">
        <result property="id" column="id" jdbcType="INTEGER"/>
        <result property="coverImg" column="cover_img" jdbcType="VARCHAR"/>
        <result property="bookName" column="book_name" jdbcType="VARCHAR"/>
        <result property="publish" column="publish" jdbcType="VARCHAR"/>
        <result property="author" column="author" jdbcType="VARCHAR"/>
        <result property="isbn" column="isbn" jdbcType="VARCHAR"/>
        <result property="remark" column="remark" jdbcType="VARCHAR"/>
        <result column="price" property="price" jdbcType="DECIMAL" />
        <result property="linkType" column="link_type" jdbcType="INTEGER"/>
        <result property="linkUrl" column="link_url" jdbcType="VARCHAR"/>
        <result property="createTime" column="create_time" jdbcType="TIMESTAMP"/>
        <result property="updateTime" column="update_time" jdbcType="TIMESTAMP"/>
        <result property="agentId" column="agent_id" jdbcType="BIGINT" />
    </resultMap>

    <sql id="Base_Column_List">
        id, cover_img, book_name, publish, author, isbn, remark, price, link_type, link_url, create_time, update_time, agent_id
    </sql>

    <!--查询单个-->
    <select id="getById" resultMap="AppletOuterBookMap">
        select
          <include refid="Base_Column_List"/>
        from book.applet_outer_book
        where id = #{id}
    </select>


    <!--新增所有列-->
    <insert id="insert" keyProperty="id" useGeneratedKeys="true">
        insert into book.applet_outer_book(cover_img, book_name, publish, author, isbn, remark, price, link_type, link_url, create_time, update_time, agent_id)
        values (#{coverImg}, #{bookName}, #{publish}, #{author}, #{isbn}, #{remark}, #{price}, #{linkType}, #{linkUrl}, now(), now(),
        #{agentId})
    </insert>

    <!--通过主键修改数据-->
    <update id="update">
        update book.applet_outer_book
        <set>
            <if test="coverImg != null and coverImg != ''">
                cover_img = #{coverImg},
            </if>
            <if test="bookName != null and bookName != ''">
                book_name = #{bookName},
            </if>
            <if test="publish != null and publish != ''">
                publish = #{publish},
            </if>
                author = #{author},
                isbn = #{isbn},
                remark = #{remark},
            <if test="price != null">
                price = #{price},
            </if>
            <if test="linkType != null">
                link_type = #{linkType},
            </if>
            <if test="linkUrl != null and linkUrl != ''">
                link_url = #{linkUrl},
            </if>
                update_time = now(),
        </set>
        where id = #{id}
    </update>

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

    <select id="listPageOuterBook" parameterType="map" resultType="com.pcloud.book.applet.dto.AppletOuterBookDTO">
        SELECT
        id, cover_img coverImg, book_name bookName, publish, author, isbn, remark, price,
         link_type linkType, link_url linkUrl, create_time createTime
        FROM
        book.applet_outer_book
        WHERE 1=1
        <if test="publish != null">
            AND publish = #{publish}
        </if>
        <if test="name != null">
            AND (
            book_name like concat("%",#{name},"%") or isbn like concat("%",#{name},"%")
            )
        </if>
        <choose>
            <when test="agentId != null">
                and agent_id = #{agentId}
            </when>
            <otherwise>
                and agent_id = 0
            </otherwise>
        </choose>
        order by create_time desc
    </select>

    <select id="getAllPublish" resultType="string" parameterType="map">
        SELECT DISTINCT publish
        FROM book.applet_outer_book
         WHERE
	      publish IS NOT NULL
        <choose>
            <when test="agentId != null">
                and agent_id = #{agentId}
            </when>
            <otherwise>
                and agent_id = 0
            </otherwise>
        </choose>
    </select>

    <select id="listPageOuterBookByBooklistId" parameterType="map" resultType="com.pcloud.book.applet.dto.AppletOuterBookDTO">
        SELECT
            a.id,
            c.id configId,
            a.cover_img coverImg,
            a.book_name bookName,
            a.publish,
            a.author,
            a.isbn,
            a.remark,
            a.price,
            a.link_type linkType,
            a.link_url linkUrl,
            c.create_time createTime
        FROM
            applet_outer_book a
        LEFT JOIN applet_outer_booklist_config c ON a.id = c.outer_book_id
        WHERE
            c.booklist_id = #{booklistId}
        ORDER BY
            c.create_time DESC,
            c.id DESC
        <if test="limit != null">
            limit #{limit}
        </if>
    </select>

</mapper>