refactor(notice): 重构公告模块

- 优化公告查询接口,新增分页查询功能
- 重构公告发布逻辑,支持批量发布
- 完善公告详情接口,增加用户信息
- 新增公告删除接口
- 优化公告状态切换逻辑
-调整公告附件处理方式
This commit is contained in:
zhuangtianxiang 2024-12-09 16:16:21 +08:00
parent 528318ff0a
commit 6fd1c6b81d
14 changed files with 613 additions and 0 deletions

View File

@ -0,0 +1,23 @@
package com.zsc.edu.gateway.common.enums;
import com.baomidou.mybatisplus.annotation.IEnum;
public enum EnableState implements IEnum<Boolean> {
ENABLE(Boolean.TRUE),
DISABLE(Boolean.FALSE);
private boolean value;
EnableState(Boolean value) {
this.value = value;
}
@Override
public Boolean getValue() {
return this.value;
}
public EnableState reverse() {
return this == ENABLE ? DISABLE : ENABLE;
}
}

View File

@ -0,0 +1,36 @@
package com.zsc.edu.gateway.common.enums;
import com.zsc.edu.gateway.exception.StateException;
import java.util.EnumSet;
/**
* @author harry_yao
*/
public interface IState<T extends Enum<T>> {
/**
* 用于检查对象当前状态是否等于correctStatus
*
* @param correctState 正确状态
*/
default void checkStatus(T correctState) {
if (this != correctState) {
throw new StateException(correctState.getClass(), this, correctState);
}
}
/**
* 用于检查对象当前状态是否在集合correctStates中
*
* @param correctStates 正确状态集合
*/
@SuppressWarnings("SuspiciousMethodCalls")
default void checkStatus(EnumSet<T> correctStates) {
if (!correctStates.contains(this)) {
throw new StateException(this.getClass(), this, correctStates);
}
}
}

View File

@ -0,0 +1,23 @@
package com.zsc.edu.gateway.common.mapstruct;
import org.mapstruct.MappingTarget;
import java.util.List;
public interface BaseMapper<D, E> {
D toDto(E entity);
E toEntity(D dto);
List<D> toDto(List<E> entityList);
List<E> toEntity(List<D> dtoList);
/**
* 更新实体类
*
* @param dto
* @param entity
*/
void convert(D dto, @MappingTarget E entity);
}

View File

