promotion:增加创建砍价的接口

This commit is contained in:
YunaiV 2023-10-04 21:55:05 +08:00
parent a37427c355
commit cc087e4eca
11 changed files with 138 additions and 64 deletions

View File

@ -88,17 +88,16 @@ public interface ErrorCodeConstants {
// ========== 砍价活动 1-013-012-000 ==========
ErrorCode BARGAIN_ACTIVITY_NOT_EXISTS = new ErrorCode(1_013_012_000, "砍价活动不存在");
ErrorCode BARGAIN_ACTIVITY_SPU_CONFLICTS = new ErrorCode(1_013_012_001, "存在商品参加了其它砍价活动");
ErrorCode BARGAIN_ACTIVITY_STATUS_DISABLE = new ErrorCode(1_013_012_002, "砍价活动已关闭不能修改");
ErrorCode BARGAIN_ACTIVITY_STATUS_DISABLE = new ErrorCode(1_013_012_002, "砍价活动已关闭不能修改");
ErrorCode BARGAIN_ACTIVITY_DELETE_FAIL_STATUS_NOT_CLOSED_OR_END = new ErrorCode(1_013_012_003, "砍价活动未关闭或未结束,不能删除");
ErrorCode BARGAIN_ACTIVITY_UPDATE_STOCK_FAIL = new ErrorCode(1_013_012_004, "砍价失败,原因:该砍价活动库存不足");
ErrorCode BARGAIN_ACTIVITY_STOCK_NOT_ENOUGH = new ErrorCode(1_013_012_004, "砍价活动库存不足");
ErrorCode BARGAIN_ACTIVITY_STATUS_CLOSED = new ErrorCode(1_013_012_005, "砍价活动已关闭");
ErrorCode BARGAIN_ACTIVITY_TIME_END = new ErrorCode(1_013_012_006, "砍价活动已经结束");
// ========== 砍价记录 1-013-013-000 ==========
ErrorCode BARGAIN_RECORD_NOT_EXISTS = new ErrorCode(1_013_013_000, "砍价记录不存在");
ErrorCode BARGAIN_RECORD_EXISTS = new ErrorCode(1_013_013_001, "砍价失败,已参与过该砍价");
ErrorCode BARGAIN_RECORD_HEAD_NOT_EXISTS = new ErrorCode(1_013_013_002, "砍价失败,父砍价不存在");
ErrorCode BARGAIN_RECORD_USER_FULL = new ErrorCode(1_013_013_003, "砍价失败,砍价人数已满");
ErrorCode BARGAIN_JOIN_RECORD_NOT_IN_PROGRESS = new ErrorCode(1_013_013_004, "砍价失败,砍价记录不在进行中");
ErrorCode BARGAIN_JOIN_FAILED_ACTIVITY_TIME_END = new ErrorCode(1_013_013_005, "砍价失败,活动已经结束");
ErrorCode BARGAIN_JOIN_ACTIVITY_STATUS_CLOSED = new ErrorCode(1_013_013_006, "砍价失败,原因:砍价活动已关闭");
ErrorCode BARGAIN_RECORD_CREATE_FAIL_EXISTS = new ErrorCode(1_013_013_001, "参与失败,您已经参与当前砍价活动");
ErrorCode BARGAIN_RECORD_CREATE_FAIL_LIMIT = new ErrorCode(1_013_013_002, "参与失败,您已达到当前砍价活动的参与上限");
ErrorCode BARGAIN_JOIN_RECORD_NOT_SUCCESS = new ErrorCode(1_013_013_004, "下单失败,砍价未成功");
}

View File

