refactor: 优化通知公告部分代码

This commit is contained in:
2024-11-05 23:21:40 +08:00
parent 4c36f23398
commit e1941eca45
10 changed files with 74 additions and 46 deletions

View File

@@ -178,7 +178,7 @@ public class UserContextHolder {
*
* @return 是否为管理员
*/
public static Boolean isAdmin() {
public static boolean isAdmin() {
StpUtil.checkLogin();
return getContext().isAdmin();
}

View File

@@ -0,0 +1,45 @@
/*
* Copyright (c) 2022-present Charles7c Authors. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package top.continew.admin.system.enums;
import lombok.Getter;
import lombok.RequiredArgsConstructor;
import top.continew.starter.core.enums.BaseEnum;
/**
* 公告通知范围枚举
*
* @author Charles7c
* @since 2023/8/20 10:55
*/
@Getter
@RequiredArgsConstructor
public enum NoticeScopeEnum implements BaseEnum<Integer> {
/**
* 所有人
*/
ALL(1, "所有人"),
/**
* 指定用户
*/
USER(2, "指定用户"),;
private final Integer value;
private final String description;
}

View File

@@ -34,6 +34,7 @@ public interface NoticeMapper extends BaseMapper<NoticeDO> {
/**
* 查询仪表盘公告列表
*
* @param userId 用户 ID
* @return 仪表盘公告列表
*/
List<DashboardNoticeResp> selectDashboardList(@Param("userId") Long userId);

View File

@@ -68,8 +68,8 @@ public class UserQuery implements Serializable {
private Long deptId;
/**
* 用户 IDS
* 用户 ID 列表
*/
@Schema(description = "用户 ID数组", example = "[1,2,3]")
@Schema(description = "用户 ID 列表", example = "[1,2,3]")
private List<Long> userIds;
}

View File

@@ -22,6 +22,7 @@ import jakarta.validation.constraints.NotBlank;
import jakarta.validation.constraints.NotNull;
import lombok.Data;
import org.hibernate.validator.constraints.Length;
import top.continew.admin.system.enums.NoticeScopeEnum;
import top.continew.starter.extension.crud.model.req.BaseReq;
import java.io.Serial;
@@ -80,9 +81,9 @@ public class NoticeReq extends BaseReq {
/**
* 通知范围
*/
@Schema(description = "通知范围(1.所有人 2.指定用户)", example = "1")
@Schema(description = "通知范围", example = "2")
@NotNull(message = "通知范围不能为空")
private Integer noticeScope;
private NoticeScopeEnum noticeScope;
/**
* 指定用户

View File

@@ -20,6 +20,7 @@ import com.alibaba.excel.annotation.ExcelIgnoreUnannotated;
import com.alibaba.excel.annotation.ExcelProperty;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Data;
import top.continew.admin.system.enums.NoticeScopeEnum;
import top.continew.starter.extension.crud.model.resp.BaseDetailResp;
import java.io.Serial;
@@ -78,8 +79,8 @@ public class NoticeDetailResp extends BaseDetailResp {
/**
* 通知范围
*/
@Schema(description = "通知范围(1.所有人 2.指定用户)", example = "1")
private Integer noticeScope;
@Schema(description = "通知范围", example = "2")
private NoticeScopeEnum noticeScope;
/**
* 指定用户

View File

@@ -128,13 +128,6 @@ public class UserServiceImpl extends BaseServiceImpl<UserMapper, UserDO, UserRes
return pageResp;
}
@Override
public List<UserResp> list(UserQuery query, SortQuery sortQuery) {
QueryWrapper<UserDO> queryWrapper = this.buildQueryWrapper(query);
List<UserDetailResp> entityList = baseMapper.selectUserList(queryWrapper);
return BeanUtil.copyToList(entityList, UserResp.class);
}
@Override
public void beforeAdd(UserReq req) {
final String errorMsgTemplate = "新增失败,[{}] 已存在";

View File

@@ -5,27 +5,14 @@
<select id="selectDashboardList"
resultType="top.continew.admin.system.model.resp.dashboard.DashboardNoticeResp">
SELECT
id,
title,
type
FROM
sys_notice
WHERE
( effective_time IS NULL OR NOW() > effective_time )
AND (
terminate_time IS NULL
OR terminate_time > NOW())
id, title, type
FROM sys_notice
WHERE (effective_time IS NULL OR NOW() > effective_time)
AND (terminate_time IS NULL OR terminate_time > NOW())
<if test="userId != null">
AND ( notice_scope = 1
OR (
notice_scope = 2 AND
<!--转化为字符串类型,因为存储的也是字符串类型-->
JSON_EXTRACT(notice_users, "$[0]") = CAST(#{userId} AS CHAR)
))
AND (notice_scope = 1 OR (notice_scope = 2 AND JSON_EXTRACT(notice_users, "$[0]") = CAST(#{userId} AS CHAR)))
</if>
ORDER BY
sort ASC,
effective_time DESC
LIMIT 5;
ORDER BY sort ASC, effective_time DESC
LIMIT 5
</select>
</mapper>

View File

@@ -21,6 +21,7 @@ import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
import top.continew.admin.system.enums.NoticeScopeEnum;
import top.continew.admin.system.model.query.NoticeQuery;
import top.continew.admin.system.model.req.NoticeReq;
import top.continew.admin.system.model.resp.NoticeDetailResp;
@@ -48,26 +49,31 @@ public class NoticeController extends BaseController<NoticeService, NoticeResp,
@Override
public BaseIdResp<Long> add(@Validated(ValidateGroup.Crud.Add.class) @RequestBody NoticeReq req) {
this.checkTime(req);
this.check(req);
return super.add(req);
}
@Override
public void update(@Validated(ValidateGroup.Crud.Update.class) @RequestBody NoticeReq req, @PathVariable Long id) {
this.checkTime(req);
this.check(req);
super.update(req, id);
}
/**
* 检查时间
* 校验
*
* @param req 创建或修改信息
*/
private void checkTime(NoticeReq req) {
private void check(NoticeReq req) {
// 校验生效时间
LocalDateTime effectiveTime = req.getEffectiveTime();
LocalDateTime terminateTime = req.getTerminateTime();
if (null != effectiveTime && null != terminateTime) {
ValidationUtils.throwIf(terminateTime.isBefore(effectiveTime), "终止时间必须晚于生效时间");
}
// 校验通知范围
if (NoticeScopeEnum.USER.equals(req.getNoticeScope())) {
ValidationUtils.throwIfEmpty(req.getNoticeUsers(), "请选择通知用户");
}
}
}

View File

@@ -46,12 +46,10 @@ import top.continew.starter.core.util.validate.ValidationUtils;
import top.continew.starter.extension.crud.annotation.CrudRequestMapping;
import top.continew.starter.extension.crud.controller.BaseController;
import top.continew.starter.extension.crud.enums.Api;
import top.continew.starter.extension.crud.model.query.SortQuery;
import top.continew.starter.extension.crud.model.resp.BaseIdResp;
import top.continew.starter.extension.crud.util.ValidateGroup;
import java.io.IOException;
import java.util.List;
/**
* 用户管理 API
@@ -63,16 +61,12 @@ import java.util.List;
@Validated
@RestController
@RequiredArgsConstructor
@CrudRequestMapping(value = "/system/user", api = {Api.PAGE, Api.GET, Api.ADD, Api.UPDATE, Api.DELETE, Api.EXPORT})
@CrudRequestMapping(value = "/system/user", api = {Api.PAGE, Api.LIST, Api.GET, Api.ADD, Api.UPDATE, Api.DELETE,
Api.EXPORT})
public class UserController extends BaseController<UserService, UserResp, UserDetailResp, UserQuery, UserReq> {
private final UserService userService;
@Override
public List<UserResp> list(UserQuery query, SortQuery sortQuery) {
return super.list(query, sortQuery);
}
@Override
public BaseIdResp<Long> add(@Validated(ValidateGroup.Crud.Add.class) @RequestBody UserReq req) {
String rawPassword = ExceptionUtils.exToNull(() -> SecureUtils.decryptByRsaPrivateKey(req.getPassword()));