@ -0,0 +1,130 @@
package com.zsc.edu.gateway.common.util;
import java.util.*;
import java.util.function.*;
import java.util.stream.Collectors;
import java.util.function.BiConsumer;
import java.util.function.BiFunction;
import java.util.function.Consumer;
import java.util.function.Function;
/**
* @Description: 树操作方法工具类
* @Copyright: Copyright (c) 赵侠客
* @Date: 2024-07-22 10:42
* @Version: 1.0
*/
public class TreeUtil {
/**
* 将list合成树
*
* @param list 需要合成树的List
* @param rootCheck 判断E中为根节点的条件x->x.getPId()==-1L , x->x.getParentId()==null,x->x.getParentMenuId()==0
* @param parentCheck 判断E中为父节点条件(x,y)->x.getId().equals(y.getPId())
* @param setSubChildren E中设置下级数据方法Menu::setSubMenus
* @param <E> 泛型实体对象
* @return 合成好的树
*/
public static <E> List<E> makeTree(List<E> list, Predicate<E> rootCheck, BiFunction<E, E, Boolean> parentCheck, BiConsumer<E, List<E>> setSubChildren) {
return list.stream().filter(rootCheck).peek(x -> setSubChildren.accept(x, makeChildren(x, list, parentCheck, setSubChildren))).collect(Collectors.toList());
}
/**
* 将树打平成tree
*
* @param tree 需要打平的树
* @param getSubChildren 设置下级数据方法Menu::getSubMenus,x->x.setSubMenus(null)
* @param setSubChildren 将下级数据置空方法x->x.setSubMenus(null)
* @param <E> 泛型实体对象
* @return 打平后的数据
*/
public static <E> List<E> flat(List<E> tree, Function<E, List<E>> getSubChildren, Consumer<E> setSubChildren) {
List<E> res = new ArrayList<>();
forPostOrder(tree, item -> {
setSubChildren.accept(item);
res.add(item);
}, getSubChildren);
return res;
}
/**
* 前序遍历
*
* @param tree 需要遍历的树
* @param consumer 遍历后对单个元素的处理方法x-> System.out.println(x) System.out::println打印元素
* @param setSubChildren 设置下级数据方法Menu::getSubMenus,x->x.setSubMenus(null)
* @param <E> 泛型实体对象
*/
public static <E> void forPreOrder(List<E> tree, Consumer<E> consumer, Function<E, List<E>> setSubChildren) {
for (E l : tree) {
consumer.accept(l);
List<E> es = setSubChildren.apply(l);
if (es != null && es.size() > 0) {
forPreOrder(es, consumer, setSubChildren);
}
}
}
/**
* 层序遍历
*
* @param tree 需要遍历的树
* @param consumer 遍历后对单个元素的处理方法x-> System.out.println(x) System.out::println打印元素
* @param setSubChildren 设置下级数据方法Menu::getSubMenus,x->x.setSubMenus(null)
* @param <E> 泛型实体对象
*/
public static <E> void forLevelOrder(List<E> tree, Consumer<E> consumer, Function<E, List<E>> setSubChildren) {
Queue<E> queue = new LinkedList<>(tree);
while (!queue.isEmpty()) {
E item = queue.poll();
consumer.accept(item);
List<E> childList = setSubChildren.apply(item);
if (childList != null && !childList.isEmpty()) {
queue.addAll(childList);
}
}
}
/**
* 后序遍历
*
* @param tree 需要遍历的树
* @param consumer 遍历后对单个元素的处理方法x-> System.out.println(x) System.out::println打印元素
* @param setSubChildren 设置下级数据方法Menu::getSubMenus,x->x.setSubMenus(null)
* @param <E> 泛型实体对象
*/
public static <E> void forPostOrder(List<E> tree, Consumer<E> consumer, Function<E, List<E>> setSubChildren) {
for (E item : tree) {
List<E> childList = setSubChildren.apply(item);
if (childList != null && !childList.isEmpty()) {
forPostOrder(childList, consumer, setSubChildren);
}
consumer.accept(item);
}
}
/**
* 对树所有子节点按comparator排序
*
* @param tree 需要排序的树
* @param comparator 排序规则ComparatorComparator.comparing(MenuVo::getRank)按Rank正序 ,(x,y)->y.getRank().compareTo(x.getRank())按Rank倒序
* @param getChildren 获取下级数据方法MenuVo::getSubMenus
* @param <E> 泛型实体对象
* @return 排序好的树
*/
public static <E> List<E> sort(List<E> tree, Comparator<? super E> comparator, Function<E, List<E>> getChildren) {
for (E item : tree) {
List<E> childList = getChildren.apply(item);
if (childList != null && !childList.isEmpty()) {
sort(childList, comparator, getChildren);
}
}
tree.sort(comparator);
return tree;
}
private static <E> List<E> makeChildren(E parent, List<E> allData, BiFunction<E, E, Boolean> parentCheck, BiConsumer<E, List<E>> children) {
return allData.stream().filter(x -> parentCheck.apply(parent, x)).peek(x -> children.accept(x, makeChildren(x, allData, parentCheck, children))).collect(Collectors.toList());
}
}

View File

