From 6439c3eb0b7f3711d38c474a7dc2779fea381e03 Mon Sep 17 00:00:00 2001 From: chen-xin-zhi <3588068430@qq.com> Date: Tue, 29 Oct 2024 18:40:03 +0800 Subject: [PATCH] =?UTF-8?q?=E5=AE=8C=E6=88=90=E4=BA=86=E5=9F=BA=E6=9C=AC?= =?UTF-8?q?=E7=9A=84=E5=95=86=E5=93=81=E7=AE=A1=E7=90=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../operategood/GoodController.java | 31 +++++++---- .../controller/userinfo/UserController.java | 29 +++++----- .../model/dto/good/GoodQueryRequest.java | 29 +++++++++- .../cultural/heritage/model/entity/Good.java | 6 +++ .../service/operategood/GoodService.java | 7 +++ .../operategood/impl/GoodServiceImpl.java | 31 +++++++++++ .../com/cultural/heritage/aop/AopConfig.java | 54 ------------------- .../cultural/heritage/aop/AopController.java | 31 ----------- .../cultural/heritage/aop/CacheManager.java | 22 -------- .../com/cultural/heritage/aop/HasRole.java | 12 ----- .../cultural/heritage/aopTest/AopConfig.java | 1 - .../heritage/aopTest/AopController.java | 0 .../heritage/aopTest/CacheManager.java | 0 .../cultural/heritage/aopTest/HasRole.java | 0 14 files changed, 107 insertions(+), 146 deletions(-) delete mode 100644 src/test/java/com/cultural/heritage/aop/AopConfig.java delete mode 100644 src/test/java/com/cultural/heritage/aop/AopController.java delete mode 100644 src/test/java/com/cultural/heritage/aop/CacheManager.java delete mode 100644 src/test/java/com/cultural/heritage/aop/HasRole.java rename src/{main => test}/java/com/cultural/heritage/aopTest/AopConfig.java (98%) rename src/{main => test}/java/com/cultural/heritage/aopTest/AopController.java (100%) rename src/{main => test}/java/com/cultural/heritage/aopTest/CacheManager.java (100%) rename src/{main => test}/java/com/cultural/heritage/aopTest/HasRole.java (100%) diff --git a/src/main/java/com/cultural/heritage/controller/operategood/GoodController.java b/src/main/java/com/cultural/heritage/controller/operategood/GoodController.java index 1bc1401..9f16647 100644 --- a/src/main/java/com/cultural/heritage/controller/operategood/GoodController.java +++ b/src/main/java/com/cultural/heritage/controller/operategood/GoodController.java @@ -1,23 +1,21 @@ package com.cultural.heritage.controller.operategood; +import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; import com.baomidou.mybatisplus.extension.plugins.pagination.Page; -import com.cultural.heritage.annotation.AuthCheck; import com.cultural.heritage.common.BaseResponse; import com.cultural.heritage.common.ErrorCode; import com.cultural.heritage.common.ResultUtils; -import com.cultural.heritage.constant.UserConstant; -import com.cultural.heritage.controller.userinfo.UserController; import com.cultural.heritage.exception.BusinessException; import com.cultural.heritage.exception.ThrowUtils; import com.cultural.heritage.model.dto.CommonRequest; import com.cultural.heritage.model.dto.good.GoodAddRequest; +import com.cultural.heritage.model.dto.good.GoodQueryRequest; import com.cultural.heritage.model.dto.good.GoodUpdateRequest; import com.cultural.heritage.model.entity.Good; import com.cultural.heritage.service.operategood.GoodService; import io.swagger.v3.oas.annotations.tags.Tag; import jakarta.annotation.Resource; -import jakarta.servlet.http.HttpServletRequest; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.BeanUtils; import org.springframework.web.bind.annotation.PostMapping; @@ -25,8 +23,6 @@ import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; -import javax.xml.transform.Result; - /** * 商品接口 @@ -98,10 +94,27 @@ public class GoodController { } -// @PostMapping("/list/page") -//// @AuthCheck(mustRole = UserConstant.ADMIN_ROLE) -// public BaseResponse> listGoodByPage(@RequestBody ) + /** + * 分页获取商品列表 + * @param goodQueryRequest 查询商品请求体 + * @return 商品信息列表 + */ + @PostMapping("/list/page") +// @AuthCheck(mustRole = UserConstant.ADMIN_ROLE) + public BaseResponse> listGoodByPage(@RequestBody GoodQueryRequest goodQueryRequest) { + long current = goodQueryRequest.getCurrent(); + long pageSize = goodQueryRequest.getPageSize(); + QueryWrapper goodQueryWrapper = goodService.getGoodQueryWrapper(goodQueryRequest); + Page page = goodService.page(new Page<>(current, pageSize), goodQueryWrapper); + return ResultUtils.success(page); + } + + +// @PostMapping("/delBatch") +// @AuthCheck(mustRole = UserConstant.ADMIN_ROLE) +// public BaseResponse delBatchGoods(List ) + } diff --git a/src/main/java/com/cultural/heritage/controller/userinfo/UserController.java b/src/main/java/com/cultural/heritage/controller/userinfo/UserController.java index 00518a7..aceb999 100644 --- a/src/main/java/com/cultural/heritage/controller/userinfo/UserController.java +++ b/src/main/java/com/cultural/heritage/controller/userinfo/UserController.java @@ -41,21 +41,18 @@ public class UserController { @Resource private UserService userService; - @Resource - private RedisTemplate redisTemplate; - @GetMapping("/test") - public String test(){ - return "匠承非遗"; - } +// @GetMapping("/test") +// public String test(){ +// return "匠承非遗"; +// } - - - @GetMapping("/list") - public BaseResponse> listUser(){ - return ResultUtils.success(userService.list()); - } +// +// @GetMapping("/list") +// public BaseResponse> listUser(){ +// return ResultUtils.success(userService.list()); +// } /** @@ -204,10 +201,12 @@ public class UserController { return ResultUtils.success(count); } + + /** - * 分页获取用户列表(仅管理员) - * @param userQueryRequest - * @return + * 分页获取用户列表 + * @param userQueryRequest 查询用户请求体 + * @return 用户信息列表 */ @PostMapping("/list/page") // @AuthCheck(mustRole = UserConstant.ADMIN_ROLE) diff --git a/src/main/java/com/cultural/heritage/model/dto/good/GoodQueryRequest.java b/src/main/java/com/cultural/heritage/model/dto/good/GoodQueryRequest.java index f0ac787..323481c 100644 --- a/src/main/java/com/cultural/heritage/model/dto/good/GoodQueryRequest.java +++ b/src/main/java/com/cultural/heritage/model/dto/good/GoodQueryRequest.java @@ -1,10 +1,35 @@ package com.cultural.heritage.model.dto.good; +import com.baomidou.mybatisplus.annotation.TableField; import com.cultural.heritage.common.PageRequest; import lombok.Data; -@Data -public class GoodQueryRequest extends PageRequest { +import java.io.Serializable; +@Data +public class GoodQueryRequest extends PageRequest implements Serializable { + + /** + * id + */ + private Long id; + + /** + * 商品名 + */ + private String name; + + /** + * 商品类型 + */ + private String type; + + /** + * 节日限定序号 + */ + private Integer festivalOrder; + + @TableField(exist = false) + private static final long serialVersionUID = 1L; } diff --git a/src/main/java/com/cultural/heritage/model/entity/Good.java b/src/main/java/com/cultural/heritage/model/entity/Good.java index c42c4cb..6259722 100644 --- a/src/main/java/com/cultural/heritage/model/entity/Good.java +++ b/src/main/java/com/cultural/heritage/model/entity/Good.java @@ -72,6 +72,12 @@ public class Good { */ private String reserveDate; + /** + * 是否上架 + */ + private Integer isShelves; + + @TableField(exist = false) private static final long serialVersionUID = 1L; diff --git a/src/main/java/com/cultural/heritage/service/operategood/GoodService.java b/src/main/java/com/cultural/heritage/service/operategood/GoodService.java index 7b83ec9..77ca689 100644 --- a/src/main/java/com/cultural/heritage/service/operategood/GoodService.java +++ b/src/main/java/com/cultural/heritage/service/operategood/GoodService.java @@ -1,7 +1,14 @@ package com.cultural.heritage.service.operategood; +import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; import com.baomidou.mybatisplus.extension.service.IService; +import com.cultural.heritage.model.dto.good.GoodQueryRequest; import com.cultural.heritage.model.entity.Good; public interface GoodService extends IService { + + /** + * 获取查询条件 + */ + QueryWrapper getGoodQueryWrapper(GoodQueryRequest goodQueryRequest); } diff --git a/src/main/java/com/cultural/heritage/service/operategood/impl/GoodServiceImpl.java b/src/main/java/com/cultural/heritage/service/operategood/impl/GoodServiceImpl.java index 9bbaa88..2ebf2c5 100644 --- a/src/main/java/com/cultural/heritage/service/operategood/impl/GoodServiceImpl.java +++ b/src/main/java/com/cultural/heritage/service/operategood/impl/GoodServiceImpl.java @@ -1,11 +1,42 @@ package com.cultural.heritage.service.operategood.impl; +import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; +import com.cultural.heritage.common.ErrorCode; +import com.cultural.heritage.constant.CommonConstant; +import com.cultural.heritage.exception.BusinessException; import com.cultural.heritage.mapper.GoodMapper; +import com.cultural.heritage.model.dto.good.GoodQueryRequest; import com.cultural.heritage.model.entity.Good; +import com.cultural.heritage.model.entity.User; import com.cultural.heritage.service.operategood.GoodService; +import com.cultural.heritage.utils.SqlUtils; +import org.apache.commons.lang3.StringUtils; import org.springframework.stereotype.Service; @Service public class GoodServiceImpl extends ServiceImpl implements GoodService { + + @Override + public QueryWrapper getGoodQueryWrapper(GoodQueryRequest goodQueryRequest) { + if (goodQueryRequest == null) { + throw new BusinessException(ErrorCode.PARAMS_ERROR, "请求参数为空"); + } + Long id = goodQueryRequest.getId(); + String name = goodQueryRequest.getName(); + String type = goodQueryRequest.getType(); + Integer festivalOrder = goodQueryRequest.getFestivalOrder(); + String sortField = goodQueryRequest.getSortField(); + String sortOrder = goodQueryRequest.getSortOrder(); + + QueryWrapper queryWrapper = new QueryWrapper<>(); + queryWrapper.eq(id != null, "id", id); + queryWrapper.eq(StringUtils.isNotBlank(name), "name", name); + queryWrapper.eq(StringUtils.isNotBlank(type), "type", type); + queryWrapper.eq(festivalOrder != null, "festivalOrder", festivalOrder); + queryWrapper.orderBy(SqlUtils.validSortField(sortField), sortOrder.equals(CommonConstant.SORT_ORDER_ASC), + sortField); + return queryWrapper; + } + } diff --git a/src/test/java/com/cultural/heritage/aop/AopConfig.java b/src/test/java/com/cultural/heritage/aop/AopConfig.java deleted file mode 100644 index 9149de7..0000000 --- a/src/test/java/com/cultural/heritage/aop/AopConfig.java +++ /dev/null @@ -1,54 +0,0 @@ -package com.cultural.heritage.aop; - -import jakarta.servlet.http.HttpServletRequest; -import org.aspectj.lang.JoinPoint; -import org.aspectj.lang.annotation.Aspect; -import org.aspectj.lang.annotation.Before; -import org.aspectj.lang.annotation.Pointcut; -import org.aspectj.lang.reflect.MethodSignature; -import org.springframework.stereotype.Component; -import org.springframework.web.context.request.RequestAttributes; -import org.springframework.web.context.request.RequestContextHolder; -import org.springframework.web.context.request.ServletRequestAttributes; - -import java.lang.reflect.Method; -import java.util.Set; - -@Component -@Aspect -public class AopConfig { - - @Pointcut("@annotation(com.cultural.heritage.aop.HasRole)") - public void pointcut(){} - - @Before("pointcut()") - public void before(JoinPoint joinPoint) { - System.out.println("before-----------------"); - - //获取到HttpServletRequest - RequestAttributes requestAttributes = RequestContextHolder.getRequestAttributes(); - ServletRequestAttributes servletRequestAttributes = (ServletRequestAttributes) requestAttributes; - HttpServletRequest request = servletRequestAttributes.getRequest(); - - String username = request.getParameter("username"); - - //获取当前用户角色的集合 - Set userRoles = CacheManager.USER_ROLE_MAP.get(username); - - //获取当前请求的方法的签名 - MethodSignature signature = (MethodSignature) joinPoint.getSignature(); - - //反射获取当前被调用的方法 - Method method = signature.getMethod(); - - //判断当前方法是否有hasRole注解 - //如果有,判断用户是否具有注解属性中要求的角色 - //如果没有hasRole注解,那么说明方法不需要判断用户的角色,可以匿名访问 - - HasRole hasRole = method.getDeclaredAnnotation(HasRole.class); - if (hasRole != null && (userRoles != null || !userRoles.contains(hasRole.value()))) { - throw new RuntimeException("用户没有访问的权限"); - } - - } -} diff --git a/src/test/java/com/cultural/heritage/aop/AopController.java b/src/test/java/com/cultural/heritage/aop/AopController.java deleted file mode 100644 index 313af66..0000000 --- a/src/test/java/com/cultural/heritage/aop/AopController.java +++ /dev/null @@ -1,31 +0,0 @@ -package com.cultural.heritage.aop; - -import org.springframework.web.bind.annotation.GetMapping; -import org.springframework.web.bind.annotation.RequestMapping; -import org.springframework.web.bind.annotation.RestController; - -@RestController -@RequestMapping("/aop") -public class AopController { - - - //调用这个方法需要用户具有admin的角色 - @HasRole("admin") - @GetMapping("query1") - public String query1() { - return "query1 需要 admin 角色"; - } - - //调用这个方法需要用户具有user的角色 - @HasRole("user") - @GetMapping("query2") - public String query2() { - return "query2 需要 user 角色"; - } - - // 任何用户都可以访问 - @GetMapping("query3") - public String query3() { - return "query3 可以匿名访问"; - } -} diff --git a/src/test/java/com/cultural/heritage/aop/CacheManager.java b/src/test/java/com/cultural/heritage/aop/CacheManager.java deleted file mode 100644 index 9cef35c..0000000 --- a/src/test/java/com/cultural/heritage/aop/CacheManager.java +++ /dev/null @@ -1,22 +0,0 @@ -package com.cultural.heritage.aop; - -import com.google.common.collect.Sets; - -import java.util.HashMap; -import java.util.Map; -import java.util.Set; - -public class CacheManager { - - public static final Map> USER_ROLE_MAP = new HashMap<>(); - - static { - //用户zhangsan具有user和admin两个角色 - Set roleSet1 = Sets.newHashSet("admin", "user"); - USER_ROLE_MAP.put("zhangsan", roleSet1); - - - Set roleSet2 = Sets.newHashSet("user"); - USER_ROLE_MAP.put("lisi", roleSet2); - } -} diff --git a/src/test/java/com/cultural/heritage/aop/HasRole.java b/src/test/java/com/cultural/heritage/aop/HasRole.java deleted file mode 100644 index 6f4f99b..0000000 --- a/src/test/java/com/cultural/heritage/aop/HasRole.java +++ /dev/null @@ -1,12 +0,0 @@ -package com.cultural.heritage.aop; - -import java.lang.annotation.ElementType; -import java.lang.annotation.Retention; -import java.lang.annotation.RetentionPolicy; -import java.lang.annotation.Target; - -@Target(ElementType.METHOD) -@Retention(RetentionPolicy.RUNTIME) -public @interface HasRole { - String value(); -} diff --git a/src/main/java/com/cultural/heritage/aopTest/AopConfig.java b/src/test/java/com/cultural/heritage/aopTest/AopConfig.java similarity index 98% rename from src/main/java/com/cultural/heritage/aopTest/AopConfig.java rename to src/test/java/com/cultural/heritage/aopTest/AopConfig.java index ad7cbef..0abebd2 100644 --- a/src/main/java/com/cultural/heritage/aopTest/AopConfig.java +++ b/src/test/java/com/cultural/heritage/aopTest/AopConfig.java @@ -1,7 +1,6 @@ package com.cultural.heritage.aopTest; import jakarta.servlet.http.HttpServletRequest; -import lombok.extern.log4j.Log4j; import lombok.extern.slf4j.Slf4j; import org.aspectj.lang.JoinPoint; import org.aspectj.lang.annotation.Aspect; diff --git a/src/main/java/com/cultural/heritage/aopTest/AopController.java b/src/test/java/com/cultural/heritage/aopTest/AopController.java similarity index 100% rename from src/main/java/com/cultural/heritage/aopTest/AopController.java rename to src/test/java/com/cultural/heritage/aopTest/AopController.java diff --git a/src/main/java/com/cultural/heritage/aopTest/CacheManager.java b/src/test/java/com/cultural/heritage/aopTest/CacheManager.java similarity index 100% rename from src/main/java/com/cultural/heritage/aopTest/CacheManager.java rename to src/test/java/com/cultural/heritage/aopTest/CacheManager.java diff --git a/src/main/java/com/cultural/heritage/aopTest/HasRole.java b/src/test/java/com/cultural/heritage/aopTest/HasRole.java similarity index 100% rename from src/main/java/com/cultural/heritage/aopTest/HasRole.java rename to src/test/java/com/cultural/heritage/aopTest/HasRole.java