|
2 | 2 |
|
3 | 3 | import com.baomidou.mybatisplus.core.mapper.BaseMapper; |
4 | 4 | import com.baomidou.mybatisplus.core.metadata.IPage; |
| 5 | +import com.baomidou.mybatisplus.extension.handlers.JacksonTypeHandler; |
5 | 6 | import com.baomidou.mybatisplus.extension.plugins.pagination.Page; |
6 | 7 | import com.ulticode.modules.notification.entity.Notification; |
7 | 8 | import org.apache.ibatis.annotations.Insert; |
|
18 | 19 | @Mapper |
19 | 20 | public interface NotificationMapper extends BaseMapper<Notification> { |
20 | 21 |
|
21 | | - @Select("SELECT COUNT(*) FROM notifications WHERE user_id = #{userId} AND is_read = 0") |
22 | | - long countUnreadByUserId(@Param("userId") String userId); |
| 22 | + @Select("SELECT COUNT(*) FROM notifications WHERE user_id = #{userId} AND is_read =0") |
| 23 | + long countUnreadByUserId(@Param("userId") String userId); |
23 | 24 |
|
24 | | - @Update("UPDATE notifications SET is_read = 1, read_at = NOW() WHERE user_id = #{userId} AND is_read = 0") |
25 | | - int markAllAsRead(@Param("userId") String userId); |
| 25 | + @Update("UPDATE notifications SET is_read =1, read_at = NOW() WHERE user_id = #{userId} AND is_read =0") |
| 26 | + int markAllAsRead(@Param("userId") String userId); |
26 | 27 |
|
27 | | - @Update("UPDATE notifications SET is_read = 1, read_at = NOW() WHERE id = #{id}") |
28 | | - int markAsRead(@Param("id") String id); |
| 28 | + @Update("UPDATE notifications SET is_read =1, read_at = NOW() WHERE id = #{id}") |
| 29 | + int markAsRead(@Param("id") String id); |
29 | 30 |
|
30 | | - @Insert("<script>INSERT INTO notifications " + |
31 | | - "(id, user_id, type, category, title, body, link, metadata, announcement_id, is_read, read_at, created_at, updated_at) VALUES " + |
32 | | - "<foreach collection='list' item='item' separator=','>" + |
33 | | - "(#{item.id}, #{item.userId}, #{item.type}, #{item.category}, #{item.title}, #{item.body}, #{item.link}, #{item.metadata}, #{item.announcementId}, #{item.isRead}, #{item.readAt}, #{item.createdAt}, #{item.updatedAt})" + |
34 | | - "</foreach></script>") |
35 | | - int batchInsert(@Param("list") List<Notification> list); |
| 31 | + /** |
| 32 | + * Batch insert notifications. |
| 33 | + * |
| 34 | + * <p>The {@code #{item.metadata}} parameter explicitly declares |
| 35 | + * {@link JacksonTypeHandler} because MyBatis does not inherit the |
| 36 | + * {@code @TableField(typeHandler=…)} metadata inside custom {@code @Insert} |
| 37 | + * SQL fragments. Without this, a {@code Map<String,Object>} field has no |
| 38 | + * JDBC type handler and MyBatis throws {@code IllegalStateException: Type |
| 39 | + * handler was null on parameter mapping for property '__frch_item_0.metadata'}. |
| 40 | + * Regression covered by {@code NotificationMapperBatchInsertTest}. |
| 41 | + */ |
| 42 | + @Insert("<script>INSERT INTO notifications " |
| 43 | + + "(id, user_id, type, category, title, body, link, metadata, announcement_id, is_read, read_at, created_at, updated_at) VALUES " |
| 44 | + + "<foreach collection='list' item='item' separator=','>" |
| 45 | + + "(#{item.id}, #{item.userId}, #{item.type}, #{item.category}, #{item.title}, #{item.body}, #{item.link}, " |
| 46 | + + "#{item.metadata, typeHandler=com.baomidou.mybatisplus.extension.handlers.JacksonTypeHandler}, " |
| 47 | + + "#{item.announcementId}, #{item.isRead}, #{item.readAt}, #{item.createdAt}, #{item.updatedAt})" |
| 48 | + + "</foreach></script>") |
| 49 | + int batchInsert(@Param("list") List<Notification> list); |
36 | 50 |
|
37 | | - /** |
38 | | - * Paginated query for deduplicated system announcements by announcement_id. |
39 | | - * Returns one representative notification per announcement group. |
40 | | - */ |
41 | | - @Select("<script>" + |
42 | | - "SELECT n.* FROM notifications n " + |
43 | | - "INNER JOIN (" + |
44 | | - " SELECT announcement_id, MIN(id) AS representative_id " + |
45 | | - " FROM notifications " + |
46 | | - " WHERE category = #{category} " + |
47 | | - " <if test='keyword != null and keyword != \"\"'> AND title LIKE CONCAT('%', #{keyword}, '%') </if>" + |
48 | | - " <if test='type != null and type != \"\"'> AND type = #{type} </if>" + |
49 | | - " <if test='announcementId != null and announcementId != \"\"'> AND announcement_id = #{announcementId} </if>" + |
50 | | - " GROUP BY announcement_id" + |
51 | | - ") dedup ON n.id = dedup.representative_id " + |
52 | | - "ORDER BY " + |
53 | | - "<choose>" + |
54 | | - " <when test='sortBy == \"title\"'>n.title</when>" + |
55 | | - " <when test='sortBy == \"type\"'>n.type</when>" + |
56 | | - " <when test='sortBy == \"category\"'>n.category</when>" + |
57 | | - " <when test='sortBy == \"announcementId\"'>n.announcement_id</when>" + |
58 | | - " <otherwise>n.created_at</otherwise>" + |
59 | | - "</choose> " + |
60 | | - "<choose>" + |
61 | | - " <when test='sortOrder == \"asc\"'>ASC</when>" + |
62 | | - " <otherwise>DESC</otherwise>" + |
63 | | - "</choose>" + |
64 | | - "</script>") |
65 | | - IPage<Notification> selectDedupedAnnouncements(Page<Notification> page, |
66 | | - @Param("category") String category, |
67 | | - @Param("keyword") String keyword, |
68 | | - @Param("type") String type, |
69 | | - @Param("announcementId") String announcementId, |
70 | | - @Param("sortBy") String sortBy, |
71 | | - @Param("sortOrder") String sortOrder); |
| 51 | + /** |
| 52 | + * Paginated query for deduplicated system announcements by announcement_id. |
| 53 | + * Returns one representative notification per announcement group. |
| 54 | + */ |
| 55 | + @Select("<script>" |
| 56 | + + "SELECT n.* FROM notifications n " |
| 57 | + + "INNER JOIN (" |
| 58 | + + " SELECT announcement_id, MIN(id) AS representative_id " |
| 59 | + + " FROM notifications " |
| 60 | + + " WHERE category = #{category} " |
| 61 | + + " <if test='keyword != null and keyword != \"\"'> AND title LIKE CONCAT('%', #{keyword}, '%') </if>" |
| 62 | + + " <if test='type != null and type != \"\"'> AND type = #{type} </if>" |
| 63 | + + " <if test='announcementId != null and announcementId != \"\"'> AND announcement_id = #{announcementId} </if>" |
| 64 | + + " GROUP BY announcement_id" |
| 65 | + + ") dedup ON n.id = dedup.representative_id " |
| 66 | + + "ORDER BY " |
| 67 | + + "<choose>" |
| 68 | + + " <when test='sortBy == \"title\"'>n.title</when>" |
| 69 | + + " <when test='sortBy == \"type\"'>n.type</when>" |
| 70 | + + " <when test='sortBy == \"category\"'>n.category</when>" |
| 71 | + + " <when test='sortBy == \"announcementId\"'>n.announcement_id</when>" |
| 72 | + + " <otherwise>n.created_at</otherwise>" |
| 73 | + + "</choose> " |
| 74 | + + "<choose>" |
| 75 | + + " <when test='sortOrder == \"asc\"'>ASC</when>" |
| 76 | + + " <otherwise>DESC</otherwise>" |
| 77 | + + "</choose>" |
| 78 | + + "</script>") |
| 79 | + IPage<Notification> selectDedupedAnnouncements(Page<Notification> page, |
| 80 | + @Param("category") String category, |
| 81 | + @Param("keyword") String keyword, |
| 82 | + @Param("type") String type, |
| 83 | + @Param("announcementId") String announcementId, |
| 84 | + @Param("sortBy") String sortBy, |
| 85 | + @Param("sortOrder") String sortOrder); |
72 | 86 | } |
0 commit comments