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.

280 lines
14 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.vet.mapper.VetExperienceArticleMapper">
  6. <resultMap type="VetExperienceArticle" id="VetExperienceArticleResult">
  7. <result property="id" column="id"/>
  8. <result property="title" column="title"/>
  9. <result property="content" column="content"/>
  10. <result property="summary" column="summary"/>
  11. <result property="coverImage" column="cover_image"/>
  12. <result property="images" column="images"/>
  13. <result property="userId" column="user_id"/>
  14. <result property="vetName" column="vet_name"/>
  15. <result property="vetAvatar" column="vet_avatar"/>
  16. <result property="vetTitle" column="vet_title"/>
  17. <result property="categoryId" column="category_id"/>
  18. <result property="categoryName" column="category_name"/>
  19. <result property="tags" column="tags"/>
  20. <result property="viewCount" column="view_count"/>
  21. <result property="likeCount" column="like_count"/>
  22. <result property="collectCount" column="collect_count"/>
  23. <result property="isTop" column="is_top"/>
  24. <result property="isFeatured" column="is_featured"/>
  25. <result property="status" column="status"/>
  26. <result property="isSensitive" column="is_sensitive"/>
  27. <result property="sensitiveWords" column="sensitive_words"/>
  28. <result property="publishTime" column="publish_time"/>
  29. <result property="createTime" column="create_time"/>
  30. <result property="updateTime" column="update_time"/>
  31. <result property="userRole" column="user_role"/>
  32. <result property="userType" column="user_type"/>
  33. </resultMap>
  34. <!-- 修改基础查询语句,始终从用户表获取用户信息 -->
  35. <sql id="selectVetExperienceArticleVo">
  36. select
  37. a.id,
  38. a.title,
  39. a.content, <!-- 富文本内容 -->
  40. a.summary,
  41. a.cover_image,
  42. a.images,
  43. a.user_id,
  44. <!-- 始终从用户表获取用户信息 -->
  45. COALESCE(u.nick_name, u.user_name) as vet_name, <!-- 用户姓名 -->
  46. u.avatar as vet_avatar, <!-- 用户头像 -->
  47. a.vet_title,
  48. a.category_id,
  49. a.category_name,
  50. a.tags,
  51. a.view_count,
  52. a.like_count,
  53. a.collect_count,
  54. a.is_top,
  55. a.is_featured,
  56. a.status,
  57. a.is_sensitive,
  58. a.sensitive_words,
  59. a.publish_time,
  60. a.create_time,
  61. a.update_time,
  62. a.user_role,
  63. a.user_type
  64. from vet_experience_article a
  65. left join sys_user u on a.user_id = u.user_id
  66. </sql>
  67. <!-- 查询兽医经验文章 -->
  68. <select id="selectVetExperienceArticleById" parameterType="Long" resultMap="VetExperienceArticleResult">
  69. <include refid="selectVetExperienceArticleVo"/>
  70. where a.id = #{id} <!-- 添加表别名 a. -->
  71. </select>
  72. <!-- 查询兽医经验文章列表(已包含关联用户表) -->
  73. <select id="selectVetExperienceArticleList" parameterType="VetExperienceArticle" resultMap="VetExperienceArticleResult">
  74. <include refid="selectVetExperienceArticleVo"/>
  75. <where>
  76. <if test="title != null and title != ''"> and a.title like concat('%', #{title}, '%')</if>
  77. <if test="userId != null"> and a.user_id = #{userId}</if>
  78. <if test="vetName != null and vetName != ''"> and (a.vet_name like concat('%', #{vetName}, '%') or u.nick_name like concat('%', #{vetName}, '%'))</if>
  79. <if test="categoryId != null"> and a.category_id = #{categoryId}</if>
  80. <if test="categoryName != null and categoryName != ''"> and a.category_name like concat('%', #{categoryName}, '%')</if>
  81. <if test="status != null and status != ''"> and a.status = #{status}</if>
  82. <if test="isTop != null and isTop != ''"> and a.is_top = #{isTop}</if>
  83. <if test="isFeatured != null and isFeatured != ''"> and a.is_featured = #{isFeatured}</if>
  84. <if test="isSensitive != null and isSensitive != ''"> and a.is_sensitive = #{isSensitive}</if>
  85. <if test="tags != null and tags != ''">
  86. and (
  87. a.tags = #{tags}
  88. or a.tags like concat(#{tags}, ',%')
  89. or a.tags like concat('%,', #{tags}, ',%')
  90. or a.tags like concat('%,', #{tags})
  91. )
  92. </if>
  93. <if test="searchKey != null and searchKey != ''">
  94. <bind name="searchKeyLike" value="'%' + searchKey + '%'" />
  95. and (
  96. a.title like #{searchKeyLike}
  97. or a.content like #{searchKeyLike}
  98. or a.summary like #{searchKeyLike}
  99. or a.tags like #{searchKeyLike}
  100. or a.category_name like #{searchKeyLike}
  101. or a.vet_name like #{searchKeyLike}
  102. or u.nick_name like #{searchKeyLike}
  103. )
  104. </if>
  105. <if test="viewCount != null and viewCount != ''"> and a.view_count = #{viewCount}</if>
  106. </where>
  107. order by a.publish_time desc
  108. </select>
  109. <!-- 根据条件查询文章列表(论坛专用) -->
  110. <select id="selectArticlesByCondition" parameterType="map" resultMap="VetExperienceArticleResult">
  111. <include refid="selectVetExperienceArticleVo"/>
  112. <where>
  113. a.status = #{status}
  114. <if test="keyword != null and keyword != ''">
  115. and (
  116. a.title like concat('%', #{keyword}, '%')
  117. or a.content like concat('%', #{keyword}, '%')
  118. or a.summary like concat('%', #{keyword}, '%')
  119. or a.tags like concat('%', #{keyword}, '%')
  120. )
  121. </if>
  122. <if test="userId != null"> and a.user_id = #{userId}</if>
  123. <if test="categoryId != null"> and a.category_id = #{categoryId}</if>
  124. <if test="excludeId != null"> and a.id != #{excludeId}</if>
  125. </where>
  126. <!-- ORDER BY必须在WHERE子句之后,LIMIT之前 -->
  127. <choose>
  128. <when test="orderBy != null and orderType != null">
  129. order by a.${orderBy} ${orderType}
  130. </when>
  131. <otherwise>
  132. order by a.publish_time desc
  133. </otherwise>
  134. </choose>
  135. <!-- LIMIT必须在ORDER BY之后 -->
  136. <!-- <if test="limit != null and limit > 0">-->
  137. <!-- limit #{limit}-->
  138. <!-- </if>-->
  139. </select>
  140. <!-- 搜索文章 -->
  141. <select id="searchArticles" resultMap="VetExperienceArticleResult">
  142. <include refid="selectVetExperienceArticleVo"/>
  143. where a.status = #{status}
  144. and (a.title like concat('%', #{keyword}, '%')
  145. or a.content like concat('%', #{keyword}, '%')
  146. or a.summary like concat('%', #{keyword}, '%'))
  147. order by a.publish_time desc
  148. </select>
  149. <!-- &lt;!&ndash; 增加浏览数 &ndash;&gt;
  150. <update id="incrementViewCount" parameterType="Long">
  151. update vet_experience_article
  152. set view_count = ifnull(view_count, 0) + 1,
  153. update_time = now()
  154. where id = #{id}
  155. </update>-->
  156. <!-- 增加点赞数 -->
  157. <update id="incrementLikeCount" parameterType="Long">
  158. update vet_experience_article
  159. set like_count = ifnull(like_count, 0) + 1,
  160. update_time = now()
  161. where id = #{id}
  162. </update>
  163. <!-- 增加收藏数 -->
  164. <update id="incrementCollectCount" parameterType="Long">
  165. update vet_experience_article
  166. set collect_count = ifnull(collect_count, 0) + 1,
  167. update_time = now()
  168. where id = #{id}
  169. </update>
  170. <!-- 新增兽医经验文章 -->
  171. <insert id="insertVetExperienceArticle" parameterType="VetExperienceArticle" useGeneratedKeys="true" keyProperty="id">
  172. insert into vet_experience_article
  173. <trim prefix="(" suffix=")" suffixOverrides=",">
  174. <if test="title != null and title != ''">title,</if>
  175. <if test="content != null and content != ''">content,</if>
  176. <if test="summary != null">summary,</if>
  177. <if test="coverImage != null">cover_image,</if>
  178. <if test="images != null">images,</if>
  179. <if test="userId != null">user_id,</if>
  180. <if test="vetName != null and vetName != ''">vet_name,</if>
  181. <if test="vetAvatar != null">vet_avatar,</if>
  182. <if test="vetTitle != null">vet_title,</if>
  183. <if test="categoryId != null">category_id,</if>
  184. <if test="categoryName != null and categoryName != ''">category_name,</if>
  185. <if test="tags != null">tags,</if>
  186. <if test="viewCount != null">view_count,</if>
  187. <if test="likeCount != null">like_count,</if>
  188. <if test="collectCount != null">collect_count,</if>
  189. <if test="isTop != null">is_top,</if>
  190. <if test="isFeatured != null">is_featured,</if>
  191. <if test="status != null and status != ''">status,</if>
  192. <if test="isSensitive != null">is_sensitive,</if>
  193. <if test="sensitiveWords != null">sensitive_words,</if>
  194. <if test="publishTime != null">publish_time,</if>
  195. <if test="userRole != null and userRole != ''">user_role,</if>
  196. <if test="userType != null and userType != ''">user_type,</if>
  197. create_time, <!-- 固定字段放在最后 -->
  198. <if test="updateTime != null">update_time,</if>
  199. </trim>
  200. <trim prefix="values (" suffix=")" suffixOverrides=",">
  201. <if test="title != null and title != ''">#{title},</if>
  202. <if test="content != null and content != ''">#{content},</if>
  203. <if test="summary != null">#{summary},</if>
  204. <if test="coverImage != null">#{coverImage},</if>
  205. <if test="images != null">#{images},</if>
  206. <if test="userId != null">#{userId},</if>
  207. <if test="vetName != null and vetName != ''">#{vetName},</if>
  208. <if test="vetAvatar != null">#{vetAvatar},</if>
  209. <if test="vetTitle != null">#{vetTitle},</if>
  210. <if test="categoryId != null">#{categoryId},</if>
  211. <if test="categoryName != null and categoryName != ''">#{categoryName},</if>
  212. <if test="tags != null">#{tags},</if>
  213. <if test="viewCount != null">#{viewCount},</if>
  214. <if test="likeCount != null">#{likeCount},</if>
  215. <if test="collectCount != null">#{collectCount},</if>
  216. <if test="isTop != null">#{isTop},</if>
  217. <if test="isFeatured != null">#{isFeatured},</if>
  218. <if test="status != null and status != ''">#{status},</if>
  219. <if test="isSensitive != null">#{isSensitive},</if>
  220. <if test="sensitiveWords != null">#{sensitiveWords},</if>
  221. <if test="publishTime != null">#{publishTime},</if>
  222. <if test="userRole != null and userRole != ''">#{userRole},</if>
  223. <if test="userType != null and userType != ''">#{userType},</if>
  224. now(), <!-- 对应create_time -->
  225. <if test="updateTime != null">#{updateTime},</if>
  226. </trim>
  227. </insert>
  228. <!-- 修改兽医经验文章 -->
  229. <update id="updateVetExperienceArticle" parameterType="VetExperienceArticle">
  230. update vet_experience_article
  231. <trim prefix="SET" suffixOverrides=",">
  232. <if test="title != null and title != ''">title = #{title},</if>
  233. <if test="content != null and content != ''">content = #{content},</if>
  234. <if test="summary != null">summary = #{summary},</if>
  235. <if test="coverImage != null">cover_image = #{coverImage},</if>
  236. <if test="images != null">images = #{images},</if>
  237. <if test="userId != null">user_id = #{userId},</if>
  238. <if test="vetName != null and vetName != ''">vet_name = #{vetName},</if>
  239. <if test="vetAvatar != null">vet_avatar = #{vetAvatar},</if>
  240. <if test="vetTitle != null">vet_title = #{vetTitle},</if>
  241. <if test="categoryId != null">category_id = #{categoryId},</if>
  242. <if test="categoryName != null and categoryName != ''">category_name = #{categoryName},</if>
  243. <if test="tags != null">tags = #{tags},</if>
  244. <if test="viewCount != null">view_count = #{viewCount},</if>
  245. <if test="likeCount != null">like_count = #{likeCount},</if>
  246. <if test="collectCount != null">collect_count = #{collectCount},</if>
  247. <if test="isTop != null">is_top = #{isTop},</if>
  248. <if test="isFeatured != null">is_featured = #{isFeatured},</if>
  249. <if test="status != null and status != ''">status = #{status},</if>
  250. <if test="isSensitive != null">is_sensitive = #{isSensitive},</if>
  251. <if test="sensitiveWords != null">sensitive_words = #{sensitiveWords},</if>
  252. <if test="publishTime != null">publish_time = #{publishTime},</if>
  253. <if test="userRole != null and userRole != ''">user_role = #{userRole},</if>
  254. <if test="userType != null and userType != ''">user_type = #{userType},</if>
  255. update_time = now(),
  256. </trim>
  257. where id = #{id}
  258. </update>
  259. <!-- 删除兽医经验文章 -->
  260. <delete id="deleteVetExperienceArticleById" parameterType="Long">
  261. delete from vet_experience_article where id = #{id}
  262. </delete>
  263. <!-- 批量删除兽医经验文章 -->
  264. <delete id="deleteVetExperienceArticleByIds" parameterType="Long">
  265. delete from vet_experience_article where id in
  266. <foreach item="id" collection="array" open="(" separator="," close=")">
  267. #{id}
  268. </foreach>
  269. </delete>
  270. </mapper>