forked from gushen/sunway-user-system
实现了前端新增需求
This commit is contained in:
@@ -5,6 +5,8 @@ package icu.sunway.ai_spring_example.Common.Constant;
|
||||
*/
|
||||
public class MessageConstant {
|
||||
public static final String ALREADY_EXISTS = "账号已存在";
|
||||
public static final String USERNAME_ALREADY_EXISTS = "用户名已经存在";
|
||||
public static final String PHONE_ALREADY_EXISTS = "手机号已经被注册";
|
||||
public static final String PASSWORD_ERROR = "密码错误";
|
||||
public static final String ACCOUNT_NOT_FOUND = "账号不存在";
|
||||
public static final String ACCOUNT_LOCKED = "账号被锁定";
|
||||
|
||||
@@ -81,6 +81,33 @@ public class ProductController {
|
||||
return ResponseEntity.success(result);
|
||||
}
|
||||
|
||||
/**
|
||||
* 商家分页查询商品列表
|
||||
*/
|
||||
@GetMapping("/page/merchant")
|
||||
public ResponseEntity<Map<String, Object>> pageByUser(
|
||||
@RequestParam(defaultValue = "1") Integer page,
|
||||
@RequestParam(defaultValue = "10") Integer size,
|
||||
Long categoryId,
|
||||
String name,
|
||||
Integer status,
|
||||
@RequestParam Long currentUserId
|
||||
) {
|
||||
|
||||
log.info("商家分页查询自己的商品列表: page={}, size={}, categoryId={}, name={}, status={}, userId={}",
|
||||
page, size, categoryId, name, status, currentUserId);
|
||||
|
||||
Page<ProductListVO> productPage = productService.getProductPageByUser(
|
||||
page, size, categoryId, name, status, currentUserId);
|
||||
|
||||
Map<String, Object> result = new HashMap<>();
|
||||
result.put("list", productPage.getRecords());
|
||||
result.put("total", productPage.getTotal());
|
||||
result.put("page", page);
|
||||
result.put("size", size);
|
||||
return ResponseEntity.success(result);
|
||||
}
|
||||
|
||||
/**
|
||||
* 查询商品详情
|
||||
*/
|
||||
|
||||
@@ -1,7 +1,9 @@
|
||||
package icu.sunway.ai_spring_example.Controllers;
|
||||
|
||||
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
|
||||
import icu.sunway.ai_spring_example.Common.Constant.DeletedConstant;
|
||||
import icu.sunway.ai_spring_example.Common.Constant.JwtClaimsConstant;
|
||||
import icu.sunway.ai_spring_example.Common.Constant.MessageConstant;
|
||||
import icu.sunway.ai_spring_example.Common.Constant.StatusConstant;
|
||||
import icu.sunway.ai_spring_example.Common.Properties.JwtProperties;
|
||||
import icu.sunway.ai_spring_example.Common.Response.ResponseEntity;
|
||||
@@ -39,6 +41,7 @@ public class UserController {
|
||||
|
||||
@Autowired
|
||||
private UserAddressService userAddressService;
|
||||
|
||||
@PostMapping("/login")
|
||||
public ResponseEntity<UserLoginVO> login(@RequestBody UserDTO userDTO){
|
||||
log.info("用户登录:{}",userDTO);
|
||||
@@ -57,6 +60,7 @@ public class UserController {
|
||||
.id(user.getId())
|
||||
.username(user.getUsername())
|
||||
.nickname(user.getNickname())
|
||||
.isAdmin(user.getIsAdmin())
|
||||
.token(token)
|
||||
.build();
|
||||
|
||||
@@ -73,6 +77,20 @@ public class UserController {
|
||||
log.info("用户注册:{}",userDTO);
|
||||
User user = new User();
|
||||
BeanUtils.copyProperties(userDTO,user);
|
||||
//保证用户名唯一
|
||||
QueryWrapper<User>queryWrapper = new QueryWrapper<>();
|
||||
queryWrapper.eq("username",user.getUsername().trim());
|
||||
User existUser = userService.getOne(queryWrapper);
|
||||
if(existUser != null){
|
||||
return ResponseEntity.error(MessageConstant.USERNAME_ALREADY_EXISTS);
|
||||
}
|
||||
//保证手机号唯一
|
||||
QueryWrapper<User> queryWrapper1 = new QueryWrapper<>();
|
||||
queryWrapper1.eq("phone",user.getPhone());
|
||||
if(userService.count(queryWrapper1) > 0){
|
||||
return ResponseEntity.error(MessageConstant.PHONE_ALREADY_EXISTS);
|
||||
}
|
||||
|
||||
user.setStatus(StatusConstant.ENABLE);
|
||||
user.setIsDeleted(DeletedConstant.NOT_DELETED);
|
||||
user.setPassword(DigestUtils.md5DigestAsHex(userDTO.getPassword().getBytes()));
|
||||
@@ -123,7 +141,11 @@ public class UserController {
|
||||
userAddressService.deleteAddress(id, userId);
|
||||
return ResponseEntity.success("地址删除成功");
|
||||
}
|
||||
|
||||
@GetMapping("/admin")
|
||||
public ResponseEntity<List<User>> getUserList() {
|
||||
log.info("开始查询用户列表");
|
||||
return ResponseEntity.success(userService.getUserList());
|
||||
}
|
||||
/**
|
||||
* 查询用户地址列表
|
||||
*/
|
||||
|
||||
@@ -26,4 +26,6 @@ public interface ProductMapper extends BaseMapper<Product> {
|
||||
@Param("name") String name,
|
||||
@Param("status") Integer status
|
||||
);
|
||||
|
||||
IPage<ProductListVO> selectProductPageByUser(Page<ProductListVO> pageInfo, Long categoryId, String name, Integer status);
|
||||
}
|
||||
@@ -15,6 +15,4 @@ public interface UserMapper extends BaseMapper<User> {
|
||||
*/
|
||||
@Select("select * from sys_user where username = #{username}")
|
||||
User getByUsername(String username);
|
||||
|
||||
void update(User user);
|
||||
}
|
||||
|
||||
@@ -98,6 +98,24 @@ public class ProductServiceImpl extends ServiceImpl<ProductMapper, Product> impl
|
||||
IPage<ProductListVO> result = productMapper.selectProductPage(pageInfo, categoryId, name, status);
|
||||
return (Page<ProductListVO>) result;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Page<ProductListVO> getProductPageByUser(Integer page, Integer size, Long categoryId, String name,
|
||||
Integer status, Long currentUserId) {
|
||||
// 参数校验
|
||||
if (currentUserId == null) {
|
||||
throw new BusinessException(MessageConstant.PARAM_ERROR);
|
||||
}
|
||||
|
||||
Page<ProductListVO> pageInfo = new Page<>(page, size);
|
||||
IPage<ProductListVO> result = productMapper.selectProductPageByUser(
|
||||
pageInfo,
|
||||
categoryId,
|
||||
name,
|
||||
status
|
||||
);
|
||||
return (Page<ProductListVO>) result;
|
||||
}
|
||||
@Override
|
||||
public ProductVO getProductDetail(Long id) {
|
||||
QueryWrapper<Product> queryWrapper = new QueryWrapper<>();
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
package icu.sunway.ai_spring_example.Service.Impl;
|
||||
|
||||
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
|
||||
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
||||
import icu.sunway.ai_spring_example.Common.Constant.MessageConstant;
|
||||
import icu.sunway.ai_spring_example.Common.Constant.StatusConstant;
|
||||
@@ -8,12 +9,12 @@ import icu.sunway.ai_spring_example.Mapper.UserMapper;
|
||||
import icu.sunway.ai_spring_example.Service.UserService;
|
||||
import icu.sunway.ai_spring_example.pojo.Dto.UserDTO;
|
||||
import icu.sunway.ai_spring_example.pojo.Entity.User;
|
||||
import icu.sunway.ai_spring_example.pojo.Vo.UserVO;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.beans.BeanUtils;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.util.DigestUtils;
|
||||
import java.util.List;
|
||||
|
||||
|
||||
@Service
|
||||
@@ -22,6 +23,7 @@ public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements Us
|
||||
|
||||
@Autowired
|
||||
private UserMapper userMapper;
|
||||
|
||||
@Override
|
||||
public User login(UserDTO userDTO) {
|
||||
String username = userDTO.getUsername();
|
||||
@@ -43,14 +45,27 @@ public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements Us
|
||||
throw new AccountNotFoundException(MessageConstant.ACCOUNT_LOCKED);
|
||||
}
|
||||
|
||||
QueryWrapper<User> queryWrapper = new QueryWrapper<>();
|
||||
queryWrapper.eq("username",user.getUsername())
|
||||
.eq("password",user.getPassword());
|
||||
user = userMapper.selectOne(queryWrapper);
|
||||
return user;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void update(UserDTO userDTO) {
|
||||
User user = new User();
|
||||
if(userDTO.getPassword() != null)userDTO.setPassword(DigestUtils.md5DigestAsHex(userDTO.getPassword().getBytes()));
|
||||
BeanUtils.copyProperties(userDTO,user);
|
||||
userMapper.update(user);
|
||||
QueryWrapper<User> queryWrapper = new QueryWrapper<>();
|
||||
queryWrapper.eq("id",user.getId());
|
||||
userMapper.update(user,queryWrapper);
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<User> getUserList() {
|
||||
QueryWrapper<User> queryWrapper = new QueryWrapper<>();
|
||||
return userMapper.selectList(queryWrapper);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -32,6 +32,19 @@ public interface ProductService extends IService<Product> {
|
||||
*/
|
||||
Page<ProductListVO> getProductPage(Integer page, Integer size, Long categoryId, String name, Integer status);
|
||||
|
||||
/**
|
||||
* 商家/管理员分页查询商品列表(商家仅能查看自己的商品)
|
||||
* @param page 页码
|
||||
* @param size 每页条数
|
||||
* @param categoryId 分类ID
|
||||
* @param name 商品名称
|
||||
* @param status 状态
|
||||
* @param currentUserId 当前登录用户ID
|
||||
* @return 分页结果
|
||||
*/
|
||||
Page<ProductListVO> getProductPageByUser(Integer page, Integer size, Long categoryId, String name, Integer status,
|
||||
Long currentUserId);
|
||||
|
||||
/**
|
||||
* 根据ID查询商品详情
|
||||
* @param id 商品ID
|
||||
|
||||
@@ -5,6 +5,8 @@ import icu.sunway.ai_spring_example.pojo.Dto.UserDTO;
|
||||
import icu.sunway.ai_spring_example.pojo.Entity.User;
|
||||
import icu.sunway.ai_spring_example.pojo.Vo.UserVO;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public interface UserService extends IService<User> {
|
||||
|
||||
/*
|
||||
@@ -19,4 +21,5 @@ public interface UserService extends IService<User> {
|
||||
*/
|
||||
void update(UserDTO userDTO);
|
||||
|
||||
List<User> getUserList();
|
||||
}
|
||||
|
||||
@@ -8,6 +8,7 @@ import java.util.List;
|
||||
public class ProductDTO {
|
||||
private Long id; // 商品ID(修改时使用)
|
||||
private Long categoryId; // 分类ID
|
||||
private Long userId;// 商家ID
|
||||
private String name; // 商品名称
|
||||
private String subtitle; // 副标题
|
||||
private String mainImage; // 主图URL
|
||||
|
||||
@@ -12,4 +12,6 @@ public class UserDTO {
|
||||
private String avatar;
|
||||
private String nickname;
|
||||
private Integer gender;
|
||||
private Integer status;
|
||||
private Integer isAdmin;
|
||||
}
|
||||
|
||||
@@ -26,6 +26,11 @@ public class Product {
|
||||
*/
|
||||
private Long categoryId;
|
||||
|
||||
/**
|
||||
* 商家ID
|
||||
*/
|
||||
private Long userId;
|
||||
|
||||
/**
|
||||
* 商品名称
|
||||
*/
|
||||
|
||||
@@ -75,4 +75,9 @@ public class User {
|
||||
*/
|
||||
@TableLogic
|
||||
private Integer isDeleted;
|
||||
|
||||
/**
|
||||
* 是否为管理员 0-否 1-是
|
||||
*/
|
||||
private Integer isAdmin;
|
||||
}
|
||||
|
||||
@@ -14,5 +14,7 @@ public class ProductListVO {
|
||||
private BigDecimal price;
|
||||
private Integer sales;
|
||||
private Integer status;
|
||||
private Integer stock;
|
||||
private Integer userId;
|
||||
private String categoryName;
|
||||
}
|
||||
@@ -14,6 +14,7 @@ import java.util.List;
|
||||
public class ProductVO {
|
||||
private Long id;
|
||||
private Long categoryId;
|
||||
private Long userId;//商家ID
|
||||
private String categoryName; // 分类名称
|
||||
private String name;
|
||||
private String subtitle;
|
||||
@@ -25,7 +26,6 @@ public class ProductVO {
|
||||
private Integer sales;
|
||||
private Integer status;
|
||||
private Integer sort;
|
||||
|
||||
private LocalDateTime createTime;
|
||||
private LocalDateTime updateTime;
|
||||
private List<ProductSkuVO> skuList; // 规格列表
|
||||
|
||||
@@ -13,5 +13,6 @@ public class UserLoginVO {
|
||||
private Long id;
|
||||
private String username;
|
||||
private String nickname;
|
||||
private Integer isAdmin;
|
||||
private String token;
|
||||
}
|
||||
|
||||
@@ -4,10 +4,12 @@
|
||||
|
||||
<select id="selectProductPage" resultType="icu.sunway.ai_spring_example.pojo.Vo.ProductListVO">
|
||||
SELECT
|
||||
p.id, p.name, p.main_image as mainImage, p.price, p.sales, p.status,
|
||||
p.id, p.name, p.main_image as mainImage, p.price, p.sales, p.status, p.stock, p.user_id as userId,
|
||||
u.nickname as userName,
|
||||
c.name as categoryName
|
||||
FROM product p
|
||||
LEFT JOIN product_category c ON p.category_id = c.id
|
||||
LEFT JOIN sys_user u ON p.user_id = u.id
|
||||
WHERE p.is_deleted = 0
|
||||
<if test="categoryId != null">
|
||||
AND (
|
||||
@@ -22,4 +24,31 @@
|
||||
ORDER BY p.sort ASC, p.create_time DESC
|
||||
</select>
|
||||
|
||||
|
||||
<select id="selectProductPageByUser" resultType="icu.sunway.ai_spring_example.pojo.Vo.ProductListVO">
|
||||
SELECT
|
||||
p.id,
|
||||
p.name,
|
||||
p.main_image AS mainImage,
|
||||
p.price,
|
||||
p.sales,
|
||||
p.status,
|
||||
p.stock,
|
||||
p.user_id AS userId,
|
||||
u.nickname AS userName,
|
||||
c.name AS categoryName
|
||||
FROM product p
|
||||
LEFT JOIN product_category c ON p.category_id = c.id
|
||||
LEFT JOIN sys_user u ON p.user_id = u.id
|
||||
WHERE p.is_deleted = 0
|
||||
<!-- 分类过滤 -->
|
||||
<if test="categoryId != null">AND p.category_id = #{categoryId}</if>
|
||||
<!-- 商品名称模糊查询 -->
|
||||
<if test="name != null and name != ''">AND p.name LIKE CONCAT('%', #{name}, '%')</if>
|
||||
<!-- 状态过滤 -->
|
||||
<if test="status != null">AND p.status = #{status}</if>
|
||||
<!-- 核心:商家只能看自己的商品,管理员不限 -->
|
||||
<if test="userId != null">AND p.user_id = #{userId}</if>
|
||||
ORDER BY p.create_time DESC
|
||||
</select>
|
||||
</mapper>
|
||||
|
||||
@@ -1,19 +0,0 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
|
||||
<mapper namespace="icu.sunway.ai_spring_example.Mapper.UserMapper">
|
||||
|
||||
<update id="update" parameterType="icu.sunway.ai_spring_example.pojo.Entity.User">
|
||||
UPDATE sys_user
|
||||
<set>
|
||||
<if test="username != null">username = #{username},</if>
|
||||
<if test="nickname != null">nickname = #{nickname},</if>
|
||||
<if test="phone != null">phone = #{phone},</if>
|
||||
<if test="email != null">email = #{email},</if>
|
||||
<if test="avatar != null">avatar = #{avatar},</if>
|
||||
<if test="nickname !=null">nickname = #{nickname},</if>
|
||||
<if test="gender != null">gender = #{gender},</if>
|
||||
update_time = NOW()
|
||||
</set>
|
||||
WHERE id = #{id}
|
||||
</update>
|
||||
</mapper>
|
||||
Reference in New Issue
Block a user