<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "mybatis-3-mapper.dtd" >
<mapper namespace="com.pcloud.book.keywords.dao.impl.BookGuideDaoImpl">

    <resultMap id="BaseResultMap" type="com.pcloud.book.keywords.entity.BookGuide">
        <id column="id" property="id" jdbcType="BIGINT"/>
        <result column="book_id" property="bookId" jdbcType="BIGINT"/>
        <result column="channel_id" property="channelId" jdbcType="BIGINT"/>
        <result column="book_group_id" property="bookGroupId" jdbcType="BIGINT"/>
        <result column="classify_id" property="classifyId" jdbcType="BIGINT"/>
        <result column="set_type" property="setType" jdbcType="TINYINT"/>
        <result column="is_recommend" property="isRecommend" jdbcType="TINYINT"/>
        <result column="recommend_language" property="recommendLanguage" jdbcType="VARCHAR"/>
        <result column="create_user" property="createUser" jdbcType="BIGINT"/>
        <result column="create_time" property="createTime" jdbcType="TIMESTAMP"/>
    </resultMap>
    <sql id="Base_Column_List">
        id, book_id, channel_id, book_group_id, classify_id, set_type, is_recommend,
        recommend_language, create_user, create_time
    </sql>

    <select id="getById" resultMap="BaseResultMap" parameterType="java.lang.Long">
        select
        <include refid="Base_Column_List"/>
        from book_guide
        where id = #{id,jdbcType=BIGINT}
    </select>


    <insert id="insert" parameterType="bookGuide" useGeneratedKeys="true"
            keyProperty="id">
        insert into book_guide
        <trim prefix="(" suffix=")" suffixOverrides=",">
            book_id,
            channel_id,
            book_group_id,
            classify_id,
            set_type,
            is_recommend,
            recommend_language,
            create_user,
            create_time,
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            #{bookId,jdbcType=BIGINT},
            #{channelId,jdbcType=BIGINT},
            #{bookGroupId,jdbcType=BIGINT},
            #{classifyId,jdbcType=BIGINT},
            #{setType,jdbcType=TINYINT},
            #{isRecommend,jdbcType=TINYINT},
            #{recommendLanguage,jdbcType=VARCHAR},
            #{createUser,jdbcType=BIGINT},
            NOW(),
        </trim>
    </insert>

    <update id="update" parameterType="bookGuide">
        update book_guide
        <set>
            <if test="isRecommend != null">
                is_recommend = #{isRecommend},
            </if>
            recommend_language = #{recommendLanguage},
            update_time = now()
        </set>
        where id = #{id,jdbcType=BIGINT}
    </update>

    <select id="getGuideByBookGroup" resultType="GuideVO" parameterType="Long">
        select
            id,
            book_id bookId,
            channel_id channelId,
            book_group_id bookGroupId,
            classify_id classifyId,
            set_type setType,
            is_recommend isRecommend,
            recommend_language recommendLanguage
        from
            book_guide
        where
          book_group_id = #{_parameter,jdbcType=BIGINT} and set_type = 0 limit 1
    </select>

    <select id="getGuideByClassify" resultType="GuideVO" parameterType="Long">
        select
        id,
        book_id bookId,
        channel_id channelId,
        book_group_id bookGroupId,
        classify_id classifyId,
        set_type setType,
        is_recommend isRecommend,
        recommend_language recommendLanguage
        from
        book_guide
        where
        classify_id = #{_parameter,jdbcType=BIGINT} and set_type = 1 limit 1
    </select>

    <select id="getBaseInfo" resultType="GuideDTO" parameterType="map">
        select
            id,
            book_id bookId,
            channel_id channelId,
            book_group_id bookGroupId,
            classify_id classifyId,
            set_type setType,
            is_recommend isRecommend,
            recommend_language recommendLanguage
        from
            book_guide
        where
            book_group_id = #{bookGroupId}
        and classify_id = #{classifyId}
        limit 1
    </select>

    <delete id="deleteById" parameterType="Long">
        delete from book_guide
        where id = #{_parameter,jdbcType=BIGINT}
    </delete>

    <!--查询所有应用作品类型引导语-->
    <select id="getAllAppGuide" resultType="com.pcloud.book.group.entity.BookGroupApp">
        SELECT
        t.book_group_id bookGroupId,
        IFNULL(t.classify_id, 0) classifyId,
        t1.serve_id serveId,
        t1.serve_type serveType,
        t.create_user createUser,
        1 AS originType
        FROM
        book_guide t
        INNER JOIN book_guide_reply t1 ON t.id = t1.book_guide_id
        WHERE
        t1.reply_type = 4
        AND t1.is_delete = 0
        GROUP BY
        t.book_group_id,
        t.classify_id,
        t1.serve_id,
        t1.serve_type
    </select>
</mapper>