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

    <resultMap id="BaseResultMap" type="com.pcloud.book.applet.entity.AppletGroupSearchRecord">
        <id column="id" property="id" jdbcType="BIGINT"/>
        <result column="book_group_id" property="bookGroupId" jdbcType="BIGINT"/>
        <result column="wechat_user_id" property="wechatUserId" jdbcType="BIGINT"/>
        <result column="book_id" property="bookId" jdbcType="BIGINT"/>
        <result column="channel_id" property="channelId" jdbcType="BIGINT"/>
        <result column="adviser_id" property="adviserId" jdbcType="BIGINT"/>
        <result column="create_time" property="createTime" jdbcType="TIMESTAMP"/>
        <result column="update_time" property="updateTime" jdbcType="TIMESTAMP"/>
    </resultMap>
    
    <sql id="Base_Column_List">
        id, book_group_id, wechat_user_id, book_id, channel_id, adviser_id, create_time, update_time
    </sql>

    <select id="getById" resultMap="BaseResultMap">
        SELECT
        <include refid="Base_Column_List"/>
        FROM applet_group_search_record
        WHERE id = #{id}
    </select>
    
    <select id="getList" resultType="com.pcloud.book.applet.dto.AppletGroupSearchRecordDTO">
        SELECT
            a.id,
            a.book_group_id bookGroupId,
            a.book_id bookId,
            a.channel_id channelId,
            a.adviser_id adviserId,
            a.wechat_user_id wechatUserId,
            d.BOOK_NAME bookName,
            d.AUTHOR author,
            d.PUBLISH publish,
            d.REMARK remark,
            d.COVER_IMG coverImg,
            d.ISBN isbn,
            c.join_group_type joinGroupType,
            g.TEMPLET_ID templetId,
            g.SECOND_TEMPLET_ID secondTempletId,
            g.third_TEMPLET_ID thirdTempletId,
            IF(c.join_group_type=1 OR c.related_book_group_id>0,1,0) hasGroup,
            c.related_book_group_id relatedBookGroupId
        FROM applet_group_search_record a
            INNER JOIN BOOK d ON a.BOOK_ID = d.BOOK_ID
            INNER JOIN book_group c ON a.book_group_id = c.id
            LEFT JOIN book_adviser g ON a.adviser_id = g.ADVISER_ID AND g.BOOK_ID = a.book_id
        WHERE 1=1
        <if test="wechatUserId!=null">
            AND a.wechat_user_id = #{wechatUserId}
        </if>
        ORDER BY a.update_time DESC
    </select>

    <insert id="insert" keyProperty="id" useGeneratedKeys="true">
        INSERT INTO applet_group_search_record(
            book_group_id, 
            wechat_user_id, 
            book_id, 
            channel_id, 
            adviser_id, 
            create_time,
            update_time
        ) VALUES (
            #{bookGroupId, jdbcType=BIGINT}, 
            #{wechatUserId, jdbcType=BIGINT}, 
            #{bookId, jdbcType=BIGINT}, 
            #{channelId, jdbcType=BIGINT}, 
            #{adviserId, jdbcType=BIGINT}, 
            NOW(),
            NOW()
        )
        ON DUPLICATE KEY UPDATE update_time=NOW()
    </insert>
    
    <insert id="batchInsert" keyProperty="id" useGeneratedKeys="true">
        INSERT INTO applet_group_search_record (
            book_group_id, 
            wechat_user_id, 
            book_id, 
            channel_id, 
            adviser_id, 
            create_time,
            update_time
        ) VALUES 
        <foreach collection="list" item="item" separator=",">
        (
            #{item.bookGroupId, jdbcType=BIGINT}, 
            #{item.wechatUserId, jdbcType=BIGINT}, 
            #{item.bookId, jdbcType=BIGINT}, 
            #{item.channelId, jdbcType=BIGINT}, 
            #{item.adviserId, jdbcType=BIGINT},
            NOW(),
            NOW()
        )
        </foreach>
    </insert>

    <!--通过主键修改数据-->
    <update id="update">
        UPDATE applet_group_search_record
        <set>
            <if test="bookGroupId != null">
                book_group_id = #{bookGroupId},
            </if>
            <if test="wechatUserId != null">
                wechat_user_id = #{wechatUserId},
            </if>
            <if test="bookId != null">
                book_id = #{bookId},
            </if>
            <if test="channelId != null">
                channel_id = #{channelId},
            </if>
            <if test="adviserId != null">
                adviser_id = #{adviserId},
            </if>
        </set>
        WHERE id = #{id}
    </update>

    <!--通过主键删除-->
    <delete id="deleteById">
        DELETE FROM applet_group_search_record where id = #{id}
    </delete>

    <!--通过主键删除-->
    <delete id="deleteByIds">
        DELETE FROM applet_group_search_record
        where id IN
        <foreach collection="list" item="item" open="(" close=")" separator=",">
            ${item}
        </foreach>
    </delete>

    <select id="getRecordIdListByUser" parameterType="long" resultType="long">
        SELECT DISTINCT id
        FROM applet_group_search_record
        WHERE wechat_user_id = #{wechatUserId}
    </select>

</mapper>