<?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.BookGroupAgentRecordDaoImpl">
    <resultMap id="BaseResultMap" type="com.pcloud.book.group.entity.BookGroupAgentRecord">
        <id column="id" property="id" jdbcType="BIGINT"/>
        <result column="agent_id" property="agentId" jdbcType="BIGINT"/>
        <result column="agent_name" property="agentName" jdbcType="VARCHAR"/>
        <result column="grade" property="grade" jdbcType="VARCHAR"/>
        <result column="subject" property="subject" jdbcType="VARCHAR"/>
        <result column="target" property="target" jdbcType="VARCHAR"/>
        <result column="alt_id" property="altId" jdbcType="VARCHAR"/>
        <result column="book_group_id" property="bookGroupId" jdbcType="BIGINT"/>
        <result column="adviser_id" property="adviserId" jdbcType="BIGINT"/>
        <result column="create_time" property="createTime" jdbcType="TIMESTAMP"/>
    </resultMap>


    <sql id="Base_Column_List">
        id, agent_id,agent_name,grade,subject,target,alt_id,book_group_id,adviser_id,create_time
    </sql>

    <insert id="insert" parameterType="com.pcloud.book.group.entity.BookGroupAgentRecord" useGeneratedKeys="true"
            keyProperty="id">
        insert into book_group_agent_record
        <trim prefix="(" suffix=")" suffixOverrides=",">
            agent_id,
            agent_name,
            grade,
            subject,
            target,
            alt_id,
            book_group_id,
            adviser_id,
            create_time
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            #{agentId,jdbcType=BIGINT},
            #{agentName,jdbcType=VARCHAR},
            #{grade,jdbcType=VARCHAR},
            #{subject,jdbcType=VARCHAR},
            #{target,jdbcType=VARCHAR},
            #{altId,jdbcType=VARCHAR},
            #{bookGroupId,jdbcType=VARCHAR},
            #{adviserId,jdbcType=BIGINT},
            NOW()
        </trim>
    </insert>

    <!--根据小号id和社群码id查询-->
    <select id="getByAltIdAndBookGroupId" parameterType="map" resultMap="BaseResultMap">
        select <include refid="Base_Column_List"/>
        from book_group_agent_record where
        alt_id=#{altId}
        and book_group_id=#{bookGroupId}
        limit 1
    </select>

    <!--根据社群码id集合查询小号-->
    <select id="getAltIdByBookGroupIds" parameterType="map" resultMap="BaseResultMap">
        select book_group_id bookGroupId,alt_id altId
        from book_group_agent_record
        where book_group_id in
        <foreach collection="bookGroupIds" index="index" item="item" open="(" separator="," close=")">
            #{item}
        </foreach>
        group by book_group_id alt_id
    </select>
</mapper>