@ -0,0 +1,69 @@
package com.zsc.edu.gateway.framework;
import com.zsc.edu.gateway.modules.system.entity.Dept;
import com.zsc.edu.gateway.modules.system.entity.User;
import com.zsc.edu.gateway.modules.system.vo.DeptTree;
import com.zsc.edu.gateway.modules.system.vo.UserTree;
import org.springframework.stereotype.Component;
import java.util.*;
import java.util.function.BiConsumer;
import java.util.function.BiFunction;
import java.util.function.Predicate;
import java.util.stream.Collectors;
/**
* @author zhuang
*/
@Component
public class DeptTreeUtil {
public static <E> List<E> makeTree(List<E> list, Predicate<E> rootCheck, BiFunction<E, E, Boolean> parentCheck, BiConsumer<E, List<E>> setSubChildren) {
return list.stream()
.filter(rootCheck)
.peek(x -> setSubChildren.accept(x, makeChildren(x, list, parentCheck, setSubChildren)))
.collect(Collectors.toList());
}
private static <E> List<E> makeChildren(E parent, List<E> allData, BiFunction<E, E, Boolean> parentCheck, BiConsumer<E, List<E>> setSubChildren) {
return allData.stream()
.filter(x -> parentCheck.apply(parent, x))
.peek(x -> setSubChildren.accept(x, makeChildren(x, allData, parentCheck, setSubChildren)))
.collect(Collectors.toList());
}
public static List<DeptTree> buildDeptTree(List<Dept> depots, Map<Long, List<User>> userMap) {
List<DeptTree> deptTrees = depots.stream()
.map(DeptTreeUtil::convertToDeptTree)
.collect(Collectors.toList());
deptTrees.forEach(deptTree -> {
List<User> users = userMap.getOrDefault(deptTree.getId(), Collections.emptyList());
deptTree.setMembers(users.stream()
.map(DeptTreeUtil::convertToUserTree)
.collect(Collectors.toList()));
});
return makeTree(
deptTrees,
deptTree -> deptTree.getPid() == null || deptTree.getPid() == 0L,
(parent, child) -> parent.getId().equals(child.getPid()),
DeptTree::setChildren
);
}
private static DeptTree convertToDeptTree(Dept dept) {
DeptTree deptTree = new DeptTree();
deptTree.setId(dept.getId());
deptTree.setPid(dept.getPid());
deptTree.setName(dept.getName());
return deptTree;
}
private static UserTree convertToUserTree(User user) {
UserTree userTree = new UserTree();
userTree.setId(user.getId());
userTree.setName(user.getName());
return userTree;
}
}

View File

@ -0,0 +1,21 @@
package com.zsc.edu.gateway.framework;
import org.springframework.http.HttpStatus;
import java.util.Calendar;
import java.util.LinkedHashMap;
import java.util.Map;
/**
* @author harry yao
*/
public class JsonExceptionUtil {
public static Map<String, Object> jsonExceptionResult(HttpStatus code, String message, String path) {
Map<String, Object> exceptionMap = new LinkedHashMap<>();
exceptionMap.put("timestamp", Calendar.getInstance().getTime());
exceptionMap.put("message", message);
exceptionMap.put("path", path);
exceptionMap.put("code", code.value());
return exceptionMap;
}
}

View File

