Skip to content

Commit

Permalink
refactor: 优化通知公告部分代码
Browse files Browse the repository at this point in the history
  • Loading branch information
Charles7c committed Nov 5, 2024
1 parent 4c36f23 commit e1941ec
Show file tree
Hide file tree
Showing 10 changed files with 74 additions and 46 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ public static String getNickname(Long userId) {
*
* @return 是否为管理员
*/
public static Boolean isAdmin() {
public static boolean isAdmin() {
StpUtil.checkLogin();
return getContext().isAdmin();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ public interface NoticeMapper extends BaseMapper<NoticeDO> {
/**
* 查询仪表盘公告列表
*
* @param userId 用户 ID
* @return 仪表盘公告列表
*/
List<DashboardNoticeResp> selectDashboardList(@Param("userId") Long userId);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
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;
Expand Down Expand Up @@ -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;

/**
* 指定用户
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
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;
Expand Down Expand Up @@ -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;

/**
* 指定用户
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -128,13 +128,6 @@ public PageResp<UserResp> page(UserQuery query, PageQuery pageQuery) {
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 = "新增失败,[{}] 已存在";
Expand Down
27 changes: 7 additions & 20 deletions continew-module-system/src/main/resources/mapper/NoticeMapper.xml
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
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;
Expand Down Expand Up @@ -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(), "请选择通知用户");
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -46,12 +46,10 @@
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
Expand All @@ -63,16 +61,12 @@
@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()));
Expand Down

0 comments on commit e1941ec

Please sign in to comment.