✅P279_商城业务-订单服务-构造订单项数据
大约 2 分钟
远程查询Spu信息
cfmall-order/src/main/java/com/gyz/cfmall/order/feign/ProductFeignService.java
package com.gyz.cfmall.order.feign;
import com.gyz.common.utils.R;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
@FeignClient("cfmall-product")
public interface ProductFeignService {
/**
* 根据skuId查询spu的信息
*
* @param skuId
* @return
*/
@GetMapping(value = "/product/spuinfo/skuId/{skuId}")
public R getSpuInfoBySkuId(@PathVariable("skuId") Long skuId);
}
cfmall-product/src/main/java/com/gyz/cfmall/product/controller/SpuInfoController.java
/**
* 根据skuId查询spu的信息
* @param skuId
* @return
*/
@GetMapping(value = "/skuId/{skuId}")
public R getSpuInfoBySkuId(@PathVariable("skuId") Long skuId) {
SpuInfoEntity spuInfoEntity = spuInfoService.getSpuInfoBySkuId(skuId);
return R.ok().setData(spuInfoEntity);
}
cfmall-product/src/main/java/com/gyz/cfmall/product/service/impl/SpuInfoServiceImpl.java
@Override
public SpuInfoEntity getSpuInfoBySkuId(Long skuId) {
//先查询sku表里的数据
SkuInfoEntity skuInfoEntity = skuInfoService.getById(skuId);
//获得spuId
Long spuId = skuInfoEntity.getSpuId();
//再通过spuId查询spuInfo信息表里的数据
SpuInfoEntity spuInfoEntity = this.baseMapper.selectById(spuId);
return spuInfoEntity;
}
构建订单项信息
cfmall-order/src/main/java/com/gyz/cfmall/order/service/impl/OrderServiceImpl.java
com.gyz.cfmall.order.service.impl.OrderServiceImpl#createOrder
private OrderCreateTo createOrder() {
OrderCreateTo createTo = new OrderCreateTo();
//构建订单信息
OrderEntity orderEntity = this.builderOrder();
//构建订单项信息
List<OrderItemEntity> itemEntityList = this.builderOrderItems(orderEntity.getOrderSn());
return createTo;
}
com.gyz.cfmall.order.service.impl.OrderServiceImpl#builderOrderItems
private List<OrderItemEntity> builderOrderItems(String orderSn) {
List<OrderItemEntity> orderItemEntityList = new ArrayList<>();
//最后确定每个购物项的价格
List<OrderItemVo> currentCartItems = cartFeignService.getCurrentCartItems();
if (currentCartItems != null && currentCartItems.size() > 0) {
orderItemEntityList = currentCartItems.stream().map((items) -> {
//构建订单项数据
OrderItemEntity orderItemEntity = builderOrderItem(items);
orderItemEntity.setOrderSn(orderSn);
return orderItemEntity;
}).collect(Collectors.toList());
}
return orderItemEntityList;
}
com.gyz.cfmall.order.service.impl.OrderServiceImpl#builderOrderItem
/**
* 构建某一个订单项的数据
* @param items
* @return
*/
private OrderItemEntity builderOrderItem(OrderItemVo items) {
OrderItemEntity orderItemEntity = new OrderItemEntity();
//1、商品的spu信息
Long skuId = items.getSkuId();
//获取spu的信息
R spuInfo = productFeignService.getSpuInfoBySkuId(skuId);
SpuInfoVo spuInfoData = spuInfo.getData("data", new TypeReference<SpuInfoVo>() {
});
orderItemEntity.setSpuId(spuInfoData.getId());
orderItemEntity.setSpuName(spuInfoData.getSpuName());
orderItemEntity.setSpuBrand(spuInfoData.getBrandName());
orderItemEntity.setCategoryId(spuInfoData.getCatalogId());
//2、商品的sku信息
orderItemEntity.setSkuId(skuId);
orderItemEntity.setSkuName(items.getTitle());
orderItemEntity.setSkuPic(items.getImage());
orderItemEntity.setSkuPrice(items.getPrice());
orderItemEntity.setSkuQuantity(items.getCount());
//使用StringUtils.collectionToDelimitedString将list集合转换为String
String skuAttrValues = StringUtils.collectionToDelimitedString(items.getSkuAttrValues(), ";");
orderItemEntity.setSkuAttrsVals(skuAttrValues);
//3、商品的优惠信息
//4、商品的积分信息
orderItemEntity.setGiftGrowth(items.getPrice().multiply(new BigDecimal(items.getCount())).intValue());
orderItemEntity.setGiftIntegration(items.getPrice().multiply(new BigDecimal(items.getCount())).intValue());
return orderItemEntity;
}
cfmall-order/src/main/java/com/gyz/cfmall/order/to/SpuInfoVo.java
package com.gyz.cfmall.order.to;
import lombok.Data;
import java.math.BigDecimal;
import java.util.Date;
@Data
public class SpuInfoVo {
/**
* 商品id
*/
private Long id;
/**
* 商品名称
*/
private String spuName;
/**
* 商品描述
*/
private String spuDescription;
/**
* 所属分类id
*/
private Long catalogId;
/**
* 品牌id
*/
private Long brandId;
/**
* 品牌名
*/
private String brandName;
private BigDecimal weight;
/**
* 上架状态[0 - 下架,1 - 上架]
*/
private Integer publishStatus;
private Date createTime;
private Date updateTime;
}