<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">

<mapper namespace="com.diaoyun.zion.chinafrica.dao.TbCfReplyDao">

    <resultMap type="com.diaoyun.zion.chinafrica.entity.TbCfReplyEntity" id="tbCfReplyMap">
        <result property="id" column="id"/>
        <result property="replyType" column="reply_type"/>
        <result property="commentId" column="comment_id"/>
        <result property="userId" column="user_id"/>
        <result property="toUserId" column="to_user_id"/>
        <result property="replyMessage" column="reply_message"/>
        <result property="delFlag" column="del_flag"/>
        <result property="createTime" column="create_time"/>
        <result property="updateTime" column="update_time"/>
    </resultMap>

    <select id="queryObject" resultType="com.diaoyun.zion.chinafrica.entity.TbCfReplyEntity">
		select
			`id`,
			`reply_type`,
			`comment_id`,
			`user_id`,
			`to_user_id`,
			`reply_message`,
			`del_flag`,
			`create_time`,
			`update_time`
		from tb_cf_reply
		where id = #{id}
	</select>

    <select id="queryList" resultType="com.diaoyun.zion.chinafrica.entity.TbCfReplyEntity">
        select
        `id`,
        `reply_type`,
        `comment_id`,
        `user_id`,
        `to_user_id`,
        `reply_message`,
        `del_flag`,
        `create_time`,
        `update_time`
        from tb_cf_reply
        WHERE 1=1
        <if test="name != null and name.trim() != ''">
            AND name LIKE concat('%',#{name},'%')
        </if>
        <choose>
            <when test="sidx != null and sidx.trim() != ''">
                order by ${sidx} ${order}
            </when>
            <otherwise>
                order by id desc
            </otherwise>
        </choose>
        <if test="offset != null and limit != null">
            limit #{offset}, #{limit}
        </if>
    </select>
    <select id="queryReplys" resultType="com.diaoyun.zion.chinafrica.entity.TbCfReplyEntityExtends">
        SELECT
        u.nick user ,
        c.id commentId,
        i.touser touser,
        r.*
        FROM
        tb_cf_reply r
        LEFT JOIN tb_cf_item_comment c ON r.comment_id = c.id
        LEFT JOIN tb_cf_user_info u ON r.user_id = u.user_id
        LEFT JOIN ( SELECT u.user_id, u.nick touser FROM tb_cf_user_info u ) i ON r.to_user_id = i.user_id
        WHERE
        r.reply_type = 0
        AND r.del_flag = 0
        AND c.id = #{id}
        ORDER BY
	    r.create_time DESC
    </select>
    <select id="queryTotal" resultType="int">
        select count(*) from tb_cf_reply
        WHERE 1=1
        <if test="name != null and name.trim() != ''">
            AND name LIKE concat('%',#{name},'%')
        </if>
    </select>

    <insert id="save" parameterType="com.diaoyun.zion.chinafrica.entity.TbCfReplyEntity">
		insert into tb_cf_reply(
			`id`,
			`reply_type`,
			`comment_id`,
			`user_id`,
			`to_user_id`,
			`reply_message`,
			`del_flag`,
			`create_time`,
			`update_time`)
		values(
			#{id},
			#{replyType},
			#{commentId},
			#{userId},
			#{toUserId},
			#{replyMessage},
			#{delFlag},
			#{createTime},
			#{updateTime})
	</insert>

    <update id="update" parameterType="com.diaoyun.zion.chinafrica.entity.TbCfReplyEntity">
        update tb_cf_reply
        <set>
            <if test="replyType != null">`reply_type` = #{replyType},</if>
            <if test="commentId != null">`comment_id` = #{commentId},</if>
            <if test="userId != null">`user_id` = #{userId},</if>
            <if test="toUserId != null">`to_user_id` = #{toUserId},</if>
            <if test="replyMessage != null">`reply_message` = #{replyMessage},</if>
            <if test="delFlag != null">`del_flag` = #{delFlag},</if>
            <if test="createTime != null">`create_time` = #{createTime},</if>
            <if test="updateTime != null">`update_time` = #{updateTime}</if>
        </set>
        where id = #{id}
    </update>

    <delete id="delete">
		delete from tb_cf_reply where id = #{value}
	</delete>

    <delete id="deleteBatch">
        delete from tb_cf_reply where id in
        <foreach item="id" collection="array" open="(" separator="," close=")">
            #{id}
        </foreach>
    </delete>

</mapper>