code review:砍价、秒杀的逻辑

This commit is contained in:
YunaiV 2023-09-20 01:07:41 +08:00
parent 1b154699c6
commit c766f7daa5
18 changed files with 68 additions and 51 deletions

View File

@ -50,6 +50,7 @@ public class LocalDateTimeUtils {
* @return 指定时间
*/
public static LocalDateTime buildTime(String timeStr) {
// TODO @puhui999这个方法的实现 LocalDateTimeUtil.parse() 的差异点是啥呀
return LocalDateTime.of(LocalDate.now(), LocalTime.parse(timeStr));
}

View File

@ -31,7 +31,7 @@ public interface CombinationRecordApi {
boolean isCombinationRecordSuccess(Long userId, Long orderId);
/**
* 更新拼团状态为 成功
* 更新拼团状态为成功
*
* @param userId 用户编号
* @param orderId 订单编号
@ -39,7 +39,7 @@ public interface CombinationRecordApi {
void updateRecordStatusToSuccess(Long userId, Long orderId);
/**
* 更新拼团状态为 失败
* 更新拼团状态为失败
*
* @param userId 用户编号
* @param orderId 订单编号

View File

@ -7,6 +7,7 @@ package cn.iocoder.yudao.module.promotion.api.seckill;
*/
public interface SeckillActivityApi {
// TODO @puhui999activityId 改成 id 好点哈
/**
* 更新秒杀库存
*

View File

@ -3,7 +3,6 @@ package cn.iocoder.yudao.module.promotion.controller.admin.bargain;
import cn.hutool.core.collection.CollUtil;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.collection.CollectionUtils;
import cn.iocoder.yudao.module.product.api.spu.ProductSpuApi;
import cn.iocoder.yudao.module.product.api.spu.dto.ProductSpuRespDTO;
import cn.iocoder.yudao.module.promotion.controller.admin.bargain.vo.BargainActivityCreateReqVO;
@ -25,6 +24,7 @@ import javax.validation.Valid;
import java.util.List;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;
import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.convertList;
@Tag(name = "管理后台 - 砍价活动")
@RestController
@ -79,7 +79,9 @@ public class BargainActivityController {
if (CollUtil.isEmpty(pageResult.getList())) {
return success(PageResult.empty(pageResult.getTotal()));
}
List<ProductSpuRespDTO> spuList = spuApi.getSpuList(CollectionUtils.convertList(pageResult.getList(), BargainActivityDO::getSpuId));
// 拼接数据
List<ProductSpuRespDTO> spuList = spuApi.getSpuList(convertList(pageResult.getList(), BargainActivityDO::getSpuId));
return success(BargainActivityConvert.INSTANCE.convertPage(pageResult, spuList));
}

View File

@ -4,7 +4,6 @@ import cn.hutool.core.collection.CollUtil;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.collection.CollectionUtils;
import cn.iocoder.yudao.module.product.api.spu.ProductSpuApi;
import cn.iocoder.yudao.module.product.api.spu.dto.ProductSpuRespDTO;
import cn.iocoder.yudao.module.promotion.controller.app.bargain.vo.activity.AppBargainActivityDetailRespVO;
@ -25,6 +24,7 @@ import javax.annotation.Resource;
import java.util.List;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;
import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.convertList;
@Tag(name = "用户 App - 砍价活动")
@RestController
@ -39,27 +39,27 @@ public class AppBargainActivityController {
@GetMapping("/page")
@Operation(summary = "获得砍价活动分页")
public CommonResult<PageResult<AppBargainActivityRespVO>> getBargainActivityPage(PageParam pageReqVO) {
PageResult<BargainActivityDO> result = bargainActivityService.getBargainActivityAppPage(pageReqVO);
PageResult<BargainActivityDO> result = bargainActivityService.getBargainActivityPageForApp(pageReqVO);
if (CollUtil.isEmpty(result.getList())) {
return success(PageResult.empty(result.getTotal()));
}
List<ProductSpuRespDTO> spuList = spuApi.getSpuList(CollectionUtils.convertList(result.getList(), BargainActivityDO::getSpuId));
// 拼接数据
List<ProductSpuRespDTO> spuList = spuApi.getSpuList(convertList(result.getList(), BargainActivityDO::getSpuId));
return success(BargainActivityConvert.INSTANCE.convertAppPage(result, spuList));
}
// TODO 芋艿增加 Spring Cache
@GetMapping("/list")
@Operation(summary = "获得砍价活动列表", description = "用于小程序首页")
@Parameter(name = "count", description = "需要展示的数量", example = "6")
public CommonResult<List<AppBargainActivityRespVO>> getBargainActivityList(
@RequestParam(name = "count", defaultValue = "6") Integer count) {
List<BargainActivityDO> list = bargainActivityService.getBargainActivityAppList(count);
List<BargainActivityDO> list = bargainActivityService.getBargainActivityListForApp(count);
if (CollUtil.isEmpty(list)) {
return success(BargainActivityConvert.INSTANCE.convertAppList(list));
}
List<ProductSpuRespDTO> spuList = spuApi.getSpuList(CollectionUtils.convertList(list, BargainActivityDO::getSpuId));
// TODO 芋艿增加 Spring Cache
// 拼接数据
List<ProductSpuRespDTO> spuList = spuApi.getSpuList(convertList(list, BargainActivityDO::getSpuId));
return success(BargainActivityConvert.INSTANCE.convertAppList(list, spuList));
}
@ -71,9 +71,9 @@ public class AppBargainActivityController {
if (activity == null) {
return success(null);
}
// 拼接数据
ProductSpuRespDTO spu = spuApi.getSpu(activity.getSpuId());
return success(BargainActivityConvert.INSTANCE.convert1(activity, spu));
return success(BargainActivityConvert.INSTANCE.convert(activity, spu));
}
}

View File

@ -4,7 +4,6 @@ import cn.hutool.core.util.ObjectUtil;
import cn.iocoder.yudao.framework.common.enums.CommonStatusEnum;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.collection.CollectionUtils;
import cn.iocoder.yudao.module.product.api.spu.ProductSpuApi;
import cn.iocoder.yudao.module.product.api.spu.dto.ProductSpuRespDTO;
import cn.iocoder.yudao.module.promotion.controller.app.seckill.vo.activity.AppSeckillActivityDetailRespVO;
@ -33,8 +32,7 @@ import java.util.List;
import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;
import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.filterList;
import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.findFirst;
import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.*;
import static cn.iocoder.yudao.framework.common.util.date.LocalDateTimeUtils.isBetween;
import static cn.iocoder.yudao.module.promotion.enums.ErrorCodeConstants.SECKILL_ACTIVITY_FAIL_STATUS_CLOSED;
@ -56,30 +54,33 @@ public class AppSeckillActivityController {
@Operation(summary = "获得当前秒杀活动") // 提供给首页使用
// TODO 芋艿需要增加 spring cache
public CommonResult<AppSeckillActivityNowRespVO> getNowSeckillActivity() {
// 1获取当前时间处在哪个秒杀阶段
// 1. 获取当前时间处在哪个秒杀阶段
// TODO @puhui999可以考虑在 service 写个方法这样 controller 不用关注过多逻辑
List<SeckillConfigDO> configList = configService.getSeckillConfigList();
SeckillConfigDO filteredConfig = findFirst(configList, config -> ObjectUtil.equal(config.getStatus(),
CommonStatusEnum.ENABLE.getStatus()) && isBetween(config.getStartTime(), config.getEndTime()));
// 11 时段不存在直接返回 null
if (filteredConfig == null) {
if (filteredConfig == null) { // 时段不存在直接返回 null
return success(null);
}
// 2查询满足当前阶段的活动
// 2. 查询满足当前阶段的活动
// TODO @puhui999最好直接返回开启的不多查询数据
List<SeckillActivityDO> activityList = activityService.getSeckillActivityListByConfigIds(Arrays.asList(filteredConfig.getId()));
List<SeckillActivityDO> filteredList = filterList(activityList, item -> ObjectUtil.equal(item.getStatus(), CommonStatusEnum.ENABLE.getStatus()));
// 21 获取 spu 信息
List<ProductSpuRespDTO> spuList = spuApi.getSpuList(CollectionUtils.convertList(filteredList, SeckillActivityDO::getSpuId));
// 3. 拼接数据
List<ProductSpuRespDTO> spuList = spuApi.getSpuList(convertList(filteredList, SeckillActivityDO::getSpuId));
return success(SeckillActivityConvert.INSTANCE.convert(filteredConfig, filteredList, spuList));
}
@GetMapping("/page")
@Operation(summary = "获得秒杀活动分页")
public CommonResult<PageResult<AppSeckillActivityRespVO>> getSeckillActivityPage(AppSeckillActivityPageReqVO pageReqVO) {
// 1查询满足当前阶段的活动
// 1. 查询满足当前阶段的活动
PageResult<SeckillActivityDO> pageResult = activityService.getSeckillActivityAppPageByConfigId(pageReqVO);
// 11 获取 spu 信息
List<ProductSpuRespDTO> spuList = spuApi.getSpuList(CollectionUtils.convertList(pageResult.getList(), SeckillActivityDO::getSpuId));
// 2. 拼接数据
List<ProductSpuRespDTO> spuList = spuApi.getSpuList(convertList(pageResult.getList(), SeckillActivityDO::getSpuId));
return success(SeckillActivityConvert.INSTANCE.convertPage(pageResult, spuList));
}
@ -88,15 +89,15 @@ public class AppSeckillActivityController {
@Parameter(name = "id", description = "活动编号", required = true, example = "1024")
public CommonResult<AppSeckillActivityDetailRespVO> getSeckillActivity(@RequestParam("id") Long id) {
// 1获取当前时间处在哪个秒杀阶段
// TODO puhui999这里 58 行是雷同的
List<SeckillConfigDO> configList = configService.getSeckillConfigList();
SeckillConfigDO filteredConfig = findFirst(configList, config -> ObjectUtil.equal(config.getStatus(),
CommonStatusEnum.ENABLE.getStatus()) && isBetween(config.getStartTime(), config.getEndTime()));
// 11 时段不存在直接返回 null
if (filteredConfig == null) {
if (filteredConfig == null) { // 时段不存在直接返回 null
return success(null);
}
// 2获取活动
// 2. 获取活动
SeckillActivityDO seckillActivity = activityService.getSeckillActivity(id);
if (seckillActivity == null) {
return success(null);
@ -106,7 +107,7 @@ public class AppSeckillActivityController {
throw exception(SECKILL_ACTIVITY_FAIL_STATUS_CLOSED);
}
// 3获取活动商品
// 3. 拼接数据
List<SeckillProductDO> products = activityService.getSeckillProductListByActivityId(seckillActivity.getId());
return success(SeckillActivityConvert.INSTANCE.convert3(seckillActivity, products, filteredConfig));
}

View File

@ -32,19 +32,12 @@ public class AppSeckillConfigController {
@Operation(summary = "获得秒杀时间段列表")
public CommonResult<List<AppSeckillConfigRespVO>> getSeckillConfigList() {
List<SeckillConfigDO> list = configService.getSeckillConfigListByStatus(CommonStatusEnum.ENABLE.getStatus());
// TODO @puhui999如果这种不用判空也问题不大
if (CollectionUtil.isEmpty(list)) {
return success(Collections.emptyList());
}
return success(SeckillConfigConvert.INSTANCE.convertList2(list));
//return success(Arrays.asList(
// new AppSeckillConfigRespVO().setId(1L).setStartTime("00:00").setEndTime("09:59")
// .setSliderPicUrls(Arrays.asList("https://static.iocoder.cn/mall/a79f5d2ea6bf0c3c11b2127332dfe2df.jpg",
// "https://static.iocoder.cn/mall/132.jpeg")),
// new AppSeckillConfigRespVO().setId(2L).setStartTime("10:00").setEndTime("12:59"),
// new AppSeckillConfigRespVO().setId(2L).setStartTime("13:00").setEndTime("22:59"),
// new AppSeckillConfigRespVO().setId(2L).setStartTime("23:00").setEndTime("23:59")
//));
}
}

View File

@ -42,9 +42,11 @@ public interface BargainActivityConvert {
default PageResult<BargainActivityRespVO> convertPage(PageResult<BargainActivityDO> page, List<ProductSpuRespDTO> spuList) {
PageResult<BargainActivityRespVO> result = convertPage(page);
// 拼接关联属性
Map<Long, ProductSpuRespDTO> spuMap = convertMap(spuList, ProductSpuRespDTO::getId);
List<BargainActivityRespVO> list = CollectionUtils.convertList(result.getList(), item -> {
findAndThen(spuMap, item.getSpuId(), spu -> {
// TODO @puhui999这里可以使用链式哈
item.setPicUrl(spu.getPicUrl());
item.setSpuName(spu.getName());
});
@ -56,7 +58,7 @@ public interface BargainActivityConvert {
AppBargainActivityDetailRespVO convert1(BargainActivityDO bean);
default AppBargainActivityDetailRespVO convert1(BargainActivityDO bean, ProductSpuRespDTO spu) {
default AppBargainActivityDetailRespVO convert(BargainActivityDO bean, ProductSpuRespDTO spu) {
AppBargainActivityDetailRespVO detail = convert1(bean);
if (spu != null) {
detail.setPicUrl(spu.getPicUrl());
@ -70,9 +72,11 @@ public interface BargainActivityConvert {
default PageResult<AppBargainActivityRespVO> convertAppPage(PageResult<BargainActivityDO> page, List<ProductSpuRespDTO> spuList) {
PageResult<AppBargainActivityRespVO> result = convertAppPage(page);
// 拼接关联属性
Map<Long, ProductSpuRespDTO> spuMap = convertMap(spuList, ProductSpuRespDTO::getId);
List<AppBargainActivityRespVO> list = CollectionUtils.convertList(result.getList(), item -> {
findAndThen(spuMap, item.getSpuId(), spu -> {
// TODO @puhui999这里可以使用链式哈
item.setPicUrl(spu.getPicUrl());
item.setMarketPrice(spu.getMarketPrice());
});
@ -89,6 +93,7 @@ public interface BargainActivityConvert {
Map<Long, ProductSpuRespDTO> spuMap = convertMap(spuList, ProductSpuRespDTO::getId);
return CollectionUtils.convertList(activityList, item -> {
findAndThen(spuMap, item.getSpuId(), spu -> {
// TODO @puhui999这里可以使用链式哈
item.setPicUrl(spu.getPicUrl());
item.setMarketPrice(spu.getMarketPrice());
});

View File

@ -94,8 +94,10 @@ public interface CombinationActivityConvert {
CombinationRecordDO convert(CombinationRecordCreateReqDTO reqDTO);
default CombinationRecordDO convert1(CombinationRecordCreateReqDTO reqDTO, CombinationActivityDO activity, MemberUserRespDTO user,
ProductSpuRespDTO spu, ProductSkuRespDTO sku) {
default CombinationRecordDO convert(CombinationRecordCreateReqDTO reqDTO,
CombinationActivityDO activity, MemberUserRespDTO user,
ProductSpuRespDTO spu, ProductSkuRespDTO sku) {
// TODO @puhui999搞成链式的 set这样会更规整一点
CombinationRecordDO record = convert(reqDTO);
record.setVirtualGroup(false);
record.setExpireTime(record.getStartTime().plusHours(activity.getLimitDuration()));

View File

@ -98,6 +98,7 @@ public interface SeckillActivityConvert {
Map<Long, ProductSpuRespDTO> spuMap = convertMap(spuList, ProductSpuRespDTO::getId);
respVO.setActivities(CollectionUtils.convertList(convertList3(activityList), item -> {
findAndThen(spuMap, item.getSpuId(), spu -> {
// TODO @puhui999可以尝试链式 set
item.setPicUrl(spu.getPicUrl());
item.setMarketPrice(spu.getMarketPrice());
item.setUnitName(DictFrameworkUtils.getDictDataLabel(DictTypeConstants.PRODUCT_UNIT, spu.getUnit()));
@ -114,6 +115,7 @@ public interface SeckillActivityConvert {
Map<Long, ProductSpuRespDTO> spuMap = convertMap(spuList, ProductSpuRespDTO::getId);
List<AppSeckillActivityRespVO> list = CollectionUtils.convertList(result.getList(), item -> {
findAndThen(spuMap, item.getSpuId(), spu -> {
// TODO @puhui999可以尝试链式 set
item.setPicUrl(spu.getPicUrl());
item.setMarketPrice(spu.getMarketPrice());
item.setUnitName(DictFrameworkUtils.getDictDataLabel(DictTypeConstants.PRODUCT_UNIT, spu.getUnit()));
@ -131,6 +133,7 @@ public interface SeckillActivityConvert {
default AppSeckillActivityDetailRespVO convert3(SeckillActivityDO seckillActivity, List<SeckillProductDO> products, SeckillConfigDO filteredConfig) {
AppSeckillActivityDetailRespVO respVO = convert2(seckillActivity);
respVO.setProducts(convertList1(products));
// TODO @puhui999可以尝试链式 set
respVO.setStartTime(buildTime(filteredConfig.getStartTime()));
respVO.setEndTime(buildTime(filteredConfig.getEndTime()));
return respVO;

View File

@ -41,7 +41,7 @@ public interface BargainActivityMapper extends BaseMapperX<BargainActivityDO> {
default int updateActivityStock(Long id, int count) {
return update(null, new LambdaUpdateWrapper<BargainActivityDO>()
.eq(BargainActivityDO::getId, id)
.gt(BargainActivityDO::getStock, count)
.ge(BargainActivityDO::getStock, count)
.setSql("stock = stock - " + count));
}

View File

@ -52,6 +52,7 @@ public interface SeckillActivityMapper extends BaseMapperX<SeckillActivityDO> {
default PageResult<SeckillActivityDO> selectPage(AppSeckillActivityPageReqVO pageReqVO, Integer status) {
return selectPage(pageReqVO, new LambdaQueryWrapperX<SeckillActivityDO>()
.eqIfPresent(SeckillActivityDO::getStatus, status)
// TODO 芋艿 find in set 的想法
.apply(ObjectUtil.isNotNull(pageReqVO.getConfigId()), "FIND_IN_SET(" + pageReqVO.getConfigId() + ",config_ids) > 0"));
}

View File

@ -63,19 +63,22 @@ public interface BargainActivityService {
*/
PageResult<BargainActivityDO> getBargainActivityPage(BargainActivityPageReqVO pageReqVO);
// TODO @puhui999这里可以改成进行中的活动尽量避免专门为 app 定制或者类似的名字哈mapper 那也是
/**
* 获取 APP 端活动分页数据
*
* @param pageReqVO 分页请求
* @return 砍价活动分页
*/
PageResult<BargainActivityDO> getBargainActivityAppPage(PageParam pageReqVO);
PageResult<BargainActivityDO> getBargainActivityPageForApp(PageParam pageReqVO);
/**
* 获取 APP 端活动展示数据
*
* @param count 需要的数量
* @return
* @return 砍价活动分页
*/
List<BargainActivityDO> getBargainActivityAppList(Integer count);
List<BargainActivityDO> getBargainActivityListForApp(Integer count);
}

View File

@ -142,18 +142,20 @@ public class BargainActivityServiceImpl implements BargainActivityService {
}
@Override
public PageResult<BargainActivityDO> getBargainActivityAppPage(PageParam pageReqVO) {
public PageResult<BargainActivityDO> getBargainActivityPageForApp(PageParam pageReqVO) {
// 只查询进行中且在时间范围内的
return bargainActivityMapper.selectAppPage(pageReqVO, CommonStatusEnum.ENABLE.getStatus(), LocalDateTime.now());
}
@Override
public List<BargainActivityDO> getBargainActivityAppList(Integer count) {
public List<BargainActivityDO> getBargainActivityListForApp(Integer count) {
// TODO @puhui999这种 default count 的逻辑可以放到 controller 然后可以使用 ObjectUtils.default 方法
if (count == null) {
count = 6;
}
PageResult<BargainActivityDO> result = bargainActivityMapper.selectAppPage(new PageParam().setPageSize(count), CommonStatusEnum.ENABLE.getStatus(), LocalDateTime.now());
// TODO @puhui999这种不要用 page会浪费一次 count
PageResult<BargainActivityDO> result = bargainActivityMapper.selectAppPage(new PageParam().setPageSize(count),
CommonStatusEnum.ENABLE.getStatus(), LocalDateTime.now());
return result.getList();
}

View File

@ -74,6 +74,7 @@ public interface CombinationActivityService {
/**
* 校验是否满足拼团条件
* 如果不满足会抛出异常
*
* @param activityId 活动编号
* @param userId 用户编号

View File

@ -223,6 +223,7 @@ public class CombinationActivityServiceImpl implements CombinationActivityServic
throw exception(COMBINATION_ACTIVITY_STATUS_DISABLE);
}
// 1.3 校验是否超出单次限购数量
// TODO puhui999count > activity.getSingleLimitCount() 会更好理解点
if (activity.getSingleLimitCount() < count) {
throw exception(COMBINATION_RECORD_FAILED_SINGLE_LIMIT_COUNT_EXCEED);
}
@ -242,7 +243,6 @@ public class CombinationActivityServiceImpl implements CombinationActivityServic
if (activity.getTotalLimitCount() < countSum) {
throw exception(COMBINATION_RECORD_FAILED_TOTAL_LIMIT_COUNT_EXCEED);
}
}
}

View File

@ -132,7 +132,7 @@ public class CombinationRecordServiceImpl implements CombinationRecordService {
MemberUserRespDTO user = memberUserApi.getUser(reqDTO.getUserId());
ProductSpuRespDTO spu = productSpuApi.getSpu(reqDTO.getSpuId());
ProductSkuRespDTO sku = productSkuApi.getSku(reqDTO.getSkuId());
recordMapper.insert(CombinationActivityConvert.INSTANCE.convert1(reqDTO, activity, user, spu, sku));
recordMapper.insert(CombinationActivityConvert.INSTANCE.convert(reqDTO, activity, user, spu, sku));
}
@Override

View File

@ -14,6 +14,8 @@ import javax.validation.constraints.NotNull;
@Data
public class TradeBeforeOrderCreateReqBO {
// TODO @puhui999注释也写下哈bo 还是写注释噢
@NotNull(message = "订单类型不能为空")
private Integer orderType;