公告模块

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.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
/**
* @author zhuang
*/
@RequiredArgsConstructor @RequiredArgsConstructor
@Component @Component
@Profile("!test") @Profile("!test")

View File

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

View File

@ -1,17 +1,20 @@
package com.zsc.edu.gateway.modules.notice.controller; 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.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zsc.edu.gateway.framework.security.UserDetailsImpl; 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.BulletinDto;
import com.zsc.edu.gateway.modules.notice.entity.Bulletin; 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.query.BulletinQuery;
import com.zsc.edu.gateway.modules.notice.service.BulletinService; 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 lombok.AllArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize; import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.core.annotation.AuthenticationPrincipal; import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.web.bind.annotation.*; import org.springframework.web.bind.annotation.*;
import java.util.List;
import java.util.Set; import java.util.Set;
/** /**
@ -25,7 +28,7 @@ import java.util.Set;
public class BulletinController { public class BulletinController {
private final BulletinService service; private final BulletinService service;
private final BulletinUserService bulletinUserService; private final BulletinVoService bulletinVoService;
/** /**
* 普通用户查看公告详情 * 普通用户查看公告详情
@ -34,7 +37,7 @@ public class BulletinController {
* @return 公告 * @return 公告
*/ */
@GetMapping("/self/{id}") @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); return service.detail(userDetails,id, Bulletin.State.publish);
} }
@ -42,14 +45,15 @@ public class BulletinController {
* 普通用户分页查询公告 * 普通用户分页查询公告
* *
* @param query 查询表单 * @param query 查询表单
* @param page 分页参数
* @return 分页数据 * @return 分页数据
*/ */
@GetMapping("/self") @GetMapping("/query")
public Page<Bulletin> query(BulletinQuery query, Page<Bulletin> page) { public IPage<BulletinVo> getBulletins( BulletinQuery query) {
query.state = Bulletin.State.publish; query.setState(Bulletin.State.publish);
return service.page(page,query.wrapper()); Page<BulletinVo> page = new Page<>(query.getPageNum(), query.getPageSize());
return bulletinVoService.selectPageByConditions(page, query);
} }
/** /**
* 管理查询公告详情 * 管理查询公告详情
* *
@ -58,7 +62,7 @@ public class BulletinController {
*/ */
@GetMapping("/{id}") @GetMapping("/{id}")
@PreAuthorize("hasAuthority('BULLETIN_QUERY')") @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); return service.detail(userDetails,id, null);
} }
@ -66,13 +70,13 @@ public class BulletinController {
* 管理员分页查询公告 * 管理员分页查询公告
* *
* @param query 查询参数 * @param query 查询参数
* @param page 分页参数
* @return 分页数据 * @return 分页数据
*/ */
@GetMapping @GetMapping()
@PreAuthorize("hasAuthority('BULLETIN_QUERY')") @PreAuthorize("hasAuthority('BULLETIN_QUERY')")
public Page<Bulletin> page(BulletinQuery query, Page<Bulletin> page) { public IPage<BulletinVo> query( BulletinQuery query) {
return service.page(page, query.wrapper()); Page<BulletinVo> page = new Page<>(query.getPageNum(), query.getPageSize());
return bulletinVoService.selectPageByConditions(page, query);
} }
/** /**
@ -118,13 +122,13 @@ public class BulletinController {
* 发布公告只能发布"编辑中"的公告 * 发布公告只能发布"编辑中"的公告
* *
* @param userDetails 操作用户 * @param userDetails 操作用户
* @param id ID * @param ids IDs
* @return 公告 * @return 公告
*/ */
@PatchMapping("/{id}/publish") @PatchMapping("/publish")
@PreAuthorize("hasAuthority('BULLETIN_PUBLISH')") @PreAuthorize("hasAuthority('BULLETIN_PUBLISH')")
public Boolean publish(@AuthenticationPrincipal UserDetailsImpl userDetails,@PathVariable("id") Long id) { public List<String> publish(@AuthenticationPrincipal UserDetailsImpl userDetails,@RequestBody List<Long> ids) {
return service.publish(userDetails, id); return service.publish(userDetails, ids);
} }
/** /**
@ -134,23 +138,12 @@ public class BulletinController {
* @param id ID * @param id ID
* @return 公告 * @return 公告
*/ */
@PatchMapping("/{id}/close") @PatchMapping("/{id}/toggleClose")
@PreAuthorize("hasAuthority('BULLETIN_CLOSE')") @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); 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}") // @GetMapping("/self/{message-id}")
// public UserMessage selfDetail(@AuthenticationPrincipal UserDetailsImpl userDetails, @PathVariable("message-id") Long messageId) { // 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); // 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.system.entity.BaseEntity;
import com.zsc.edu.gateway.modules.attachment.entity.Attachment; import com.zsc.edu.gateway.modules.attachment.entity.Attachment;
import lombok.*; import lombok.*;
import org.springframework.data.mongodb.core.aggregation.ArrayOperators;
import java.time.LocalDateTime; import java.time.LocalDateTime;
import java.util.List; import java.util.List;
@ -37,7 +36,7 @@ public class Bulletin extends BaseEntity {
/** /**
* 是否置顶 * 是否置顶
*/ */
public boolean top; public Boolean top;
/** /**
* 编辑者ID * 编辑者ID
@ -92,6 +91,12 @@ public class Bulletin extends BaseEntity {
*/ */
public String content; 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 static class Other extends MessagePayload {
public Other(String content) { public Other(String content) {
this.content = content; this.content = content;
this.type = MessageType.其他; this.type = MessageType.other;
} }
} }
@ -30,7 +30,7 @@ public abstract class MessagePayload {
this.username = username; this.username = username;
this.password = password; this.password = password;
this.resetTime = resetTime; this.resetTime = resetTime;
this.type =MessageType.重置密码; this.type =MessageType.resetThePassword;
this.content = String.format("尊敬的用户%s您的密码已于%s被管理员重置新密码为%s" + this.content = String.format("尊敬的用户%s您的密码已于%s被管理员重置新密码为%s" +
"请及时登录系统修改密码!", username, resetTime.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")), password); "请及时登录系统修改密码!", 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; 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.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.zsc.edu.gateway.modules.notice.entity.Bulletin; import com.zsc.edu.gateway.modules.notice.entity.Bulletin;
import com.zsc.edu.gateway.modules.system.entity.Authority; import com.zsc.edu.gateway.modules.notice.vo.BulletinVo;
import com.zsc.edu.gateway.modules.system.entity.User;
import lombok.AllArgsConstructor; import lombok.AllArgsConstructor;
import lombok.Data; import lombok.Data;
import lombok.NoArgsConstructor; import lombok.NoArgsConstructor;
@ -21,35 +20,24 @@ import java.util.Objects;
@Data @Data
@AllArgsConstructor @AllArgsConstructor
@NoArgsConstructor @NoArgsConstructor
public class BulletinQuery { public class BulletinQuery {
/** private Integer pageNum = 1;
* 标题模糊查询 private Integer pageSize = 10;
*/ private String title;
public String title; private Bulletin.State state;
private LocalDateTime publishTimeBegin;
/** private LocalDateTime publishTimeEnd;
* 状态只用于管理员查询 private Boolean isRead;
*/ public LambdaQueryWrapper<BulletinVo> wrapper() {
public Bulletin.State state; 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);
@DateTimeFormat(iso = DateTimeFormat.ISO.DATE_TIME) }
public LocalDateTime publishTimeBegin; queryWrapper.eq(Objects.nonNull(this.isRead), BulletinVo::getIsRead, this.isRead);
/**
* 公告发布时间区间终止
*/
@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);
return queryWrapper; 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.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.zsc.edu.gateway.modules.notice.entity.Bulletin; 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.MessageType;
import com.zsc.edu.gateway.modules.notice.entity.UserMessage; import com.zsc.edu.gateway.modules.notice.entity.UserMessage;
import lombok.AllArgsConstructor; import lombok.AllArgsConstructor;
@ -11,7 +12,7 @@ import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.util.StringUtils; import org.springframework.util.StringUtils;
import java.time.LocalDateTime; import java.time.LocalDateTime;
import java.util.Set; import java.util.Objects;
/** /**
* 用户消息Query * 用户消息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 LocalDateTime createAtEnd;
// public LambdaQueryWrapper<UserMessage> wrapper() { public LambdaQueryWrapper<UserMessage> wrapper() {
// LambdaQueryWrapper<UserMessage> queryWrapper = new LambdaQueryWrapper<>(); LambdaQueryWrapper<UserMessage> queryWrapper = new LambdaQueryWrapper<>();
// queryWrapper.like(StringUtils.hasText(this.title), UserMessage::getTitle, this.title); LambdaQueryWrapper<Message> messageQueryWrapper = new LambdaQueryWrapper<>();
// queryWrapper.eq(StringUtils.hasText((CharSequence) this.states), UserMessage::getState, this.states); messageQueryWrapper.like(StringUtils.hasText(this.title), Message::getTitle, this.title);
// return queryWrapper; // 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; 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.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.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.Param;
import org.apache.ibatis.annotations.Select;
/** /**
* 公告Repo * 公告Repo
@ -13,8 +20,6 @@ import org.apache.ibatis.annotations.Param;
public interface BulletinRepository extends BaseMapper<Bulletin> { 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> { public interface BulletinUserRepository extends BaseMapper<BulletinUser> {
@Select("select * from sys_bulletin_user sbu where sbu.bulletin_id=#{bulletinId} and sbu.user_id=#{userId}") // @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); // 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);
} }

View File

@ -1,10 +1,15 @@
package com.zsc.edu.gateway.modules.notice.service; 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.baomidou.mybatisplus.extension.service.IService;
import com.zsc.edu.gateway.framework.security.UserDetailsImpl; 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.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.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; import java.util.Set;
/** /**
@ -15,19 +20,18 @@ import java.util.Set;
public interface BulletinService extends IService<Bulletin> { 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); Bulletin create(UserDetailsImpl userDetails, BulletinDto dto);
Boolean update(UserDetailsImpl userDetails, BulletinDto dto, Long id); Boolean update(UserDetailsImpl userDetails, BulletinDto dto, Long id);
Boolean open(Long id);
Boolean toggleTop(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 close(UserDetailsImpl userDetails,Long id);
Boolean insertInto(Long bulletinId, Set<String> attachmentIds); 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.baomidou.mybatisplus.extension.service.IService;
import com.zsc.edu.gateway.modules.notice.dto.UserMessageDto; 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 org.springframework.transaction.annotation.Transactional;
import java.util.Set;
/** /**
* 用户消息Service * 用户消息Service
* *
@ -12,4 +17,8 @@ import org.springframework.transaction.annotation.Transactional;
public interface UserMessageService extends IService<UserMessageService> { public interface UserMessageService extends IService<UserMessageService> {
Boolean createByAdmin(UserMessageDto dto); 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; package com.zsc.edu.gateway.modules.notice.service.impl;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; 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.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zsc.edu.gateway.exception.ConstraintException; 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.framework.security.UserDetailsImpl;
import com.zsc.edu.gateway.modules.notice.dto.BulletinDto; 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.Bulletin;
import com.zsc.edu.gateway.modules.notice.entity.BulletinAttachment; 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.repo.BulletinRepository;
import com.zsc.edu.gateway.modules.notice.service.BulletinAttachmentService; 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.BulletinService;
import com.zsc.edu.gateway.modules.notice.service.BulletinUserService; 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 lombok.AllArgsConstructor;
import org.springframework.beans.BeanUtils; import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import java.time.LocalDateTime; import java.time.LocalDateTime;
import java.util.EnumSet; import java.util.*;
import java.util.List; import java.util.stream.Collectors;
import java.util.Set;
import static com.zsc.edu.gateway.modules.notice.entity.Bulletin.State.*; 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 BulletinRepository repo;
private final BulletinAttachmentService bulletinAttachmentService; private final BulletinAttachmentService bulletinAttachmentService;
private final BulletinUserService bulletinUserService; private final BulletinUserService bulletinUserService;
private final UserRepository userRepository;
/** /**
* 查询公告详情 * 查询公告详情
* *
@ -42,11 +48,14 @@ public class BulletinServiceImpl extends ServiceImpl<BulletinRepository, Bulleti
* @return 公告详情 * @return 公告详情
*/ */
@Override @Override
public Bulletin detail(UserDetailsImpl userDetails,Long id, Bulletin.State state) { public BulletinVo detail(UserDetailsImpl userDetails, Long id, Bulletin.State state) {
Bulletin bulletin = repo.selectByBulletinId(id); BulletinVo bulletin = repo.selectByBulletinId(id);
if (state != null) { 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); bulletinUserService.isRead(userDetails, id);
return bulletin; return bulletin;
} }
@ -93,21 +102,43 @@ public class BulletinServiceImpl extends ServiceImpl<BulletinRepository, Bulleti
* 发布公告只能发布"编辑中"的公告 * 发布公告只能发布"编辑中"的公告
* *
* @param userDetails 操作用户 * @param userDetails 操作用户
* @param id ID * @param ids ids
* @return 已发布的公告 * @return 已发布的公告
*/ */
@Override @Override
public Boolean publish(UserDetailsImpl userDetails, Long id) { public List<String> publish(UserDetailsImpl userDetails, List<Long> ids)throws PublishFailedException, EmptyIdsException {
Bulletin bulletin = getById(id); List<String> results=new ArrayList<>();
bulletin.state.checkStatus(edit); if (ids == null || ids.isEmpty()) {
bulletin.state = publish; throw new EmptyIdsException("您输入的集合为空");
bulletin.setPublishUserId(userDetails.getId()); }
bulletin.setPublishTime(LocalDateTime.now()); List<Bulletin> bulletins=getBulletinsByIds(ids);
return updateById(bulletin); 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());
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 userDetails 操作用户
* @param id ID * @param id ID
@ -117,25 +148,18 @@ public class BulletinServiceImpl extends ServiceImpl<BulletinRepository, Bulleti
public Boolean close(UserDetailsImpl userDetails, Long id) { public Boolean close(UserDetailsImpl userDetails, Long id) {
Bulletin bulletin = getById(id); Bulletin bulletin = getById(id);
bulletin.top = false; bulletin.top = false;
if(bulletin.state==close){
bulletin.state.checkStatus(close);
bulletin.state = edit;
return updateById(bulletin);
}
bulletin.state.checkStatus(publish); bulletin.state.checkStatus(publish);
bulletin.state = close; bulletin.state = close;
bulletin.setCloseUserId(userDetails.getId()); bulletin.setCloseUserId(userDetails.getId());
bulletin.setCloseTime(LocalDateTime.now()); bulletin.setCloseTime(LocalDateTime.now());
return updateById(bulletin); 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); 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; 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.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zsc.edu.gateway.framework.security.UserDetailsImpl; import com.zsc.edu.gateway.framework.security.UserDetailsImpl;
@ -17,7 +18,6 @@ import org.springframework.stereotype.Service;
@Service @Service
public class BulletinUserServiceImpl extends ServiceImpl<BulletinUserRepository, BulletinUser> implements BulletinUserService { public class BulletinUserServiceImpl extends ServiceImpl<BulletinUserRepository, BulletinUser> implements BulletinUserService {
private BulletinUserRepository bulletinUserRepository;
/** /**
* 已读公告每次已读自动获取用户id与公告id加入联表 * 已读公告每次已读自动获取用户id与公告id加入联表
* *
@ -27,17 +27,25 @@ public class BulletinUserServiceImpl extends ServiceImpl<BulletinUserRepository,
*/ */
@Override @Override
public Boolean isRead(UserDetailsImpl userDetails,Long id) { public Boolean isRead(UserDetailsImpl userDetails,Long id) {
BulletinUser bulletinUser = new BulletinUser(); if (id == null || userDetails.getId() == null) {
bulletinUser.setBulletinId(id); throw new IllegalArgumentException("Bulletin ID and User ID cannot be null");
bulletinUser.setUserId(userDetails.getId());
bulletinUser.isRead=false;
if(!bulletinUserRepository.selectByBulletinIdAndUserId(id,userDetails.getId())){
return save(bulletinUser);
}else{
return false;
} }
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 // * @param id ID
// * @return 用户消息详情 // * @return 用户消息详情
// */ // */
// @Override
// public UserMessage detail(Long id) { // public UserMessage detail(Long id) {
// return getById(id); // return getById(id);
// } // }
@ -89,6 +90,7 @@
// * @param payload 消息内容 // * @param payload 消息内容
// */ // */
// @Transactional // @Transactional
// @Override
// public boolean createBySystem(Set<User> receivers, MessagePayload payload) { // public boolean createBySystem(Set<User> receivers, MessagePayload payload) {
// AtomicBoolean email = new AtomicBoolean(false); // AtomicBoolean email = new AtomicBoolean(false);
// AtomicBoolean sms = 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.zsc.edu.gateway.modules.system.entity.User;
import com.baomidou.mybatisplus.core.mapper.BaseMapper; import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.Select;
/** /**
* <p> * <p>
@ -13,4 +14,7 @@ import com.baomidou.mybatisplus.core.mapper.BaseMapper;
*/ */
public interface UserRepository extends BaseMapper<User> { public interface UserRepository extends BaseMapper<User> {
User selectByUsername(String username); 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: configuration:
log-impl: org.apache.ibatis.logging.stdout.StdOutImpl log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
default-enum-type-handler: com.baomidou.mybatisplus.core.handlers.MybatisEnumTypeHandler default-enum-type-handler: com.baomidou.mybatisplus.core.handlers.MybatisEnumTypeHandler
map-underscore-to-camel-case: true
spring: spring:
datasource: datasource:

View File

@ -3,7 +3,7 @@
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd"> "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.zsc.edu.gateway.modules.notice.repo.BulletinRepository"> <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"/> <id column="id" jdbcType="BIGINT" property="id"/>
<result column="title" jdbcType="VARCHAR" property="title"/> <result column="title" jdbcType="VARCHAR" property="title"/>
<result column="state" jdbcType="INTEGER" property="state"/> <result column="state" jdbcType="INTEGER" property="state"/>
@ -18,28 +18,21 @@
<result column="edit_user_name" jdbcType="VARCHAR" property="editUsername"/> <result column="edit_user_name" jdbcType="VARCHAR" property="editUsername"/>
<result column="publish_user_name" jdbcType="VARCHAR" property="publishUsername"/> <result column="publish_user_name" jdbcType="VARCHAR" property="publishUsername"/>
<result column="close_user_name" jdbcType="VARCHAR" property="closeUsername"/> <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_"> <result column="file_name" jdbcType="VARCHAR" property="fileName"/>
<id column="id" jdbcType="BIGINT" property="id"/> <result column="mime_type" jdbcType="VARCHAR" property="mimeType"/>
<result column="file_name" jdbcType="VARCHAR" property="fileName"/> <result column="upload_time" jdbcType="TIMESTAMP" property="uploadTime"/>
<result column="mime_type" jdbcType="VARCHAR" property="mimeType"/> <result column="url" jdbcType="VARCHAR" property="url"/>
<result column="upload_time" jdbcType="TIMESTAMP" property="uploadTime"/> <result column="is_read" jdbcType="BOOLEAN" property="isRead"/>
<result column="url" jdbcType="VARCHAR" property="url"/>
</collection>
</resultMap> </resultMap>
<select id="selectByBulletinId" resultMap="BulletinMap"> <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, 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
sb.content,sb.create_by,sb.create_time,sb.update_by,sb.update_time,a.*
from sys_bulletin sb from sys_bulletin sb
left join sys_bulletin_attach sba on sb.id=sba.bulletin_id left join sys_bulletin_attach sba on sb.id=sba.bulletin_id
left join attachment a on a.id=sba.attachment_id left join attachment a on a.id=sba.attachment_id
where sb.id=#{bulletinId} left join sys_bulletin_user sbu on sb.id=sbu.bulletin_id
</select> left join sys_user su on sbu.user_id=su.id
where sb.id=#{bulletinId}
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> </select>
</mapper> </mapper>

View File

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

View File

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