公告模块

This commit is contained in:
zhuangtianxiang 2024-11-26 16:16:08 +08:00
parent 47f0dd437a
commit 060d90ecb2
22 changed files with 304 additions and 236 deletions

View File

@ -14,6 +14,9 @@ import org.springframework.context.annotation.Profile;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Component;
/**
* @author zhuang
*/
@RequiredArgsConstructor
@Component
@Profile("!test")

View File

@ -17,7 +17,7 @@ public class MybatisPlusConfig {
@Bean
public MybatisPlusInterceptor mybatisPlusInterceptor() {
MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.MYSQL));
interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.POSTGRE_SQL));
// // 添加数据权限插件
// MyDataPermissionInterceptor dataPermissionInterceptor = new MyDataPermissionInterceptor();
// // 添加自定义的数据权限处理器

View File

@ -1,17 +1,20 @@
package com.zsc.edu.gateway.modules.notice.controller;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zsc.edu.gateway.framework.security.UserDetailsImpl;
import com.zsc.edu.gateway.modules.notice.dto.BulletinDto;
import com.zsc.edu.gateway.modules.notice.entity.Bulletin;
import com.zsc.edu.gateway.modules.notice.query.BulletinQuery;
import com.zsc.edu.gateway.modules.notice.service.BulletinService;
import com.zsc.edu.gateway.modules.notice.service.BulletinUserService;
import com.zsc.edu.gateway.modules.notice.service.BulletinVoService;
import com.zsc.edu.gateway.modules.notice.vo.BulletinVo;
import lombok.AllArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.web.bind.annotation.*;
import java.util.List;
import java.util.Set;
/**
@ -25,7 +28,7 @@ import java.util.Set;
public class BulletinController {
private final BulletinService service;
private final BulletinUserService bulletinUserService;
private final BulletinVoService bulletinVoService;
/**
* 普通用户查看公告详情
@ -34,7 +37,7 @@ public class BulletinController {
* @return 公告
*/
@GetMapping("/self/{id}")
public Bulletin selfDetail(@AuthenticationPrincipal UserDetailsImpl userDetails,@PathVariable("id") Long id) {
public BulletinVo selfDetail(@AuthenticationPrincipal UserDetailsImpl userDetails,@PathVariable("id") Long id) {
return service.detail(userDetails,id, Bulletin.State.publish);
}
@ -42,14 +45,15 @@ public class BulletinController {
* 普通用户分页查询公告
*
* @param query 查询表单
* @param page 分页参数
* @return 分页数据
*/
@GetMapping("/self")
public Page<Bulletin> query(BulletinQuery query, Page<Bulletin> page) {
query.state = Bulletin.State.publish;
return service.page(page,query.wrapper());
@GetMapping("/query")
public IPage<BulletinVo> getBulletins( BulletinQuery query) {
query.setState(Bulletin.State.publish);
Page<BulletinVo> page = new Page<>(query.getPageNum(), query.getPageSize());
return bulletinVoService.selectPageByConditions(page, query);
}
/**
* 管理查询公告详情
*
@ -58,7 +62,7 @@ public class BulletinController {
*/
@GetMapping("/{id}")
@PreAuthorize("hasAuthority('BULLETIN_QUERY')")
public Bulletin detail(@AuthenticationPrincipal UserDetailsImpl userDetails,@PathVariable("id") Long id) {
public BulletinVo detail(@AuthenticationPrincipal UserDetailsImpl userDetails, @PathVariable("id") Long id) {
return service.detail(userDetails,id, null);
}
@ -66,13 +70,13 @@ public class BulletinController {
* 管理员分页查询公告
*
* @param query 查询参数
* @param page 分页参数
* @return 分页数据
*/
@GetMapping
@GetMapping()
@PreAuthorize("hasAuthority('BULLETIN_QUERY')")
public Page<Bulletin> page(BulletinQuery query, Page<Bulletin> page) {
return service.page(page, query.wrapper());
public IPage<BulletinVo> query( BulletinQuery query) {
Page<BulletinVo> page = new Page<>(query.getPageNum(), query.getPageSize());
return bulletinVoService.selectPageByConditions(page, query);
}
/**
@ -118,13 +122,13 @@ public class BulletinController {
* 发布公告只能发布"编辑中"的公告
*
* @param userDetails 操作用户
* @param id ID
* @param ids IDs
* @return 公告
*/
@PatchMapping("/{id}/publish")
@PatchMapping("/publish")
@PreAuthorize("hasAuthority('BULLETIN_PUBLISH')")
public Boolean publish(@AuthenticationPrincipal UserDetailsImpl userDetails,@PathVariable("id") Long id) {
return service.publish(userDetails, id);
public List<String> publish(@AuthenticationPrincipal UserDetailsImpl userDetails,@RequestBody List<Long> ids) {
return service.publish(userDetails, ids);
}
/**
@ -134,23 +138,12 @@ public class BulletinController {
* @param id ID
* @return 公告
*/
@PatchMapping("/{id}/close")
@PatchMapping("/{id}/toggleClose")
@PreAuthorize("hasAuthority('BULLETIN_CLOSE')")
public Boolean close(@AuthenticationPrincipal UserDetailsImpl userDetails, @PathVariable("id") Long id) {
public Boolean toggleClose(@AuthenticationPrincipal UserDetailsImpl userDetails, @PathVariable("id") Long id) {
return service.close(userDetails, id);
}
/**
* 开启公告切换到编辑中
*
* @param id id
* @return true
*/
@PatchMapping("/{id}/open")
@PreAuthorize("hasAuthority('BULLETIN_CLOSE')")
public Boolean open(@PathVariable("id") Long id){
return service.open(id);
}
/**
* 删除公告只能删除"编辑中"的公告
*

View File

@ -39,7 +39,7 @@
// */
// @GetMapping("/self/{message-id}")
// public UserMessage selfDetail(@AuthenticationPrincipal UserDetailsImpl userDetails, @PathVariable("message-id") Long messageId) {
// UserMessage.Id id = new UserMessage.Id(userDetails.id, messageId);
// UserMessage id = new UserMessage.Id(userDetails.id, messageId);
// return service.detail(id);
// }
//

View File

@ -7,7 +7,6 @@ import com.zsc.edu.gateway.common.enums.IState;
import com.zsc.edu.gateway.modules.system.entity.BaseEntity;
import com.zsc.edu.gateway.modules.attachment.entity.Attachment;
import lombok.*;
import org.springframework.data.mongodb.core.aggregation.ArrayOperators;
import java.time.LocalDateTime;
import java.util.List;
@ -37,7 +36,7 @@ public class Bulletin extends BaseEntity {
/**
* 是否置顶
*/
public boolean top;
public Boolean top;
/**
* 编辑者ID
@ -92,6 +91,12 @@ public class Bulletin extends BaseEntity {
*/
public String content;
/**
* 已读状态
*/
@TableField(exist = false)
public Boolean isRead;
/**
* 附件列表
*/

View File

@ -25,7 +25,7 @@ public class Message extends BaseEntity {
/**
* 消息类型
*/
public com.zsc.edu.gateway.modules.notice.entity.MessageType type = MessageType.其他;
public MessageType type = MessageType.other;
/**
* 是否系统生成

View File

@ -17,7 +17,7 @@ public abstract class MessagePayload {
public static class Other extends MessagePayload {
public Other(String content) {
this.content = content;
this.type = MessageType.其他;
this.type = MessageType.other;
}
}
@ -30,7 +30,7 @@ public abstract class MessagePayload {
this.username = username;
this.password = password;
this.resetTime = resetTime;
this.type =MessageType.重置密码;
this.type =MessageType.resetThePassword;
this.content = String.format("尊敬的用户%s您的密码已于%s被管理员重置新密码为%s" +
"请及时登录系统修改密码!", username, resetTime.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")), password);
}

View File

@ -1,11 +1,32 @@
package com.zsc.edu.gateway.modules.notice.entity;
import com.baomidou.mybatisplus.annotation.IEnum;
import com.zsc.edu.gateway.common.enums.IState;
/**
* 消息类型
*
* @author harry_yao
* @author zhuang
*/
public enum MessageType {
其他,
重置密码
public enum MessageType implements IEnum<Integer>,IState<MessageType> {
other(1,"其他"),
resetThePassword(2,"重置密码");
private final Integer value;
private final String name;
MessageType(Integer value, String name) {
this.value = value;
this.name = name;
}
@Override
public Integer getValue() {
return value;
}
@Override
public String toString() {
return name;
}
}

View File

@ -2,8 +2,7 @@ package com.zsc.edu.gateway.modules.notice.query;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.zsc.edu.gateway.modules.notice.entity.Bulletin;
import com.zsc.edu.gateway.modules.system.entity.Authority;
import com.zsc.edu.gateway.modules.system.entity.User;
import com.zsc.edu.gateway.modules.notice.vo.BulletinVo;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
@ -21,35 +20,24 @@ import java.util.Objects;
@Data
@AllArgsConstructor
@NoArgsConstructor
public class BulletinQuery {
/**
* 标题模糊查询
*/
public String title;
/**
* 状态只用于管理员查询
*/
public Bulletin.State state;
/**
* 公告发布时间区间起始
*/
@DateTimeFormat(iso = DateTimeFormat.ISO.DATE_TIME)
public LocalDateTime publishTimeBegin;
/**
* 公告发布时间区间终止
*/
@DateTimeFormat(iso = DateTimeFormat.ISO.DATE_TIME)
public LocalDateTime publishTimeEnd;
public LambdaQueryWrapper<Bulletin> wrapper() {
LambdaQueryWrapper<Bulletin> queryWrapper = new LambdaQueryWrapper<>();
queryWrapper.like(StringUtils.hasText(this.title), Bulletin::getTitle, this.title);
queryWrapper.eq(Objects.nonNull(this.state), Bulletin::getState, this.state);
private Integer pageNum = 1;
private Integer pageSize = 10;
private String title;
private Bulletin.State state;
private LocalDateTime publishTimeBegin;
private LocalDateTime publishTimeEnd;
private Boolean isRead;
public LambdaQueryWrapper<BulletinVo> wrapper() {
LambdaQueryWrapper<BulletinVo> queryWrapper = new LambdaQueryWrapper<>();
queryWrapper.like(StringUtils.hasText(this.title), BulletinVo::getTitle, this.title);
queryWrapper.eq(Objects.nonNull(this.state), BulletinVo::getState, this.state);
if(Objects.nonNull(this.publishTimeBegin)&&Objects.nonNull(this.publishTimeEnd)) {
queryWrapper.between(BulletinVo::getPublishTime,publishTimeBegin,publishTimeEnd);
}
queryWrapper.eq(Objects.nonNull(this.isRead), BulletinVo::getIsRead, this.isRead);
return queryWrapper;
}
}

View File

@ -2,6 +2,7 @@ package com.zsc.edu.gateway.modules.notice.query;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.zsc.edu.gateway.modules.notice.entity.Bulletin;
import com.zsc.edu.gateway.modules.notice.entity.Message;
import com.zsc.edu.gateway.modules.notice.entity.MessageType;
import com.zsc.edu.gateway.modules.notice.entity.UserMessage;
import lombok.AllArgsConstructor;
@ -11,7 +12,7 @@ import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.util.StringUtils;
import java.time.LocalDateTime;
import java.util.Set;
import java.util.Objects;
/**
* 用户消息Query
@ -36,7 +37,7 @@ public class UserMessageQuery {
/**
* 消息类型
*/
public Set<MessageType> types;
public MessageType type;
/**
* 用户名或真实姓名用户名准确查询姓名模糊查询
@ -66,10 +67,11 @@ public class UserMessageQuery {
public LocalDateTime createAtEnd;
// public LambdaQueryWrapper<UserMessage> wrapper() {
// LambdaQueryWrapper<UserMessage> queryWrapper = new LambdaQueryWrapper<>();
// queryWrapper.like(StringUtils.hasText(this.title), UserMessage::getTitle, this.title);
// queryWrapper.eq(StringUtils.hasText((CharSequence) this.states), UserMessage::getState, this.states);
// return queryWrapper;
// }
public LambdaQueryWrapper<UserMessage> wrapper() {
LambdaQueryWrapper<UserMessage> queryWrapper = new LambdaQueryWrapper<>();
LambdaQueryWrapper<Message> messageQueryWrapper = new LambdaQueryWrapper<>();
messageQueryWrapper.like(StringUtils.hasText(this.title), Message::getTitle, this.title);
// messageQueryWrapper.eq(Objects::nonNull(this.type) Message::getType, this.type);
return queryWrapper;
}
}

View File

@ -1,9 +1,16 @@
package com.zsc.edu.gateway.modules.notice.repo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zsc.edu.gateway.modules.notice.dto.PageDto;
import com.zsc.edu.gateway.modules.notice.entity.Bulletin;
import com.zsc.edu.gateway.modules.notice.query.BulletinQuery;
import com.zsc.edu.gateway.modules.notice.vo.BulletinVo;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
/**
* 公告Repo
@ -13,8 +20,6 @@ import org.apache.ibatis.annotations.Param;
public interface BulletinRepository extends BaseMapper<Bulletin> {
Bulletin selectByBulletinId(@Param("bulletinId") Long bulletinId);
BulletinVo selectByBulletinId(@Param("bulletinId") Long bulletinId);
Bulletin selectAll();
}

View File

@ -11,9 +11,6 @@ import org.apache.ibatis.annotations.Select;
*/
public interface BulletinUserRepository extends BaseMapper<BulletinUser> {
@Select("select * from sys_bulletin_user sbu where sbu.bulletin_id=#{bulletinId} and sbu.user_id=#{userId}")
Boolean selectByBulletinIdAndUserId(@Param("bulletinId") Long bulletinId, @Param("userId") Long userId);
@Select("select * from sys_bulletin_user sbu where sbu.bulletin_id=#{bulletinId}")
BulletinUser selectReadByBulletinId(@Param("bulletinId") Long bulletinId);
// @Select("select * from sys_bulletin_user sbu where sbu.bulletin_id=#{bulletinId} and sbu.user_id=#{userId}")
// Boolean selectByBulletinIdAndUserId(@Param("bulletinId") Long bulletinId, @Param("userId") Long userId);
}

View File

@ -1,10 +1,15 @@
package com.zsc.edu.gateway.modules.notice.service;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.IService;
import com.zsc.edu.gateway.framework.security.UserDetailsImpl;
import com.zsc.edu.gateway.modules.notice.dto.BulletinDto;
import com.zsc.edu.gateway.modules.notice.dto.PageDto;
import com.zsc.edu.gateway.modules.notice.entity.Bulletin;
import com.zsc.edu.gateway.modules.notice.query.BulletinQuery;
import com.zsc.edu.gateway.modules.notice.vo.BulletinVo;
import java.util.List;
import java.util.Set;
/**
@ -15,19 +20,18 @@ import java.util.Set;
public interface BulletinService extends IService<Bulletin> {
Bulletin detail(UserDetailsImpl userDetails,Long id, Bulletin.State state);
BulletinVo detail(UserDetailsImpl userDetails, Long id, Bulletin.State state);
Bulletin create(UserDetailsImpl userDetails, BulletinDto dto);
Boolean update(UserDetailsImpl userDetails, BulletinDto dto, Long id);
Boolean open(Long id);
Boolean toggleTop(Long id);
Boolean publish(UserDetailsImpl userDetails,Long id);
List<String> publish(UserDetailsImpl userDetails, List<Long> id);
Boolean close(UserDetailsImpl userDetails,Long id);
Boolean insertInto(Long bulletinId, Set<String> attachmentIds);
}

View File

@ -2,8 +2,13 @@ package com.zsc.edu.gateway.modules.notice.service;
import com.baomidou.mybatisplus.extension.service.IService;
import com.zsc.edu.gateway.modules.notice.dto.UserMessageDto;
import com.zsc.edu.gateway.modules.notice.entity.MessagePayload;
import com.zsc.edu.gateway.modules.notice.entity.UserMessage;
import com.zsc.edu.gateway.modules.system.entity.User;
import org.springframework.transaction.annotation.Transactional;
import java.util.Set;
/**
* 用户消息Service
*
@ -12,4 +17,8 @@ import org.springframework.transaction.annotation.Transactional;
public interface UserMessageService extends IService<UserMessageService> {
Boolean createByAdmin(UserMessageDto dto);
public UserMessage detail(Long id);
public boolean createBySystem(Set<User> receivers, MessagePayload payload);
}

View File

@ -1,24 +1,29 @@
package com.zsc.edu.gateway.modules.notice.service.impl;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zsc.edu.gateway.exception.ConstraintException;
import com.zsc.edu.gateway.exception.EmptyIdsException;
import com.zsc.edu.gateway.exception.PublishFailedException;
import com.zsc.edu.gateway.framework.security.UserDetailsImpl;
import com.zsc.edu.gateway.modules.notice.dto.BulletinDto;
import com.zsc.edu.gateway.modules.notice.entity.Bulletin;
import com.zsc.edu.gateway.modules.notice.entity.BulletinAttachment;
import com.zsc.edu.gateway.modules.notice.query.BulletinQuery;
import com.zsc.edu.gateway.modules.notice.repo.BulletinRepository;
import com.zsc.edu.gateway.modules.notice.service.BulletinAttachmentService;
import com.zsc.edu.gateway.modules.notice.service.BulletinService;
import com.zsc.edu.gateway.modules.notice.service.BulletinUserService;
import com.zsc.edu.gateway.modules.notice.vo.BulletinVo;
import com.zsc.edu.gateway.modules.system.repo.UserRepository;
import lombok.AllArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import java.time.LocalDateTime;
import java.util.EnumSet;
import java.util.List;
import java.util.Set;
import java.util.*;
import java.util.stream.Collectors;
import static com.zsc.edu.gateway.modules.notice.entity.Bulletin.State.*;
@ -34,6 +39,7 @@ public class BulletinServiceImpl extends ServiceImpl<BulletinRepository, Bulleti
private final BulletinRepository repo;
private final BulletinAttachmentService bulletinAttachmentService;
private final BulletinUserService bulletinUserService;
private final UserRepository userRepository;
/**
* 查询公告详情
*
@ -42,11 +48,14 @@ public class BulletinServiceImpl extends ServiceImpl<BulletinRepository, Bulleti
* @return 公告详情
*/
@Override
public Bulletin detail(UserDetailsImpl userDetails,Long id, Bulletin.State state) {
Bulletin bulletin = repo.selectByBulletinId(id);
public BulletinVo detail(UserDetailsImpl userDetails, Long id, Bulletin.State state) {
BulletinVo bulletin = repo.selectByBulletinId(id);
if (state != null) {
bulletin.state.checkStatus(state);
bulletin.getState().checkStatus(state);
}
bulletin.setEditUsername(userRepository.selectNameById(bulletin.getEditUserId()));
bulletin.setPublishUsername(userRepository.selectNameById(bulletin.getPublishUserId()));
bulletin.setCloseUsername(userRepository.selectNameById(bulletin.getCloseUserId()));
bulletinUserService.isRead(userDetails, id);
return bulletin;
}
@ -93,21 +102,43 @@ public class BulletinServiceImpl extends ServiceImpl<BulletinRepository, Bulleti
* 发布公告只能发布"编辑中"的公告
*
* @param userDetails 操作用户
* @param id ID
* @param ids ids
* @return 已发布的公告
*/
@Override
public Boolean publish(UserDetailsImpl userDetails, Long id) {
Bulletin bulletin = getById(id);
bulletin.state.checkStatus(edit);
public List<String> publish(UserDetailsImpl userDetails, List<Long> ids)throws PublishFailedException, EmptyIdsException {
List<String> results=new ArrayList<>();
if (ids == null || ids.isEmpty()) {
throw new EmptyIdsException("您输入的集合为空");
}
List<Bulletin> bulletins=getBulletinsByIds(ids);
for (Bulletin bulletin : bulletins) {
try {
bulletin.state.checkStatus(EnumSet.of(edit));
} catch (Exception e) {
throw new PublishFailedException("发布失败: " + e.getMessage());
}
}
for(Bulletin bulletin:bulletins){
try{
bulletin.state = publish;
bulletin.setPublishUserId(userDetails.getId());
bulletin.setPublishTime(LocalDateTime.now());
return updateById(bulletin);
boolean result=updateById(bulletin);
if(result){
results.add("发布成功");
}else {
throw new PublishFailedException("发布失败");
}
}catch(Exception e){
throw new PublishFailedException("发布失败: " + e.getMessage());
}
}
return results;
}
/**
* 关闭公告只能关闭"已发布"的公告
* 切换关闭状态只能关闭"已发布"的公告只能开启已关闭的公告
*
* @param userDetails 操作用户
* @param id ID
@ -117,25 +148,18 @@ public class BulletinServiceImpl extends ServiceImpl<BulletinRepository, Bulleti
public Boolean close(UserDetailsImpl userDetails, Long id) {
Bulletin bulletin = getById(id);
bulletin.top = false;
if(bulletin.state==close){
bulletin.state.checkStatus(close);
bulletin.state = edit;
return updateById(bulletin);
}
bulletin.state.checkStatus(publish);
bulletin.state = close;
bulletin.setCloseUserId(userDetails.getId());
bulletin.setCloseTime(LocalDateTime.now());
return updateById(bulletin);
}
/**
* 开启公告只能开启已关闭的公告变成编辑中
*
*
*/
@Override
public Boolean open(Long id){
Bulletin bulletin = getById(id);
bulletin.top = false;
bulletin.state.checkStatus(close);
bulletin.state = edit;
return updateById(bulletin);
}
/**
* 切换公告置顶状态
*
@ -162,4 +186,13 @@ public class BulletinServiceImpl extends ServiceImpl<BulletinRepository, Bulleti
return bulletinAttachmentService.saveBatch(bulletinAttachments);
}
private List<Bulletin> getBulletinsByIds(List<Long> ids) {
if (ids == null || ids.isEmpty()) {
return Collections.emptyList();
}
LambdaQueryWrapper<Bulletin> queryWrapper=new LambdaQueryWrapper<>();
queryWrapper.in(Bulletin::getId, ids);
return repo.selectList(queryWrapper);
}
}

View File

@ -1,5 +1,6 @@
package com.zsc.edu.gateway.modules.notice.service.impl;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zsc.edu.gateway.framework.security.UserDetailsImpl;
@ -17,7 +18,6 @@ import org.springframework.stereotype.Service;
@Service
public class BulletinUserServiceImpl extends ServiceImpl<BulletinUserRepository, BulletinUser> implements BulletinUserService {
private BulletinUserRepository bulletinUserRepository;
/**
* 已读公告每次已读自动获取用户id与公告id加入联表
*
@ -27,17 +27,25 @@ public class BulletinUserServiceImpl extends ServiceImpl<BulletinUserRepository,
*/
@Override
public Boolean isRead(UserDetailsImpl userDetails,Long id) {
BulletinUser bulletinUser = new BulletinUser();
bulletinUser.setBulletinId(id);
bulletinUser.setUserId(userDetails.getId());
bulletinUser.isRead=false;
if(!bulletinUserRepository.selectByBulletinIdAndUserId(id,userDetails.getId())){
return save(bulletinUser);
}else{
return false;
if (id == null || userDetails.getId() == null) {
throw new IllegalArgumentException("Bulletin ID and User ID cannot be null");
}
QueryWrapper<BulletinUser> queryWrapper = new QueryWrapper<>();
queryWrapper.eq("bulletin_id", id)
.eq("user_id", userDetails.getId());
BulletinUser existingUser = getOne(queryWrapper);
if (existingUser == null) {
BulletinUser newUser = new BulletinUser();
newUser.setBulletinId(id);
newUser.setUserId(userDetails.getId());
newUser.setIsRead(false);
save(newUser);
} else {
UpdateWrapper<BulletinUser> updateWrapper = new UpdateWrapper<>();
updateWrapper.eq("bulletin_id", id).eq("user_id", userDetails.getId()).set("is_read",false);
}
return true;
}
/**
* 更新公告后修改已读状态
*

View File

@ -47,6 +47,7 @@
// * @param id ID
// * @return 用户消息详情
// */
// @Override
// public UserMessage detail(Long id) {
// return getById(id);
// }
@ -89,6 +90,7 @@
// * @param payload 消息内容
// */
// @Transactional
// @Override
// public boolean createBySystem(Set<User> receivers, MessagePayload payload) {
// AtomicBoolean email = new AtomicBoolean(false);
// AtomicBoolean sms = new AtomicBoolean(false);

View File

@ -2,6 +2,7 @@ package com.zsc.edu.gateway.modules.system.repo;
import com.zsc.edu.gateway.modules.system.entity.User;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.Select;
/**
* <p>
@ -13,4 +14,7 @@ import com.baomidou.mybatisplus.core.mapper.BaseMapper;
*/
public interface UserRepository extends BaseMapper<User> {
User selectByUsername(String username);
@Select("select sys_user.name from sys_user where sys_user.id=#{id}")
String selectNameById(Long id);
}

View File

@ -8,6 +8,7 @@ mybatis-plus:
configuration:
log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
default-enum-type-handler: com.baomidou.mybatisplus.core.handlers.MybatisEnumTypeHandler
map-underscore-to-camel-case: true
spring:
datasource:

View File

@ -3,7 +3,7 @@
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.zsc.edu.gateway.modules.notice.repo.BulletinRepository">
<resultMap id="BulletinMap" type="com.zsc.edu.gateway.modules.notice.entity.Bulletin" autoMapping="true">
<resultMap id="BulletinMap" type="com.zsc.edu.gateway.modules.notice.vo.BulletinVo" autoMapping="true">
<id column="id" jdbcType="BIGINT" property="id"/>
<result column="title" jdbcType="VARCHAR" property="title"/>
<result column="state" jdbcType="INTEGER" property="state"/>
@ -18,28 +18,21 @@
<result column="edit_user_name" jdbcType="VARCHAR" property="editUsername"/>
<result column="publish_user_name" jdbcType="VARCHAR" property="publishUsername"/>
<result column="close_user_name" jdbcType="VARCHAR" property="closeUsername"/>
<collection property="attachments" ofType="com.zsc.edu.gateway.modules.attachment.entity.Attachment" autoMapping="true" columnPrefix="attachment_">
<id column="id" jdbcType="BIGINT" property="id"/>
<result column="file_name" jdbcType="VARCHAR" property="fileName"/>
<result column="mime_type" jdbcType="VARCHAR" property="mimeType"/>
<result column="upload_time" jdbcType="TIMESTAMP" property="uploadTime"/>
<result column="url" jdbcType="VARCHAR" property="url"/>
</collection>
<result column="is_read" jdbcType="BOOLEAN" property="isRead"/>
</resultMap>
<select id="selectByBulletinId" resultMap="BulletinMap">
select sb.id,sb.title,sb.state,sb.top,sb.edit_user_id as edit_user_name,sb.edit_time,sb.publish_user_id as publish_user_name,sb.publish_time,sb.close_user_id as close_user_name,sb.close_time,
sb.content,sb.create_by,sb.create_time,sb.update_by,sb.update_time,a.*
select sb.*,a.id as attachment_id,a.file_name as attachment_file_name,a.mime_type as attachment_mime_type,a.url as attachment_url,a.upload_time as attachment_upload_time,sbu.is_read
from sys_bulletin sb
left join sys_bulletin_attach sba on sb.id=sba.bulletin_id
left join attachment a on a.id=sba.attachment_id
left join sys_bulletin_user sbu on sb.id=sbu.bulletin_id
left join sys_user su on sbu.user_id=su.id
where sb.id=#{bulletinId}
</select>
order by sb.top DESC, sb.create_time DESC;
<select id="selectAll" resultMap="BulletinMap">
select sb.id,sb.title,sb.state,sb.top,sb.edit_user_id as edit_user_name,sb.edit_time,sb.publish_user_id as publish_user_name,sb.publish_time,sb.close_user_id as close_user_name,sb.close_time,
sb.content,sb.create_by,sb.create_time,sb.update_by,sb.update_time,a.*
from sys_bulletin sb
left join sys_bulletin_attach sba on sb.id=sba.bulletin_id
left join attachment a on a.id=sba.attachment_id
</select>
</mapper>

View File

@ -15,7 +15,7 @@ class IotGatewayApplicationTests {
private BulletinRepository bulletinRepository;
@Test
void contextLoads() {
bulletinRepository.selectAll();
// bulletinRepository.selectAll();
}

View File

@ -1,89 +1,89 @@
package com.zsc.edu.gateway.service;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.zsc.edu.gateway.domain.BulletinBuilder;
import com.zsc.edu.gateway.exception.ConstraintException;
import com.zsc.edu.gateway.framework.security.UserDetailsImpl;
import com.zsc.edu.gateway.modules.notice.dto.BulletinDto;
import com.zsc.edu.gateway.modules.notice.entity.Bulletin;
import com.zsc.edu.gateway.modules.notice.repo.BulletinRepository;
import com.zsc.edu.gateway.modules.notice.service.BulletinService;
import jakarta.annotation.Resource;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import java.util.List;
import static org.junit.jupiter.api.Assertions.*;
public class BulletinServiceTest {
@Resource
private BulletinService service;
@Resource
private BulletinRepository repo;
Bulletin bulletin1;
Bulletin bulletin2;
@BeforeEach
void setUp() {
bulletin1 = BulletinBuilder.bBulletin().title("测试1").build();
repo.insert(bulletin1);
bulletin2 = BulletinBuilder.bBulletin().title("测试2").build();
repo.insert(bulletin2);
}
@Test
void list() {
LambdaQueryWrapper<Bulletin> queryWrapper = new LambdaQueryWrapper<>();
assertEquals(2, service.list(queryWrapper.like(Bulletin::getTitle, "测试")).size());
assertEquals(1, service.list(queryWrapper.eq(Bulletin::getTitle, bulletin1.getTitle())).size());
assertEquals(2, service.list().size());
}
@Test
void createBulletin() {
BulletinDto dto = new BulletinDto();
dto.setTitle("测试");
dto.setTop(true);
dto.setContent("测试测试");
dto.setRemark("测试公告增加");
BulletinDto dto2 = new BulletinDto();
dto2.setTitle(bulletin2.getTitle());
dto2.setTop(bulletin2.isTop());
dto2.setRemark(bulletin2.getRemark());
UserDetailsImpl userDetails = new UserDetailsImpl();
userDetails.setUsername("admin");
Bulletin bulletin=service.create(userDetails,dto);
assertNotNull(bulletin.getId());
List<Bulletin> list = service.list();
assertEquals(3, list.size());
// 不能创建其他已存在标题公告
assertThrows(ConstraintException.class, () -> service.create(userDetails,dto2));
}
@Test
void updateBulletin() {
BulletinDto dto = new BulletinDto();
dto.setTitle("测试3");
dto.setContent("测试测");
dto.setTop(true);
dto.setRemark("测试公告更新");
UserDetailsImpl userDetails = new UserDetailsImpl();
userDetails.setUsername("admin");
assertTrue(service.update(userDetails,dto, bulletin2.id));
Bulletin bulletin = service.getOne(new LambdaQueryWrapper<Bulletin>().eq(Bulletin::getTitle, dto.getTitle()));
assertEquals(bulletin.getTitle(), dto.getTitle());
assertEquals(bulletin.getId(), bulletin2.id);
// 不能改为其他已存在的同名同代码部门
assertThrows(ConstraintException.class,
() -> service.update(userDetails,new BulletinDto(bulletin1.getTitle(),true,null,null), bulletin2.id));
}
@AfterEach
void tearDown() {
repo.delete(new QueryWrapper<>());
}
}
//package com.zsc.edu.gateway.service;
//
//import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
//import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
//import com.zsc.edu.gateway.domain.BulletinBuilder;
//import com.zsc.edu.gateway.exception.ConstraintException;
//import com.zsc.edu.gateway.framework.security.UserDetailsImpl;
//import com.zsc.edu.gateway.modules.notice.dto.BulletinDto;
//import com.zsc.edu.gateway.modules.notice.entity.Bulletin;
//import com.zsc.edu.gateway.modules.notice.repo.BulletinRepository;
//import com.zsc.edu.gateway.modules.notice.service.BulletinService;
//import jakarta.annotation.Resource;
//import org.junit.jupiter.api.AfterEach;
//import org.junit.jupiter.api.BeforeEach;
//import org.junit.jupiter.api.Test;
//
//import java.util.List;
//
//import static org.junit.jupiter.api.Assertions.*;
//
//public class BulletinServiceTest {
// @Resource
// private BulletinService service;
// @Resource
// private BulletinRepository repo;
//
// Bulletin bulletin1;
// Bulletin bulletin2;
//
// @BeforeEach
// void setUp() {
// bulletin1 = BulletinBuilder.bBulletin().title("测试1").build();
// repo.insert(bulletin1);
// bulletin2 = BulletinBuilder.bBulletin().title("测试2").build();
// repo.insert(bulletin2);
// }
//
// @Test
// void list() {
// LambdaQueryWrapper<Bulletin> queryWrapper = new LambdaQueryWrapper<>();
// assertEquals(2, service.list(queryWrapper.like(Bulletin::getTitle, "测试")).size());
// assertEquals(1, service.list(queryWrapper.eq(Bulletin::getTitle, bulletin1.getTitle())).size());
// assertEquals(2, service.list().size());
// }
//
// @Test
// void createBulletin() {
// BulletinDto dto = new BulletinDto();
// dto.setTitle("测试");
// dto.setTop(true);
// dto.setContent("测试测试");
// dto.setRemark("测试公告增加");
// BulletinDto dto2 = new BulletinDto();
// dto2.setTitle(bulletin2.getTitle());
//// dto2.setTop(bulletin2.isTop());
// dto2.setRemark(bulletin2.getRemark());
// UserDetailsImpl userDetails = new UserDetailsImpl();
// userDetails.setUsername("admin");
// Bulletin bulletin=service.create(userDetails,dto);
// assertNotNull(bulletin.getId());
// List<Bulletin> list = service.list();
// assertEquals(3, list.size());
// // 不能创建其他已存在标题公告
// assertThrows(ConstraintException.class, () -> service.create(userDetails,dto2));
// }
//
// @Test
// void updateBulletin() {
// BulletinDto dto = new BulletinDto();
// dto.setTitle("测试3");
// dto.setContent("测试测");
// dto.setTop(true);
// dto.setRemark("测试公告更新");
// UserDetailsImpl userDetails = new UserDetailsImpl();
// userDetails.setUsername("admin");
// assertTrue(service.update(userDetails,dto, bulletin2.id));
// Bulletin bulletin = service.getOne(new LambdaQueryWrapper<Bulletin>().eq(Bulletin::getTitle, dto.getTitle()));
// assertEquals(bulletin.getTitle(), dto.getTitle());
// assertEquals(bulletin.getId(), bulletin2.id);
// // 不能改为其他已存在的同名同代码部门
// assertThrows(ConstraintException.class,
// () -> service.update(userDetails,new BulletinDto(bulletin1.getTitle(),true,null,null), bulletin2.id));
// }
//
// @AfterEach
// void tearDown() {
// repo.delete(new QueryWrapper<>());
// }
//}