更新了商品类别

This commit is contained in:
chen-xin-zhi 2024-12-20 22:50:06 +08:00
parent 04a727d66f
commit c969bf0ef5
5 changed files with 55 additions and 15 deletions

View File

@ -31,10 +31,7 @@ import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController; import org.springframework.web.bind.annotation.RestController;
import java.math.BigDecimal; import java.math.BigDecimal;
import java.util.ArrayList; import java.util.*;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@RestController @RestController
@RequestMapping("/cart") @RequestMapping("/cart")
@ -106,6 +103,7 @@ public class CartRecordController {
} }
/** /**
* 用户删除购物车的物品 * 用户删除购物车的物品
* @param idList 删除的商品id列表 * @param idList 删除的商品id列表
@ -124,6 +122,7 @@ public class CartRecordController {
} }
/** /**
* 小程序端用户查询购物车中的商品 * 小程序端用户查询购物车中的商品
* @return 商品列表信息 * @return 商品列表信息
@ -136,6 +135,7 @@ public class CartRecordController {
Long userId = loginUser.getId(); Long userId = loginUser.getId();
QueryWrapper<CartRecord> queryWrapper = new QueryWrapper<>(); QueryWrapper<CartRecord> queryWrapper = new QueryWrapper<>();
queryWrapper.eq("userId", userId); queryWrapper.eq("userId", userId);
queryWrapper.orderByDesc("id");
List<CartRecord> list = cartRecordService.list(queryWrapper); List<CartRecord> list = cartRecordService.list(queryWrapper);
// 封装用户购物车中的商品信息 // 封装用户购物车中的商品信息
List<CartRecordVO> cartRecordVOList = cartRecordService.transformToCartRecordVOList(list); List<CartRecordVO> cartRecordVOList = cartRecordService.transformToCartRecordVOList(list);
@ -215,6 +215,7 @@ public class CartRecordController {
cartOrderVO.setQuantity(quantity); cartOrderVO.setQuantity(quantity);
cartOrderVOList.add(cartOrderVO); cartOrderVOList.add(cartOrderVO);
} }
Collections.reverse(cartOrderVOList);
return ResultUtils.success(cartOrderVOList); return ResultUtils.success(cartOrderVOList);
} }

View File

@ -233,7 +233,10 @@ public class CategoryController {
if (StringUtils.isBlank(typeName)) { if (StringUtils.isBlank(typeName)) {
throw new BusinessException(ErrorCode.PARAMS_ERROR); throw new BusinessException(ErrorCode.PARAMS_ERROR);
} }
List<Good> goodList = goodService.getGoodListByTypeName(typeName); QueryWrapper<Good> queryWrapper = new QueryWrapper<>();
queryWrapper.eq("type", typeName);
queryWrapper.orderByDesc("id");
List<Good> goodList = goodService.list(queryWrapper);
Map<Long, List<Good>> map = new HashMap<>(); Map<Long, List<Good>> map = new HashMap<>();
map.put(id, goodList); map.put(id, goodList);
return ResultUtils.success(map); return ResultUtils.success(map);

View File

@ -39,10 +39,7 @@ import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController; import org.springframework.web.bind.annotation.RestController;
import java.math.BigDecimal; import java.math.BigDecimal;
import java.util.ArrayList; import java.util.*;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@RestController @RestController
@RequestMapping("/order") @RequestMapping("/order")
@ -106,13 +103,15 @@ public class OrderController {
queryWrapper.in("id", cartIds); queryWrapper.in("id", cartIds);
List<CartRecord> cartRecordList = cartRecordService.list(queryWrapper); List<CartRecord> cartRecordList = cartRecordService.list(queryWrapper);
// 计算订单总金额 List<Long> goodIds = cartRecordList.stream().map(CartRecord::getGoodId).toList();
BigDecimal totalAmount = BigDecimal.ZERO; QueryWrapper<Good> goodQueryWrapper = new QueryWrapper<>();
for (CartRecord cartRecord : cartRecordList) { goodQueryWrapper.in("id", goodIds);
totalAmount = totalAmount.add(cartRecord.getSubtotal()); List<Good> goodList = goodService.list(goodQueryWrapper);
// 封装map集合商品id, 商品信息
Map<Long, Good> goodMap = new HashMap<>();
for (Good good : goodList) {
goodMap.put(good.getId(), good);
} }
orderMainInfoAddRequest.setTotalAmount(totalAmount);
// 封装成一个map集合购物车id, 购物车商品数量 // 封装成一个map集合购物车id, 购物车商品数量
Map<Long, Integer> map = new HashMap<>(); Map<Long, Integer> map = new HashMap<>();
for (CartOrderItemAddRequest cartOrderItemAddRequest : cartOrderItemAddRequestList) { for (CartOrderItemAddRequest cartOrderItemAddRequest : cartOrderItemAddRequestList) {
@ -120,6 +119,18 @@ public class OrderController {
Integer quantity = cartOrderItemAddRequest.getQuantity(); Integer quantity = cartOrderItemAddRequest.getQuantity();
map.put(cartRecordId, quantity); map.put(cartRecordId, quantity);
} }
// 计算订单总金额
BigDecimal totalAmount = BigDecimal.ZERO;
for (CartRecord cartRecord : cartRecordList) {
Long goodId = cartRecord.getGoodId();
Good good = goodMap.get(goodId);
Long cartRecordId = cartRecord.getId();
Integer quantity = map.get(cartRecordId);
totalAmount = totalAmount.add(good.getPrice().multiply(BigDecimal.valueOf(quantity)));
}
orderMainInfoAddRequest.setTotalAmount(totalAmount);
// 封装成订单明细主要信息请求体列表 // 封装成订单明细主要信息请求体列表
List<OrderItemMainInfoAddRequest> orderItemMainInfoAddRequestList = new ArrayList<>(); List<OrderItemMainInfoAddRequest> orderItemMainInfoAddRequestList = new ArrayList<>();
OrderItemMainInfoAddRequest orderItemMainInfoAddRequest; OrderItemMainInfoAddRequest orderItemMainInfoAddRequest;
@ -156,6 +167,9 @@ public class OrderController {
Long userId = loginUser.getId(); Long userId = loginUser.getId();
// 校验单独购买的商品数据是否准确 // 校验单独购买的商品数据是否准确
orderService.validSingleGoodOrder(orderMainInfoAddRequest); orderService.validSingleGoodOrder(orderMainInfoAddRequest);
// 计算单个商品购买的总金额
BigDecimal totalAmount = orderService.calculateTotalAmount(orderMainInfoAddRequest);
orderMainInfoAddRequest.setTotalAmount(totalAmount);
// 创建通用订单(常规类和服务类商品) // 创建通用订单(常规类和服务类商品)
Long orderId = orderService.createCommonOrder(orderMainInfoAddRequest, userId, false, null); Long orderId = orderService.createCommonOrder(orderMainInfoAddRequest, userId, false, null);
return ResultUtils.success(orderId); return ResultUtils.success(orderId);
@ -214,6 +228,7 @@ public class OrderController {
Long id = loginUser.getId(); Long id = loginUser.getId();
QueryWrapper<Order> queryWrapper = new QueryWrapper<>(); QueryWrapper<Order> queryWrapper = new QueryWrapper<>();
queryWrapper.eq("userId", id); queryWrapper.eq("userId", id);
queryWrapper.orderByDesc("id");
List<Order> orders = orderService.list(queryWrapper); List<Order> orders = orderService.list(queryWrapper);
List<OrderVO> orderVOS = orders.stream().map(order -> { List<OrderVO> orderVOS = orders.stream().map(order -> {
Long orderId = order.getId(); Long orderId = order.getId();

View File

@ -7,6 +7,7 @@ import com.cultural.heritage.model.dto.order.OrderQueryRequest;
import com.cultural.heritage.model.dto.order.capital.OrderMainInfoAddRequest; import com.cultural.heritage.model.dto.order.capital.OrderMainInfoAddRequest;
import com.cultural.heritage.model.entity.Order; import com.cultural.heritage.model.entity.Order;
import java.math.BigDecimal;
import java.util.List; import java.util.List;
public interface OrderService extends IService<Order> { public interface OrderService extends IService<Order> {
@ -41,4 +42,11 @@ public interface OrderService extends IService<Order> {
* 校验单独购买的商品数据是否准确 * 校验单独购买的商品数据是否准确
*/ */
void validSingleGoodOrder(OrderMainInfoAddRequest orderMainInfoAddRequest); void validSingleGoodOrder(OrderMainInfoAddRequest orderMainInfoAddRequest);
/**
* 计算单个商品购买的总金额
*/
BigDecimal calculateTotalAmount(OrderMainInfoAddRequest orderMainInfoAddRequest);
} }

View File

@ -286,6 +286,19 @@ public class OrderServiceImpl extends ServiceImpl<OrderMapper, Order> implements
/**
* 计算单个商品购买的总金额
*/
@Override
public BigDecimal calculateTotalAmount(OrderMainInfoAddRequest orderMainInfoAddRequest) {
List<OrderItemMainInfoAddRequest> orderItemMainInfoAddRequestList = orderMainInfoAddRequest.getOrderItemMainInfoAddRequestList();
OrderItemMainInfoAddRequest orderItemMainInfoAddRequest = orderItemMainInfoAddRequestList.get(0);
Integer quantity = orderItemMainInfoAddRequest.getQuantity();
Long goodId = orderItemMainInfoAddRequest.getGoodId();
Good good = goodService.getById(goodId);
return good.getPrice().multiply(BigDecimal.valueOf(quantity));
}
/** /**
* 根据id获取订单(地址联系人优惠券商品)信息 * 根据id获取订单(地址联系人优惠券商品)信息