购物车和订单中心以及优惠券相关功能初版
This commit is contained in:
+93
@@ -0,0 +1,93 @@
|
||||
package com.xiaowang.yuminailcore.controller;
|
||||
|
||||
import com.xiaowang.yuminailcore.annotation.RequirePermission;
|
||||
import com.xiaowang.yuminailcore.common.Result;
|
||||
import com.xiaowang.yuminailcore.dto.CouponDTO;
|
||||
import com.xiaowang.yuminailcore.service.CouponService;
|
||||
import com.xiaowang.yuminailcore.vo.CouponVO;
|
||||
import com.xiaowang.yuminailcore.vo.PageResultVO;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* 后台优惠券管理控制器
|
||||
*/
|
||||
@RestController
|
||||
@RequestMapping("/admin/coupon")
|
||||
@RequiredArgsConstructor
|
||||
public class AdminCouponController {
|
||||
|
||||
private final CouponService couponService;
|
||||
|
||||
/**
|
||||
* 创建优惠券
|
||||
*/
|
||||
@PostMapping("/create")
|
||||
@RequirePermission("coupon:create")
|
||||
public Result<Long> createCoupon(@RequestBody CouponDTO couponDTO, @RequestParam(required = false) Long shopId) {
|
||||
Long couponId = couponService.createCoupon(couponDTO, shopId);
|
||||
return Result.success(couponId);
|
||||
}
|
||||
|
||||
/**
|
||||
* 更新优惠券
|
||||
*/
|
||||
@PutMapping("/update/{id}")
|
||||
@RequirePermission("coupon:edit")
|
||||
public Result<Boolean> updateCoupon(@PathVariable Long id, @RequestBody CouponDTO couponDTO, @RequestParam(required = false) Long shopId) {
|
||||
boolean success = couponService.updateCoupon(id, couponDTO, shopId);
|
||||
return Result.success(success);
|
||||
}
|
||||
|
||||
/**
|
||||
* 删除优惠券
|
||||
*/
|
||||
@DeleteMapping("/delete/{id}")
|
||||
@RequirePermission("coupon:delete")
|
||||
public Result<Boolean> deleteCoupon(@PathVariable Long id, @RequestParam(required = false) Long shopId) {
|
||||
boolean success = couponService.deleteCoupon(id, shopId);
|
||||
return Result.success(success);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取优惠券详情
|
||||
*/
|
||||
@GetMapping("/detail/{id}")
|
||||
@RequirePermission("coupon:view")
|
||||
public Result<CouponVO> getCouponById(@PathVariable Long id) {
|
||||
CouponVO couponVO = couponService.getCouponById(id);
|
||||
return Result.success(couponVO);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取优惠券列表
|
||||
*/
|
||||
@GetMapping("/list")
|
||||
@RequirePermission("coupon:view")
|
||||
public Result<PageResultVO<CouponVO>> listCoupons(@RequestParam Integer page, @RequestParam Integer size, @RequestParam(required = false) Long shopId) {
|
||||
PageResultVO<CouponVO> pageResult = couponService.listCoupons(page, size, shopId);
|
||||
return Result.success(pageResult);
|
||||
}
|
||||
|
||||
/**
|
||||
* 为用户分配优惠券
|
||||
*/
|
||||
@PostMapping("/assign")
|
||||
@RequirePermission("coupon:assign")
|
||||
public Result<Boolean> assignCouponToUser(@RequestParam Long userId, @RequestParam Long couponId, @RequestParam(required = false) Long shopId) {
|
||||
boolean success = couponService.assignCouponToUser(userId, couponId, shopId);
|
||||
return Result.success(success);
|
||||
}
|
||||
|
||||
/**
|
||||
* 发放优惠券
|
||||
*/
|
||||
@PostMapping("/distribute")
|
||||
@RequirePermission("coupon:assign")
|
||||
public Result<Boolean> distributeCoupon(@RequestParam Long couponId, @RequestParam List<Long> userIds, @RequestParam Integer quantity, @RequestParam(required = false) Long shopId) {
|
||||
boolean success = couponService.distributeCoupon(couponId, userIds, quantity, shopId);
|
||||
return Result.success(success);
|
||||
}
|
||||
}
|
||||
+82
@@ -0,0 +1,82 @@
|
||||
package com.xiaowang.yuminailcore.controller;
|
||||
|
||||
import com.xiaowang.yuminailcore.annotation.RequirePermission;
|
||||
import com.xiaowang.yuminailcore.common.Result;
|
||||
import com.xiaowang.yuminailcore.dto.TagDTO;
|
||||
import com.xiaowang.yuminailcore.service.TagService;
|
||||
import com.xiaowang.yuminailcore.vo.TagVO;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* 后台标签管理控制器
|
||||
*/
|
||||
@RestController
|
||||
@RequestMapping("/admin/tag")
|
||||
@RequiredArgsConstructor
|
||||
public class AdminTagController {
|
||||
|
||||
private final TagService tagService;
|
||||
|
||||
/**
|
||||
* 创建标签
|
||||
*/
|
||||
@PostMapping("/create")
|
||||
@RequirePermission("tag:create")
|
||||
public Result<Long> createTag(@RequestBody TagDTO tagDTO) {
|
||||
Long id = tagService.createTag(tagDTO);
|
||||
return Result.success(id);
|
||||
}
|
||||
|
||||
/**
|
||||
* 更新标签
|
||||
*/
|
||||
@PutMapping("/update/{id}")
|
||||
@RequirePermission("tag:edit")
|
||||
public Result<Boolean> updateTag(@PathVariable Long id, @RequestBody TagDTO tagDTO) {
|
||||
boolean success = tagService.updateTag(id, tagDTO);
|
||||
return Result.success(success);
|
||||
}
|
||||
|
||||
/**
|
||||
* 删除标签
|
||||
*/
|
||||
@DeleteMapping("/delete/{id}")
|
||||
@RequirePermission("tag:delete")
|
||||
public Result<Boolean> deleteTag(@PathVariable Long id) {
|
||||
boolean success = tagService.deleteTag(id);
|
||||
return Result.success(success);
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据ID查询标签
|
||||
*/
|
||||
@GetMapping("/get/{id}")
|
||||
@RequirePermission("tag:view")
|
||||
public Result<TagVO> getTagById(@PathVariable Long id) {
|
||||
TagVO tagVO = tagService.getTagById(id);
|
||||
return Result.success(tagVO);
|
||||
}
|
||||
|
||||
/**
|
||||
* 查询所有标签
|
||||
*/
|
||||
@GetMapping("/list/all")
|
||||
@RequirePermission("tag:view")
|
||||
public Result<List<TagVO>> listAllTags() {
|
||||
List<TagVO> tagVOs = tagService.listAllTags();
|
||||
return Result.success(tagVOs);
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据店铺ID查询标签
|
||||
*/
|
||||
@GetMapping("/list/by-shop/{shopId}")
|
||||
@RequirePermission("tag:view")
|
||||
public Result<List<TagVO>> listTagsByShopId(@PathVariable Long shopId) {
|
||||
List<TagVO> tagVOs = tagService.listTagsByShopId(shopId);
|
||||
return Result.success(tagVOs);
|
||||
}
|
||||
}
|
||||
+7
@@ -68,6 +68,13 @@ public class AdminUserController {
|
||||
return Result.success(success);
|
||||
}
|
||||
|
||||
@GetMapping("/info")
|
||||
public Result<UserVO> getUserInfo(HttpServletRequest request) {
|
||||
Long userId = (Long) request.getAttribute("userId");
|
||||
UserVO userVO = userService.getUserById(userId);
|
||||
return Result.success(userVO);
|
||||
}
|
||||
|
||||
@Data
|
||||
public static class StatusRequest {
|
||||
private Integer status;
|
||||
|
||||
@@ -0,0 +1,54 @@
|
||||
package com.xiaowang.yuminailcore.controller;
|
||||
|
||||
import com.xiaowang.yuminailcore.common.Result;
|
||||
import com.xiaowang.yuminailcore.dto.CartDTO;
|
||||
import com.xiaowang.yuminailcore.service.CartService;
|
||||
import com.xiaowang.yuminailcore.vo.CartVO;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import java.util.List;
|
||||
|
||||
@RestController
|
||||
@RequestMapping("/cart")
|
||||
public class CartController {
|
||||
|
||||
@Resource
|
||||
private CartService cartService;
|
||||
|
||||
@PostMapping("/add")
|
||||
public Result<Void> addToCart(@RequestBody CartDTO cartDTO, HttpServletRequest request) {
|
||||
Long userId = (Long) request.getAttribute("userId");
|
||||
cartService.addToCart(userId, cartDTO);
|
||||
return Result.success();
|
||||
}
|
||||
|
||||
@DeleteMapping("/remove/{productId}")
|
||||
public Result<Void> removeFromCart(@PathVariable Long productId, HttpServletRequest request) {
|
||||
Long userId = (Long) request.getAttribute("userId");
|
||||
cartService.removeFromCart(userId, productId);
|
||||
return Result.success();
|
||||
}
|
||||
|
||||
@PutMapping("/update")
|
||||
public Result<Void> updateCartQuantity(@RequestBody CartDTO cartDTO, HttpServletRequest request) {
|
||||
Long userId = (Long) request.getAttribute("userId");
|
||||
cartService.updateCartQuantity(userId, cartDTO);
|
||||
return Result.success();
|
||||
}
|
||||
|
||||
@GetMapping("/list")
|
||||
public Result<List<CartVO>> getCartList(HttpServletRequest request) {
|
||||
Long userId = (Long) request.getAttribute("userId");
|
||||
List<CartVO> cartList = cartService.getCartList(userId);
|
||||
return Result.success(cartList);
|
||||
}
|
||||
|
||||
@DeleteMapping("/clear")
|
||||
public Result<Void> clearCart(HttpServletRequest request) {
|
||||
Long userId = (Long) request.getAttribute("userId");
|
||||
cartService.clearCart(userId);
|
||||
return Result.success();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,62 @@
|
||||
package com.xiaowang.yuminailcore.controller;
|
||||
|
||||
import com.xiaowang.yuminailcore.annotation.RequirePermission;
|
||||
import com.xiaowang.yuminailcore.common.Result;
|
||||
import com.xiaowang.yuminailcore.dto.OrderDTO;
|
||||
import com.xiaowang.yuminailcore.service.OrderService;
|
||||
import com.xiaowang.yuminailcore.vo.OrderVO;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import java.util.List;
|
||||
|
||||
@RestController
|
||||
@RequestMapping("/order")
|
||||
public class OrderController {
|
||||
|
||||
@Resource
|
||||
private OrderService orderService;
|
||||
|
||||
@PostMapping("/create")
|
||||
public Result<OrderVO> createOrder(@RequestBody OrderDTO orderDTO, HttpServletRequest request) {
|
||||
Long userId = (Long) request.getAttribute("userId");
|
||||
OrderVO orderVO = orderService.createOrder(userId, orderDTO);
|
||||
return Result.success(orderVO);
|
||||
}
|
||||
|
||||
@GetMapping("/list")
|
||||
public Result<List<OrderVO>> getOrderList(HttpServletRequest request) {
|
||||
Long userId = (Long) request.getAttribute("userId");
|
||||
List<OrderVO> orderList = orderService.getOrderList(userId);
|
||||
return Result.success(orderList);
|
||||
}
|
||||
|
||||
@GetMapping("/detail/{orderId}")
|
||||
public Result<OrderVO> getOrderDetail(@PathVariable Long orderId) {
|
||||
OrderVO orderVO = orderService.getOrderDetail(orderId);
|
||||
return Result.success(orderVO);
|
||||
}
|
||||
|
||||
@PutMapping("/cancel/{orderId}")
|
||||
public Result<Void> cancelOrder(@PathVariable Long orderId, HttpServletRequest request) {
|
||||
Long userId = (Long) request.getAttribute("userId");
|
||||
orderService.cancelOrder(orderId, userId);
|
||||
return Result.success();
|
||||
}
|
||||
|
||||
// 后台管理接口
|
||||
@RequirePermission("order:view")
|
||||
@GetMapping("/admin/list")
|
||||
public Result<List<OrderVO>> getAllOrders() {
|
||||
List<OrderVO> orderList = orderService.getAllOrders();
|
||||
return Result.success(orderList);
|
||||
}
|
||||
|
||||
@RequirePermission("order:process")
|
||||
@PutMapping("/admin/process/{orderId}")
|
||||
public Result<Void> processOrder(@PathVariable Long orderId, @RequestParam Integer status) {
|
||||
orderService.processOrder(orderId, status);
|
||||
return Result.success();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
package com.xiaowang.yuminailcore.dto;
|
||||
|
||||
import lombok.Data;
|
||||
|
||||
@Data
|
||||
public class CartDTO {
|
||||
private Long productId;
|
||||
private Integer quantity;
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
package com.xiaowang.yuminailcore.dto;
|
||||
|
||||
import lombok.Data;
|
||||
|
||||
@Data
|
||||
public class OrderDTO {
|
||||
private Long couponId;
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
package com.xiaowang.yuminailcore.entity;
|
||||
|
||||
import com.baomidou.mybatisplus.annotation.TableName;
|
||||
import lombok.Data;
|
||||
|
||||
import java.time.LocalDateTime;
|
||||
|
||||
@Data
|
||||
@TableName("t_cart")
|
||||
public class CartDO extends BaseEntity {
|
||||
private Long userId;
|
||||
private Long productId;
|
||||
private Integer quantity;
|
||||
private LocalDateTime createTime;
|
||||
private LocalDateTime updateTime;
|
||||
private Integer isDelete;
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
package com.xiaowang.yuminailcore.entity;
|
||||
|
||||
import com.baomidou.mybatisplus.annotation.TableName;
|
||||
import lombok.Data;
|
||||
|
||||
import java.math.BigDecimal;
|
||||
import java.time.LocalDateTime;
|
||||
|
||||
@Data
|
||||
@TableName("t_order")
|
||||
public class OrderDO extends BaseEntity {
|
||||
private String orderNo;
|
||||
private Long userId;
|
||||
private BigDecimal totalAmount;
|
||||
private BigDecimal actualAmount;
|
||||
private Long couponId;
|
||||
private Integer status;
|
||||
private LocalDateTime createTime;
|
||||
private LocalDateTime updateTime;
|
||||
private Integer isDelete;
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
package com.xiaowang.yuminailcore.entity;
|
||||
|
||||
import com.baomidou.mybatisplus.annotation.TableName;
|
||||
import lombok.Data;
|
||||
|
||||
import java.math.BigDecimal;
|
||||
import java.time.LocalDateTime;
|
||||
|
||||
@Data
|
||||
@TableName("t_order_item")
|
||||
public class OrderItemDO extends BaseEntity {
|
||||
private Long orderId;
|
||||
private Long productId;
|
||||
private Integer quantity;
|
||||
private BigDecimal price;
|
||||
private LocalDateTime createTime;
|
||||
private LocalDateTime updateTime;
|
||||
private Integer isDelete;
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
package com.xiaowang.yuminailcore.mapper;
|
||||
|
||||
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
|
||||
import com.xiaowang.yuminailcore.entity.CartDO;
|
||||
import org.apache.ibatis.annotations.Mapper;
|
||||
import org.apache.ibatis.annotations.Param;
|
||||
import org.apache.ibatis.annotations.Select;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@Mapper
|
||||
public interface CartMapper extends BaseMapper<CartDO> {
|
||||
@Select("SELECT * FROM t_cart WHERE user_id = #{userId}")
|
||||
List<CartDO> selectByUserId(@Param("userId") Long userId);
|
||||
|
||||
@Select("SELECT * FROM t_cart WHERE user_id = #{userId} AND product_id = #{productId}")
|
||||
CartDO selectByUserIdAndProductId(@Param("userId") Long userId, @Param("productId") Long productId);
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
package com.xiaowang.yuminailcore.mapper;
|
||||
|
||||
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
|
||||
import com.xiaowang.yuminailcore.entity.OrderItemDO;
|
||||
import org.apache.ibatis.annotations.Mapper;
|
||||
import org.apache.ibatis.annotations.Param;
|
||||
import org.apache.ibatis.annotations.Select;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@Mapper
|
||||
public interface OrderItemMapper extends BaseMapper<OrderItemDO> {
|
||||
@Select("SELECT * FROM t_order_item WHERE order_id = #{orderId}")
|
||||
List<OrderItemDO> selectByOrderId(@Param("orderId") Long orderId);
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
package com.xiaowang.yuminailcore.mapper;
|
||||
|
||||
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
|
||||
import com.xiaowang.yuminailcore.entity.OrderDO;
|
||||
import org.apache.ibatis.annotations.Mapper;
|
||||
import org.apache.ibatis.annotations.Param;
|
||||
import org.apache.ibatis.annotations.Select;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@Mapper
|
||||
public interface OrderMapper extends BaseMapper<OrderDO> {
|
||||
@Select("SELECT * FROM t_order WHERE user_id = #{userId}")
|
||||
List<OrderDO> selectByUserId(@Param("userId") Long userId);
|
||||
|
||||
@Select("SELECT * FROM t_order WHERE order_no = #{orderNo}")
|
||||
OrderDO selectByOrderNo(@Param("orderNo") String orderNo);
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
package com.xiaowang.yuminailcore.service;
|
||||
|
||||
import com.xiaowang.yuminailcore.dto.CartDTO;
|
||||
import com.xiaowang.yuminailcore.vo.CartVO;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public interface CartService {
|
||||
void addToCart(Long userId, CartDTO cartDTO);
|
||||
void removeFromCart(Long userId, Long productId);
|
||||
void updateCartQuantity(Long userId, CartDTO cartDTO);
|
||||
List<CartVO> getCartList(Long userId);
|
||||
void clearCart(Long userId);
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
package com.xiaowang.yuminailcore.service;
|
||||
|
||||
import com.xiaowang.yuminailcore.dto.OrderDTO;
|
||||
import com.xiaowang.yuminailcore.vo.OrderVO;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public interface OrderService {
|
||||
OrderVO createOrder(Long userId, OrderDTO orderDTO);
|
||||
List<OrderVO> getOrderList(Long userId);
|
||||
OrderVO getOrderDetail(Long orderId);
|
||||
void cancelOrder(Long orderId, Long userId);
|
||||
void processOrder(Long orderId, Integer status);
|
||||
List<OrderVO> getAllOrders();
|
||||
}
|
||||
+112
@@ -0,0 +1,112 @@
|
||||
package com.xiaowang.yuminailcore.service.impl;
|
||||
|
||||
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
|
||||
import com.xiaowang.yuminailcore.dto.CartDTO;
|
||||
import com.xiaowang.yuminailcore.entity.CartDO;
|
||||
import com.xiaowang.yuminailcore.entity.ProductDO;
|
||||
import com.xiaowang.yuminailcore.mapper.CartMapper;
|
||||
import com.xiaowang.yuminailcore.mapper.ProductMapper;
|
||||
import com.xiaowang.yuminailcore.service.CartService;
|
||||
import com.xiaowang.yuminailcore.vo.CartVO;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
import java.math.BigDecimal;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
@Service
|
||||
public class CartServiceImpl implements CartService {
|
||||
|
||||
@Resource
|
||||
private CartMapper cartMapper;
|
||||
|
||||
@Resource
|
||||
private ProductMapper productMapper;
|
||||
|
||||
@Override
|
||||
@Transactional
|
||||
public void addToCart(Long userId, CartDTO cartDTO) {
|
||||
// 检查商品是否存在
|
||||
ProductDO product = productMapper.selectById(cartDTO.getProductId());
|
||||
if (product == null || product.getIsDelete() == 1) {
|
||||
throw new RuntimeException("商品不存在");
|
||||
}
|
||||
|
||||
// 检查购物车中是否已有该商品
|
||||
CartDO existingCart = cartMapper.selectByUserIdAndProductId(userId, cartDTO.getProductId());
|
||||
if (existingCart != null) {
|
||||
// 更新数量
|
||||
existingCart.setQuantity(existingCart.getQuantity() + cartDTO.getQuantity());
|
||||
cartMapper.updateById(existingCart);
|
||||
} else {
|
||||
// 添加新商品到购物车
|
||||
CartDO newCart = new CartDO();
|
||||
newCart.setUserId(userId);
|
||||
newCart.setProductId(cartDTO.getProductId());
|
||||
newCart.setQuantity(cartDTO.getQuantity());
|
||||
newCart.setIsDelete(0);
|
||||
cartMapper.insert(newCart);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
@Transactional
|
||||
public void removeFromCart(Long userId, Long productId) {
|
||||
QueryWrapper<CartDO> wrapper = new QueryWrapper<>();
|
||||
wrapper.eq("user_id", userId)
|
||||
.eq("product_id", productId)
|
||||
.eq("is_delete", 0);
|
||||
cartMapper.delete(wrapper);
|
||||
}
|
||||
|
||||
@Override
|
||||
@Transactional
|
||||
public void updateCartQuantity(Long userId, CartDTO cartDTO) {
|
||||
CartDO cart = cartMapper.selectByUserIdAndProductId(userId, cartDTO.getProductId());
|
||||
if (cart != null) {
|
||||
cart.setQuantity(cartDTO.getQuantity());
|
||||
cartMapper.updateById(cart);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<CartVO> getCartList(Long userId) {
|
||||
List<CartDO> cartList = cartMapper.selectByUserId(userId);
|
||||
List<CartVO> cartVOList = new ArrayList<>();
|
||||
|
||||
for (CartDO cart : cartList) {
|
||||
if (cart.getIsDelete() == 0) {
|
||||
ProductDO product = productMapper.selectById(cart.getProductId());
|
||||
if (product != null && product.getIsDelete() == 0) {
|
||||
CartVO cartVO = new CartVO();
|
||||
cartVO.setId(cart.getId());
|
||||
cartVO.setProductId(product.getId());
|
||||
cartVO.setProductTitle(product.getTitle());
|
||||
// 简单处理,取第一张图片
|
||||
String imageFileIds = product.getImageFileIds();
|
||||
if (imageFileIds != null && !imageFileIds.isEmpty()) {
|
||||
String firstImageId = imageFileIds.split(",")[0];
|
||||
cartVO.setProductImage("/api/file/get/" + firstImageId);
|
||||
}
|
||||
cartVO.setPrice(product.getPrice());
|
||||
cartVO.setQuantity(cart.getQuantity());
|
||||
cartVO.setTotalPrice(product.getPrice().multiply(new BigDecimal(cart.getQuantity())));
|
||||
cartVOList.add(cartVO);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return cartVOList;
|
||||
}
|
||||
|
||||
@Override
|
||||
@Transactional
|
||||
public void clearCart(Long userId) {
|
||||
QueryWrapper<CartDO> wrapper = new QueryWrapper<>();
|
||||
wrapper.eq("user_id", userId)
|
||||
.eq("is_delete", 0);
|
||||
cartMapper.delete(wrapper);
|
||||
}
|
||||
}
|
||||
+309
@@ -0,0 +1,309 @@
|
||||
package com.xiaowang.yuminailcore.service.impl;
|
||||
|
||||
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
|
||||
import com.xiaowang.yuminailcore.dto.OrderDTO;
|
||||
import com.xiaowang.yuminailcore.entity.*;
|
||||
import com.xiaowang.yuminailcore.mapper.*;
|
||||
import com.xiaowang.yuminailcore.service.OrderService;
|
||||
import com.xiaowang.yuminailcore.vo.OrderItemVO;
|
||||
import com.xiaowang.yuminailcore.vo.OrderVO;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
import java.math.BigDecimal;
|
||||
import java.time.LocalDateTime;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.UUID;
|
||||
|
||||
@Service
|
||||
public class OrderServiceImpl implements OrderService {
|
||||
|
||||
@Resource
|
||||
private OrderMapper orderMapper;
|
||||
|
||||
@Resource
|
||||
private OrderItemMapper orderItemMapper;
|
||||
|
||||
@Resource
|
||||
private CartMapper cartMapper;
|
||||
|
||||
@Resource
|
||||
private ProductMapper productMapper;
|
||||
|
||||
@Resource
|
||||
private CouponMapper couponMapper;
|
||||
|
||||
@Resource
|
||||
private UserCouponMapper userCouponMapper;
|
||||
|
||||
@Override
|
||||
@Transactional
|
||||
public OrderVO createOrder(Long userId, OrderDTO orderDTO) {
|
||||
// 获取购物车商品
|
||||
List<CartDO> cartList = cartMapper.selectByUserId(userId);
|
||||
if (cartList.isEmpty()) {
|
||||
throw new RuntimeException("购物车为空");
|
||||
}
|
||||
|
||||
// 计算总金额
|
||||
BigDecimal totalAmount = BigDecimal.ZERO;
|
||||
List<OrderItemDO> orderItems = new ArrayList<>();
|
||||
|
||||
for (CartDO cart : cartList) {
|
||||
if (cart.getIsDelete() == 0) {
|
||||
ProductDO product = productMapper.selectById(cart.getProductId());
|
||||
if (product != null && product.getIsDelete() == 0) {
|
||||
BigDecimal itemTotal = product.getPrice().multiply(new BigDecimal(cart.getQuantity()));
|
||||
totalAmount = totalAmount.add(itemTotal);
|
||||
|
||||
// 创建订单商品
|
||||
OrderItemDO orderItem = new OrderItemDO();
|
||||
orderItem.setProductId(product.getId());
|
||||
orderItem.setQuantity(cart.getQuantity());
|
||||
orderItem.setPrice(product.getPrice());
|
||||
orderItems.add(orderItem);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (totalAmount.compareTo(BigDecimal.ZERO) == 0) {
|
||||
throw new RuntimeException("购物车商品无效");
|
||||
}
|
||||
|
||||
// 处理优惠券
|
||||
BigDecimal actualAmount = totalAmount;
|
||||
Long couponId = orderDTO.getCouponId();
|
||||
if (couponId != null && couponId > 0) {
|
||||
// 检查优惠券是否存在且可用
|
||||
UserCouponDO userCoupon = userCouponMapper.selectOne(
|
||||
new QueryWrapper<UserCouponDO>()
|
||||
.eq("user_id", userId)
|
||||
.eq("coupon_id", couponId)
|
||||
.eq("status", 1)
|
||||
);
|
||||
|
||||
if (userCoupon != null) {
|
||||
CouponDO coupon = couponMapper.selectById(couponId);
|
||||
if (coupon != null && coupon.getStatus() == 1) {
|
||||
// 计算优惠金额
|
||||
if (coupon.getCouponType() == 1) { // 折扣券
|
||||
actualAmount = totalAmount.multiply(coupon.getDiscountValue()).divide(new BigDecimal(10), 2, BigDecimal.ROUND_HALF_UP);
|
||||
} else if (coupon.getCouponType() == 2) { // 满减券
|
||||
if (totalAmount.compareTo(coupon.getMinAmount()) >= 0) {
|
||||
actualAmount = totalAmount.subtract(coupon.getReduceAmount());
|
||||
if (actualAmount.compareTo(BigDecimal.ZERO) < 0) {
|
||||
actualAmount = BigDecimal.ZERO;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 更新优惠券状态
|
||||
userCoupon.setStatus(2); // 已使用
|
||||
userCoupon.setUseTime(LocalDateTime.now());
|
||||
userCouponMapper.updateById(userCoupon);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 生成订单号
|
||||
String orderNo = "ORD" + LocalDateTime.now().format(java.time.format.DateTimeFormatter.ofPattern("yyyyMMddHHmmss")) + UUID.randomUUID().toString().substring(0, 8).toUpperCase();
|
||||
|
||||
// 创建订单
|
||||
OrderDO order = new OrderDO();
|
||||
order.setOrderNo(orderNo);
|
||||
order.setUserId(userId);
|
||||
order.setTotalAmount(totalAmount);
|
||||
order.setActualAmount(actualAmount);
|
||||
order.setCouponId(couponId);
|
||||
order.setStatus(1); // 待使用
|
||||
order.setIsDelete(0);
|
||||
orderMapper.insert(order);
|
||||
|
||||
// 创建订单商品
|
||||
for (OrderItemDO item : orderItems) {
|
||||
item.setOrderId(order.getId());
|
||||
item.setIsDelete(0);
|
||||
orderItemMapper.insert(item);
|
||||
}
|
||||
|
||||
// 清空购物车
|
||||
cartMapper.delete(new QueryWrapper<CartDO>().eq("user_id", userId).eq("is_delete", 0));
|
||||
|
||||
// 构建返回对象
|
||||
return buildOrderVO(order);
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<OrderVO> getOrderList(Long userId) {
|
||||
List<OrderDO> orderList = orderMapper.selectByUserId(userId);
|
||||
List<OrderVO> orderVOList = new ArrayList<>();
|
||||
|
||||
for (OrderDO order : orderList) {
|
||||
if (order.getIsDelete() == 0) {
|
||||
orderVOList.add(buildOrderVO(order));
|
||||
}
|
||||
}
|
||||
|
||||
return orderVOList;
|
||||
}
|
||||
|
||||
@Override
|
||||
public OrderVO getOrderDetail(Long orderId) {
|
||||
OrderDO order = orderMapper.selectById(orderId);
|
||||
if (order == null || order.getIsDelete() == 1) {
|
||||
throw new RuntimeException("订单不存在");
|
||||
}
|
||||
|
||||
return buildOrderVO(order);
|
||||
}
|
||||
|
||||
@Override
|
||||
@Transactional
|
||||
public void cancelOrder(Long orderId, Long userId) {
|
||||
OrderDO order = orderMapper.selectById(orderId);
|
||||
if (order == null || order.getIsDelete() == 1) {
|
||||
throw new RuntimeException("订单不存在");
|
||||
}
|
||||
|
||||
if (!order.getUserId().equals(userId)) {
|
||||
throw new RuntimeException("无权操作此订单");
|
||||
}
|
||||
|
||||
if (order.getStatus() != 1) { // 只有待使用状态可以取消
|
||||
throw new RuntimeException("订单状态不允许取消");
|
||||
}
|
||||
|
||||
// 更新订单状态
|
||||
order.setStatus(3); // 已取消
|
||||
orderMapper.updateById(order);
|
||||
|
||||
// 返还优惠券
|
||||
if (order.getCouponId() != null && order.getCouponId() > 0) {
|
||||
UserCouponDO userCoupon = userCouponMapper.selectOne(
|
||||
new QueryWrapper<UserCouponDO>()
|
||||
.eq("user_id", userId)
|
||||
.eq("coupon_id", order.getCouponId())
|
||||
.eq("status", 2)
|
||||
);
|
||||
|
||||
if (userCoupon != null) {
|
||||
userCoupon.setStatus(1); // 未使用
|
||||
userCoupon.setUseTime(null);
|
||||
userCouponMapper.updateById(userCoupon);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
@Transactional
|
||||
public void processOrder(Long orderId, Integer status) {
|
||||
OrderDO order = orderMapper.selectById(orderId);
|
||||
if (order == null || order.getIsDelete() == 1) {
|
||||
throw new RuntimeException("订单不存在");
|
||||
}
|
||||
|
||||
if (status != 2 && status != 3) { // 只能设置为已完成或已取消
|
||||
throw new RuntimeException("无效的订单状态");
|
||||
}
|
||||
|
||||
if (order.getStatus() != 1) { // 只有待使用状态可以处理
|
||||
throw new RuntimeException("订单状态不允许处理");
|
||||
}
|
||||
|
||||
// 更新订单状态
|
||||
order.setStatus(status);
|
||||
orderMapper.updateById(order);
|
||||
|
||||
// 如果取消订单,返还优惠券
|
||||
if (status == 3 && order.getCouponId() != null && order.getCouponId() > 0) {
|
||||
UserCouponDO userCoupon = userCouponMapper.selectOne(
|
||||
new QueryWrapper<UserCouponDO>()
|
||||
.eq("user_id", order.getUserId())
|
||||
.eq("coupon_id", order.getCouponId())
|
||||
.eq("status", 2)
|
||||
);
|
||||
|
||||
if (userCoupon != null) {
|
||||
userCoupon.setStatus(1); // 未使用
|
||||
userCoupon.setUseTime(null);
|
||||
userCouponMapper.updateById(userCoupon);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<OrderVO> getAllOrders() {
|
||||
List<OrderDO> orderList = orderMapper.selectList(new QueryWrapper<OrderDO>().eq("is_delete", 0));
|
||||
List<OrderVO> orderVOList = new ArrayList<>();
|
||||
|
||||
for (OrderDO order : orderList) {
|
||||
orderVOList.add(buildOrderVO(order));
|
||||
}
|
||||
|
||||
return orderVOList;
|
||||
}
|
||||
|
||||
private OrderVO buildOrderVO(OrderDO order) {
|
||||
OrderVO orderVO = new OrderVO();
|
||||
orderVO.setId(order.getId());
|
||||
orderVO.setOrderNo(order.getOrderNo());
|
||||
orderVO.setTotalAmount(order.getTotalAmount());
|
||||
orderVO.setActualAmount(order.getActualAmount());
|
||||
orderVO.setCouponId(order.getCouponId());
|
||||
orderVO.setStatus(order.getStatus());
|
||||
|
||||
// 设置状态文本
|
||||
switch (order.getStatus()) {
|
||||
case 1:
|
||||
orderVO.setStatusText("待使用");
|
||||
break;
|
||||
case 2:
|
||||
orderVO.setStatusText("已完成");
|
||||
break;
|
||||
case 3:
|
||||
orderVO.setStatusText("已取消");
|
||||
break;
|
||||
}
|
||||
|
||||
// 设置优惠券名称
|
||||
if (order.getCouponId() != null && order.getCouponId() > 0) {
|
||||
CouponDO coupon = couponMapper.selectById(order.getCouponId());
|
||||
if (coupon != null) {
|
||||
orderVO.setCouponName(coupon.getCouponName());
|
||||
}
|
||||
}
|
||||
|
||||
orderVO.setCreateTime(order.getCreateTime());
|
||||
|
||||
// 设置订单商品
|
||||
List<OrderItemDO> orderItems = orderItemMapper.selectByOrderId(order.getId());
|
||||
List<OrderItemVO> orderItemVOs = new ArrayList<>();
|
||||
|
||||
for (OrderItemDO item : orderItems) {
|
||||
if (item.getIsDelete() == 0) {
|
||||
ProductDO product = productMapper.selectById(item.getProductId());
|
||||
if (product != null) {
|
||||
OrderItemVO itemVO = new OrderItemVO();
|
||||
itemVO.setId(item.getId());
|
||||
itemVO.setProductId(product.getId());
|
||||
itemVO.setProductTitle(product.getTitle());
|
||||
// 简单处理,取第一张图片
|
||||
String imageFileIds = product.getImageFileIds();
|
||||
if (imageFileIds != null && !imageFileIds.isEmpty()) {
|
||||
String firstImageId = imageFileIds.split(",")[0];
|
||||
itemVO.setProductImage("/api/file/get/" + firstImageId);
|
||||
}
|
||||
itemVO.setQuantity(item.getQuantity());
|
||||
itemVO.setPrice(item.getPrice());
|
||||
itemVO.setTotalPrice(item.getPrice().multiply(new BigDecimal(item.getQuantity())));
|
||||
orderItemVOs.add(itemVO);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
orderVO.setItems(orderItemVOs);
|
||||
return orderVO;
|
||||
}
|
||||
}
|
||||
+6
@@ -198,6 +198,12 @@ public class PermissionServiceImpl extends ServiceImpl<PermissionMapper, Permiss
|
||||
|
||||
@Override
|
||||
public boolean hasPermission(Long userId, String permissionCode) {
|
||||
// 先检查用户是否为管理员,管理员拥有所有权限
|
||||
UserDO userDO = userMapper.selectById(userId);
|
||||
if (userDO != null && userDO.getType() == 4) {
|
||||
return true;
|
||||
}
|
||||
|
||||
List<UserPermissionDO> userPermissions = userPermissionMapper.selectList(
|
||||
Wrappers.lambdaQuery(UserPermissionDO.class)
|
||||
.eq(UserPermissionDO::getUserId, userId)
|
||||
|
||||
@@ -0,0 +1,16 @@
|
||||
package com.xiaowang.yuminailcore.vo;
|
||||
|
||||
import lombok.Data;
|
||||
|
||||
import java.math.BigDecimal;
|
||||
|
||||
@Data
|
||||
public class CartVO {
|
||||
private Long id;
|
||||
private Long productId;
|
||||
private String productTitle;
|
||||
private String productImage;
|
||||
private BigDecimal price;
|
||||
private Integer quantity;
|
||||
private BigDecimal totalPrice;
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
package com.xiaowang.yuminailcore.vo;
|
||||
|
||||
import lombok.Data;
|
||||
|
||||
import java.math.BigDecimal;
|
||||
|
||||
@Data
|
||||
public class OrderItemVO {
|
||||
private Long id;
|
||||
private Long productId;
|
||||
private String productTitle;
|
||||
private String productImage;
|
||||
private Integer quantity;
|
||||
private BigDecimal price;
|
||||
private BigDecimal totalPrice;
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
package com.xiaowang.yuminailcore.vo;
|
||||
|
||||
import lombok.Data;
|
||||
|
||||
import java.math.BigDecimal;
|
||||
import java.time.LocalDateTime;
|
||||
import java.util.List;
|
||||
|
||||
@Data
|
||||
public class OrderVO {
|
||||
private Long id;
|
||||
private String orderNo;
|
||||
private BigDecimal totalAmount;
|
||||
private BigDecimal actualAmount;
|
||||
private Long couponId;
|
||||
private String couponName;
|
||||
private Integer status;
|
||||
private String statusText;
|
||||
private LocalDateTime createTime;
|
||||
private List<OrderItemVO> items;
|
||||
}
|
||||
@@ -371,7 +371,13 @@ INSERT INTO t_permission (permission_code, permission_name, permission_group, de
|
||||
('permission:create', '新增权限', '权限管理', '新增系统权限', NOW(), NOW(), 0),
|
||||
('permission:edit', '编辑权限', '权限管理', '编辑系统权限', NOW(), NOW(), 0),
|
||||
('permission:delete', '删除权限', '权限管理', '删除系统权限', NOW(), NOW(), 0),
|
||||
('permission:assign', '分配权限', '权限管理', '为用户分配权限', NOW(), NOW(), 0);
|
||||
('permission:assign', '分配权限', '权限管理', '为用户分配权限', NOW(), NOW(), 0),
|
||||
|
||||
-- 标签管理权限
|
||||
('tag:view', '查看标签', '标签管理', '查看标签列表和详情', NOW(), NOW(), 0),
|
||||
('tag:create', '新增标签', '标签管理', '新增标签', NOW(), NOW(), 0),
|
||||
('tag:edit', '编辑标签', '标签管理', '编辑标签信息', NOW(), NOW(), 0),
|
||||
('tag:delete', '删除标签', '标签管理', '删除标签', NOW(), NOW(), 0);
|
||||
|
||||
|
||||
-- 为管理员(type=4)分配所有权限
|
||||
@@ -413,7 +419,11 @@ WHERE u.type = 2
|
||||
'shop:view',
|
||||
'shop:edit',
|
||||
'permission:view',
|
||||
'permission:assign'
|
||||
'permission:assign',
|
||||
'tag:view',
|
||||
'tag:create',
|
||||
'tag:edit',
|
||||
'tag:delete'
|
||||
);
|
||||
|
||||
|
||||
|
||||
@@ -0,0 +1,101 @@
|
||||
-- 购物车表
|
||||
create table t_cart
|
||||
(
|
||||
id bigint unsigned auto_increment comment '购物车唯一主键ID'
|
||||
primary key comment '购物车主键索引',
|
||||
user_id bigint unsigned not null comment '关联用户表主键',
|
||||
product_id bigint unsigned not null comment '关联商品表主键',
|
||||
quantity int unsigned default 1 not null comment '商品数量',
|
||||
create_time datetime default CURRENT_TIMESTAMP not null comment '创建时间',
|
||||
update_time datetime default CURRENT_TIMESTAMP not null on update CURRENT_TIMESTAMP comment '更新时间',
|
||||
is_delete tinyint unsigned default 0 null comment '逻辑删除:0-未删,1-已删',
|
||||
constraint uk_user_product
|
||||
unique (user_id, product_id) comment '用户-商品唯一索引'
|
||||
) comment '购物车表';
|
||||
|
||||
create index idx_user_id
|
||||
on t_cart (user_id) comment '按用户查询购物车的索引';
|
||||
|
||||
-- 订单表
|
||||
create table t_order
|
||||
(
|
||||
id bigint unsigned auto_increment comment '订单唯一主键ID'
|
||||
primary key comment '订单主键索引',
|
||||
order_no varchar(50) not null comment '订单编号',
|
||||
user_id bigint unsigned not null comment '关联用户表主键',
|
||||
total_amount decimal(10, 2) not null comment '订单总金额',
|
||||
actual_amount decimal(10, 2) not null comment '实际支付金额',
|
||||
coupon_id bigint unsigned default 0 null comment '使用的优惠券ID',
|
||||
status tinyint unsigned default 1 not null comment '订单状态:1-待使用,2-已完成,3-已取消',
|
||||
create_time datetime default CURRENT_TIMESTAMP not null comment '创建时间',
|
||||
update_time datetime default CURRENT_TIMESTAMP not null on update CURRENT_TIMESTAMP comment '更新时间',
|
||||
is_delete tinyint unsigned default 0 null comment '逻辑删除:0-未删,1-已删',
|
||||
constraint uk_order_no
|
||||
unique (order_no) comment '订单编号唯一索引'
|
||||
) comment '订单表';
|
||||
|
||||
create index idx_status
|
||||
on t_order (status) comment '按状态查询订单的索引';
|
||||
|
||||
create index idx_user_id
|
||||
on t_order (user_id) comment '按用户查询订单的索引';
|
||||
|
||||
-- 订单商品表
|
||||
create table t_order_item
|
||||
(
|
||||
id bigint unsigned auto_increment comment '订单商品唯一主键ID'
|
||||
primary key comment '订单商品主键索引',
|
||||
order_id bigint unsigned not null comment '关联订单表主键',
|
||||
product_id bigint unsigned not null comment '关联商品表主键',
|
||||
quantity int unsigned not null comment '商品数量',
|
||||
price decimal(10, 2) not null comment '商品单价',
|
||||
create_time datetime default CURRENT_TIMESTAMP not null comment '创建时间',
|
||||
update_time datetime default CURRENT_TIMESTAMP not null on update CURRENT_TIMESTAMP comment '更新时间',
|
||||
is_delete tinyint unsigned default 0 null comment '逻辑删除:0-未删,1-已删'
|
||||
) comment '订单商品表';
|
||||
|
||||
create index idx_order_id
|
||||
on t_order_item (order_id) comment '按订单查询商品的索引';
|
||||
|
||||
-- 插入订单管理相关权限
|
||||
INSERT INTO t_permission (permission_code, permission_name, permission_group, description, create_time, update_time, is_delete) VALUES
|
||||
('order:view', '查看订单', '订单管理', '查看订单列表和详情', NOW(), NOW(), 0),
|
||||
('order:edit', '编辑订单', '订单管理', '编辑订单信息', NOW(), NOW(), 0),
|
||||
('order:delete', '删除订单', '订单管理', '删除订单', NOW(), NOW(), 0),
|
||||
('order:process', '处理订单', '订单管理', '处理订单状态', NOW(), NOW(), 0);
|
||||
|
||||
-- 为管理员(type=4)分配订单管理权限
|
||||
INSERT INTO t_user_permission (user_id, permission_id, create_time, update_time)
|
||||
SELECT
|
||||
u.id as user_id,
|
||||
p.id as permission_id,
|
||||
NOW() as create_time,
|
||||
NOW() as update_time
|
||||
FROM t_user u
|
||||
CROSS JOIN t_permission p
|
||||
WHERE u.type = 4
|
||||
AND p.is_delete = 0
|
||||
AND p.permission_code IN (
|
||||
'order:view',
|
||||
'order:edit',
|
||||
'order:delete',
|
||||
'order:process'
|
||||
);
|
||||
|
||||
-- 为店长(type=2)分配订单管理权限
|
||||
INSERT INTO t_user_permission (user_id, permission_id, create_time, update_time)
|
||||
SELECT
|
||||
u.id as user_id,
|
||||
p.id as permission_id,
|
||||
NOW() as create_time,
|
||||
NOW() as update_time
|
||||
FROM t_user u
|
||||
CROSS JOIN t_permission p
|
||||
WHERE u.type = 2
|
||||
AND p.is_delete = 0
|
||||
AND p.permission_code IN (
|
||||
'order:view',
|
||||
'order:edit',
|
||||
'order:delete',
|
||||
'order:process'
|
||||
);
|
||||
Reference in New Issue
Block a user