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.

193 lines
8.8 KiB

  1. <?xml version="1.0" encoding="UTF-8" ?>
  2. <!DOCTYPE mapper
  3. PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
  4. "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
  5. <mapper namespace="com.chenhai.system.mapper.SysAnswersMapper">
  6. <resultMap type="SysAnswers" id="SysAnswersResult">
  7. <result property="id" column="id" />
  8. <result property="questionId" column="question_id" />
  9. <result property="userId" column="user_id" />
  10. <result property="username" column="username" />
  11. <result property="nickName" column="nick_name" />
  12. <result property="avatar" column="avatar" />
  13. <result property="content" column="content" />
  14. <result property="createdAt" column="created_at" />
  15. <result property="updatedAt" column="updated_at" />
  16. <result property="images" column="images" />
  17. <result property="parentId" column="parent_id" />
  18. <result property="level" column="level" />
  19. <result property="path" column="path" />
  20. <result property="replyCount" column="reply_count" />
  21. <result property="likeCount" column="like_count" />
  22. <!-- &lt;!&ndash; 添加新字段 &ndash;&gt;-->
  23. <!-- <result property="latestReplyContent" column="latest_reply_content" />-->
  24. <!-- <result property="latestReplyNickName" column="latest_reply_nick_name" />-->
  25. <!-- <result property="latestReplyAvatar" column="latest_reply_avatar" />-->
  26. <!-- <result property="latestReplyTime" column="latest_reply_time" />-->
  27. </resultMap>
  28. <sql id="selectSysAnswersVo">
  29. select
  30. a.id,
  31. a.question_id,
  32. a.user_id,
  33. a.username,
  34. u.nick_name,
  35. u.avatar,
  36. a.content,
  37. a.created_at,
  38. a.updated_at,
  39. a.images,
  40. a.parent_id,
  41. a.level,
  42. a.path,
  43. a.reply_count,
  44. a.like_count
  45. from sys_answers a
  46. left join sys_user u on a.user_id = u.user_id
  47. </sql>
  48. <select id="selectSysAnswersList" parameterType="SysAnswers" resultMap="SysAnswersResult">
  49. <include refid="selectSysAnswersVo"/>
  50. <where>
  51. <if test="questionId != null "> and a.question_id = #{questionId}</if>
  52. <if test="userId != null and userId != ''"> and a.user_id = #{userId}</if>
  53. <if test="username != null and username != ''"> and a.username like concat('%', #{username}, '%')</if>
  54. <if test="nickName != null and nickName != ''"> and u.nick_name like concat('%', #{nickName}, '%')</if>
  55. <if test="content != null and content != ''"> and a.content like concat('%', #{content}, '%')</if>
  56. <if test="parentId != null "> and a.parent_id = #{parentId}</if>
  57. <if test="level != null "> and a.level = #{level}</if>
  58. <if test="createdAt != null "> and date(a.created_at) = date(#{createdAt})</if>
  59. <if test="params.beginTime != null and params.beginTime != ''">
  60. and date_format(a.created_at,'%y%m%d') &gt;= date_format(#{params.beginTime},'%y%m%d')
  61. </if>
  62. <if test="params.endTime != null and params.endTime != ''">
  63. and date_format(a.created_at,'%y%m%d') &lt;= date_format(#{params.endTime},'%y%m%d')
  64. </if>
  65. <if test="images != null and images != ''"> and a.images like concat('%', #{images}, '%')</if>
  66. </where>
  67. order by a.created_at desc
  68. </select>
  69. <select id="selectSysAnswersById" parameterType="Long" resultMap="SysAnswersResult">
  70. <include refid="selectSysAnswersVo"/>
  71. where a.id = #{id}
  72. </select>
  73. <!-- 查询顶级答复列表(parent_id = 0) -->
  74. <select id="selectRootAnswers" parameterType="Long" resultMap="SysAnswersResult">
  75. <include refid="selectSysAnswersVo"/>
  76. where a.question_id = #{questionId} and a.parent_id = 0
  77. order by a.created_at desc
  78. </select>
  79. <!-- 查询子回复列表 -->
  80. <select id="selectChildrenAnswers" parameterType="Long" resultMap="SysAnswersResult">
  81. <include refid="selectSysAnswersVo"/>
  82. where a.parent_id = #{parentId}
  83. order by a.created_at asc
  84. </select>
  85. <!-- 查询答复的完整树结构 -->
  86. <select id="selectAnswerTree" parameterType="Long" resultMap="SysAnswersResult">
  87. <include refid="selectSysAnswersVo"/>
  88. where a.question_id = #{questionId}
  89. order by a.path, a.created_at asc
  90. </select>
  91. <insert id="insertSysAnswers" parameterType="SysAnswers" useGeneratedKeys="true" keyProperty="id">
  92. insert into sys_answers
  93. <trim prefix="(" suffix=")" suffixOverrides=",">
  94. <if test="questionId != null">question_id,</if>
  95. <if test="userId != null and userId != ''">user_id,</if>
  96. <if test="username != null and username != ''">username,</if>
  97. <if test="content != null and content != ''">content,</if>
  98. <if test="images != null and images != ''">images,</if>
  99. <if test="parentId != null">parent_id,</if>
  100. <if test="level != null">level,</if>
  101. <if test="path != null and path != ''">path,</if>
  102. <if test="replyCount != null">reply_count,</if>
  103. <if test="likeCount != null">like_count,</if>
  104. created_at,
  105. </trim>
  106. <trim prefix="values (" suffix=")" suffixOverrides=",">
  107. <if test="questionId != null">#{questionId},</if>
  108. <if test="userId != null and userId != ''">#{userId},</if>
  109. <if test="username != null and username != ''">#{username},</if>
  110. <if test="content != null and content != ''">#{content},</if>
  111. <if test="images != null and images != ''">#{images},</if>
  112. <if test="parentId != null">#{parentId},</if>
  113. <if test="level != null">#{level},</if>
  114. <if test="path != null and path != ''">#{path},</if>
  115. <if test="replyCount != null">#{replyCount},</if>
  116. <if test="likeCount != null">#{likeCount},</if>
  117. sysdate(),
  118. </trim>
  119. </insert>
  120. <update id="updateSysAnswers" parameterType="SysAnswers">
  121. update sys_answers
  122. <trim prefix="SET" suffixOverrides=",">
  123. <if test="questionId != null">question_id = #{questionId},</if>
  124. <if test="userId != null and userId != ''">user_id = #{userId},</if>
  125. <if test="username != null and username != ''">username = #{username},</if>
  126. <!-- <if test="nickName != null and nickName != ''">nick_name = #{nickName},</if>-->
  127. <!-- <if test="avatar != null and avatar != ''">avatar = #{avatar},</if>-->
  128. <if test="content != null and content != ''">content = #{content},</if>
  129. <if test="images != null and images != ''">images = #{images},</if>
  130. <if test="parentId != null">parent_id = #{parentId},</if>
  131. <if test="level != null">level = #{level},</if>
  132. <if test="path != null and path != ''">path = #{path},</if>
  133. <if test="replyCount != null">reply_count = #{replyCount},</if>
  134. <if test="likeCount != null">like_count = #{likeCount},</if>
  135. updated_at = sysdate(),
  136. </trim>
  137. where id = #{id}
  138. </update>
  139. <!-- 更新答复的回复数量 -->
  140. <update id="updateReplyCount" parameterType="Long">
  141. update sys_answers
  142. set reply_count = (
  143. select cnt from (
  144. select count(*) as cnt
  145. from sys_answers
  146. where parent_id = #{id}
  147. ) as temp
  148. ),
  149. updated_at = sysdate()
  150. where id = #{id}
  151. </update>
  152. <!-- 更新点赞数量 -->
  153. <update id="updateLikeCount">
  154. update sys_answers
  155. set like_count = like_count + #{increment},
  156. updated_at = sysdate()
  157. where id = #{id}
  158. </update>
  159. <delete id="deleteSysAnswersById" parameterType="Long">
  160. delete from sys_answers where id = #{id}
  161. </delete>
  162. <delete id="deleteSysAnswersByIds" parameterType="String">
  163. delete from sys_answers where id in
  164. <foreach item="id" collection="array" open="(" separator="," close=")">
  165. #{id}
  166. </foreach>
  167. </delete>
  168. <delete id="deleteAnswersByQuestionId" parameterType="Long">
  169. delete from sys_answers where question_id = #{questionId}
  170. </delete>
  171. <select id="countAnswersByQuestionId" parameterType="Long" resultType="Long">
  172. select count(*) from sys_answers where question_id = #{questionId}
  173. </select>
  174. <!-- 查询某个答复的所有后代ID(用于级联删除) -->
  175. <select id="selectDescendantIds" parameterType="Long" resultType="Long">
  176. select id from sys_answers where path like concat('%/', #{id}, '/%')
  177. </select>
  178. </mapper>