mall: 下单的代码 review

This commit is contained in:
zhijiantianya@gmail.com 2022-09-30 09:53:45 +00:00
parent 952e6aa4a8
commit 67436ea4c6
21 changed files with 48 additions and 56 deletions

View File

@ -31,10 +31,11 @@ public interface ProductSkuApi {
List<ProductSkuRespDTO> getSkuList(Collection<Long> ids);
/**
* 批量扣减sku库存
* 批量扣减 SKU 库存
*
* @param batchReqDTO sku库存信息列表
*/
// TODO @LeeYan9: decrementSkuStockBatch? 啊哈, 动名词;
void decrementStockBatch(SkuDecrementStockBatchReqDTO batchReqDTO);
}

View File

@ -7,6 +7,7 @@ import lombok.NoArgsConstructor;
import java.util.List;
/**
* TODO @LeeYan9: 1) 类注释; 2) Product 开头哈;
* @author LeeYan9
* @since 2022-08-26
*/
@ -15,7 +16,7 @@ import java.util.List;
@AllArgsConstructor
public class SkuDecrementStockBatchReqDTO {
// TODO @LeeYan9: 参数校验
private List<Item> items;
@Data
@ -24,6 +25,7 @@ public class SkuDecrementStockBatchReqDTO {
/**
* 商品 SPU 编号自增
*/
// TODO @LeeYan9: 是不是不用传递哈
private Long productId;
/**
@ -38,6 +40,7 @@ public class SkuDecrementStockBatchReqDTO {
}
// TODO @LeeYan9: 构造方法, 是不是可以满足啦
public static SkuDecrementStockBatchReqDTO of(List<Item> items) {
return new SkuDecrementStockBatchReqDTO(items);
}

View File

@ -7,6 +7,7 @@ import lombok.Data;
import java.util.List;
// TODO @LeeYan9: ProductSpuRespDTO
/**
* @author LeeYan9
* @since 2022-08-26

View File

@ -16,6 +16,7 @@ import java.util.Collections;
import java.util.List;
/**
* TODO LeeYan9: 类注释;
* @author LeeYan9
* @since 2022-09-06
*/
@ -28,6 +29,7 @@ public class ProductSkuApiImpl implements ProductSkuApi {
@Override
public List<SkuInfoRespDTO> getSkusByIds(Collection<Long> skuIds) {
// TODO TODO LeeYan9: AllEmpty?
if (CollectionUtils.isAnyEmpty(skuIds)) {
return Collections.emptyList();
}
@ -38,6 +40,7 @@ public class ProductSkuApiImpl implements ProductSkuApi {
@Override
@Transactional(rollbackFor = Exception.class)
public void decrementStockBatch(SkuDecrementStockBatchReqDTO batchReqDTO) {
// TODO @LeeYan9: 最好 Service for 循环;
productSkuMapper.decrementStockBatch(batchReqDTO.getItems());
}
}

View File

@ -14,6 +14,8 @@ import java.util.Collections;
import java.util.List;
/**
* TODO LeeYan9: 类注释;
*
* @author LeeYan9
* @since 2022-09-06
*/
@ -26,6 +28,7 @@ public class ProductSpuApiImpl implements ProductSpuApi {
@Override
public List<SpuInfoRespDTO> getSpuList(Collection<Long> spuIds) {
// TODO TODO LeeYan9: AllEmpty?
if (CollectionUtils.isAnyEmpty(spuIds)) {
return Collections.emptyList();
}

View File

@ -38,5 +38,4 @@ public interface ProductSkuConvert {
List<SkuInfoRespDTO> convertList03(List<ProductSkuDO> list);
}

View File

@ -37,5 +37,4 @@ public interface ProductSpuConvert {
List<SpuInfoRespDTO> convertList2(List<ProductSpuDO> list);
}

View File

@ -23,16 +23,13 @@ import javax.servlet.http.HttpServletRequest;
@Api(tags = "用户 App - 交易订单")
@RestController
@RequestMapping("/trade/order")
@RequiredArgsConstructor
@RequiredArgsConstructor // TODO @LeeYan9: 先统一使用 @Resource 注入哈; 项目只有三层, 依赖注入会存在, 所以使用 @Resource; 也因此, 最好全局保持一致
@Validated
@Slf4j
public class AppTradeOrderController {
// TODO 在思考下
private final TradeOrderService tradeOrderService;
@GetMapping("/get-create-info")
@ApiOperation("基于商品,确认创建订单")
@PreAuthenticated
@ -46,15 +43,12 @@ public class AppTradeOrderController {
@PreAuthenticated
public CommonResult<Long> createTradeOrder(@RequestBody AppTradeOrderCreateReqVO createReqVO,
HttpServletRequest servletRequest) {
// return success(tradeOrderService.createTradeOrder(UserSecurityContextHolder.getUserId(),
// HttpUtil.getIp(servletRequest), createReqVO));
// 获取登录用户
Long loginUserId = SecurityFrameworkUtils.getLoginUserId();
// 获取用户ip地址
String clientIp = ServletUtil.getClientIP(servletRequest);
// 创建交易订单预支付记录
Long result = tradeOrderService.createTradeOrder(loginUserId, clientIp, createReqVO);
return CommonResult.success(result);
}

View File

@ -8,16 +8,11 @@ import org.mapstruct.Mapping;
import org.mapstruct.Mappings;
import org.mapstruct.factory.Mappers;
/**
* @author LeeYan9
* @since 2022-08-26
*/
@Mapper
public interface TradeOrderConvert {
TradeOrderConvert INSTANCE = Mappers.getMapper(TradeOrderConvert.class);
@Mappings({
@Mapping(source = "order.couponId", target = "couponId"),
@Mapping(target = "remark", ignore = true),
@ -25,4 +20,5 @@ public interface TradeOrderConvert {
@Mapping(source = "createVO.addressId", target = "receiverAreaId")
})
TradeOrderDO convert(AppTradeOrderCreateReqVO createVO, PriceCalculateRespDTO.Order order);
}

View File

@ -7,18 +7,11 @@ import org.mapstruct.factory.Mappers;
import java.util.List;
/**
* @author LeeYan9
* @since 2022-08-26
*/
@Mapper
public interface TradeOrderItemConvert {
TradeOrderItemConvert INSTANCE = Mappers.getMapper(TradeOrderItemConvert.class);
/**
* @param items sku列表价格
* @return 订单项
*/
List<TradeOrderItemDO> convertList(List<PriceCalculateRespDTO.OrderItem> items);
}

View File

@ -11,10 +11,6 @@ import org.mapstruct.factory.Mappers;
import java.util.Date;
/**
* @author LeeYan9
* @since 2022-08-26
*/
@Mapper
public interface PayOrderConvert {
@ -30,4 +26,5 @@ public interface PayOrderConvert {
default Date convertCreateTimeToPayExpireTime(Date cancelTime) {
return DateUtil.offsetMinute(new Date(), 30);
}
}

View File

@ -7,10 +7,6 @@ import org.mapstruct.Mapping;
import org.mapstruct.Mappings;
import org.mapstruct.factory.Mappers;
/**
* @author LeeYan9
* @since 2022-08-26
*/
@Mapper
public interface PriceConvert {
@ -19,5 +15,6 @@ public interface PriceConvert {
@Mappings(
@Mapping(source = "userId" , target = "userId")
)
PriceCalculateReqDTO convert(AppTradeOrderCreateReqVO createReqVO , Long userId);
PriceCalculateReqDTO convert(AppTradeOrderCreateReqVO createReqVO, Long userId);
}

View File

@ -17,5 +17,6 @@ public interface ProductSkuConvert {
ProductSkuConvert INSTANCE = Mappers.getMapper(ProductSkuConvert.class);
List<SkuDecrementStockBatchReqDTO.Item> convert(List<TradeOrderItemDO> tradeOrderItems);
List<SkuDecrementStockBatchReqDTO.Item> convert(List<TradeOrderItemDO> list);
}

View File

@ -4,10 +4,6 @@ import cn.iocoder.yudao.framework.mybatis.core.mapper.BaseMapperX;
import cn.iocoder.yudao.module.trade.dal.dataobject.order.TradeOrderDO;
import org.apache.ibatis.annotations.Mapper;
/**
* @author LeeYan9
* @since 2022-08-26
*/
@Mapper
public interface TradeOrderMapper extends BaseMapperX<TradeOrderDO> {
}

View File

@ -4,10 +4,6 @@ import cn.iocoder.yudao.framework.mybatis.core.mapper.BaseMapperX;
import cn.iocoder.yudao.module.trade.dal.dataobject.order.TradeOrderItemDO;
import org.apache.ibatis.annotations.Mapper;
/**
* @author LeeYan9
* @since 2022-08-26
*/
@Mapper
public interface TradeOrderItemMapper extends BaseMapperX<TradeOrderItemDO> {
}

View File

@ -3,6 +3,7 @@ package cn.iocoder.yudao.module.trade.framework.order.config;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Configuration;
// TODO @LeeYan9: 可以直接给 TradeOrderProperties 一个 @Component生效哈
/**
* @author LeeYan9
* @since 2022-09-15

View File

@ -3,15 +3,16 @@ package cn.iocoder.yudao.module.trade.service.order;
import cn.iocoder.yudao.module.trade.controller.app.order.vo.AppTradeOrderCreateReqVO;
/**
* TODO @LeeYan9: 类注释
* @author LeeYan9
* @since 2022-08-26
*/
public interface TradeOrderService {
/**
* 创建交易订单
* 创建交易订单 TODO @LeeYan9: 方法注释, 和参数要空一行
* @param loginUserId 登录用户
* @param clientIp 用户ip地址
* @param clientIp 用户ip地址 // TODO @LeeYan9: 中英文之间, 空一行哈
* @param createReqVO 创建交易订单请求模型
* @return 交易订单创建结果
*/

View File

@ -3,6 +3,7 @@ package cn.iocoder.yudao.module.trade.service.order;
import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.text.StrBuilder;
import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.StrUtil;
import cn.iocoder.yudao.framework.common.enums.CommonStatusEnum;
import cn.iocoder.yudao.framework.common.enums.TerminalEnum;
import cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil;
@ -45,42 +46,41 @@ import java.util.Map;
import java.util.Objects;
/**
* TODO @LeeYan9: 注释
* @author LeeYan9
* @since 2022-08-26
*/
@Service
public class TradeOrderServiceImpl implements TradeOrderService {
// TODO @LeeYan9: 相同类型的, 可以放在一起,不用空行; 例如说 Mapper API Properties
@Resource
private TradeOrderMapper tradeOrderMapper;
@Resource
private TradeOrderItemMapper tradeOrderItemMapper;
@Resource
private PriceApi priceApi;
@Resource
private ProductSkuApi productSkuApi;
@Resource
private ProductSpuApi productSpuApi;
@Resource
private PayOrderApi payOrderApi;
@Resource
private TradeOrderProperties tradeOrderProperties;
// TODO LeeYan9: 静态变量, 需要在最前面哈; 另外, 静态变量的注释最好写下;
private static final String BLANK_PLACEHOLDER = " ";
private static final String MULTIPLIER_PLACEHOLDER = "x";
@Override
@Transactional(rollbackFor = Exception.class)
public Long createTradeOrder(Long loginUserId, String clientIp, AppTradeOrderCreateReqVO createReqVO) {
List<Item> items = createReqVO.getItems();
List<Item> items = createReqVO.getItems(); // TODO @LeeYan9: 方法第一行, 不用空哈;
// 商品SKU检查 sku可售状态,库存
List<ProductSkuRespDTO> skuInfos = productSkuApi.getSkuList(CollectionUtils.convertSet(items, Item::getSkuId));
Map<Long, ProductSkuRespDTO> skuInfoMap = CollectionUtils.convertMap(skuInfos, ProductSkuRespDTO::getId);
@ -93,33 +93,38 @@ public class TradeOrderServiceImpl implements TradeOrderService {
// 价格计算
PriceCalculateReqDTO priceCalculateReqDTO = PriceConvert.INSTANCE.convert(createReqVO, loginUserId);
PriceCalculateRespDTO priceResp = priceApi.calculatePrice(priceCalculateReqDTO);
// TODO @LeeYan9: 是可以思考下, 订单的营销优惠记录, 应该记录在哪里, 微信讨论起来!
// 订单信息记录
TradeOrderDO tradeOrderDO = TradeOrderConvert.INSTANCE.convert(createReqVO, priceResp.getOrder());
fillTradeOrderInfoFromReqInfo(tradeOrderDO,createReqVO,loginUserId, clientIp);
fillTradeOrderInfoFromReqInfo(tradeOrderDO,createReqVO,loginUserId, clientIp); // TODO @LeeYan9: tradeOrderDO, createReqVO, loginUserId, clientIp
tradeOrderMapper.insert(tradeOrderDO);
// 订单项信息记录
List<TradeOrderItemDO> tradeOrderItems = TradeOrderItemConvert.INSTANCE.convertList(priceResp.getOrder().getItems());
//-填充订单项-SKU信息
// 填充订单项-SKU信息
fillItemsInfoFromSkuAndOrder(tradeOrderDO, tradeOrderItems, skuInfoMap);
tradeOrderItemMapper.insertBatch(tradeOrderItems);
// TODO @LeeYan9: 先扣减库存哈; 可能会扣减失败; 毕竟 get update 之间, 会有并发的可能性
// 库存扣减
List<SkuDecrementStockBatchReqDTO.Item> skuDecrementStockItems = ProductSkuConvert.INSTANCE.convert(tradeOrderItems);
productSkuApi.decrementStockBatch(SkuDecrementStockBatchReqDTO.of(skuDecrementStockItems));
// 构建预支付请求参数
// TODO @LeeYan9: 需要更新到订单上
PayOrderInfoCreateReqDTO payOrderCreateReqDTO = PayOrderConvert.INSTANCE.convert(tradeOrderDO);
fillPayOrderInfoFromItems(payOrderCreateReqDTO, tradeOrderItems);
// 生成预支付
return payOrderApi.createPayOrder(payOrderCreateReqDTO);
}
// TODO @LeeYan9: 填充就好, 不用 from ;
private void fillTradeOrderInfoFromReqInfo(TradeOrderDO tradeOrderDO, AppTradeOrderCreateReqVO createReqVO,
Long loginUserId, String clientIp) {
tradeOrderDO.setUserId(loginUserId);
tradeOrderDO.setUserIp(clientIp);
tradeOrderDO.setSn(IdUtil.getSnowflakeNextId() + "");
tradeOrderDO.setSn(IdUtil.getSnowflakeNextId() + ""); // TODO @LeeYan9: 思考下, 怎么生成好点哈; 这个是会展示给用户的;
tradeOrderDO.setStatus(TradeOrderStatusEnum.WAITING_PAYMENT.getStatus());
tradeOrderDO.setType(TradeOrderTypeEnum.NORMAL.getType());
tradeOrderDO.setRefundStatus(TradeOrderRefundStatusEnum.NONE.getStatus());
@ -139,7 +144,6 @@ public class TradeOrderServiceImpl implements TradeOrderService {
// 填写商品信息
StrBuilder subject = new StrBuilder();
StrBuilder body = new StrBuilder();
for (TradeOrderItemDO tradeOrderItem : tradeOrderItems) {
// append subject
subject.append(BLANK_PLACEHOLDER);
@ -151,6 +155,7 @@ public class TradeOrderServiceImpl implements TradeOrderService {
body.append(tradeOrderItem.getCount());
}
// 设置 subject & body
// TODO @LeeYan9: 可以抽象一个 StrUtils 方法; 或者看看 hutool 有没自带的哈
payOrderInfoCreateReqDTO.setSubject(StrUtils.maxLength(subject.subString(1), 32));
payOrderInfoCreateReqDTO.setBody(StrUtils.maxLength(body.subString(1), 128));
}
@ -182,6 +187,7 @@ public class TradeOrderServiceImpl implements TradeOrderService {
}
}
// TODO @LeeYan9: checkSpuXXX 会不会好点哈? ps: 这个貌似是 sku
private void checkSaleableAndStockFromSpu(Map<Long, ProductSkuRespDTO> skuInfoMap,
List<Item> items) {
// sku 不存在

View File

@ -30,6 +30,7 @@ import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.when;
// TODO @芋艿: 单测的 review; 最后搞;
/**
* @author LeeYan9
* @since 2022-09-07

View File

@ -3,12 +3,13 @@ package cn.iocoder.yudao.module.pay.api.order;
import javax.validation.Valid;
/**
* 支付单 API 接口
*
* @author LeeYan9
* @since 2022-08-26
*/
public interface PayOrderApi {
/**
* 创建支付单
*

View File

@ -9,8 +9,10 @@ import javax.validation.constraints.NotNull;
import java.io.Serializable;
import java.util.Date;
// TODO @LeeYan9: 1) 新建一个 dto , 然后挪进去哈; 2) order下; Info 可以去掉;
/**
* 支付单创建 Request DTO
*
* @author LeeYan9
*/
@Data
@ -53,6 +55,7 @@ public class PayOrderInfoCreateReqDTO implements Serializable {
* 支付金额单位
*/
@NotNull(message = "支付金额不能为空")
// TODO @LeeYan9: 是不是 @Min 注解呀, Integer
@DecimalMin(value = "0", inclusive = false, message = "支付金额必须大于零")
private Integer amount;