From 55bb72d1a1898e0b475c4b0fb44e553a06a7fd88 Mon Sep 17 00:00:00 2001 From: mashuai Date: Tue, 9 Jul 2024 13:09:27 +0800 Subject: [PATCH] =?UTF-8?q?=E9=80=9A=E7=9F=A5=E5=85=AC=E5=91=8A=E5=BC=B9?= =?UTF-8?q?=E7=AA=97?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../sgzb/system/config/ExceptionEnum.java | 33 ++++++++++ .../controller/SysNoticeController.java | 21 ++++++ .../bonus/sgzb/system/domain/NoticeDto.java | 19 ++++++ .../sgzb/system/mapper/SysNoticeMapper.java | 23 +++++++ .../system/service/ISysNoticeService.java | 17 +++++ .../service/impl/SysNoticeServiceImpl.java | 64 +++++++++++++++++++ .../mapper/system/SysNoticeMapper.xml | 31 ++++++++- 7 files changed, 206 insertions(+), 2 deletions(-) create mode 100644 sgzb-modules/sgzb-system/src/main/java/com/bonus/sgzb/system/config/ExceptionEnum.java create mode 100644 sgzb-modules/sgzb-system/src/main/java/com/bonus/sgzb/system/domain/NoticeDto.java diff --git a/sgzb-modules/sgzb-system/src/main/java/com/bonus/sgzb/system/config/ExceptionEnum.java b/sgzb-modules/sgzb-system/src/main/java/com/bonus/sgzb/system/config/ExceptionEnum.java new file mode 100644 index 00000000..bcc0f62c --- /dev/null +++ b/sgzb-modules/sgzb-system/src/main/java/com/bonus/sgzb/system/config/ExceptionEnum.java @@ -0,0 +1,33 @@ +package com.bonus.sgzb.system.config; + +import lombok.AllArgsConstructor; +import lombok.Getter; + +/** + * @Author ma_sh + * @create 2024/7/8 11:16 + */ +@Getter +@AllArgsConstructor +public enum ExceptionEnum { + + PARAM_NULL(1001, "参数为空"), + SUCCESS(200, "操作成功"), + SAVE_TO_DATABASE(500, "新增保存失败,请联系管理员!!!"), + DELETE_TO_DATABASE(500, "删除失败,请联系管理员!!!"), + BIND_TO_DATABASE(500, "绑定失败,请联系管理员!!!"), + UN_BIND_TO_DATABASE(500, "解绑失败,请联系管理员!!!"), + UPDATE_TO_DATABASE(500, "修改失败,请联系管理员!!!"); + + private Integer code; + + private String msg; + + public Integer getCode() { + return code; + } + + public String getMsg() { + return msg; + } +} diff --git a/sgzb-modules/sgzb-system/src/main/java/com/bonus/sgzb/system/controller/SysNoticeController.java b/sgzb-modules/sgzb-system/src/main/java/com/bonus/sgzb/system/controller/SysNoticeController.java index 3525d6cd..fbe50deb 100644 --- a/sgzb-modules/sgzb-system/src/main/java/com/bonus/sgzb/system/controller/SysNoticeController.java +++ b/sgzb-modules/sgzb-system/src/main/java/com/bonus/sgzb/system/controller/SysNoticeController.java @@ -1,6 +1,8 @@ package com.bonus.sgzb.system.controller; import java.util.List; + +import com.bonus.sgzb.system.domain.NoticeDto; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.validation.annotation.Validated; import org.springframework.web.bind.annotation.DeleteMapping; @@ -67,6 +69,25 @@ public class SysNoticeController extends BaseController return toAjax(noticeService.insertNotice(notice)); } + /** + * 新增已读公告用户id + */ + @PostMapping("/addUserId") + public AjaxResult addUserId(@Validated @RequestBody NoticeDto notice) + { + return noticeService.insertUserId(notice); + } + + /** + * 根据用户筛选,获取当天最新公告信息 + * @return + */ + @GetMapping("/getList") + public AjaxResult getList() + { + return noticeService.getList(); + } + /** * 修改通知公告 */ diff --git a/sgzb-modules/sgzb-system/src/main/java/com/bonus/sgzb/system/domain/NoticeDto.java b/sgzb-modules/sgzb-system/src/main/java/com/bonus/sgzb/system/domain/NoticeDto.java new file mode 100644 index 00000000..f55eb5f5 --- /dev/null +++ b/sgzb-modules/sgzb-system/src/main/java/com/bonus/sgzb/system/domain/NoticeDto.java @@ -0,0 +1,19 @@ +package com.bonus.sgzb.system.domain; + +import lombok.Data; + +import java.util.List; + +/** + * @Author ma_sh + * @create 2024/7/8 11:09 + */ +@Data +public class NoticeDto { + + /** 公告ID */ + private List noticeId; + + /** 已读公告用户id */ + private String userId; +} diff --git a/sgzb-modules/sgzb-system/src/main/java/com/bonus/sgzb/system/mapper/SysNoticeMapper.java b/sgzb-modules/sgzb-system/src/main/java/com/bonus/sgzb/system/mapper/SysNoticeMapper.java index 727bde6d..1bb5e2e3 100644 --- a/sgzb-modules/sgzb-system/src/main/java/com/bonus/sgzb/system/mapper/SysNoticeMapper.java +++ b/sgzb-modules/sgzb-system/src/main/java/com/bonus/sgzb/system/mapper/SysNoticeMapper.java @@ -2,6 +2,7 @@ package com.bonus.sgzb.system.mapper; import java.util.List; import com.bonus.sgzb.system.domain.SysNotice; +import org.apache.ibatis.annotations.Param; /** * 通知公告表 数据层 @@ -57,4 +58,26 @@ public interface SysNoticeMapper * @return 结果 */ public int deleteNoticeByIds(Long[] noticeIds); + + /** + * 根据公告ID查询用户ID + * @param noticeId + * @return + */ + String selectUserIdByNoticeId(Long noticeId); + + /** + * 根据公告ID修改用户ID + * @param noticeId + * @param concatenated + * @return + */ + int updateUserId(@Param("noticeId") Long noticeId, @Param("userId") String concatenated); + + /** + * 根据用户ID查询公告内容 + * @param userId + * @return + */ + List getList(String userId); } \ No newline at end of file diff --git a/sgzb-modules/sgzb-system/src/main/java/com/bonus/sgzb/system/service/ISysNoticeService.java b/sgzb-modules/sgzb-system/src/main/java/com/bonus/sgzb/system/service/ISysNoticeService.java index 47f7b1b9..efcc8138 100644 --- a/sgzb-modules/sgzb-system/src/main/java/com/bonus/sgzb/system/service/ISysNoticeService.java +++ b/sgzb-modules/sgzb-system/src/main/java/com/bonus/sgzb/system/service/ISysNoticeService.java @@ -1,6 +1,9 @@ package com.bonus.sgzb.system.service; import java.util.List; + +import com.bonus.sgzb.common.core.web.domain.AjaxResult; +import com.bonus.sgzb.system.domain.NoticeDto; import com.bonus.sgzb.system.domain.SysNotice; /** @@ -57,4 +60,18 @@ public interface ISysNoticeService * @return 结果 */ public int deleteNoticeByIds(Long[] noticeIds); + + /** + * 新增已读公告用户id + * @param notice + * @return + */ + AjaxResult insertUserId(NoticeDto notice); + + /** + * 根据用户筛选,获取当天最新公告信息 + * @return + */ + AjaxResult getList(); + } diff --git a/sgzb-modules/sgzb-system/src/main/java/com/bonus/sgzb/system/service/impl/SysNoticeServiceImpl.java b/sgzb-modules/sgzb-system/src/main/java/com/bonus/sgzb/system/service/impl/SysNoticeServiceImpl.java index 41b9656f..db14c7d8 100644 --- a/sgzb-modules/sgzb-system/src/main/java/com/bonus/sgzb/system/service/impl/SysNoticeServiceImpl.java +++ b/sgzb-modules/sgzb-system/src/main/java/com/bonus/sgzb/system/service/impl/SysNoticeServiceImpl.java @@ -1,6 +1,14 @@ package com.bonus.sgzb.system.service.impl; import java.util.List; + +import com.alibaba.nacos.common.utils.CollectionUtils; +import com.alibaba.nacos.common.utils.StringUtils; +import com.bonus.sgzb.common.core.web.domain.AjaxResult; +import com.bonus.sgzb.common.security.utils.SecurityUtils; +import com.bonus.sgzb.system.config.ExceptionEnum; +import com.bonus.sgzb.system.domain.NoticeDto; +import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import com.bonus.sgzb.system.domain.SysNotice; @@ -13,6 +21,7 @@ import com.bonus.sgzb.system.service.ISysNoticeService; * @author ruoyi */ @Service +@Slf4j public class SysNoticeServiceImpl implements ISysNoticeService { @Autowired @@ -89,4 +98,59 @@ public class SysNoticeServiceImpl implements ISysNoticeService { return noticeMapper.deleteNoticeByIds(noticeIds); } + + /** + * 新增已读公告用户id + * @param notice + * @return + */ + @Override + public AjaxResult insertUserId(NoticeDto notice) { + if (notice == null || CollectionUtils.isEmpty(notice.getNoticeId()) || notice.getUserId() == null) { + return AjaxResult.error(ExceptionEnum.PARAM_NULL.getCode(), ExceptionEnum.PARAM_NULL.getMsg()); + } + // 外部初始化 StringBuilder 用于构建拼接后的用户id字符串 + StringBuilder concatenatedBuilder = new StringBuilder(); + try { + //根据公告id查询已读公告用户id + List noticeIdList = notice.getNoticeId(); + // 循环处理每个公告ID + for (Long noticeId : noticeIdList) { + // 清空 StringBuilder,准备添加新的用户id + concatenatedBuilder.setLength(0); + String userIds = noticeMapper.selectUserIdByNoticeId(noticeId); + if (StringUtils.isNotBlank(userIds)) { + String[] split = userIds.split(","); + for (String userId : split) { + if (StringUtils.isNotBlank(userId)) { + concatenatedBuilder.append(userId.trim()).append(","); + } + } + } + // 添加固定的 notice.getUserId() + concatenatedBuilder.append(notice.getUserId()); + String concatenated = concatenatedBuilder.toString(); + // 将已读公告用户id更新到数据库 + int res = noticeMapper.updateUserId(noticeId, concatenated); + if (res == 0) { + log.error("更新公告ID为 " + noticeId + " 的已读用户ID失败"); + throw new RuntimeException("更新公告ID为 " + noticeId + " 的已读用户ID失败"); + } + } + return AjaxResult.success(); + } catch (Exception e) { + log.error("更新已读公告用户ID时出现异常:" + e.getMessage()); + return AjaxResult.error(ExceptionEnum.UPDATE_TO_DATABASE.getCode(), ExceptionEnum.UPDATE_TO_DATABASE.getMsg()); + } + } + + /** + * 根据用户筛选,获取当天最新公告信息 + * @return + */ + @Override + public AjaxResult getList() { + String userId = SecurityUtils.getLoginUser().getSysUser().getUserId().toString(); + return AjaxResult.success(noticeMapper.getList(userId)); + } } diff --git a/sgzb-modules/sgzb-system/src/main/resources/mapper/system/SysNoticeMapper.xml b/sgzb-modules/sgzb-system/src/main/resources/mapper/system/SysNoticeMapper.xml index ac882139..6c2fe66a 100644 --- a/sgzb-modules/sgzb-system/src/main/resources/mapper/system/SysNoticeMapper.xml +++ b/sgzb-modules/sgzb-system/src/main/resources/mapper/system/SysNoticeMapper.xml @@ -52,7 +52,31 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" AND sn.create_by like concat('%', #{createBy}, '%') - + + + insert into sys_notice ( notice_title, @@ -85,7 +109,10 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" where notice_id = #{noticeId} - + + update sys_notice set user_id = #{userId} where notice_id = #{noticeId} + + delete from sys_notice where notice_id = #{noticeId}