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

    <resultMap  id="BaseResultMap" type="com.pcloud.book.personalstage.entity.UserReplaceCode">
        <id property="id" column="id" jdbcType="BIGINT"/>
        <result property="wxId" column="wx_id" jdbcType="VARCHAR"/>
        <result property="robotId" column="robot_id" jdbcType="VARCHAR"/>
        <result property="personalStageId" column="personal_stage_id" jdbcType="BIGINT"/>
        <result property="replaceCodeId" column="replace_code_id" jdbcType="BIGINT"/>
        <result property="paperId" column="paper_id" jdbcType="BIGINT"/>
        <result property="content" column="content" jdbcType="VARCHAR"/>
        <result property="createTime" column="create_time" jdbcType="TIMESTAMP"/>
    </resultMap>

    <sql id="Base_Column_List">
        id, wx_id, robot_id,personal_stage_id,replace_code_id,paper_id,content,create_time
    </sql>

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

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

    <insert id="insert" parameterType="com.pcloud.book.personalstage.entity.UserReplaceCode" useGeneratedKeys="true" keyProperty="id">
        insert into user_replace_code
        <trim prefix="(" suffix=")" suffixOverrides=",">
            wx_id,
            robot_id,
            personal_stage_id,
            replace_code_id,
            paper_id,
            content,
            create_time
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            #{wxId,jdbcType=VARCHAR},
            #{robotId,jdbcType=VARCHAR},
            #{personalStageId,jdbcType=BIGINT},
            #{replaceCodeId,jdbcType=BIGINT},
            #{paperId,jdbcType=BIGINT},
            #{content,jdbcType=VARCHAR},
            now()
        </trim>
    </insert>

    <select id="getLastUserReplace" parameterType="map" resultMap="BaseResultMap">
        select <include refid="Base_Column_List"/>
        FROM
            user_replace_code
        WHERE
            id IN (
            SELECT
                MAX(id)
            FROM
                user_replace_code t
            WHERE
                t.robot_id = #{robotId}
                AND t.wx_id = #{wxId}
                GROUP BY
                t.replace_code_id
                ORDER BY
                create_time DESC
        )
    </select>

    <select id="getUserReplaceRecordByCodeId" parameterType="map" resultType="string">
        SELECT
        content
        FROM
        user_replace_code
        WHERE robot_id = #{robotId}
        AND wx_id = #{wxId}
        AND replace_code_id = #{replaceCodeId}
        ORDER BY
        create_time DESC
    </select>

    <select id="getLastUserReplaceByCodeId" parameterType="map" resultMap="BaseResultMap">
        SELECT
        <include refid="Base_Column_List"/>
        FROM
        user_replace_code
        WHERE robot_id = #{robotId}
        AND wx_id = #{wxId}
        AND replace_code_id = #{replaceCodeId}
        ORDER BY
        create_time DESC
        limit 1
    </select>

    <select id="getLastOneUserReplace" parameterType="map" resultMap="BaseResultMap">
        select <include refid="Base_Column_List"/>
        FROM
        user_replace_code
        WHERE
        robot_id = #{robotId}
        AND wx_id = #{wxId}
        AND replace_code_id=#{replaceCodeId}
        ORDER BY create_time DESC
        LIMIT 1
    </select>

</mapper>