Skip to content

Commit

Permalink
【功能新增】商城:积分商城
Browse files Browse the repository at this point in the history
  • Loading branch information
YunaiV committed Oct 4, 2024
1 parent 6c325d9 commit 39ac641
Show file tree
Hide file tree
Showing 27 changed files with 511 additions and 15 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package cn.iocoder.yudao.module.promotion.api.point;

import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.module.promotion.api.point.dto.PointValidateJoinRespDTO;
import cn.iocoder.yudao.module.promotion.enums.ApiConstants;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestParam;

@FeignClient(name = ApiConstants.NAME) // TODO 芋艿:fallbackFactory =
@Tag(name = "RPC 服务 - 秒杀活动")
public interface PointActivityApi {

String PREFIX = ApiConstants.PREFIX + "/point-activity";

@GetMapping(PREFIX + "/validate-join")
@Operation(summary = "【下单前】校验是否参与积分商城活动")
@Parameters({
@Parameter(name = "activityId", description = "活动编号", required = true, example = "1"),
@Parameter(name = "skuId", description = "SKU 编号", required = true, example = "2"),
@Parameter(name = "count", description = "数量", required = true, example = "3"),
})
CommonResult<PointValidateJoinRespDTO> validateJoinPointActivity(@RequestParam("activityId") Long activityId,
@RequestParam("skuId") Long skuId,
@RequestParam("count")Integer count);

@PutMapping(PREFIX + "/update-stock-decr")
@Operation(summary = "更新积分商品库存(减少)")
@Parameters({
@Parameter(name = "id", description = "活动编号", required = true, example = "1"),
@Parameter(name = "skuId", description = "SKU 编号", required = true, example = "2"),
@Parameter(name = "count", description = "数量", required = true, example = "3"),
})
CommonResult<Boolean> updatePointStockDecr(@RequestParam("id") Long id,
@RequestParam("skuId") Long skuId,
@RequestParam("count")Integer count);

@PutMapping(PREFIX + "/update-stock-incr")
@Operation(summary = "更新积分商城商品库存(增加)")
@Parameters({
@Parameter(name = "id", description = "活动编号", required = true, example = "1"),
@Parameter(name = "skuId", description = "SKU 编号", required = true, example = "2"),
@Parameter(name = "count", description = "数量", required = true, example = "3"),
})
CommonResult<Boolean> updatePointStockIncr(@RequestParam("id") Long id,
@RequestParam("skuId") Long skuId,
@RequestParam("count")Integer count);

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package cn.iocoder.yudao.module.promotion.api.point.dto;

import lombok.Data;

/**
* 校验参与积分商城 Response DTO
*/
@Data
public class PointValidateJoinRespDTO {

/**
* 可兑换次数
*/
private Integer count;
/**
* 所需兑换积分
*/
private Integer point;
/**
* 所需兑换金额,单位:分
*/
private Integer price;

}
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ CommonResult<Boolean> updateSeckillStockIncr(@RequestParam("id") Long id,
@RequestParam("skuId") Long skuId,
@RequestParam("count")Integer count);

@GetMapping("/validate-join")
@GetMapping(PREFIX + "/validate-join")
@Operation(summary = "【下单前】校验是否参与秒杀活动") // 如果校验失败,则抛出业务异常
@Parameters({
@Parameter(name = "activityId", description = "活动编号", required = true, example = "1"),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,10 @@ public interface ErrorCodeConstants {
ErrorCode POINT_ACTIVITY_UPDATE_FAIL_STATUS_CLOSED = new ErrorCode(1_013_007_002, "积分商城活动已关闭,不能修改");
ErrorCode POINT_ACTIVITY_DELETE_FAIL_STATUS_NOT_CLOSED_OR_END = new ErrorCode(1_013_007_003, "积分商城活动未关闭或未结束,不能删除");
ErrorCode POINT_ACTIVITY_CLOSE_FAIL_STATUS_CLOSED = new ErrorCode(1_013_007_004, "积分商城活动已关闭,不能重复关闭");
ErrorCode POINT_ACTIVITY_JOIN_ACTIVITY_STATUS_CLOSED = new ErrorCode(1_013_007_005, "积分商品兑换失败,原因:积分商城活动已关闭");
ErrorCode POINT_ACTIVITY_JOIN_ACTIVITY_SINGLE_LIMIT_COUNT_EXCEED = new ErrorCode(1_013_007_006, "积分商品兑换失败,原因:单次限购超出");
ErrorCode POINT_ACTIVITY_JOIN_ACTIVITY_PRODUCT_NOT_EXISTS = new ErrorCode(1_013_007_007, "积分商品兑换失败,原因:商品不存在");
ErrorCode POINT_ACTIVITY_UPDATE_STOCK_FAIL = new ErrorCode(1_013_007_008, "积分商品兑换失败,原因:积分商品库存不足");

// ========== 秒杀活动 1-013-008-000 ==========
ErrorCode SECKILL_ACTIVITY_NOT_EXISTS = new ErrorCode(1_013_008_000, "秒杀活动不存在");
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package cn.iocoder.yudao.module.promotion.api.point;

import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.module.promotion.api.point.dto.PointValidateJoinRespDTO;
import cn.iocoder.yudao.module.promotion.service.point.PointActivityService;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RestController;

import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

/**
* 积分商城活动 Api 接口实现类
*
* @author HUIHUI
*/
@RestController // 提供 RESTful API 接口,给 Feign 调用
@Validated
public class PointActivityApiImpl implements PointActivityApi {

@Resource
private PointActivityService pointActivityService;

@Override
public CommonResult<PointValidateJoinRespDTO> validateJoinPointActivity(Long activityId, Long skuId, Integer count) {
return success(pointActivityService.validateJoinPointActivity(activityId, skuId, count));
}

@Override
public CommonResult<Boolean> updatePointStockDecr(Long id, Long skuId, Integer count) {
pointActivityService.updatePointStockDecr(id, skuId, count);
return success(true);
}

@Override
public CommonResult<Boolean> updatePointStockIncr(Long id, Long skuId, Integer count) {
pointActivityService.updatePointStockIncr(id, skuId, count);
return success(true);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,11 @@ public CommonResult<AppPointActivityDetailRespVO> getPointActivity(@RequestParam

// 2. 拼接数据
List<PointProductDO> products = pointActivityService.getPointProductListByActivityIds(Collections.singletonList(id));
AppPointActivityDetailRespVO respVO = BeanUtils.toBean(activity, AppPointActivityDetailRespVO.class);
respVO.setProducts(BeanUtils.toBean(products, AppPointActivityDetailRespVO.Product.class));
PointProductDO minProduct = getMinObject(products, PointProductDO::getPoint);
assert minProduct != null;
AppPointActivityDetailRespVO respVO = BeanUtils.toBean(activity, AppPointActivityDetailRespVO.class)
.setProducts(BeanUtils.toBean(products, AppPointActivityDetailRespVO.Product.class))
.setPoint(minProduct.getPoint()).setPrice(minProduct.getPrice());
return success(respVO);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,14 @@ public class AppPointActivityDetailRespVO {
@Schema(description = "商品信息数组", requiredMode = Schema.RequiredMode.REQUIRED)
private List<Product> products;

//======================= 显示所需兑换积分最少的 SKU 信息 =======================

@Schema(description = "兑换积分", requiredMode = Schema.RequiredMode.REQUIRED)
private Integer point;

@Schema(description = "兑换金额,单位:分", requiredMode = Schema.RequiredMode.REQUIRED, example = "15860")
private Integer price;

@Schema(description = "商品信息")
@Data
public static class Product {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
package cn.iocoder.yudao.module.promotion.dal.mysql.point;

import cn.hutool.core.lang.Assert;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.mybatis.core.mapper.BaseMapperX;
import cn.iocoder.yudao.framework.mybatis.core.query.LambdaQueryWrapperX;
import cn.iocoder.yudao.module.promotion.controller.admin.point.vo.activity.PointActivityPageReqVO;
import cn.iocoder.yudao.module.promotion.dal.dataobject.point.PointActivityDO;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import org.apache.ibatis.annotations.Mapper;

/**
Expand All @@ -25,4 +27,33 @@ default PageResult<PointActivityDO> selectPage(PointActivityPageReqVO reqVO) {
.orderByDesc(PointActivityDO::getId));
}

/**
* 更新活动库存(减少)
*
* @param id 活动编号
* @param count 扣减的库存数量(正数)
* @return 影响的行数
*/
default int updateStockDecr(Long id, int count) {
Assert.isTrue(count > 0);
return update(null, new LambdaUpdateWrapper<PointActivityDO>()
.eq(PointActivityDO::getId, id)
.ge(PointActivityDO::getStock, count)
.setSql("stock = stock - " + count));
}

/**
* 更新活动库存(增加)
*
* @param id 活动编号
* @param count 增加的库存数量(正数)
* @return 影响的行数
*/
default int updateStockIncr(Long id, int count) {
Assert.isTrue(count > 0);
return update(null, new LambdaUpdateWrapper<PointActivityDO>()
.eq(PointActivityDO::getId, id)
.setSql("stock = stock + " + count));
}

}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package cn.iocoder.yudao.module.promotion.dal.mysql.point;

import cn.hutool.core.lang.Assert;
import cn.iocoder.yudao.framework.mybatis.core.mapper.BaseMapperX;
import cn.iocoder.yudao.module.promotion.dal.dataobject.point.PointProductDO;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
Expand Down Expand Up @@ -29,4 +30,37 @@ default void updateByActivityId(PointProductDO pointProductDO) {
.eq(PointProductDO::getActivityId, pointProductDO.getActivityId()));
}

default PointProductDO selectListByActivityIdAndSkuId(Long activityId, Long skuId) {
return selectOne(PointProductDO::getActivityId, activityId,
PointProductDO::getSkuId, skuId);
}

/**
* 更新活动库存(减少)
*
* @param id 活动编号
* @param count 扣减的库存数量(减少库存)
* @return 影响的行数
*/
default int updateStockDecr(Long id, int count) {
Assert.isTrue(count > 0);
return update(null, new LambdaUpdateWrapper<PointProductDO>()
.eq(PointProductDO::getId, id)
.ge(PointProductDO::getStock, count)
.setSql("stock = stock - " + count));
}

/**
* 更新活动库存(增加)
*
* @param id 活动编号
* @param count 需要增加的库存(增加库存)
* @return 影响的行数
*/
default int updateStockIncr(Long id, int count) {
Assert.isTrue(count > 0);
return update(null, new LambdaUpdateWrapper<PointProductDO>()
.eq(PointProductDO::getId, id)
.setSql("stock = stock + " + count));
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package cn.iocoder.yudao.module.promotion.service.point;

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.module.promotion.api.point.dto.PointValidateJoinRespDTO;
import cn.iocoder.yudao.module.promotion.controller.admin.point.vo.activity.PointActivityPageReqVO;
import cn.iocoder.yudao.module.promotion.controller.admin.point.vo.activity.PointActivitySaveReqVO;
import cn.iocoder.yudao.module.promotion.dal.dataobject.point.PointActivityDO;
Expand Down Expand Up @@ -32,6 +33,24 @@ public interface PointActivityService {
*/
void updatePointActivity(@Valid PointActivitySaveReqVO updateReqVO);

/**
* 更新积分商城商品库存(减少)
*
* @param id 活动编号
* @param skuId sku 编号
* @param count 数量(正数)
*/
void updatePointStockDecr(Long id, Long skuId, Integer count);

/**
* 更新积分商城商品库存(增加)
*
* @param id 活动编号
* @param skuId sku 编号
* @param count 数量(正数)
*/
void updatePointStockIncr(Long id, Long skuId, Integer count);

/**
* 关闭积分商城活动
*
Expand Down Expand Up @@ -78,4 +97,16 @@ public interface PointActivityService {
*/
List<PointProductDO> getPointProductListByActivityIds(Collection<Long> activityIds);

/**
* 【下单前】校验是否参与积分商城活动
*
* 如果校验失败,则抛出业务异常
*
* @param activityId 活动编号
* @param skuId SKU 编号
* @param count 数量
* @return 积分商城商品信息
*/
PointValidateJoinRespDTO validateJoinPointActivity(Long activityId, Long skuId, Integer count);

}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import cn.iocoder.yudao.module.product.api.sku.dto.ProductSkuRespDTO;
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.api.point.dto.PointValidateJoinRespDTO;
import cn.iocoder.yudao.module.promotion.controller.admin.point.vo.activity.PointActivityPageReqVO;
import cn.iocoder.yudao.module.promotion.controller.admin.point.vo.activity.PointActivitySaveReqVO;
import cn.iocoder.yudao.module.promotion.controller.admin.point.vo.product.PointProductSaveReqVO;
Expand Down Expand Up @@ -102,6 +103,43 @@ public void updatePointActivity(PointActivitySaveReqVO updateReqVO) {
updateSeckillProduct(updateObj, updateReqVO.getProducts());
}

@Override
@Transactional(rollbackFor = Exception.class)
public void updatePointStockDecr(Long id, Long skuId, Integer count) {
// 1.1 校验活动库存是否充足
PointActivityDO activity = validatePointActivityExists(id);
if (count > activity.getStock()) {
throw exception(POINT_ACTIVITY_UPDATE_STOCK_FAIL);
}
// 1.2 校验商品库存是否充足
PointProductDO product = pointProductMapper.selectListByActivityIdAndSkuId(id, skuId);
if (product == null || count > product.getStock()) {
throw exception(POINT_ACTIVITY_UPDATE_STOCK_FAIL);
}

// 2.1 更新活动商品库存
int updateCount = pointProductMapper.updateStockDecr(product.getId(), count);
if (updateCount == 0) {
throw exception(POINT_ACTIVITY_UPDATE_STOCK_FAIL);
}

// 2.2 更新活动库存
updateCount = pointActivityMapper.updateStockDecr(id, count);
if (updateCount == 0) {
throw exception(POINT_ACTIVITY_UPDATE_STOCK_FAIL);
}
}

@Override
@Transactional(rollbackFor = Exception.class)
public void updatePointStockIncr(Long id, Long skuId, Integer count) {
PointProductDO product = pointProductMapper.selectListByActivityIdAndSkuId(id, skuId);
// 更新活动商品库存
pointProductMapper.updateStockIncr(product.getId(), count);
// 更新活动库存
pointActivityMapper.updateStockIncr(id, count);
}

@Override
@Transactional(rollbackFor = Exception.class)
public void closePointActivity(Long id) {
Expand Down Expand Up @@ -244,4 +282,28 @@ public List<PointProductDO> getPointProductListByActivityIds(Collection<Long> ac
return pointProductMapper.selectListByActivityId(activityIds);
}

@Override
public PointValidateJoinRespDTO validateJoinPointActivity(Long activityId, Long skuId, Integer count) {
// 1. 校验积分商城活动是否存在
PointActivityDO activity = validatePointActivityExists(activityId);
if (CommonStatusEnum.isDisable(activity.getStatus())) {
throw exception(POINT_ACTIVITY_JOIN_ACTIVITY_STATUS_CLOSED);
}

// 2.1 校验积分商城商品是否存在
PointProductDO product = pointProductMapper.selectListByActivityIdAndSkuId(activityId, skuId);
if (product == null) {
throw exception(POINT_ACTIVITY_JOIN_ACTIVITY_PRODUCT_NOT_EXISTS);
}
// 2.2 超过单次购买限制
if (count > product.getCount()) {
throw exception(POINT_ACTIVITY_JOIN_ACTIVITY_SINGLE_LIMIT_COUNT_EXCEED);
}
// 2.2 校验库存是否充足
if (count > product.getStock()) {
throw exception(POINT_ACTIVITY_UPDATE_STOCK_FAIL);
}
return BeanUtils.toBean(product, PointValidateJoinRespDTO.class);
}

}
Loading

0 comments on commit 39ac641

Please sign in to comment.