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

    <resultMap id="BaseResultMap" type="com.pcloud.book.applet.entity.BuyBookRequest">
        <id column="id" property="id" jdbcType="BIGINT"/>
        <result column="book_id" property="bookId" jdbcType="BIGINT"/>
        <result column="adviser_id" property="adviserId" jdbcType="BIGINT"/>
        <result column="channel_id" property="channelId" jdbcType="BIGINT"/>
        <result column="agent_id" property="agentId" jdbcType="BIGINT"/>
        <result column="wechat_user_id" property="wechatUserId" jdbcType="BIGINT"/>
        <result column="user_name" property="userName" jdbcType="VARCHAR"/>
        <result column="phone" property="phone" jdbcType="VARCHAR"/>
        <result column="address" property="address" jdbcType="VARCHAR"/>
        <result column="create_time" property="createTime" jdbcType="TIMESTAMP"/>
        <result column="update_time" property="updateTime" jdbcType="TIMESTAMP"/>
    </resultMap>

    <sql id="Base_Column_List">
        id, book_id, adviser_id, channel_id, agent_id, wechat_user_id, user_name, phone, address, create_time, update_time        
    </sql>

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

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

    <insert id="insert" keyProperty="id" useGeneratedKeys="true">
        INSERT INTO buy_book_request(
            book_id, 
            adviser_id, 
            channel_id, 
            agent_id, 
            wechat_user_id, 
            user_name, 
            phone, 
            address, 
            create_time, 
            update_time
        ) VALUES (
            #{bookId, jdbcType=BIGINT}, 
            #{adviserId, jdbcType=BIGINT}, 
            #{channelId, jdbcType=BIGINT}, 
            #{agentId, jdbcType=BIGINT}, 
            #{wechatUserId, jdbcType=BIGINT}, 
            #{userName, jdbcType=VARCHAR}, 
            #{phone, jdbcType=VARCHAR}, 
            #{address, jdbcType=VARCHAR}, 
            now(),
            now()
        )
    </insert>

    <insert id="batchInsert">
        INSERT INTO buy_book_request (
        book_id,
        adviser_id,
        channel_id,
        agent_id,
        wechat_user_id,
        user_name,
        phone,
        address,
        create_time,
        update_time
        ) VALUES
        <foreach collection="list" item="item" separator=",">
            (
            #{item.bookId, jdbcType=BIGINT},
            #{item.adviserId, jdbcType=BIGINT},
            #{item.channelId, jdbcType=BIGINT},
            #{item.agentId, jdbcType=BIGINT},
            #{item.wechatUserId, jdbcType=BIGINT},
            #{item.userName, jdbcType=VARCHAR},
            #{item.phone, jdbcType=VARCHAR},
            #{item.address, jdbcType=VARCHAR},
            #{item.createTime, jdbcType=TIMESTAMP},
            #{item.updateTime, jdbcType=TIMESTAMP}
            )
        </foreach>
    </insert>

    <!--通过主键修改数据-->
    <update id="update">
        UPDATE buy_book_request
        <set>
            <if test="bookId != null">
                book_id = #{bookId},
            </if>
            <if test="adviserId != null">
                adviser_id = #{adviserId},
            </if>
            <if test="channelId != null">
                channel_id = #{channelId},
            </if>
            <if test="agentId != null">
                agent_id = #{agentId},
            </if>
            <if test="wechatUserId != null">
                wechat_user_id = #{wechatUserId},
            </if>
            <if test="userName != null and userName != ''">
                user_name = #{userName},
            </if>
            <if test="phone != null and phone != ''">
                phone = #{phone},
            </if>
            <if test="address != null and address != ''">
                address = #{address},
            </if>
            <if test="createTime != null">
                create_time = #{createTime},
            </if>
            <if test="updateTime != null">
                update_time = #{updateTime},
            </if>
        </set>
        WHERE id = #{id}
    </update>

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

    <select id="getUserCount" resultType="com.pcloud.book.applet.entity.BuyBookRequest">
        select
        wechat_user_id wechatUserId,
        book_id bookId
        from
        buy_book_request
        order by
        create_time desc
    </select>

</mapper>