<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "mybatis-3-mapper.dtd" >
<mapper namespace="com.pcloud.book.group.dao.impl.BookClassifyBuyRecordDaoImpl">
    <resultMap id="BaseResultMap" type="com.pcloud.book.group.entity.BookClassifyBuyRecord">
        <id column="id" property="id" jdbcType="BIGINT"/>
        <result column="book_id" property="bookId" jdbcType="BIGINT"/>
        <result column="channel_id" property="channelId" jdbcType="BIGINT"/>
        <result column="book_qrcode_id" property="bookQrcodeId" jdbcType="BIGINT"/>
        <result column="classify_id" property="classifyId" jdbcType="BIGINT"/>
        <result column="product_id" property="productId" jdbcType="BIGINT"/>
        <result column="product_spec_id" property="productSpecId" jdbcType="BIGINT"/>
        <result column="price" property="price" jdbcType="DECIMAL"/>
        <result column="product_id" property="productId" jdbcType="BIGINT"/>
        <result column="product_spec_id" property="productSpecId" jdbcType="BIGINT"/>
        <result column="wechat_user_id" property="wechatUserId" jdbcType="BIGINT"/>
        <result column="create_time" property="createTime" jdbcType="TIMESTAMP"/>
    </resultMap>

    <insert id="insert" parameterType="bookClassifyBuyRecord" useGeneratedKeys="true"
            keyProperty="id">
        insert into book_classify_buy_record
        <trim prefix="(" suffix=")" suffixOverrides=",">
            book_id,
            channel_id,
            book_qrcode_id,
            classify_id,
            price,
            product_id,
            product_spec_id,
            wechat_user_id,
            create_time,
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            #{bookId,jdbcType=BIGINT},
            #{channelId,jdbcType=BIGINT},
            #{bookQrcodeId,jdbcType=BIGINT},
            #{classifyId,jdbcType=BIGINT},
            #{price,jdbcType=DECIMAL},
            #{productId,jdbcType=BIGINT},
            #{productSpecId,jdbcType=BIGINT},
            #{wechatUserId,jdbcType=BIGINT},
            NOW(),
        </trim>
    </insert>

    <select id="checkUserBuy" resultType="Boolean" parameterType="map">
        select
            count(1)
        from
            book_classify_buy_record
        where
            wechat_user_id = #{wechatUserId,jdbcType=BIGINT}
        and
            classify_Id = #{classifyId}
    </select>


    <select id="checkUsersBuy" resultType = "Long" parameterType="map">
        select
        classify_Id
        from
        book_classify_buy_record
        where
        wechat_user_id = #{wechatUserId,jdbcType=BIGINT}
        and
        classify_Id in
        <foreach collection = "classifyIds" index="index" item = "item" open = "("  separator= "," close = ")">
            ${item}
        </foreach>
    </select>
</mapper>