refactor: 购物车重构

This commit is contained in:
hxr 2024-03-31 23:56:54 +08:00
parent a1c68b15dc
commit f0bbb626ae
13 changed files with 88 additions and 70 deletions

View File

@ -2,7 +2,7 @@ package com.youlai.mall.oms.controller.app;
import com.youlai.common.result.Result;
import com.youlai.common.security.util.SecurityUtils;
import com.youlai.mall.oms.model.dto.CartItemDTO;
import com.youlai.mall.oms.model.dto.CartItemDto;
import com.youlai.mall.oms.service.app.CartService;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Operation;
@ -30,7 +30,7 @@ public class CartController {
@Operation(summary = "查询购物车")
@GetMapping
public <T> Result<T> getCart() {
List<CartItemDTO> result = cartService.listCartItems(SecurityUtils.getMemberId());
List<CartItemDto> result = cartService.listCartItems(SecurityUtils.getMemberId());
return Result.success((T) result);
}
@ -52,7 +52,7 @@ public class CartController {
@PutMapping("/skuId/{skuId}")
public <T> Result<T> updateCartItem(
@PathVariable Long skuId,
@RequestBody CartItemDTO cartItem
@RequestBody CartItemDto cartItem
) {
cartItem.setSkuId(skuId);
boolean result = cartService.updateCartItem(cartItem);

View File

@ -1,7 +1,7 @@
package com.youlai.mall.oms.converter;
import com.youlai.mall.oms.model.dto.CartItemDTO;
import com.youlai.mall.oms.model.dto.CartItemDto;
import com.youlai.mall.pms.model.dto.SkuInfoDTO;
import org.mapstruct.Mapper;
import org.mapstruct.Mapping;
@ -19,6 +19,6 @@ public interface CartConverter {
@Mappings({
@Mapping(target = "skuId", source = "id"),
})
CartItemDTO sku2CartItem(SkuInfoDTO skuInfo);
CartItemDto sku2CartItem(SkuInfoDTO skuInfo);
}

View File

@ -0,0 +1,30 @@
package com.youlai.mall.oms.model.dto;
import lombok.Data;
import java.io.Serializable;
/**
* 购物车商品项
*
* @author Ray Hao
* @since 0.0.1
*/
@Data
public class CartItemDto implements Serializable {
/**
* 商品库存ID
*/
private Long skuId;
/**
* 商品数量
*/
private Integer count;
/**
* 是否选中
*/
private Boolean checked;
}

View File

@ -3,12 +3,16 @@ package com.youlai.mall.oms.model.dto;
import lombok.Data;
import java.io.Serializable;
import java.util.Set;
/**
* 购物车商品传输层实体
* 购物车商品项
*
* @author Ray Hao
* @since 0.0.1
*/
@Data
public class CartItemDTO implements Serializable {
public class CartItemVo implements Serializable {
/**
* 商品库存ID
@ -16,48 +20,38 @@ public class CartItemDTO implements Serializable {
private Long skuId;
/**
* 商品库存名称
* 商品名称
*/
private String skuName;
private String spuName;
/**
* 商品编码
* 规格集合
*/
private String skuSn;
private Set<String> specs;
/**
* 商品图片
*/
private String picUrl;
private String imageUrl;
/**
* 商品数量
* 加购数量·
*/
private Integer count;
/**
* 加入购物车时价格因会变动不能作为订单计算因子订单提交时需验价
* 商品价格
*/
private Long price;
/**
* 优惠券金额
*/
private Long coupon;
/**
* 是否选中
*/
private Boolean checked;
/**
* 商品库存数量页面控制能选择最大数量
* 商品库存
*/
private Integer stock;
/**
* 商品名称
*/
private String spuName;
}

View File

@ -1,6 +1,6 @@
package com.youlai.mall.oms.service.app;
import com.youlai.mall.oms.model.dto.CartItemDTO;
import com.youlai.mall.oms.model.dto.CartItemDto;
import java.util.List;
@ -12,13 +12,13 @@ import java.util.List;
*/
public interface CartService {
List<CartItemDTO> listCartItems(Long memberId);
List<CartItemDto> listCartItems(Long memberId);
boolean deleteCart();
boolean addCartItem(Long skuId);
boolean updateCartItem(CartItemDTO cartItem);
boolean updateCartItem(CartItemDto cartItem);
boolean removeCartItem(Long skuId);

View File

@ -5,7 +5,7 @@ import com.youlai.common.security.util.SecurityUtils;
import com.youlai.common.web.exception.BizException;
import com.youlai.mall.oms.constant.OrderConstants;
import com.youlai.mall.oms.converter.CartConverter;
import com.youlai.mall.oms.model.dto.CartItemDTO;
import com.youlai.mall.oms.model.dto.CartItemDto;
import com.youlai.mall.oms.service.app.CartService;
import com.youlai.mall.pms.api.SkuFeignClient;
import com.youlai.mall.pms.model.dto.SkuInfoDTO;
@ -39,10 +39,10 @@ public class CartServiceImpl implements CartService {
private final CartConverter cartConverter;
@Override
public List<CartItemDTO> listCartItems(Long memberId) {
public List<CartItemDto> listCartItems(Long memberId) {
if (memberId != null) {
BoundHashOperations cartHashOperations = getCartHashOperations(memberId);
List<CartItemDTO> cartItems = cartHashOperations.values();
List<CartItemDto> cartItems = cartHashOperations.values();
return cartItems;
}
return Collections.EMPTY_LIST;
@ -64,10 +64,10 @@ public class CartServiceImpl implements CartService {
@Override
public boolean addCartItem(Long skuId) {
Long memberId = SecurityUtils.getMemberId();
BoundHashOperations<String, String, CartItemDTO> cartHashOperations = getCartHashOperations(memberId);
BoundHashOperations<String, String, CartItemDto> cartHashOperations = getCartHashOperations(memberId);
String hKey = String.valueOf(skuId);
CartItemDTO cartItem = cartHashOperations.get(hKey);
CartItemDto cartItem = cartHashOperations.get(hKey);
if (cartItem != null) {
// 购物车已存在该商品更新商品数量
@ -90,7 +90,7 @@ public class CartServiceImpl implements CartService {
* 更新购物车总商品数量选中状态
*/
@Override
public boolean updateCartItem(CartItemDTO cartItem) {
public boolean updateCartItem(CartItemDto cartItem) {
Long memberId;
try {
memberId = SecurityUtils.getMemberId();
@ -100,7 +100,7 @@ public class CartServiceImpl implements CartService {
BoundHashOperations cartHashOperations = getCartHashOperations(memberId);
String hKey = cartItem.getSkuId() + "";
if (cartHashOperations.get(hKey) != null) {
CartItemDTO cacheCartItem = (CartItemDTO) cartHashOperations.get(hKey);
CartItemDto cacheCartItem = (CartItemDto) cartHashOperations.get(hKey);
if (cartItem.getChecked() != null) {
cacheCartItem.setChecked(cartItem.getChecked());
}
@ -143,7 +143,7 @@ public class CartServiceImpl implements CartService {
}
BoundHashOperations cartHashOperations = getCartHashOperations(memberId);
for (Object value : cartHashOperations.values()) {
CartItemDTO cartItem = (CartItemDTO) value;
CartItemDto cartItem = (CartItemDto) value;
cartItem.setChecked(checked);
String hKey = cartItem.getSkuId() + "";
cartHashOperations.put(hKey, cartItem);
@ -165,7 +165,7 @@ public class CartServiceImpl implements CartService {
}
BoundHashOperations cartHashOperations = getCartHashOperations(memberId);
for (Object value : cartHashOperations.values()) {
CartItemDTO cartItem = (CartItemDTO) value;
CartItemDto cartItem = (CartItemDto) value;
if (cartItem.getChecked()) {
cartHashOperations.delete(cartItem.getSkuId() + "");
}

View File

@ -31,7 +31,7 @@ import com.youlai.mall.oms.enums.OrderStatusEnum;
import com.youlai.mall.oms.enums.PaymentMethodEnum;
import com.youlai.mall.oms.mapper.OrderMapper;
import com.youlai.mall.oms.model.bo.OrderBO;
import com.youlai.mall.oms.model.dto.CartItemDTO;
import com.youlai.mall.oms.model.dto.CartItemDto;
import com.youlai.mall.oms.model.dto.OrderItemDTO;
import com.youlai.mall.oms.model.entity.OmsOrder;
import com.youlai.mall.oms.model.entity.OmsOrderItem;
@ -461,9 +461,9 @@ public class OrderServiceImpl extends ServiceImpl<OrderMapper, OmsOrder> impleme
orderItemDTO.setQuantity(1); // 直接购买商品的数量为1
orderItems.add(orderItemDTO);
} else { // 购物车结算
List<CartItemDTO> cartItems = cartService.listCartItems(memberId);
List<CartItemDto> cartItems = cartService.listCartItems(memberId);
orderItems = cartItems.stream()
.filter(CartItemDTO::getChecked)
.filter(CartItemDto::getChecked)
.map(cartItem -> {
OrderItemDTO orderItemDTO = new OrderItemDTO();
BeanUtil.copyProperties(cartItem, orderItemDTO);

View File

@ -1,7 +1,7 @@
package com.youlai.mall.sms.controller.app;
import com.youlai.common.result.Result;
import com.youlai.mall.sms.model.vo.AdBannerVO;
import com.youlai.mall.sms.model.vo.BannerVO;
import com.youlai.mall.sms.service.SmsAdvertService;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Operation;
@ -21,10 +21,10 @@ import java.util.List;
public class AdvertController {
private SmsAdvertService smsAdvertService;
@Operation(summary= "广告横幅列表")
@Operation(summary= "APP首页广告横幅列表")
@GetMapping("/banners")
public Result<List<AdBannerVO>> listAdBanners() {
List<AdBannerVO> list = smsAdvertService.listAdBanners();
public Result<List<BannerVO>> getBannerList() {
List<BannerVO> list = smsAdvertService.getBannerList();
return Result.success(list);
}
}

View File

@ -3,7 +3,7 @@ package com.youlai.mall.sms.converter;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.youlai.mall.sms.model.entity.SmsAdvert;
import com.youlai.mall.sms.model.vo.AdBannerVO;
import com.youlai.mall.sms.model.vo.BannerVO;
import com.youlai.mall.sms.model.vo.AdvertPageVO;
import org.mapstruct.Mapper;
@ -22,7 +22,7 @@ public interface AdvertConverter {
Page<AdvertPageVO> entity2PageVo(Page<SmsAdvert> po);
AdBannerVO entity2BannerVo(SmsAdvert entity);
BannerVO entity2BannerVo(SmsAdvert entity);
List<AdBannerVO> entity2BannerVo(List<SmsAdvert> entities);
List<BannerVO> entity2BannerVo(List<SmsAdvert> entities);
}

View File

@ -16,10 +16,10 @@ public class SmsAdvert extends BaseEntity {
private String title;
private String picUrl;
private String imageUrl;
@JsonFormat( pattern = "yyyy-MM-dd", timezone = "GMT+8")
private Date beginTime;
private Date startTime;
@JsonFormat(pattern = "yyyy-MM-dd", timezone = "GMT+8")
private Date endTime;

View File

@ -3,15 +3,15 @@ package com.youlai.mall.sms.model.vo;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Data;
@Schema(description = "广告横幅对象")
@Schema(description = "横幅视图对象")
@Data
public class AdBannerVO {
public class BannerVO {
@Schema(description="广告标题")
private String title;
@Schema(description="横幅图片URL")
private String picUrl;
private String imageUrl;
@Schema(description="跳转URL")
private String redirectUrl;

View File

@ -4,7 +4,7 @@ import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.IService;
import com.youlai.mall.sms.model.entity.SmsAdvert;
import com.youlai.mall.sms.model.query.AdvertPageQuery;
import com.youlai.mall.sms.model.vo.AdBannerVO;
import com.youlai.mall.sms.model.vo.BannerVO;
import com.youlai.mall.sms.model.vo.AdvertPageVO;
import java.util.List;
@ -19,5 +19,5 @@ public interface SmsAdvertService extends IService<SmsAdvert> {
*/
Page<AdvertPageVO> getAdvertPage(AdvertPageQuery queryParams);
List<AdBannerVO> listAdBanners();
List<BannerVO> getBannerList();
}

View File

@ -8,7 +8,7 @@ import com.youlai.mall.sms.converter.AdvertConverter;
import com.youlai.mall.sms.model.entity.SmsAdvert;
import com.youlai.mall.sms.mapper.SmsAdvertMapper;
import com.youlai.mall.sms.model.query.AdvertPageQuery;
import com.youlai.mall.sms.model.vo.AdBannerVO;
import com.youlai.mall.sms.model.vo.BannerVO;
import com.youlai.mall.sms.model.vo.AdvertPageVO;
import com.youlai.mall.sms.service.SmsAdvertService;
import lombok.RequiredArgsConstructor;
@ -19,7 +19,7 @@ import java.util.List;
/**
* 广告业务实现类
*
* @author haoxr
* @author Ray Hao
* @since 2022/5/28
*/
@Service
@ -31,33 +31,27 @@ public class SmsAdvertServiceImpl extends ServiceImpl<SmsAdvertMapper, SmsAdvert
/**
* 广告分页列表
*
* @param queryParams
* @return
* @param queryParams 查询参数
* @return 广告分页列表
*/
@Override
public Page<AdvertPageVO> getAdvertPage(AdvertPageQuery queryParams) {
Page<SmsAdvert> entities = this.baseMapper.getAdvertPage(new Page<>(queryParams.getPageNum(),
Page<SmsAdvert> page = this.baseMapper.getAdvertPage(new Page<>(queryParams.getPageNum(),
queryParams.getPageSize()),
queryParams);
Page<AdvertPageVO> advertVOPage = advertConverter.entity2PageVo(entities);
return advertVOPage;
return advertConverter.entity2PageVo(page);
}
/**
* 广告横幅列表对象
*
* @return
* 获取广告横幅列表
*/
@Override
public List<AdBannerVO> listAdBanners() {
public List<BannerVO> getBannerList() {
List<SmsAdvert> entities = this.list(new LambdaQueryWrapper<SmsAdvert>().
eq(SmsAdvert::getStatus, StatusEnum.ENABLE.getValue())
.select(SmsAdvert::getTitle, SmsAdvert::getPicUrl, SmsAdvert::getRedirectUrl)
.select(SmsAdvert::getTitle, SmsAdvert::getImageUrl, SmsAdvert::getRedirectUrl)
);
List<AdBannerVO> list = advertConverter.entity2BannerVo(entities);
return list;
return advertConverter.entity2BannerVo(entities);
}
}