@ -0,0 +1,130 @@
package com.zsc.edu.gateway.framework;
import java.util.*;
import java.util.function.*;
import java.util.stream.Collectors;
import java.util.function.BiConsumer;
import java.util.function.BiFunction;
import java.util.function.Consumer;
import java.util.function.Function;
/**
* @Description: 树操作方法工具类
* @Copyright: Copyright (c) 赵侠客
* @Date: 2024-07-22 10:42
* @Version: 1.0
*/
public class TreeUtil {
/**
* 将list合成树
*
* @param list 需要合成树的List
* @param rootCheck 判断E中为根节点的条件x->x.getPId()==-1L , x->x.getParentId()==null,x->x.getParentMenuId()==0
* @param parentCheck 判断E中为父节点条件(x,y)->x.getId().equals(y.getPId())
* @param setSubChildren E中设置下级数据方法Menu::setSubMenus
* @param <E> 泛型实体对象
* @return 合成好的树
*/
public static <E> List<E> makeTree(List<E> list, Predicate<E> rootCheck, BiFunction<E, E, Boolean> parentCheck, BiConsumer<E, List<E>> setSubChildren) {
return list.stream().filter(rootCheck).peek(x -> setSubChildren.accept(x, makeChildren(x, list, parentCheck, setSubChildren))).collect(Collectors.toList());
}
/**
* 将树打平成tree
*
* @param tree 需要打平的树
* @param getSubChildren 设置下级数据方法Menu::getSubMenus,x->x.setSubMenus(null)
* @param setSubChildren 将下级数据置空方法x->x.setSubMenus(null)
* @param <E> 泛型实体对象
* @return 打平后的数据
*/
public static <E> List<E> flat(List<E> tree, Function<E, List<E>> getSubChildren, Consumer<E> setSubChildren) {
List<E> res = new ArrayList<>();
forPostOrder(tree, item -> {
setSubChildren.accept(item);
res.add(item);
}, getSubChildren);
return res;
}
/**
* 前序遍历
*
* @param tree 需要遍历的树
* @param consumer 遍历后对单个元素的处理方法x-> System.out.println(x) System.out::println打印元素
* @param setSubChildren 设置下级数据方法Menu::getSubMenus,x->x.setSubMenus(null)
* @param <E> 泛型实体对象
*/
public static <E> void forPreOrder(List<E> tree, Consumer<E> consumer, Function<E, List<E>> setSubChildren) {
for (E l : tree) {
consumer.accept(l);
List<E> es = setSubChildren.apply(l);
if (es != null && es.size() > 0) {
forPreOrder(es, consumer, setSubChildren);
}
}
}
/**
* 层序遍历
*
* @param tree 需要遍历的树
* @param consumer 遍历后对单个元素的处理方法x-> System.out.println(x) System.out::println打印元素
* @param setSubChildren 设置下级数据方法Menu::getSubMenus,x->x.setSubMenus(null)
* @param <E> 泛型实体对象
*/
public static <E> void forLevelOrder(List<E> tree, Consumer<E> consumer, Function<E, List<E>> setSubChildren) {
Queue<E> queue = new LinkedList<>(tree);
while (!queue.isEmpty()) {
E item = queue.poll();
consumer.accept(item);
List<E> childList = setSubChildren.apply(item);
if (childList != null && !childList.isEmpty()) {
queue.addAll(childList);
}
}
}
/**
* 后序遍历
*
* @param tree 需要遍历的树
* @param consumer 遍历后对单个元素的处理方法x-> System.out.println(x) System.out::println打印元素
* @param setSubChildren 设置下级数据方法Menu::getSubMenus,x->x.setSubMenus(null)
* @param <E> 泛型实体对象
*/
public static <E> void forPostOrder(List<E> tree, Consumer<E> consumer, Function<E, List<E>> setSubChildren) {
for (E item : tree) {
List<E> childList = setSubChildren.apply(item);
if (childList != null && !childList.isEmpty()) {
forPostOrder(childList, consumer, setSubChildren);
}
consumer.accept(item);
}
}
/**
* 对树所有子节点按comparator排序
*
* @param tree 需要排序的树
* @param comparator 排序规则ComparatorComparator.comparing(MenuVo::getRank)按Rank正序 ,(x,y)->y.getRank().compareTo(x.getRank())按Rank倒序
* @param getChildren 获取下级数据方法MenuVo::getSubMenus
* @param <E> 泛型实体对象
* @return 排序好的树
*/
public static <E> List<E> sort(List<E> tree, Comparator<? super E> comparator, Function<E, List<E>> getChildren) {
for (E item : tree) {
List<E> childList = getChildren.apply(item);
if (childList != null && !childList.isEmpty()) {
sort(childList, comparator, getChildren);
}
}
tree.sort(comparator);
return tree;
}
private static <E> List<E> makeChildren(E parent, List<E> allData, BiFunction<E, E, Boolean> parentCheck, BiConsumer<E, List<E>> children) {
return allData.stream().filter(x -> parentCheck.apply(parent, x)).peek(x -> children.accept(x, makeChildren(x, allData, parentCheck, children))).collect(Collectors.toList());
}
}

