<?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.personalstage.dao.impl.PersonalStageJumpLinkupDaoImpl">

    <resultMap  id="BaseResultMap" type="com.pcloud.book.personalstage.entity.PersonalStageJumpLinkup">
        <id property="id" column="id" jdbcType="BIGINT"/>
        <result property="personalStageJumpId" column="personal_stage_jump_id" jdbcType="BIGINT"/>
        <result property="linkupContent" column="linkup_content" jdbcType="VARCHAR"/>
        <result property="toStageStartTime" column="to_stage_start_time" jdbcType="INTEGER"/>
        <result property="replyType" column="reply_type" jdbcType="INTEGER"/>
        <result property="picUrl" column="pic_url" jdbcType="VARCHAR"/>
        <result property="resourceId" column="resource_id" jdbcType="BIGINT"/>
        <result column="personal_applets_id" property="personalAppletsId" jdbcType="BIGINT"/>
    </resultMap>

    <sql id="Base_Column_List">
        id, personal_stage_jump_id, linkup_content, to_stage_start_time, reply_type, pic_url, resource_id,personal_applets_id
    </sql>

    <select id="getById" resultMap="BaseResultMap" parameterType="java.lang.Long">
        select
        <include refid="Base_Column_List"/>
        from personal_stage_jump_linkup
        where id = #{id,jdbcType=BIGINT}
    </select>

    <select id="getByJumpId" resultMap="BaseResultMap" parameterType="java.lang.Long">
        select
        <include refid="Base_Column_List"/>
        from personal_stage_jump_linkup
        where personal_stage_jump_id = #{jumpId,jdbcType=BIGINT}
        order by to_stage_start_time
    </select>

    <select id="listBy" resultMap="BaseResultMap" parameterType="map">
        select
        <include refid="Base_Column_List"/>
        from personal_stage_jump_linkup
        where personal_stage_jump_id IN
        <foreach collection="jumpIds" item="jumpId" separator="," open="(" close=")">
            ${jumpId}
        </foreach>
    </select>

    <delete id="deleteById" parameterType="long">
        delete from personal_stage_jump_linkup
        where id = #{id,jdbcType=BIGINT}
    </delete>

    <delete id="deleteByJumpId" parameterType="long">
        delete from personal_stage_jump_linkup
        where personal_stage_jump_id = #{personalStageJumpId,jdbcType=BIGINT}
    </delete>

    <insert id="insert" parameterType="com.pcloud.book.personalstage.entity.PersonalStageJumpLinkup" useGeneratedKeys="true" keyProperty="id">
        insert into personal_stage_jump_linkup
        <trim prefix="(" suffix=")" suffixOverrides=",">
            personal_stage_jump_id,
            linkup_content,
            to_stage_start_time,
            reply_type,
            pic_url,
            resource_id,
            personal_applets_id
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            #{personalStageJumpId,jdbcType=BIGINT},
            #{linkupContent,jdbcType=VARCHAR},
            #{toStageStartTime,jdbcType=INTEGER},
            #{replyType,jdbcType=INTEGER},
            #{picUrl,jdbcType=VARCHAR},
            #{resourceId,jdbcType=BIGINT},
            #{personalAppletsId,jdbcType=BIGINT}
        </trim>
    </insert>

    <insert id="batchInsert" parameterType="list" useGeneratedKeys="true" keyProperty="id">
        insert into personal_stage_jump_linkup
        <trim prefix="(" suffix=")" suffixOverrides=",">
            personal_stage_jump_id,
            linkup_content,
            to_stage_start_time,
            reply_type,
            pic_url,
            resource_id,
            personal_applets_id
        </trim>
        values
        <foreach collection="list" item="item" separator=",">
            <trim prefix="(" suffix=")" suffixOverrides=",">
                #{item.personalStageJumpId,jdbcType=BIGINT},
                #{item.linkupContent,jdbcType=VARCHAR},
                #{item.toStageStartTime,jdbcType=INTEGER},
                #{item.replyType,jdbcType=INTEGER},
                #{item.picUrl,jdbcType=VARCHAR},
                #{item.resourceId,jdbcType=BIGINT},
                #{item.personalAppletsId,jdbcType=BIGINT}
            </trim>
        </foreach>
    </insert>
    
    <select id="mapStageJumpLinkupCountByRobotClassify" parameterType="long" resultType="com.pcloud.book.personalstage.dto.PersonalStageDTO">
        SELECT
        s.id id,
        COUNT(l.id) jumpLinkupCount
        FROM
        personal_stage_jump_linkup l LEFT JOIN personal_stage_jump j ON l.personal_stage_jump_id=j.id
        LEFT JOIN personal_stage s ON s.id=j.personal_stage_id
        WHERE s.robot_classify_id = #{robotClassifyId}
        GROUP BY s.id
    </select>

</mapper>