<?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.PersonalStageJumpDaoImpl">

    <resultMap  id="BaseResultMap" type="com.pcloud.book.personalstage.entity.PersonalStageJump">
        <id property="id" column="id" jdbcType="BIGINT"/>
        <result property="personalStageId" column="personal_stage_id" jdbcType="BIGINT"/>
        <result property="jumpType" column="jump_type" jdbcType="INTEGER"/>
        <result property="afterPersonalStageId" column="after_personal_stage_id" jdbcType="BIGINT"/>
        <result property="openEmail" column="open_email" jdbcType="BOOLEAN"/>
        <result property="transferMessage" column="transfer_message" jdbcType="VARCHAR"/>
        <result property="transferMoney" column="transfer_money" jdbcType="DOUBLE"/>
        <result property="createTime" column="create_time" jdbcType="TIMESTAMP"/>
        <result property="toStageStartTime" column="to_stage_start_time" jdbcType="INTEGER"/>
    </resultMap>

    <sql id="Base_Column_List">
       id, personal_stage_id, jump_type, after_personal_stage_id, open_email, transfer_message, transfer_money, create_time, to_stage_start_time
    </sql>

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

    <select id="getDtoById" resultType="com.pcloud.book.personalstage.dto.PersonalStageJumpDto" parameterType="java.lang.Long">
        SELECT
            a.id,
            a.personal_stage_id personalStageId,
            a.jump_type jumpType,
            a.to_stage_start_time toStageStartTime,
            a.after_personal_stage_id afterPersonalStageId,
            a.open_email openEmail,
            a.transfer_message transferMessage,
            a.transfer_money transferMoney,
            b.name personalStageName
        FROM personal_stage_jump a, personal_stage b
        WHERE a.after_personal_stage_id = b.id AND a.id = #{jumpId,jdbcType=BIGINT}
    </select>

    <select id="getJumpList" resultType="com.pcloud.book.personalstage.dto.PersonalStageJumpDto" parameterType="long">
        SELECT
            a.id,
            a.personal_stage_id personalStageId,
            a.jump_type jumpType,
            a.to_stage_start_time toStageStartTime,
            a.after_personal_stage_id afterPersonalStageId,
            a.open_email openEmail,
            a.transfer_message transferMessage,
            a.transfer_money transferMoney,
            b.name personalStageName
        FROM personal_stage_jump a LEFT JOIN personal_stage b ON a.after_personal_stage_id = b.id
        WHERE a.personal_stage_id = #{personalStageId,jdbcType=BIGINT}
    </select>

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

    <insert id="insert" parameterType="com.pcloud.book.personalstage.entity.PersonalStageJump" useGeneratedKeys="true" keyProperty="id">
        insert into personal_stage_jump
        <trim prefix="(" suffix=")" suffixOverrides=",">
            personal_stage_id,
            jump_type,
            after_personal_stage_id,
            open_email,
            transfer_message,
            transfer_money,
            create_time,
            to_stage_start_time
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            #{personalStageId,jdbcType=BIGINT},
            #{jumpType,jdbcType=INTEGER},
            #{afterPersonalStageId,jdbcType=BIGINT},
            #{openEmail,jdbcType=BOOLEAN},
            #{transferMessage,jdbcType=VARCHAR},
            #{transferMoney,jdbcType=DOUBLE},
            NOW(),
            #{toStageStartTime,jdbcType=INTEGER}
        </trim>
    </insert>

    <select id="getCountByPersonalStageId" parameterType="long" resultType="integer">
        select count(1) from personal_stage_jump
        where personal_stage_id=#{personalStageId}
    </select>

    <update id="update" parameterType="com.pcloud.book.personalstage.entity.PersonalStageJump">
        UPDATE personal_stage_jump
        <set>
            <if test="personalStageId != null">
                personal_stage_id = #{personalStageId},
            </if>
            <if test="jumpType != null">
                jump_type = #{jumpType},
            </if>
            <if test="afterPersonalStageId != null">
                after_personal_stage_id = #{afterPersonalStageId},
            </if>
            <if test="openEmail != null" >
                open_email = #{openEmail,jdbcType=BOOLEAN},
            </if>
            <if test="transferMessage != null">
                transfer_message = #{transferMessage},
            </if>
            <if test="transferMoney != null">
                transfer_money = #{transferMoney},
            </if>
            <if test="toStageStartTime != null">
                to_stage_start_time = #{toStageStartTime},
            </if>
        </set>
        where id = #{id,jdbcType=INTEGER}
    </update>

    <select id="getByJumpType" parameterType="map" resultMap="BaseResultMap">
        select <include refid="Base_Column_List"/>
        from personal_stage_jump
        where personal_stage_id = #{personalStageId} and jump_type = #{jumpType}
    </select>
</mapper>