View File

@ -0,0 +1,31 @@
package com.zsc.edu.gateway.modules.notice.entity;
import com.baomidou.mybatisplus.annotation.TableName;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
/**
* @author zhuang
*/
@Setter
@Getter
@NoArgsConstructor
@AllArgsConstructor
@TableName("sys_bulletin_user")
public class BulletinUser {
/**
* 公告ID
*/
public Long bulletinId;
/**
* 用户ID
*/
public Long userId;
/**
* 是否已读
*/
public Boolean isRead = true;
}

View File

@ -0,0 +1,15 @@
package com.zsc.edu.gateway.modules.notice.mapper;
import com.zsc.edu.gateway.common.mapstruct.BaseMapper;
import com.zsc.edu.gateway.modules.notice.dto.BulletinAttachmentDto;
import com.zsc.edu.gateway.modules.notice.entity.BulletinAttachment;
import org.mapstruct.Mapper;
import org.mapstruct.ReportingPolicy;
/**
* @author zhuang
*/
@Mapper(componentModel = "spring", unmappedTargetPolicy = ReportingPolicy.IGNORE)
public interface BulletinAttachmentMapper extends BaseMapper<BulletinAttachmentDto, BulletinAttachment> {
}

View File

@ -0,0 +1,16 @@
package com.zsc.edu.gateway.modules.notice.repo;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.zsc.edu.gateway.modules.notice.entity.BulletinUser;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
/**
* @author zhuang
*/
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);
}

View File

@ -0,0 +1,16 @@
package com.zsc.edu.gateway.modules.notice.service;
import com.baomidou.mybatisplus.extension.service.IService;
import com.zsc.edu.gateway.framework.security.UserDetailsImpl;
import com.zsc.edu.gateway.modules.notice.entity.BulletinUser;
/**
* @author zhuang
*/
public interface BulletinUserService extends IService<BulletinUser> {
Boolean isRead(UserDetailsImpl userDetails, Long id);
void toggleIsRead(Long id);
}

View File

@ -0,0 +1,61 @@
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;
import com.zsc.edu.gateway.modules.notice.entity.BulletinUser;
import com.zsc.edu.gateway.modules.notice.repo.BulletinUserRepository;
import com.zsc.edu.gateway.modules.notice.service.BulletinUserService;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;
/**
* @author zhuang
*/
@AllArgsConstructor
@Service
public class BulletinUserServiceImpl extends ServiceImpl<BulletinUserRepository, BulletinUser> implements BulletinUserService {
/**
* 已读公告每次已读自动获取用户id与公告id加入联表
*
* @param userDetails userDetails
* @param id id
* return true
*/
@Override
public Boolean isRead(UserDetailsImpl userDetails, Long id) {
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;
}
/**
* 更新公告后修改已读状态
*
* @param id id
*/
@Override
public void toggleIsRead(Long id) {
UpdateWrapper<BulletinUser> updateWrapper = new UpdateWrapper<>();
updateWrapper.eq("bulletin_id", id).set("is_read", true);
}
}

View File

@ -0,0 +1,26 @@
package com.zsc.edu.gateway.modules.system.vo;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.zsc.edu.gateway.modules.system.entity.User;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import java.util.ArrayList;
import java.util.List;
/**
* @author zhuang
*/
@Data
@AllArgsConstructor
@NoArgsConstructor
public class DeptTree {
private Long id;
@JsonIgnore
private Long pid;
private String name;
List<DeptTree> children = new ArrayList<>();
List<UserTree> members = new ArrayList<>();
}

View File

@ -0,0 +1,16 @@
package com.zsc.edu.gateway.modules.system.vo;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
/**
* @author zhuang
*/
@Data
@AllArgsConstructor
@NoArgsConstructor
public class UserTree {
private Long id;
private String name;
}