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

    <resultMap  id="BaseResultMap" type="com.pcloud.book.personalstage.entity.PersonalStageUser">
        <id property="id" column="id" jdbcType="BIGINT"/>
        <result property="personalStageId" column="personal_stage_id" jdbcType="BIGINT"/>
        <result property="wxId" column="wx_id" jdbcType="VARCHAR"/>
        <result property="robotId" column="robot_id" jdbcType="VARCHAR"/>
        <result property="robotClassifyId" column="robot_classify_id" jdbcType="BIGINT"/>
        <result property="state" column="state" jdbcType="INTEGER"/>
        <result property="notKeywordSendCount" column="not_keyword_send_count" jdbcType="INTEGER"/>
        <result property="createTime" column="create_time" jdbcType="TIMESTAMP"/>
        <result property="updateTime" column="update_time" jdbcType="TIMESTAMP"/>
        <result property="requireNumber" column="require_number" jdbcType="VARCHAR"/>
        <result property="score" column="score" jdbcType="INTEGER"/>
        <result property="progressStartTime" column="progress_start_time" jdbcType="TIMESTAMP"/>
        <result property="fusingReplyCount" column="fusing_reply_count" jdbcType="INTEGER"/>
    </resultMap>

    <sql id="Base_Column_List">
        id, personal_stage_id, wx_id, robot_id, robot_classify_id, state, not_keyword_send_count, create_time,update_time,require_number,score,progress_start_time,fusing_reply_count
    </sql>

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

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

    <insert id="insert" parameterType="com.pcloud.book.personalstage.entity.PersonalStageUser" useGeneratedKeys="true" keyProperty="id">
        insert into personal_stage_user
        <trim prefix="(" suffix=")" suffixOverrides=",">
            personal_stage_id,
            wx_id,
            robot_id,
            robot_classify_id,
            state,
            not_keyword_send_count,
            create_time,
            update_time
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            #{personalStageId,jdbcType=BIGINT},
            #{wxId,jdbcType=VARCHAR},
            #{robotId,jdbcType=VARCHAR},
            #{robotClassifyId,jdbcType=BIGINT},
            #{state,jdbcType=INTEGER},
            #{notKeywordSendCount,jdbcType=INTEGER},
            NOW(),
            now()
        </trim>
    </insert>

    <update id="update" parameterType="com.pcloud.book.personalstage.entity.PersonalStageUser">
        update personal_stage_user
        <set>
            <if test="personalStageId != null">
                personal_stage_id = #{personalStageId,jdbcType=BIGINT},
            </if>
            <if test="wxId != null">
                wx_id = #{wxId,jdbcType=VARCHAR},
            </if>
            <if test="robotId != null">
                robot_id = #{robotId,jdbcType=VARCHAR},
            </if>
            <if test="robotClassifyId != null">
                robot_classify_id = #{robotClassifyId,jdbcType=BIGINT},
            </if>
            <if test="state != null">
                state = #{state,jdbcType=INTEGER},
            </if>
            <if test="notKeywordSendCount != null">
                not_keyword_send_count = #{notKeywordSendCount,jdbcType=INTEGER},
            </if>
            <if test="progressStartTime != null">
                progress_start_time = #{progressStartTime},
            </if>
            <if test="fusingReplyCount != null">
                fusing_reply_count = #{fusingReplyCount},
            </if>
            update_time=now()
        </set>
        where id = #{id,jdbcType=BIGINT}
    </update>

    <update id="updateRequireNumber" parameterType="map">
        UPDATE personal_stage_user
        SET require_number = #{requireNumber}
        WHERE id = #{personalStageUserId} AND (require_number is null OR require_number='')
    </update>

    <select id="getLast" parameterType="map" resultMap="BaseResultMap">
        select <include refid="Base_Column_List"/>
        from personal_stage_user
        where 1=1
        <if test="wxId!=null">
            and wx_id=#{wxId}
        </if>
        <if test="robotId!=null">
            and robot_id = #{robotId}
        </if>
        <if test="personalStageId!=null">
            and personal_stage_id=#{personalStageId}
        </if>
        order by create_time desc
        LIMIT 0,1
    </select>

    <select id="getNotKeywordSendCount" parameterType="map" resultType="integer">
        select not_keyword_send_count
        from personal_stage_user
        where 1=1
        <if test="wxId!=null">
            and wx_id=#{wxId}
        </if>
        <if test="robotId!=null">
            and robot_id = #{robotId}
        </if>
        <if test="personalStageId!=null">
            and personal_stage_id=#{personalStageId}
        </if>
        order by id desc
        LIMIT 0,1
    </select>

    <update id="updateScore" parameterType="map">
        update personal_stage_user set
        score = score + #{score} where id = #{id}
    </update>

    <update id="updateFusingReplyCount" parameterType="java.lang.Long">
        update personal_stage_user set
        fusing_reply_count = fusing_reply_count + 1 where id = #{id}
    </update>

</mapper>