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

    <resultMap  id="BaseResultMap" type="com.pcloud.book.personalstage.entity.PersonalStage">
        <id property="id" column="id" jdbcType="BIGINT"/>
        <result property="name" column="name" jdbcType="VARCHAR"/>
        <result property="notKeywordFusingCount" column="not_keyword_fusing_count" jdbcType="INTEGER"/>
        <result property="notKeywordFusingTime" column="not_keyword_fusing_time" jdbcType="BIGINT"/>
        <result property="robotClassifyId" column="robot_classify_id" jdbcType="BIGINT"/>
        <result property="paperId" column="paper_id" jdbcType="BIGINT"/>
        <result property="seqNum" column="seq_num" jdbcType="INTEGER"/>
        <result property="showProgress" column="show_progress" jdbcType="INTEGER"/>
        <result property="createTime" column="create_time" jdbcType="TIMESTAMP"/>
        <result property="sendMode" column="send_mode" jdbcType="INTEGER"/>
        <result property="notReplyFusingCount" column="not_reply_fusing_count" jdbcType="INTEGER"/>
        <result property="replaceCodeId" column="replace_code_id" jdbcType="BIGINT"/>
    </resultMap>

    <sql id="Base_Column_List">
        id, name, not_keyword_fusing_count, not_keyword_fusing_time, robot_classify_id, paper_id, seq_num, show_progress, create_time,send_mode,not_reply_fusing_count
        ,replace_code_id
    </sql>

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

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

    <insert id="insert" parameterType="com.pcloud.book.personalstage.entity.PersonalStage" useGeneratedKeys="true" keyProperty="id">
        insert into personal_stage
        <trim prefix="(" suffix=")" suffixOverrides=",">
            name,
            not_keyword_fusing_count,
            not_keyword_fusing_time,
            robot_classify_id,
            paper_id,
            seq_num,
            show_progress,
            create_time,
            send_mode,
            not_reply_fusing_count,
            replace_code_id,
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            #{name,jdbcType=VARCHAR},
            #{notKeywordFusingCount,jdbcType=INTEGER},
            #{notKeywordFusingTime,jdbcType=BIGINT},
            #{robotClassifyId,jdbcType=BIGINT},
            #{paperId},
            #{seqNum,jdbcType=INTEGER},
            #{showProgress,jdbcType=INTEGER},
            NOW(),
            #{sendMode},
            #{notReplyFusingCount},
            #{replaceCodeId}
        </trim>
    </insert>

    <update id="update" parameterType="com.pcloud.book.personalstage.entity.PersonalStage">
        update personal_stage
        <set>
            <if test="name != null">
                name = #{name,jdbcType=VARCHAR},
            </if>
            <if test="notKeywordFusingCount != null">
                not_keyword_fusing_count = #{notKeywordFusingCount,jdbcType=INTEGER},
            </if>
            <if test="notKeywordFusingTime != null">
                not_keyword_fusing_time = #{notKeywordFusingTime,jdbcType=BIGINT},
            </if>
            <if test="robotClassifyId != null">
                robot_classify_id = #{robotClassifyId,jdbcType=BIGINT},
            </if>
            paper_id = #{paperId},
            <if test="seqNum != null">
                seq_num = #{seqNum,jdbcType=INTEGER},
            </if>
            <if test="showProgress != null">
                show_progress = #{showProgress,jdbcType=INTEGER},
            </if>
            <if test="sendMode != null">
                send_mode = #{sendMode,jdbcType=INTEGER},
            </if>
                not_reply_fusing_count = #{notReplyFusingCount,jdbcType=INTEGER},
                replace_code_id = #{replaceCodeId,jdbcType=BIGINT},
        </set>
        where id = #{id,jdbcType=BIGINT}
    </update>

    <update id="updateSeqNum" parameterType="map">
        update personal_stage
        set seq_num=#{seqNum}
        where id=#{id}
    </update>

    <select id="getPersonalStageList" parameterType="map" resultType="com.pcloud.book.personalstage.dto.PersonalStageDTO">
        SELECT
            s.id,
            s.`name`,
            sum(IF(r.relevance_type = 1, 1, 0)) notKeywordReplyCount,
            sum(IF(r.relevance_type = 2, 1, 0)) notKeywordFusingReplyCount,
            s.paper_id paperId,
            s.seq_num seqNum,
            s.create_time createTime
        FROM
            personal_stage s
        LEFT JOIN personal_stage_reply r ON r.personal_stage_id = s.id
        AND r.relevance_type != 3
        WHERE
            s.robot_classify_id = #{robotClassifyId}
        GROUP BY
            s.id
        ORDER BY
            s.seq_num ASC,
            s.id ASC
    </select>

    <select id="getMaxSeqNum" resultType="integer">
        select ifnull(max(seq_num),0) from personal_stage
    </select>

    <select id="getCountByRobotClassifyId" parameterType="long" resultType="integer">
        select count(1) from personal_stage
        where robot_classify_id = #{robotClassifyId}
    </select>

    <select id="getFirstStage" parameterType="long" resultMap="BaseResultMap">
        select <include refid="Base_Column_List"/>
        from personal_stage
        where robot_classify_id = #{robotClassifyId}
        order by seq_num, id
        limit 1
    </select>

    <select id="getPaperIdListByRobotClassify" parameterType="long" resultType="long">
        SELECT DISTINCT paper_id FROM personal_stage
        WHERE
        robot_classify_id = #{robotClassifyId}
        AND paper_id>0
    </select>

</mapper>