<?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.book.dao.impl.SearchRecordSaleDaoImpl">

    <resultMap id="BaseResultMap" type="com.pcloud.book.book.entity.SearchRecord">
        <id column="id" property="id" jdbcType="INTEGER"/>
        <result column="content" property="content" jdbcType="VARCHAR"/>
        <result column="book_id" property="bookId" jdbcType="INTEGER"/>
        <result column="agent_id" property="agentId" jdbcType="INTEGER"/>
        <result column="wx_id" property="wxId" jdbcType="VARCHAR"/>
        <result column="wechat_user_id" property="wechatUserId" jdbcType="INTEGER"/>
        <result column="hot_book_id" property="hotBookId" jdbcType="INTEGER"/>
        <result column="user_delete" property="userDelete" jdbcType="BOOLEAN"/>
        <result column="create_time" property="createTime" jdbcType="TIMESTAMP"/>
    </resultMap>

    <sql id="Base_Column_List">
        id, content, book_id, agent_id, wx_id, wechat_user_id, hot_book_id, user_delete, create_time        
    </sql>

    <select id="getById" resultMap="BaseResultMap">
        SELECT
        <include refid="Base_Column_List"/>
        FROM search_record_sale
        WHERE id = #{id}
    </select>

    <select id="getList" resultMap="BaseResultMap">
        SELECT
        <include refid="Base_Column_List"/>
        FROM search_record_sale
    </select>

    <insert id="insert" keyProperty="id" useGeneratedKeys="true">
        INSERT INTO search_record_sale(
            content, 
            book_id, 
            agent_id, 
            wx_id, 
            wechat_user_id, 
            hot_book_id,
            create_time,
            update_time
        ) VALUES (
            #{content, jdbcType=VARCHAR},
            #{bookId, jdbcType=INTEGER},
            #{agentId, jdbcType=INTEGER},
            #{wxId, jdbcType=VARCHAR},
            #{wechatUserId, jdbcType=INTEGER},
            #{hotBookId, jdbcType=INTEGER},
           now(),
           now()
        )
    </insert>

    <insert id="batchInsert">
        INSERT INTO search_record_sale (
        content,
        book_id,
        agent_id,
        wx_id,
        wechat_user_id,
        hot_book_id,
        create_time,
        update_time
        ) VALUES
        <foreach collection="list" item="item" separator=",">
            (
            #{item.content, jdbcType=VARCHAR},
            #{item.bookId, jdbcType=INTEGER},
            #{item.agentId, jdbcType=INTEGER},
            #{item.wxId, jdbcType=VARCHAR},
            #{item.wechatUserId, jdbcType=INTEGER},
            #{item.hotBookId, jdbcType=INTEGER},
            now(),
            now()
            )
        </foreach>
    </insert>

    <!--通过主键修改数据-->
    <update id="update">
        UPDATE search_record_sale
        <set>
            <if test="content != null and content != ''">
                content = #{content},
            </if>
            <if test="bookId != null">
                book_id = #{bookId},
            </if>
            <if test="agentId != null">
                agent_id = #{agentId},
            </if>
            <if test="wxId != null and wxId != ''">
                wx_id = #{wxId},
            </if>
            <if test="wechatUserId != null">
                wechat_user_id = #{wechatUserId},
            </if>
            <if test="hotBookId != null">
                hot_book_id = #{hotBookId},
            </if>
            <if test="userDelete != null">
                user_delete = #{userDelete},
            </if>
            <if test="createTime != null">
                create_time = #{createTime},
            </if>
            update_time = #{updateTime},
        </set>
        WHERE id = #{id}
    </update>

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

    <select id="getSearchList" parameterType="map" resultType="com.pcloud.book.book.vo.SearchBookVO">
        select
        content,
        book_id bookId,
        agent_id agentId,
        count(1) searchCount
        from search_record_sale
        where 1=1
        <if test="content!=null">
            and content like CONCAT('%', #{content}, '%')
        </if>
        group by
        content,book_id,agent_id
    </select>

    <select id="getSearchRecordList" parameterType="map" resultMap="BaseResultMap">
        select <include refid="Base_Column_List"/>
        from search_record_sale
        where content=#{content}
        <if test="bookId!=null">
            and book_id=#{bookId}
        </if>
        <if test="agentId!=null">
            and agent_id=#{agentId}
        </if>
    </select>


    <delete id="deleteByWxId" parameterType="string" >
		delete from search_record_sale
		where wx_id=#{wxId}
	</delete>

    <select id="getRecent5WxIds" parameterType="long" resultType="string">
		select distinct wx_id
		from search_record_sale
		where book_id=#{bookId}
		limit 5
	</select>

    <update id="updateUserDeleteByWxId" parameterType="map" >
		update search_record_sale
		set user_delete=#{userDelete}
		where wx_id=#{wxId}
	</update>

    <select id="getBookSearchTop" parameterType="map" resultType="com.pcloud.book.book.vo.BookSearchTopVO">
		select
		content bookName,
		book_id bookId,
		agent_id agentId
		from search_record_sale
		where book_id is not null
		and agent_id is not null
		group by book_id,agent_id
		order by count(1) desc
		limit #{top}
	</select>

    <update id="updateUserDeleteByWechatUserId" parameterType="map" >
		update search_record_sale
		set user_delete=#{userDelete},
	        update_time = now()
		where wechat_user_id=#{wechatUserId}
	</update>

    <select id="getBookSearchCount" parameterType="map" resultType="Integer">
        select ifnull(count(1),0) from(
        SELECT
        b.BOOK_ID bookId
        FROM
        (select book_id bookId from
        search_record_sale
        where wechat_user_id>0 and book_id>0
        GROUP BY book_id)c
        INNER JOIN
        book_adviser a on c.bookId=a.book_id
        INNER JOIN book b ON b.BOOK_ID = a.BOOK_ID AND b.IS_DELETE = 0
        WHERE
        1=1
        <if test="keyword !=null">
            AND (b.BOOK_NAME LIKE CONCAT('%', #{keyword}, '%') or b.ISBN LIKE
            CONCAT(#{keyword},'%'))
        </if>
        GROUP BY b.BOOK_ID
        )t
    </select>

    <select id="getBookSearchIds" parameterType="map" resultType="com.pcloud.book.book.dto.AdviserBookInfoDTO">
        SELECT
        b.BOOK_ID bookId,
        b.ISBN isbn,
        b.BOOK_NAME bookName,
        a.ADVISER_ID adviserId
        FROM
        (select book_id bookId from
        search_record_sale
        where wechat_user_id>0 and book_id>0
        GROUP BY book_id)c
        INNER JOIN
        book_adviser a on c.bookId=a.book_id
        INNER JOIN book b ON b.BOOK_ID = a.BOOK_ID AND b.IS_DELETE = 0
        WHERE
        1=1
        <if test="keyword !=null">
            AND (b.BOOK_NAME LIKE CONCAT('%', #{keyword}, '%') or  b.ISBN LIKE CONCAT(#{keyword},'%'))
        </if>
        group by b.BOOK_ID
        order by count(1) desc
        limit #{pageNum}, #{numPerPage}
    </select>

    <select id="getWechatUserIds" parameterType="map" resultType="Long">
		select distinct wechat_user_id
		from search_record_sale
		where book_id=#{bookId}
		and wechat_user_id>0
	</select>


</mapper>