完成了基本的商品管理

This commit is contained in:
chen-xin-zhi 2024-10-29 18:40:03 +08:00
parent 7e1b0b0b04
commit 6439c3eb0b
14 changed files with 107 additions and 146 deletions

View File

@ -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<Page<Good>> listGoodByPage(@RequestBody )
/**
* 分页获取商品列表
* @param goodQueryRequest 查询商品请求体
* @return 商品信息列表
*/
@PostMapping("/list/page")
// @AuthCheck(mustRole = UserConstant.ADMIN_ROLE)
public BaseResponse<Page<Good>> listGoodByPage(@RequestBody GoodQueryRequest goodQueryRequest) {
long current = goodQueryRequest.getCurrent();
long pageSize = goodQueryRequest.getPageSize();
QueryWrapper<Good> goodQueryWrapper = goodService.getGoodQueryWrapper(goodQueryRequest);
Page<Good> page = goodService.page(new Page<>(current, pageSize), goodQueryWrapper);
return ResultUtils.success(page);
}
// @PostMapping("/delBatch")
// @AuthCheck(mustRole = UserConstant.ADMIN_ROLE)
// public BaseResponse<Boolean> delBatchGoods(List<Integer> )
}

View File

@ -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<List<User>> listUser(){
return ResultUtils.success(userService.list());
}
//
// @GetMapping("/list")
// public BaseResponse<List<User>> 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)

View File

@ -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;
}

View File

@ -72,6 +72,12 @@ public class Good {
*/
private String reserveDate;
/**
* 是否上架
*/
private Integer isShelves;
@TableField(exist = false)
private static final long serialVersionUID = 1L;

View File

@ -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<Good> {
/**
* 获取查询条件
*/
QueryWrapper<Good> getGoodQueryWrapper(GoodQueryRequest goodQueryRequest);
}

View File

@ -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<GoodMapper, Good> implements GoodService {
@Override
public QueryWrapper<Good> 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<Good> 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;
}
}

View File

@ -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<String> 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("用户没有访问的权限");
}
}
}

View File

@ -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 可以匿名访问";
}
}

View File

@ -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<String, Set<String>> USER_ROLE_MAP = new HashMap<>();
static {
//用户zhangsan具有user和admin两个角色
Set<String> roleSet1 = Sets.newHashSet("admin", "user");
USER_ROLE_MAP.put("zhangsan", roleSet1);
Set<String> roleSet2 = Sets.newHashSet("user");
USER_ROLE_MAP.put("lisi", roleSet2);
}
}

View File

@ -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();
}

View File

@ -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;