@ -17,7 +17,7 @@ public enum BargainRecordStatusEnum implements IntArrayValuable {
IN_PROGRESS(1, "砍价中"),
SUCCESS(2, "砍价成功"),
FAILED(3, "砍价失败"),
FAILED(3, "砍价失败"), // 活动到期时会自动将到期的砍价全部设置为过期
;
public static final int[] ARRAYS = Arrays.stream(values()).mapToInt(BargainRecordStatusEnum::getStatus).toArray();

View File

@ -0,0 +1,9 @@
### /promotion/bargain-record/create 创建砍价记录
POST {{appApi}}/promotion/bargain-record/create
Authorization: Bearer {{appToken}}
Content-Type: application/json
tenant-id: {{appTenentId}}
{
"activityId": 1
}

View File

@ -4,19 +4,23 @@ 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.date.LocalDateTimeUtils;
import cn.iocoder.yudao.framework.security.core.annotations.PreAuthenticated;
import cn.iocoder.yudao.module.promotion.controller.app.bargain.vo.record.AppBargainRecordCreateReqVO;
import cn.iocoder.yudao.module.promotion.controller.app.bargain.vo.record.AppBargainRecordDetailRespVO;
import cn.iocoder.yudao.module.promotion.controller.app.bargain.vo.record.AppBargainRecordRespVO;
import cn.iocoder.yudao.module.promotion.controller.app.bargain.vo.record.AppBargainRecordSummaryRespVO;
import cn.iocoder.yudao.module.promotion.service.bargain.BargainRecordService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import java.time.Duration;
import java.util.ArrayList;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;
import static cn.iocoder.yudao.framework.security.core.util.SecurityFrameworkUtils.getLoginUserId;
@Tag(name = "用户 App - 砍价记录")
@RestController
@ -24,6 +28,9 @@ import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;
@Validated
public class AppBargainRecordController {
@Resource
private BargainRecordService bargainRecordService;
@GetMapping("/get-summary")
@Operation(summary = "获得砍价记录的概要信息", description = "用于小程序首页")
// TODO 芋艿增加 @Cache 缓存1 分钟过期
@ -137,9 +144,11 @@ public class AppBargainRecordController {
}
@PostMapping("/create")
@Operation(summary = "创建砍价记录", description = "参与拼团活动")
@Operation(summary = "创建砍价记录", description = "参与砍价活动")
@PreAuthenticated
public CommonResult<Long> createBargainRecord(@RequestBody AppBargainRecordCreateReqVO reqVO) {
return success(1L);
Long recordId = bargainRecordService.createBargainRecord(getLoginUserId(), reqVO);
return success(recordId);
}
}

View File

@ -60,24 +60,29 @@ public class BargainActivityDO extends BaseDO {
*/
private Long skuId;
/**
* 砍价起始价格单位
* 砍价起始价格单位
*/
private Integer bargainFirstPrice;
/**
* 砍价底价单位
*/
private Integer bargainPrice;
private Integer bargainMinPrice;
/**
* 砍价活动库存
*/
private Integer stock;
/**
* 达到该人数才能砍到低价
* 砍价人数
*
* 需要多少人砍价才能成功 {@link BargainRecordDO#getStatus()} 更新为 {@link BargainRecordDO#getStatus()} 成功状态
*/
private Integer userSize;
/**
* 最大帮砍次数
* 帮砍次数
*
* 单个活动用户可以帮砍的次数
* 例如说帮砍次数为 1 A B 同时将该活动链接发给 CC 只能帮其中一个人砍价
*/
private Integer bargainCount;
@ -93,6 +98,9 @@ public class BargainActivityDO extends BaseDO {
* 用户每次砍价的最大金额单位
*/
private Integer randomMaxPrice;
// ========== 统计字段 ==========
/**
* 砍价成功数量
*/

View File

@ -29,17 +29,17 @@ public class BargainRecordDO extends BaseDO {
*/
@TableId
private Long id;
/**
* 砍价活动编号
*/
private Long activityId;
/**
* 用户编号
*/
private Long userId;
/**
* 砍价活动编号
*
* 关联 {@link BargainActivityDO#getId()} 字段
*/
private Long activityId;
/**
* 商品 SPU 编号
*/
@ -50,17 +50,13 @@ public class BargainRecordDO extends BaseDO {
private Long skuId;
/**
* 砍价底价单位
* 砍价起始价格单位
*/
private Integer bargainFirstPrice;
/**
* 当前砍价单位
*/
private Integer bargainPrice;
/**
* 商品原价单位
*/
private Integer price;
/**
* 应付金额单位
*/
private Integer payPrice;
/**
* 砍价状态
@ -68,22 +64,14 @@ public class BargainRecordDO extends BaseDO {
* 枚举 {@link BargainRecordStatusEnum}
*/
private Integer status;
/**
* 订单编号
*/
private Long orderId;
/**
* 结束时间
*/
private LocalDateTime endTime;
/**
* 过期时间
*
* 到达该时间时其他用户无法帮助砍价但是还是允许下单
* 订单编号
*/
private LocalDateTime expireTime;
private Long orderId;
}

View File

@ -2,8 +2,11 @@ package cn.iocoder.yudao.module.promotion.dal.mysql.bargain;
import cn.iocoder.yudao.framework.mybatis.core.mapper.BaseMapperX;
import cn.iocoder.yudao.module.promotion.dal.dataobject.bargain.BargainRecordDO;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.apache.ibatis.annotations.Mapper;
import java.util.List;
/**
* 砍价记录 Mapper
*
@ -17,4 +20,20 @@ public interface BargainRecordMapper extends BaseMapperX<BargainRecordDO> {
BargainRecordDO::getUserId, userId);
}
default List<BargainRecordDO> selectListByUserIdAndActivityIdAndStatus(
Long userId, Long activityId, Integer status) {
return selectList(new LambdaQueryWrapper<>(BargainRecordDO.class)
.eq(BargainRecordDO::getUserId, userId)
.eq(BargainRecordDO::getActivityId, activityId)
.eq(BargainRecordDO::getStatus, status));
}
default Long selectCountByUserIdAndActivityIdAndStatus(
Long userId, Long activityId, Integer status) {
return selectCount(new LambdaQueryWrapper<>(BargainRecordDO.class)
.eq(BargainRecordDO::getUserId, userId)
.eq(BargainRecordDO::getActivityId, activityId)
.eq(BargainRecordDO::getStatus, status));
}
}

View File

@ -55,6 +55,14 @@ public interface BargainActivityService {
*/
BargainActivityDO getBargainActivity(Long id);
/**
* 校验砍价活动是否可以参与发起砍价下单帮好友砍价
*
* @param id 编号
* @return 砍价活动
*/
BargainActivityDO validateBargainActivityCanJoin(Long id);
/**
* 获得砍价活动分页
*

View File

@ -1,5 +1,6 @@
package cn.iocoder.yudao.module.promotion.service.bargain;
import cn.hutool.core.util.ObjUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.iocoder.yudao.framework.common.enums.CommonStatusEnum;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
@ -82,13 +83,13 @@ public class BargainActivityServiceImpl implements BargainActivityService {
throw exception(BARGAIN_ACTIVITY_NOT_EXISTS);
}
if (count > activity.getStock()) {
throw exception(BARGAIN_ACTIVITY_UPDATE_STOCK_FAIL);
throw exception(BARGAIN_ACTIVITY_STOCK_NOT_ENOUGH);
}
// 更新砍价库存
int updateCount = bargainActivityMapper.updateStock(id, count);
if (updateCount == 0) {
throw exception(BARGAIN_ACTIVITY_UPDATE_STOCK_FAIL);
throw exception(BARGAIN_ACTIVITY_STOCK_NOT_ENOUGH);
}
}
@ -138,6 +139,21 @@ public class BargainActivityServiceImpl implements BargainActivityService {
return bargainActivityMapper.selectById(id);
}
@Override
public BargainActivityDO validateBargainActivityCanJoin(Long id) {
BargainActivityDO activity = bargainActivityMapper.selectById(id);
if (activity == null) {
throw exception(BARGAIN_ACTIVITY_NOT_EXISTS);
}
if (ObjUtil.notEqual(activity.getStatus(), CommonStatusEnum.ENABLE.getStatus())) {
throw exception(BARGAIN_ACTIVITY_STATUS_CLOSED);
}
if (activity.getStock() <= 0) {
throw exception(BARGAIN_ACTIVITY_STOCK_NOT_ENOUGH);
}
return activity;
}
@Override
public PageResult<BargainActivityDO> getBargainActivityPage(BargainActivityPageReqVO pageReqVO) {
return bargainActivityMapper.selectPage(pageReqVO);

View File

@ -2,6 +2,7 @@ package cn.iocoder.yudao.module.promotion.service.bargain;
import cn.iocoder.yudao.module.promotion.api.bargain.dto.BargainValidateJoinRespDTO;
import cn.iocoder.yudao.module.promotion.controller.app.bargain.vo.record.AppBargainRecordCreateReqVO;
/**
* 砍价记录 service 接口
@ -10,6 +11,15 @@ import cn.iocoder.yudao.module.promotion.api.bargain.dto.BargainValidateJoinResp
*/
public interface BargainRecordService {
/**
* 会员创建砍价记录参与参加活动
*
* @param userId 用户编号
* @param reqVO 创建信息
* @return 砍价记录编号
*/
Long createBargainRecord(Long userId, AppBargainRecordCreateReqVO reqVO);
/**
* 下单前校验是否参与砍价活动
* <p>

View File

@ -1,10 +1,10 @@
package cn.iocoder.yudao.module.promotion.service.bargain;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.lang.Assert;
import cn.hutool.core.util.ObjUtil;
import cn.iocoder.yudao.framework.common.enums.CommonStatusEnum;
import cn.iocoder.yudao.framework.common.util.date.LocalDateTimeUtils;
import cn.iocoder.yudao.module.promotion.api.bargain.dto.BargainValidateJoinRespDTO;
import cn.iocoder.yudao.module.promotion.controller.app.bargain.vo.record.AppBargainRecordCreateReqVO;
import cn.iocoder.yudao.module.promotion.dal.dataobject.bargain.BargainActivityDO;
import cn.iocoder.yudao.module.promotion.dal.dataobject.bargain.BargainRecordDO;
import cn.iocoder.yudao.module.promotion.dal.mysql.bargain.BargainRecordMapper;
@ -13,7 +13,6 @@ import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;
import javax.annotation.Resource;
import java.util.Objects;
import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
@ -34,7 +33,30 @@ public class BargainRecordServiceImpl implements BargainRecordService {
@Resource
private BargainRecordMapper bargainRecordMapper;
// TODO puhui999create 需要校验下限购数量
@Override
public Long createBargainRecord(Long userId, AppBargainRecordCreateReqVO reqVO) {
// 1. 校验砍价活动
BargainActivityDO activity = bargainActivityService.validateBargainActivityCanJoin(reqVO.getActivityId());
// 2.1 校验当前是否已经有参与中的砍价活动
if (CollUtil.isNotEmpty(bargainRecordMapper.selectListByUserIdAndActivityIdAndStatus(
userId, reqVO.getActivityId(), BargainRecordStatusEnum.IN_PROGRESS.getStatus()))) {
throw exception(BARGAIN_RECORD_CREATE_FAIL_EXISTS);
}
// 2.2 是否超过参与的上限
if (bargainRecordMapper.selectCountByUserIdAndActivityIdAndStatus(
userId, reqVO.getActivityId(), BargainRecordStatusEnum.SUCCESS.getStatus()) >= activity.getTotalLimitCount()) {
throw exception(BARGAIN_RECORD_CREATE_FAIL_LIMIT);
}
// 3. 创建砍价记录
BargainRecordDO record = BargainRecordDO.builder().userId(userId)
.activityId(reqVO.getActivityId()).spuId(activity.getSpuId()).skuId(activity.getSkuId())
.bargainFirstPrice(activity.getBargainFirstPrice()).bargainPrice(activity.getBargainFirstPrice())
.status(BargainRecordStatusEnum.IN_PROGRESS.getStatus()).build();
bargainRecordMapper.insert(record);
return record.getId();
}
@Override
public BargainValidateJoinRespDTO validateJoinBargain(Long userId, Long bargainRecordId, Long skuId) {
@ -45,28 +67,14 @@ public class BargainRecordServiceImpl implements BargainRecordService {
}
// 1.2 拼团记录未在进行中
if (ObjUtil.notEqual(record.getStatus(), BargainRecordStatusEnum.IN_PROGRESS)) {
throw exception(BARGAIN_JOIN_RECORD_NOT_IN_PROGRESS);
throw exception(BARGAIN_JOIN_RECORD_NOT_SUCCESS);
}
// 2.1 砍价活动不存在
BargainActivityDO activity = bargainActivityService.getBargainActivity(record.getActivityId());
if (activity == null) {
throw exception(BARGAIN_ACTIVITY_NOT_EXISTS);
}
if (ObjUtil.notEqual(activity.getStatus(), CommonStatusEnum.ENABLE.getStatus())) {
throw exception(BARGAIN_JOIN_ACTIVITY_STATUS_CLOSED);
}
// 2. 校验砍价活动
BargainActivityDO activity = bargainActivityService.validateBargainActivityCanJoin(record.getActivityId());
Assert.isTrue(Objects.equals(skuId, activity.getSkuId()), "砍价商品不匹配"); // 防御性校验
// 2.2 活动已过期
if (LocalDateTimeUtils.isBetween(activity.getStartTime(), activity.getEndTime())) {
throw exception(BARGAIN_JOIN_FAILED_ACTIVITY_TIME_END);
}
// 2.3 库存不足
if (activity.getStock() <= 0) {
throw exception(BARGAIN_ACTIVITY_UPDATE_STOCK_FAIL);
}
return new BargainValidateJoinRespDTO().setActivityId(activity.getId()).setName(activity.getName())
.setBargainPrice(record.getPayPrice());
.setBargainPrice(record.getBargainPrice());
}
}