<?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="mini_wechat_user_id" property="miniWechatUserId" jdbcType="BIGINT"/>
        <result column="wechat_nick_name" property="nickName" jdbcType="VARCHAR"/>
        <result column="mini_open_id" property="miniOpenId" jdbcType="VARCHAR"/>
        <result column="reply_state"  property="replyState" jdbcType="BIGINT"/>
        <result column="is_delete" property="isDelete" jdbcType="BIGINT"/>
        <result column="create_time" property="createTime" jdbcType="TIMESTAMP"/>
        <result column="update_time" property="updateTime" jdbcType="TIMESTAMP"/>
    </resultMap>


    <resultMap id="BaseResultMap4DTO" type="com.pcloud.book.applet.dto.BuyBookRequestDTO">
        <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="mini_wechat_user_id" property="miniWechatUserId" jdbcType="BIGINT"/>
        <result column="wechat_nick_name" property="nickName" jdbcType="VARCHAR"/>
        <result column="mini_open_id" property="miniOpenId" jdbcType="VARCHAR"/>
        <result column="reply_state"  property="replyState" jdbcType="BIGINT"/>
        <result column="is_delete" property="isDelete" 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_id, adviser_id, channel_id, agent_id, wechat_user_id, user_name, phone, address, mini_wechat_user_id, wechat_nick_name, mini_open_id, reply_state, is_delete, 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="BaseResultMap4DTO">
        SELECT
        <include refid="Base_Column_List"/>
        FROM buy_book_request
        <where>
            <if test="agentId != null">
                agent_id = #{agentId}
            </if>
            and is_delete= #{isDelete}
        </where>
        order by create_time desc 
    </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,
            mini_wechat_user_id,
            wechat_nick_name,
            mini_open_id,
            reply_state,
            is_delete,
            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},
            #{miniWechatUserId, jdbcType=BIGINT},
            #{nickName, jdbcType=VARCHAR},
            #{miniOpenId, jdbcType=VARCHAR},
            #{replyState, jdbcType=BIGINT},
            #{isDelete, jdbcType=BIGINT},
            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,
        mini_wechat_user_id,
        wechat_nick_name,
        mini_open_id,
        reply_state,
        is_delete,
        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},
            #{miniWechatUserId, jdbcType=BIGINT},
            #{nickName, jdbcType=VARCHAR},
            #{miniOpenId, jdbcType=VARCHAR},
            #{replyState, jdbcType=BIGINT},
            #{isDelete, jdbcType=BIGINT},
            #{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="miniWechatUserId != null and miniWechatUserId != ''">
                mini_wechat_user_id = #{miniWechatUserId},
            </if>
            <if test="nickName != null and nickName != ''">
                wechat_nick_name = #{nickName},
            </if>
            <if test="miniOpenId != null and miniOpenId != ''">
                mini_open_id = #{miniOpenId},
            </if>
            <if test="replyState != null and replyState != ''">
                reply_state = #{replyState},
            </if>
            <if test="isDelete != null and isDelete != ''">
                is_delete = #{isDelete},
            </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>

    <select id="getRequestByWechatUserIdAndState" resultMap="BaseResultMap4DTO" parameterType="map">
        select
        <include refid="Base_Column_List"/>
        from
        buy_book_request
        <where>
            <if test="wechatUserId != null">
                wechat_user_id = #{wechatUserId, jdbcType=BIGINT}
            </if>
            <if test="miniWechatUserId != null">
                mini_wechat_user_id = #{miniWechatUserId, jdbcType=BIGINT}
            </if>
            <if test="replyState != null ">
            And
            reply_state = #{replyState, jdbcType=BIGINT}
            </if>
            And
            is_delete = #{isDelete, jdbcType=BIGINT}
        </where>
        order by
        create_time desc
    </select>

    <select id="listIdsByWeChatUserId" parameterType="long" resultType="long">
        select
        id
        from
        buy_book_request
        where
        wechat_user_id = #{wechatUserId, jdbcType=BIGINT}
    </select>

    <select id="listAllWeChatUserId" resultType="long">
        select
        distinct wechat_user_id
        FROM
        buy_book_request
    </select>

    <update id="update2ProcessingData" parameterType="map">
        update buy_book_request
    <set>
    <if test="miniWechatUserId != null and miniWechatUserId != ''">
        mini_wechat_user_id = #{miniWechatUserId},
    </if>
    <if test="nickName != null and nickName != ''">
        wechat_nick_name = #{nickName},
    </if>
    <if test="miniOpenId != null and miniOpenId != ''">
        mini_open_id = #{miniOpenId},
    </if>
    <if test="replyState != null and replyState != ''">
        reply_state = #{replyState},
    </if>
    <if test="isDelete != null and isDelete != ''">
        is_delete = #{isDelete},
    </if>
    </set>
     where wechat_user_id = #{wechatUserId, jdbcType=BIGINT}
    </update>

</mapper>