You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 

194 lines
8.8 KiB

<?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.chenhai.system.mapper.SysAnswersMapper">
<resultMap type="SysAnswers" id="SysAnswersResult">
<result property="id" column="id" />
<result property="questionId" column="question_id" />
<result property="userId" column="user_id" />
<result property="username" column="username" />
<result property="nickName" column="nick_name" />
<result property="avatar" column="avatar" />
<result property="content" column="content" />
<result property="createdAt" column="created_at" />
<result property="updatedAt" column="updated_at" />
<result property="images" column="images" />
<result property="parentId" column="parent_id" />
<result property="level" column="level" />
<result property="path" column="path" />
<result property="replyCount" column="reply_count" />
<result property="likeCount" column="like_count" />
<!-- &lt;!&ndash; 添加新字段 &ndash;&gt;-->
<!-- <result property="latestReplyContent" column="latest_reply_content" />-->
<!-- <result property="latestReplyNickName" column="latest_reply_nick_name" />-->
<!-- <result property="latestReplyAvatar" column="latest_reply_avatar" />-->
<!-- <result property="latestReplyTime" column="latest_reply_time" />-->
</resultMap>
<sql id="selectSysAnswersVo">
select
a.id,
a.question_id,
a.user_id,
a.username,
u.nick_name,
u.avatar,
a.content,
a.created_at,
a.updated_at,
a.images,
a.parent_id,
a.level,
a.path,
a.reply_count,
a.like_count
from sys_answers a
left join sys_user u on a.user_id = u.user_id
</sql>
<select id="selectSysAnswersList" parameterType="SysAnswers" resultMap="SysAnswersResult">
<include refid="selectSysAnswersVo"/>
<where>
<if test="questionId != null "> and a.question_id = #{questionId}</if>
<if test="userId != null and userId != ''"> and a.user_id = #{userId}</if>
<if test="username != null and username != ''"> and a.username like concat('%', #{username}, '%')</if>
<if test="nickName != null and nickName != ''"> and u.nick_name like concat('%', #{nickName}, '%')</if>
<if test="content != null and content != ''"> and a.content like concat('%', #{content}, '%')</if>
<if test="parentId != null "> and a.parent_id = #{parentId}</if>
<if test="level != null "> and a.level = #{level}</if>
<if test="createdAt != null "> and date(a.created_at) = date(#{createdAt})</if>
<if test="params.beginTime != null and params.beginTime != ''">
and date_format(a.created_at,'%y%m%d') &gt;= date_format(#{params.beginTime},'%y%m%d')
</if>
<if test="params.endTime != null and params.endTime != ''">
and date_format(a.created_at,'%y%m%d') &lt;= date_format(#{params.endTime},'%y%m%d')
</if>
<if test="images != null and images != ''"> and a.images like concat('%', #{images}, '%')</if>
</where>
order by a.created_at desc
</select>
<select id="selectSysAnswersById" parameterType="Long" resultMap="SysAnswersResult">
<include refid="selectSysAnswersVo"/>
where a.id = #{id}
</select>
<!-- 查询顶级答复列表(parent_id = 0) -->
<select id="selectRootAnswers" parameterType="Long" resultMap="SysAnswersResult">
<include refid="selectSysAnswersVo"/>
where a.question_id = #{questionId} and a.parent_id = 0
order by a.created_at desc
</select>
<!-- 查询子回复列表 -->
<select id="selectChildrenAnswers" parameterType="Long" resultMap="SysAnswersResult">
<include refid="selectSysAnswersVo"/>
where a.parent_id = #{parentId}
order by a.created_at asc
</select>
<!-- 查询答复的完整树结构 -->
<select id="selectAnswerTree" parameterType="Long" resultMap="SysAnswersResult">
<include refid="selectSysAnswersVo"/>
where a.question_id = #{questionId}
order by a.path, a.created_at asc
</select>
<insert id="insertSysAnswers" parameterType="SysAnswers" useGeneratedKeys="true" keyProperty="id">
insert into sys_answers
<trim prefix="(" suffix=")" suffixOverrides=",">
<if test="questionId != null">question_id,</if>
<if test="userId != null and userId != ''">user_id,</if>
<if test="username != null and username != ''">username,</if>
<if test="content != null and content != ''">content,</if>
<if test="images != null and images != ''">images,</if>
<if test="parentId != null">parent_id,</if>
<if test="level != null">level,</if>
<if test="path != null and path != ''">path,</if>
<if test="replyCount != null">reply_count,</if>
<if test="likeCount != null">like_count,</if>
created_at,
</trim>
<trim prefix="values (" suffix=")" suffixOverrides=",">
<if test="questionId != null">#{questionId},</if>
<if test="userId != null and userId != ''">#{userId},</if>
<if test="username != null and username != ''">#{username},</if>
<if test="content != null and content != ''">#{content},</if>
<if test="images != null and images != ''">#{images},</if>
<if test="parentId != null">#{parentId},</if>
<if test="level != null">#{level},</if>
<if test="path != null and path != ''">#{path},</if>
<if test="replyCount != null">#{replyCount},</if>
<if test="likeCount != null">#{likeCount},</if>
sysdate(),
</trim>
</insert>
<update id="updateSysAnswers" parameterType="SysAnswers">
update sys_answers
<trim prefix="SET" suffixOverrides=",">
<if test="questionId != null">question_id = #{questionId},</if>
<if test="userId != null and userId != ''">user_id = #{userId},</if>
<if test="username != null and username != ''">username = #{username},</if>
<!-- <if test="nickName != null and nickName != ''">nick_name = #{nickName},</if>-->
<!-- <if test="avatar != null and avatar != ''">avatar = #{avatar},</if>-->
<if test="content != null and content != ''">content = #{content},</if>
<if test="images != null and images != ''">images = #{images},</if>
<if test="parentId != null">parent_id = #{parentId},</if>
<if test="level != null">level = #{level},</if>
<if test="path != null and path != ''">path = #{path},</if>
<if test="replyCount != null">reply_count = #{replyCount},</if>
<if test="likeCount != null">like_count = #{likeCount},</if>
updated_at = sysdate(),
</trim>
where id = #{id}
</update>
<!-- 更新答复的回复数量 -->
<update id="updateReplyCount" parameterType="Long">
update sys_answers
set reply_count = (
select cnt from (
select count(*) as cnt
from sys_answers
where parent_id = #{id}
) as temp
),
updated_at = sysdate()
where id = #{id}
</update>
<!-- 更新点赞数量 -->
<update id="updateLikeCount">
update sys_answers
set like_count = like_count + #{increment},
updated_at = sysdate()
where id = #{id}
</update>
<delete id="deleteSysAnswersById" parameterType="Long">
delete from sys_answers where id = #{id}
</delete>
<delete id="deleteSysAnswersByIds" parameterType="String">
delete from sys_answers where id in
<foreach item="id" collection="array" open="(" separator="," close=")">
#{id}
</foreach>
</delete>
<delete id="deleteAnswersByQuestionId" parameterType="Long">
delete from sys_answers where question_id = #{questionId}
</delete>
<select id="countAnswersByQuestionId" parameterType="Long" resultType="Long">
select count(*) from sys_answers where question_id = #{questionId}
</select>
<!-- 查询某个答复的所有后代ID(用于级联删除) -->
<select id="selectDescendantIds" parameterType="Long" resultType="Long">
select id from sys_answers where path like concat('%/', #{id}, '/%')
</select>
</mapper>