更新了商品类别

This commit is contained in:
chen-xin-zhi 2024-12-19 13:52:56 +08:00
parent 110b49d2fc
commit 562ac2af62
16 changed files with 339 additions and 79 deletions

View File

@ -10,7 +10,7 @@ import com.cultural.heritage.common.ResultUtils;
import com.cultural.heritage.constant.UserConstant;
import com.cultural.heritage.exception.BusinessException;
import com.cultural.heritage.exception.ThrowUtils;
import com.cultural.heritage.model.dto.CommonDelBatchRequest;
import com.cultural.heritage.model.dto.CommonBatchRequest;
import com.cultural.heritage.model.dto.CommonRequest;
import com.cultural.heritage.model.dto.category.CategoryAddRequest;
import com.cultural.heritage.model.dto.category.CategoryUpdateRequest;
@ -113,7 +113,7 @@ public class CategoryController {
@PostMapping("/delBatch")
@Operation(summary = "Web端管理员批量删除商品类别", description = "参数:类别删除请求体,权限:管理员(admin, boss)方法名deleteCategory")
@AuthCheck(mustRole = UserConstant.ADMIN_ROLE)
public BaseResponse<Boolean> delBatchCategory(@RequestBody CommonDelBatchRequest commonDelBatchRequest) {
public BaseResponse<Boolean> delBatchCategory(@RequestBody CommonBatchRequest commonDelBatchRequest) {
if (commonDelBatchRequest == null || CollectionUtils.isEmpty(commonDelBatchRequest.getIdList())) {
throw new BusinessException(ErrorCode.PARAMS_ERROR);
}
@ -206,6 +206,7 @@ public class CategoryController {
String typeName = category.getTypeName();
QueryWrapper<Good> queryWrapper = new QueryWrapper<>();
queryWrapper.eq("type", typeName);
queryWrapper.eq("isShelves", 1);
long count = goodService.count(queryWrapper);
return count != 0;
}).toList();

View File

@ -14,7 +14,7 @@ import com.cultural.heritage.common.ResultUtils;
import com.cultural.heritage.constant.UserConstant;
import com.cultural.heritage.exception.BusinessException;
import com.cultural.heritage.exception.ThrowUtils;
import com.cultural.heritage.model.dto.CommonDelBatchRequest;
import com.cultural.heritage.model.dto.CommonBatchRequest;
import com.cultural.heritage.model.dto.CommonRequest;
import com.cultural.heritage.model.dto.appointment.AppointmentDateAddRequest;
import com.cultural.heritage.model.dto.good.GoodAddRequest;
@ -26,6 +26,7 @@ import com.cultural.heritage.model.dto.good.service.ServiceGoodSingleUpdateReque
import com.cultural.heritage.model.dto.good.service.ServiceGoodUpdateRequest;
import com.cultural.heritage.model.dto.timeperiod.TimePeriodAddRequest;
import com.cultural.heritage.model.entity.AppointmentDate;
import com.cultural.heritage.model.entity.CartRecord;
import com.cultural.heritage.model.entity.Good;
import com.cultural.heritage.model.entity.TimePeriod;
import com.cultural.heritage.model.vo.appointment.AppointmentDateTimePeriodVO;
@ -35,6 +36,7 @@ import com.cultural.heritage.model.vo.good.ServiceGoodCardVO;
import com.cultural.heritage.model.vo.good.ServiceGoodVO;
import com.cultural.heritage.model.vo.timeperiod.TimePeriodVO;
import com.cultural.heritage.service.good.AppointmentDateService;
import com.cultural.heritage.service.good.CartRecordService;
import com.cultural.heritage.service.good.GoodService;
import com.cultural.heritage.service.good.TimePeriodService;
import com.cultural.heritage.service.user.UserService;
@ -47,6 +49,7 @@ import org.springframework.beans.BeanUtils;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;
import java.math.BigDecimal;
import java.util.*;
@ -76,6 +79,11 @@ public class GoodController {
private TimePeriodService timePeriodService;
@Resource
private CartRecordService cartRecordService;
/**
* Web端管理员添加常规类商品
@ -271,12 +279,27 @@ public class GoodController {
if (goodUpdateRequest == null || goodUpdateRequest.getId() <= 0) {
throw new BusinessException(ErrorCode.PARAMS_ERROR);
}
// 获取原来的商品价格
Long sourceId = goodUpdateRequest.getId();
Good resourceGood = goodService.getById(sourceId);
BigDecimal resourcePrice = resourceGood.getPrice();
Good good = new Good();
BeanUtils.copyProperties(goodUpdateRequest, good);
// 校验
goodService.validGood(good, true);
boolean result = goodService.updateById(good);
ThrowUtils.throwIf(!result, ErrorCode.OPERATION_ERROR);
// 如果更新了商品的价格就需要更新购物车中的小计
Long id = good.getId();
BigDecimal targetPrice = good.getPrice();
if (resourcePrice.compareTo(targetPrice) != 0) {
// 更新购物车中的小计
List<CartRecord> cartRecordList = goodService.updateCartGoodPrice(id, targetPrice);
boolean update = cartRecordService.updateBatchById(cartRecordList);
ThrowUtils.throwIf(!update, ErrorCode.OPERATION_ERROR, "购物车商品价格更新失败");
}
return ResultUtils.success(true);
}
@ -323,7 +346,7 @@ public class GoodController {
@PostMapping("/delBatch")
@Operation(summary = "Web端管理员批量删除常规类商品", description = "参数:商品批量删除请求体,权限:管理员(admin, boss)方法名delBatchGoods")
@AuthCheck(mustRole = UserConstant.ADMIN_ROLE)
public BaseResponse<Boolean> delBatchGoods(@RequestBody CommonDelBatchRequest commonDelBatchRequest) {
public BaseResponse<Boolean> delBatchGoods(@RequestBody CommonBatchRequest commonDelBatchRequest) {
List<Long> idList = commonDelBatchRequest.getIdList();
boolean result = goodService.removeBatchByIds(idList);
ThrowUtils.throwIf(!result, ErrorCode.OPERATION_ERROR);
@ -681,17 +704,6 @@ public class GoodController {
// /**
// * (小程序端)在支付订单前找出不合理的商品项
// * @return 存在问题的购物车id列表
// */
// @PostMapping("/cart/pay/error")
// @Operation(summary = "(小程序端)在支付订单前找出不合理的商品项", description = "参数:无权限所有人方法名listInvalidCartIds")
// public BaseResponse<List<Long>> listInvalidCartIdsBeforePay(@RequestBody List<CartPayRequest> cartPayRequests, HttpServletRequest request) {
// User loginUser = userService.getLoginUser(request);
//
// }
}

View File

@ -12,19 +12,21 @@ import com.cultural.heritage.constant.UserConstant;
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.order.OrderAddRequest;
import com.cultural.heritage.model.dto.order.OrderItemAddRequest;
import com.cultural.heritage.model.dto.cart.CartOrderAddRequest;
import com.cultural.heritage.model.dto.cart.CartOrderItemAddRequest;
import com.cultural.heritage.model.dto.order.OrderQueryRequest;
import com.cultural.heritage.model.dto.order.OrderUpdateRequest;
import com.cultural.heritage.model.dto.order.capital.OrderItemMainInfoAddRequest;
import com.cultural.heritage.model.dto.order.capital.OrderMainInfoAddRequest;
import com.cultural.heritage.model.entity.CartRecord;
import com.cultural.heritage.model.entity.Order;
import com.cultural.heritage.model.entity.OrderItems;
import com.cultural.heritage.model.entity.User;
import com.cultural.heritage.model.vo.order.OrderVO;
import com.cultural.heritage.service.good.CartRecordService;
import com.cultural.heritage.service.order.OrderItemService;
import com.cultural.heritage.service.order.OrderService;
import com.cultural.heritage.service.user.UserService;
import com.cultural.heritage.utils.OrderNumberUtils;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
@ -37,7 +39,10 @@ import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@RestController
@RequestMapping("/order")
@ -60,6 +65,69 @@ public class OrderController {
private UserService userService;
@Resource
private CartRecordService cartRecordService;
/**
* 用户通过购物车创建订单
*/
@PostMapping("/add/cart")
@Transactional(rollbackFor = Exception.class)
@Operation(summary = "用户通过购物车创建订单", description = "参数:")
public BaseResponse<Long> addCartOrder(@RequestBody CartOrderAddRequest cartOrderAddRequest, HttpServletRequest request) {
if (cartOrderAddRequest == null) {
throw new BusinessException(ErrorCode.PARAMS_ERROR);
}
// 校验用户当前是否处于登录态
User loginUser = userService.getLoginUser(request);
Long userId = loginUser.getId();
// 封装成订单创建请求体
OrderMainInfoAddRequest orderMainInfoAddRequest = new OrderMainInfoAddRequest();
BeanUtils.copyProperties(cartOrderAddRequest, orderMainInfoAddRequest);
// 校验购物车数据是否准确
List<CartOrderItemAddRequest> cartOrderItemAddRequestList = cartOrderAddRequest.getCartOrderItemAddRequestList();
// 获取购物车id列表
List<Long> cartIds = cartOrderItemAddRequestList.stream().map(CartOrderItemAddRequest::getCartRecordId).toList();
// 根据id列表获取购物车商品信息
QueryWrapper<CartRecord> queryWrapper = new QueryWrapper<>();
queryWrapper.in("id", cartIds);
List<CartRecord> cartRecordList = cartRecordService.list(queryWrapper);
// 封装成一个map集合购物车id, 购物车商品数量
Map<Long, Integer> map = new HashMap<>();
for (CartOrderItemAddRequest cartOrderItemAddRequest : cartOrderItemAddRequestList) {
Long cartRecordId = cartOrderItemAddRequest.getCartRecordId();
Integer quantity = cartOrderItemAddRequest.getQuantity();
map.put(cartRecordId, quantity);
}
// 封装成订单明细主要信息请求体列表
List<OrderItemMainInfoAddRequest> orderItemMainInfoAddRequestList = new ArrayList<>();
OrderItemMainInfoAddRequest orderItemMainInfoAddRequest;
for (CartRecord cartRecord : cartRecordList) {
Long goodId = cartRecord.getGoodId();
Long id = cartRecord.getId();
Integer quantity = map.get(id);
orderItemMainInfoAddRequest = new OrderItemMainInfoAddRequest(goodId, quantity, null, null);
orderItemMainInfoAddRequestList.add(orderItemMainInfoAddRequest);
}
// 填充到订单主要信息请求体
orderMainInfoAddRequest.setOrderItemMainInfoAddRequestList(orderItemMainInfoAddRequestList);
Long orderId = orderService.createCommonOrder(orderMainInfoAddRequest, userId);
return ResultUtils.success(orderId);
}
/**
* 用户创建订单
* @param orderMainInfoAddRequest 订单创建请求体
@ -67,43 +135,17 @@ public class OrderController {
*/
@PostMapping("/add")
@Transactional(rollbackFor = Exception.class)
@Operation(summary = "小程序端创建用户订单", description = "参数:订单创建请求体,排序字段 sortField: totalAmount id createTime权限所有人方法名addOrder")
@Operation(summary = "小程序端创建用户订单", description = "参数:订单创建请求体,权限所有人方法名addOrder")
public BaseResponse<Long> addOrder(@RequestBody OrderMainInfoAddRequest orderMainInfoAddRequest, HttpServletRequest request) {
if (orderMainInfoAddRequest == null) {
throw new BusinessException(ErrorCode.PARAMS_ERROR);
}
// 校验用户当前是否处于登录态
User loginUser = userService.getLoginUser(request);
// 封装订单详细信息请求体
OrderAddRequest orderAddRequest = orderService.initOrderMainInfo(orderMainInfoAddRequest);
if (orderAddRequest == null) {
throw new BusinessException(ErrorCode.PARAMS_ERROR);
}
// 创建一个总订单
Order order = new Order();
BeanUtils.copyProperties(orderAddRequest, order);
order.setUserId(loginUser.getId());
// 生成订单编号
String orderNumber = OrderNumberUtils.generateOrderId();
order.setOrderNumber(orderNumber);
// 设置订单状态
order.setOrderStatus(OrderStatusConstant.PENDING_PAYMENT);
// 校验订单
orderService.validOrder(order);
boolean result = orderService.save(order);
ThrowUtils.throwIf(!result, ErrorCode.OPERATION_ERROR);
// 创建订单明细
Long id = order.getId();
List<OrderItemAddRequest> orderItemList = orderAddRequest.getOrderItemList();
List<OrderItems> newOrderItemsList = orderItemList.stream().map(orderItem -> {
OrderItems orderItems = new OrderItems();
BeanUtils.copyProperties(orderItem, orderItems);
orderItems.setOrderId(id);
return orderItems;
}).toList();
boolean save = orderItemService.saveBatch(newOrderItemsList);
ThrowUtils.throwIf(!save, ErrorCode.OPERATION_ERROR);
return ResultUtils.success(id);
Long userId = loginUser.getId();
// 创建通用订单(常规类和服务类商品)
Long orderId = orderService.createCommonOrder(orderMainInfoAddRequest, userId);
return ResultUtils.success(orderId);
}
@ -249,5 +291,4 @@ public class OrderController {
}

View File

@ -9,7 +9,7 @@ import java.util.List;
@Data
@Schema(description = "商品批量删除请求体", requiredProperties = {"idList"})
public class CommonDelBatchRequest implements Serializable {
public class CommonBatchRequest implements Serializable {
@Schema(description = "批量删除的商品id列表(id > 0)", example = "[8, 9, 17]")

View File

@ -0,0 +1,69 @@
package com.cultural.heritage.model.dto.cart;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Data;
import java.io.Serial;
import java.io.Serializable;
import java.math.BigDecimal;
import java.util.List;
@Data
@Schema(description = "购物车订单创建请求体", requiredProperties = {"orderType", "userName", "addressId",
"couponId", "totalAmount", "note", "cartPayRequestList"})
public class CartOrderAddRequest implements Serializable {
/**
* 订单类别
*/
@Schema(description = "订单类别", example = "product")
private String orderType;
/**
* 用户昵称
*/
@Schema(description = "用户昵称", example = "张三")
private String userName;
/**
* 地址id
*/
@Schema(description = "地址id(id > 0)", example = "12")
private Long addressId;
/**
* 优惠券id
*/
@Schema(description = "优惠券id(id > 0)", example = "2")
private Long couponId;
/**
* 订单总金额
*/
@Schema(description = "订单总金额", example = "500")
private BigDecimal totalAmount;
/**
* 订单备注
*/
@Schema(description = "订单备注", example = "希望能完整体验非遗文化,保持产品原貌,妥善包装")
private String note;
/**
* 购物车订单明细创建请求体
*/
@Schema(description = "购物车订单明细创建请求体")
private List<CartOrderItemAddRequest> cartOrderItemAddRequestList;
@Serial
private static final long serialVersionUID = 1L;
}

View File

@ -4,15 +4,19 @@ package com.cultural.heritage.model.dto.cart;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Data;
import java.io.Serial;
import java.io.Serializable;
@Data
@Schema(description = "购物车商品项支付请求体", requiredProperties = {"id", "quantity"})
public class CartPayRequest {
@Schema(description = "购物车订单明细创建请求体", requiredProperties = {"cartRecordId", "quantity"})
public class CartOrderItemAddRequest implements Serializable {
/**
* 购物车id
*/
@Schema(description = "购物车id(id > 0)", example = "20")
private Long id;
private Long cartRecordId;
/**
* 数量
@ -21,5 +25,6 @@ public class CartPayRequest {
private Integer quantity;
@Serial
private static final long serialVersionUID = 1L;
}

View File

@ -12,7 +12,7 @@ import java.math.BigDecimal;
import java.util.List;
@Data
@Schema(description = "订单添加请求体", requiredProperties = {"userId", "orderNumber", "addressSnapshot", "contactsSnapshot",
@Schema(description = "订单添加请求体", requiredProperties = {"userId", "addressSnapshot", "contactsSnapshot",
"couponSnapshot", "totalAmount", "orderStatus", "orderItemList"})
public class OrderAddRequest implements Serializable {
@ -30,12 +30,6 @@ public class OrderAddRequest implements Serializable {
@Schema(description = "用户昵称", example = "张三")
private String userName;
/**
* 订单编号
*/
@Schema(description = "订单编号", example = "20241105342838428324834")
private String orderNumber;
/**
* 地址信息快照

View File

@ -1,12 +1,14 @@
package com.cultural.heritage.model.dto.order.capital;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.AllArgsConstructor;
import lombok.Data;
import java.io.Serial;
import java.io.Serializable;
@Data
@AllArgsConstructor
@Schema(description = "订单明细主要信息请求体", requiredProperties = {"goodId", "quantity"})
public class OrderItemMainInfoAddRequest implements Serializable {
@ -41,4 +43,5 @@ public class OrderItemMainInfoAddRequest implements Serializable {
@Serial
private static final long serialVersionUID = 1L;
}

View File

@ -95,6 +95,10 @@ public class Order implements Serializable {
*/
private String note;
/**
* 物流单号
*/
private String trackingNumber;
/**
* 创建时间

View File

@ -77,6 +77,11 @@ public class OrderVO implements Serializable {
*/
private String note;
/**
* 物流单号
*/
private String trackingNumber;
/**
* 订单明细信息

View File

@ -1,6 +1,7 @@
package com.cultural.heritage.service.good;
import com.baomidou.mybatisplus.extension.service.IService;
import com.cultural.heritage.model.dto.cart.CartOrderItemAddRequest;
import com.cultural.heritage.model.dto.cart.CartRecordAddRequest;
import com.cultural.heritage.model.entity.CartRecord;
import com.cultural.heritage.model.vo.cart.CartRecordVO;
@ -28,10 +29,11 @@ public interface CartRecordService extends IService<CartRecord> {
void validCart(CartRecord cartRecord, boolean update);
/**
* 校验购物车商品项的准确性
* 根据购物车订单明细列表校验购物车商品项的准确性
*/
boolean validIsConsistent(CartRecord cartRecord);
boolean validIsConsistent(List<CartOrderItemAddRequest> cartOrderItemAddRequestList);

View File

@ -3,8 +3,10 @@ package com.cultural.heritage.service.good;
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.CartRecord;
import com.cultural.heritage.model.entity.Good;
import java.math.BigDecimal;
import java.util.List;
public interface GoodService extends IService<Good> {
@ -26,4 +28,9 @@ public interface GoodService extends IService<Good> {
*/
void validGood(Good good, boolean update);
/**
* 更新购物车中的小计
*/
List<CartRecord> updateCartGoodPrice(Long goodId, BigDecimal targetPrice);
}

View File

@ -7,12 +7,13 @@ import com.cultural.heritage.exception.BusinessException;
import com.cultural.heritage.exception.ThrowUtils;
import com.cultural.heritage.mapper.CartRecordMapper;
import com.cultural.heritage.mapper.GoodMapper;
import com.cultural.heritage.model.dto.cart.CartOrderItemAddRequest;
import com.cultural.heritage.model.dto.cart.CartRecordAddRequest;
import com.cultural.heritage.model.entity.CartRecord;
import com.cultural.heritage.model.entity.Good;
import com.cultural.heritage.model.entity.User;
import com.cultural.heritage.model.vo.cart.CartRecordVO;
import com.cultural.heritage.model.vo.cart.CartGoodVO;
import com.cultural.heritage.model.vo.cart.CartRecordVO;
import com.cultural.heritage.service.good.CartRecordService;
import com.cultural.heritage.service.good.GoodService;
import com.cultural.heritage.service.user.UserService;
@ -24,7 +25,9 @@ import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@Service
@Slf4j
@ -57,12 +60,22 @@ public class CartRecordServiceImpl extends ServiceImpl<CartRecordMapper, CartRec
queryWrapper.eq("goodId", goodId);
CartRecord cartRecord = this.baseMapper.selectOne(queryWrapper);
if (cartRecord != null) {
// 判断数量是否超出库存
int currentQuantity = cartRecord.getQuantity() + cartRecordAddRequest.getQuantity();
if (currentQuantity > good.getInventory()) {
throw new BusinessException(ErrorCode.SYSTEM_ERROR, "商品库存不足, 无法添加");
}
// 如果购物车中已存在该商品, 就叠加数量
cartRecord.setQuantity(cartRecord.getQuantity() + cartRecordAddRequest.getQuantity());
cartRecord.setSubtotal(good.getPrice().multiply(BigDecimal.valueOf(cartRecord.getQuantity())));
cartRecord.setQuantity(currentQuantity);
cartRecord.setSubtotal(good.getPrice().multiply(BigDecimal.valueOf(currentQuantity)));
boolean result = this.updateById(cartRecord);
ThrowUtils.throwIf(!result, ErrorCode.OPERATION_ERROR, "更新购物车商品数量失败");
} else {
// 判断数量是否超出库存
Integer currentQuantity = cartRecordAddRequest.getQuantity();
if (currentQuantity > good.getInventory()) {
throw new BusinessException(ErrorCode.SYSTEM_ERROR, "商品库存不足, 无法添加");
}
// 如果购物车不存在该商品就向购物车中添加一项
CartRecord cartGood = new CartRecord();
BeanUtils.copyProperties(cartRecordAddRequest, cartGood);
@ -85,6 +98,18 @@ public class CartRecordServiceImpl extends ServiceImpl<CartRecordMapper, CartRec
*/
@Override
public List<CartRecordVO> transformToCartRecordVOList(List<CartRecord> list) {
// 获取所有商品id
List<Long> goodIds = list.stream().map(CartRecord::getGoodId).toList();
QueryWrapper<Good> queryWrapper = new QueryWrapper<>();
queryWrapper.in("id", goodIds);
List<Good> goodList = goodService.list(queryWrapper);
// 封装map集合商品id, 商品详情信息
Map<Long, Good> map = new HashMap<>();
for (Good good : goodList) {
map.put(good.getId(), good);
}
return list.stream().map(cartRecord -> {
// 封装购物车记录VO
CartRecordVO cartRecordVO = new CartRecordVO();
@ -92,9 +117,7 @@ public class CartRecordServiceImpl extends ServiceImpl<CartRecordMapper, CartRec
// 封装商品类GoodVO
Long goodId = cartRecord.getGoodId();
QueryWrapper<Good> queryWrapper = new QueryWrapper<>();
queryWrapper.eq("id", goodId);
Good good = goodMapper.selectOne(queryWrapper);
Good good = map.get(goodId);
CartGoodVO goodVO = new CartGoodVO();
BeanUtils.copyProperties(good, goodVO);
cartRecordVO.setCartGoodVO(goodVO);
@ -134,11 +157,8 @@ public class CartRecordServiceImpl extends ServiceImpl<CartRecordMapper, CartRec
/**
* 校验购物车商品项的准确性
*/
@Override
public boolean validIsConsistent(CartRecord cartRecord) {
private boolean validIsConsistent(CartRecord cartRecord, Good good) {
// 校验购物车中的商品是否存在
Long goodId = cartRecord.getGoodId();
Good good = goodService.getById(goodId);
if (good == null || good.getIsShelves() == 0) {
log.info("商品不存在或者已下架, 购物车id为" + cartRecord.getId());
return false;
@ -154,13 +174,35 @@ public class CartRecordServiceImpl extends ServiceImpl<CartRecordMapper, CartRec
}
/**
* 根据购物车订单明细列表校验购物车商品项的准确性
*/
@Override
public boolean validIsConsistent(List<CartOrderItemAddRequest> cartOrderItemAddRequestList) {
return false;
}
/**
* 找出存在问题的购物车id列表
*/
@Override
public List<Long> getInvalidCartIds(List<CartRecord> cartRecords) {
List<CartRecord> cartRecordList = cartRecords.stream().filter(cartRecord -> !validIsConsistent(cartRecord)).toList();
// 获取购物车的商品id列表
List<Long> goodIds = cartRecords.stream().map(CartRecord::getGoodId).toList();
QueryWrapper<Good> queryWrapper = new QueryWrapper<>();
queryWrapper.in("id", goodIds);
List<Good> goodList = goodService.list(queryWrapper);
// 封装map集合商品id, 商品详情信息
Map<Long, Good> map = new HashMap<>();
for (Good good : goodList) {
map.put(good.getId(), good);
}
List<CartRecord> cartRecordList = cartRecords.stream().filter(cartRecord -> {
Long goodId = cartRecord.getGoodId();
Good good = map.get(goodId);
return !validIsConsistent(cartRecord, good);
}).toList();
return cartRecordList.stream().map(CartRecord::getId).toList();
}
}

View File

@ -5,11 +5,14 @@ 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.CartRecordMapper;
import com.cultural.heritage.mapper.GoodMapper;
import com.cultural.heritage.model.dto.good.GoodQueryRequest;
import com.cultural.heritage.model.entity.CartRecord;
import com.cultural.heritage.model.entity.Good;
import com.cultural.heritage.service.good.GoodService;
import com.cultural.heritage.utils.SqlUtils;
import jakarta.annotation.Resource;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
@ -22,6 +25,11 @@ import java.util.List;
@Service
public class GoodServiceImpl extends ServiceImpl<GoodMapper, Good> implements GoodService {
@Resource
private CartRecordMapper cartRecordMapper;
/**
* 商品查询条件
*/
@ -107,5 +115,21 @@ public class GoodServiceImpl extends ServiceImpl<GoodMapper, Good> implements Go
}
/**
* 更新购物车中的小计
*/
@Override
public List<CartRecord> updateCartGoodPrice(Long goodId, BigDecimal targetPrice) {
QueryWrapper<CartRecord> queryWrapper = new QueryWrapper<>();
queryWrapper.eq("goodId", goodId);
List<CartRecord> cartRecordList = cartRecordMapper.selectList(queryWrapper);
// 更新购物车中的小计
for (CartRecord cartRecord : cartRecordList) {
Integer quantity = cartRecord.getQuantity();
cartRecord.setSubtotal(targetPrice.multiply(BigDecimal.valueOf(quantity)));
}
return cartRecordList;
}
}

View File

@ -26,4 +26,11 @@ public interface OrderService extends IService<Order> {
* 校验订单
*/
void validOrder(Order order);
/**
* 创建通用订单(常规类和服务类商品)
*/
Long createCommonOrder(OrderMainInfoAddRequest orderMainInfoAddRequest, Long userId);
}

View File

@ -5,6 +5,7 @@ import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cultural.heritage.common.ErrorCode;
import com.cultural.heritage.constant.CommonConstant;
import com.cultural.heritage.constant.OrderStatusConstant;
import com.cultural.heritage.exception.BusinessException;
import com.cultural.heritage.exception.ThrowUtils;
import com.cultural.heritage.mapper.*;
@ -18,8 +19,11 @@ import com.cultural.heritage.model.dto.snapshot.ContactsSnapshot;
import com.cultural.heritage.model.dto.snapshot.CouponSnapshot;
import com.cultural.heritage.model.dto.snapshot.GoodSnapshot;
import com.cultural.heritage.model.entity.Order;
import com.cultural.heritage.model.entity.OrderItems;
import com.cultural.heritage.model.enums.GoodTypeEnum;
import com.cultural.heritage.service.order.OrderItemService;
import com.cultural.heritage.service.order.OrderService;
import com.cultural.heritage.utils.OrderNumberUtils;
import com.cultural.heritage.utils.SqlUtils;
import jakarta.annotation.Resource;
import org.apache.commons.lang3.ObjectUtils;
@ -52,6 +56,10 @@ public class OrderServiceImpl extends ServiceImpl<OrderMapper, Order> implements
@Resource
private OrderItemService orderItemService;
/**
* 获取查询条件
*/
@ -178,6 +186,47 @@ public class OrderServiceImpl extends ServiceImpl<OrderMapper, Order> implements
/**
* 创建通用订单(常规类和服务类商品)
*/
@Override
public Long createCommonOrder(OrderMainInfoAddRequest orderMainInfoAddRequest, Long userId) {
// 封装订单详细信息请求体
OrderAddRequest orderAddRequest = this.initOrderMainInfo(orderMainInfoAddRequest);
if (orderAddRequest == null) {
throw new BusinessException(ErrorCode.PARAMS_ERROR);
}
// 创建一个总订单
Order order = new Order();
BeanUtils.copyProperties(orderAddRequest, order);
order.setUserId(userId);
// 生成订单编号
String orderNumber = OrderNumberUtils.generateOrderId();
order.setOrderNumber(orderNumber);
// 设置物流单号
order.setTrackingNumber("");
// 设置订单状态
order.setOrderStatus(OrderStatusConstant.PENDING_PAYMENT);
// 校验订单
this.validOrder(order);
boolean result = this.save(order);
ThrowUtils.throwIf(!result, ErrorCode.OPERATION_ERROR);
// 创建订单明细
Long id = order.getId();
List<OrderItemAddRequest> orderItemList = orderAddRequest.getOrderItemList();
List<OrderItems> newOrderItemsList = orderItemList.stream().map(orderItem -> {
OrderItems orderItems = new OrderItems();
BeanUtils.copyProperties(orderItem, orderItems);
orderItems.setOrderId(id);
return orderItems;
}).toList();
boolean save = orderItemService.saveBatch(newOrderItemsList);
ThrowUtils.throwIf(!save, ErrorCode.OPERATION_ERROR);
return id;
}
/**
* 根据id获取订单(地址联系人优惠券商品)信息
*/
@ -185,20 +234,15 @@ public class OrderServiceImpl extends ServiceImpl<OrderMapper, Order> implements
// 创建查询条件
QueryWrapper<T> queryWrapper = new QueryWrapper<>();
queryWrapper.eq("id", id);
// 查询实体对象
T entity = mapper.selectOne(queryWrapper);
// 判断实体对象是否存在不存在则抛出异常
ThrowUtils.throwIf(entity == null, ErrorCode.NOT_FOUND_ERROR);
try {
// 创建快照对象实例
S snapshot = snapshotClass.getDeclaredConstructor().newInstance();
// 复制属性到快照对象
BeanUtils.copyProperties(entity, snapshot);
return snapshot;
} catch (Exception e) {
throw new BusinessException(ErrorCode.SYSTEM_ERROR);