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

    <resultMap id="BaseResultMap" type="com.pcloud.book.book.entity.SearchRecordDis">
        <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="search_count" property="searchCount" jdbcType="INTEGER"/>
        <result column="create_time" property="createTime" jdbcType="TIMESTAMP"/>
        <result column="update_time" property="updateTime" jdbcType="TIMESTAMP"/>
    </resultMap>

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

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

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

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

    <insert id="insert" keyProperty="id" useGeneratedKeys="true">
        INSERT INTO search_record_dis_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_dis_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_dis_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>
            search_count=search_count+1,
            update_time = now(),
        </set>
        WHERE id = #{id}
    </update>

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




    <select id="getByCondition" parameterType="map" resultMap="BaseResultMap">
        select <include refid="Base_Column_List"/>
        from search_record_dis_sale
        where user_delete=0
        and content=#{content}
        <if test="wechatUserId!=null">
            and wechat_user_id=#{wechatUserId}
        </if>
        <if test="wxId!=null">
            and wx_id=#{wxId}
        </if>
        limit 1
    </select>

    <update id="updateUserDeleteByWxId" parameterType="map" >
		update search_record_dis_sale
		set user_delete=#{userDelete},update_time = now()
		where wx_id=#{wxId}
	</update>

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

    <select id="getUserSearchList4H5" parameterType="map" resultMap="SearchRecordResultMap">
        select
        <include refid="Base_Column_List"/>
        FROM
        search_record_dis_sale
        WHERE
        user_delete = 0
        <if test="wxId!=null">
            and wx_id = #{wxId}
        </if>
        <if test="wechatUserId!=null">
            and wechat_user_id = #{wechatUserId}
        </if>
        ORDER by update_time DESC
    </select>

    <select id="countUserSearchList4H5" parameterType="map" resultType="integer">
        select
        count(1)
        FROM
        search_record_dis_sale
        WHERE
        user_delete = 0
        <if test="wxId!=null">
            and wx_id = #{wxId}
        </if>
        <if test="wechatUserId!=null">
            and wechat_user_id = #{wechatUserId}
        </if>
    </select>


</mapper>