<?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.CopyCipherRecordDaoImpl">
    <resultMap id="BaseResultMap" type="com.pcloud.book.group.entity.CopyCipherRecord">
        <id column="id" property="id" jdbcType="BIGINT"/>
        <result column="cipher" property="cipher" jdbcType="VARCHAR"/>
        <result column="book_group_id" property="bookGroupId" jdbcType="BIGINT"/>
        <result column="wechat_user_id" property="wechatUserId" jdbcType="BIGINT"/>
        <result column="create_time" property="createTime" jdbcType="TIMESTAMP"/>
    </resultMap>


    <sql id="Base_Column_List">
        id, cipher, book_group_id, wechat_user_id, create_time
    </sql>

    <insert id="insert" parameterType="com.pcloud.book.group.entity.CopyCipherRecord" useGeneratedKeys="true"
            keyProperty="id">
        insert into copy_cipher_record
        <trim prefix="(" suffix=")" suffixOverrides=",">
            cipher,
            book_group_id,
            wechat_user_id,
            create_time
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            #{cipher,jdbcType=VARCHAR},
            #{bookGroupId,jdbcType=BIGINT},
            #{wechatUserId,jdbcType=BIGINT},
            NOW(),
        </trim>
    </insert>

    <select id="getCountByBookGroupIds" parameterType="map" resultType="com.pcloud.book.group.dto.BookGroupIdAndCountDTO">
        SELECT
        count(1) count,
        book_group_id bookGroupId
        FROM
        (
        SELECT
        t.book_group_id,
        t.wechat_user_id
        FROM
        copy_cipher_record t
        WHERE
        t.book_group_id IN
        <foreach collection="bookGroupIds" open="(" separator="," close=")" index="index" item="item">
            #{item}
        </foreach>
        GROUP BY
        t.book_group_id,
        t.wechat_user_id
        ) tt
        WHERE
        tt.book_group_id IS NOT NULL
        GROUP BY
        tt.book_group_id
    </select>
</mapper>