Commit 16e436a0f54fc5b305dc914c6b31d19a7e771a9b
1 parent
cc087e4e
promotion:增加助力砍价的接口
Showing
12 changed files
with
238 additions
and
9 deletions
yudao-module-mall/yudao-module-promotion-api/src/main/java/cn/iocoder/yudao/module/promotion/enums/ErrorCodeConstants.java
| @@ -100,4 +100,11 @@ public interface ErrorCodeConstants { | @@ -100,4 +100,11 @@ public interface ErrorCodeConstants { | ||
| 100 | ErrorCode BARGAIN_RECORD_CREATE_FAIL_LIMIT = new ErrorCode(1_013_013_002, "参与失败,您已达到当前砍价活动的参与上限"); | 100 | ErrorCode BARGAIN_RECORD_CREATE_FAIL_LIMIT = new ErrorCode(1_013_013_002, "参与失败,您已达到当前砍价活动的参与上限"); |
| 101 | ErrorCode BARGAIN_JOIN_RECORD_NOT_SUCCESS = new ErrorCode(1_013_013_004, "下单失败,砍价未成功"); | 101 | ErrorCode BARGAIN_JOIN_RECORD_NOT_SUCCESS = new ErrorCode(1_013_013_004, "下单失败,砍价未成功"); |
| 102 | 102 | ||
| 103 | + // ========== 砍价助力 1-013-014-000 ========== | ||
| 104 | + ErrorCode BARGAIN_HELP_CREATE_FAIL_RECORD_NOT_IN_PROCESS = new ErrorCode(1_013_014_000, "助力失败,砍价记录不处于进行中"); | ||
| 105 | + ErrorCode BARGAIN_HELP_CREATE_FAIL_RECORD_SELF = new ErrorCode(1_013_014_001, "助力失败,不能助力自己"); | ||
| 106 | + ErrorCode BARGAIN_HELP_CREATE_FAIL_LIMIT = new ErrorCode(1_013_014_002, "助力失败,您已达到当前砍价活动的助力上限"); | ||
| 107 | + ErrorCode BARGAIN_HELP_CREATE_FAIL_CONFLICT = new ErrorCode(1_013_014_003, "助力失败,请重试"); | ||
| 108 | + ErrorCode BARGAIN_HELP_CREATE_FAIL_HELP_EXISTS = new ErrorCode(1_013_014_004, "助力失败,您已经助力过了"); | ||
| 109 | + | ||
| 103 | } | 110 | } |
yudao-module-mall/yudao-module-promotion-biz/src/main/java/cn/iocoder/yudao/module/promotion/controller/app/bargain/AppBargainHelpController.http
0 → 100644
yudao-module-mall/yudao-module-promotion-biz/src/main/java/cn/iocoder/yudao/module/promotion/controller/app/bargain/AppBargainHelpController.java
| @@ -3,16 +3,20 @@ package cn.iocoder.yudao.module.promotion.controller.app.bargain; | @@ -3,16 +3,20 @@ package cn.iocoder.yudao.module.promotion.controller.app.bargain; | ||
| 3 | import cn.iocoder.yudao.framework.common.pojo.CommonResult; | 3 | import cn.iocoder.yudao.framework.common.pojo.CommonResult; |
| 4 | import cn.iocoder.yudao.module.promotion.controller.app.bargain.vo.help.AppBargainHelpCreateReqVO; | 4 | import cn.iocoder.yudao.module.promotion.controller.app.bargain.vo.help.AppBargainHelpCreateReqVO; |
| 5 | import cn.iocoder.yudao.module.promotion.controller.app.bargain.vo.help.AppBargainHelpRespVO; | 5 | import cn.iocoder.yudao.module.promotion.controller.app.bargain.vo.help.AppBargainHelpRespVO; |
| 6 | +import cn.iocoder.yudao.module.promotion.dal.dataobject.bargain.BargainHelpDO; | ||
| 7 | +import cn.iocoder.yudao.module.promotion.service.bargain.BargainHelpService; | ||
| 6 | import io.swagger.v3.oas.annotations.Operation; | 8 | import io.swagger.v3.oas.annotations.Operation; |
| 7 | import io.swagger.v3.oas.annotations.tags.Tag; | 9 | import io.swagger.v3.oas.annotations.tags.Tag; |
| 8 | import org.springframework.validation.annotation.Validated; | 10 | import org.springframework.validation.annotation.Validated; |
| 9 | import org.springframework.web.bind.annotation.*; | 11 | import org.springframework.web.bind.annotation.*; |
| 10 | 12 | ||
| 13 | +import javax.annotation.Resource; | ||
| 11 | import java.time.LocalDateTime; | 14 | import java.time.LocalDateTime; |
| 12 | import java.util.ArrayList; | 15 | import java.util.ArrayList; |
| 13 | import java.util.List; | 16 | import java.util.List; |
| 14 | 17 | ||
| 15 | import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success; | 18 | import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success; |
| 19 | +import static cn.iocoder.yudao.framework.security.core.util.SecurityFrameworkUtils.getLoginUserId; | ||
| 16 | 20 | ||
| 17 | @Tag(name = "用户 App - 砍价助力") | 21 | @Tag(name = "用户 App - 砍价助力") |
| 18 | @RestController | 22 | @RestController |
| @@ -20,10 +24,14 @@ import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success; | @@ -20,10 +24,14 @@ import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success; | ||
| 20 | @Validated | 24 | @Validated |
| 21 | public class AppBargainHelpController { | 25 | public class AppBargainHelpController { |
| 22 | 26 | ||
| 27 | + @Resource | ||
| 28 | + private BargainHelpService bargainHelpService; | ||
| 29 | + | ||
| 23 | @PostMapping("/create") | 30 | @PostMapping("/create") |
| 24 | @Operation(summary = "创建砍价助力", description = "给拼团记录砍一刀") // 返回结果为砍价金额,单位:分 | 31 | @Operation(summary = "创建砍价助力", description = "给拼团记录砍一刀") // 返回结果为砍价金额,单位:分 |
| 25 | - public CommonResult<Long> createBargainHelp(@RequestBody AppBargainHelpCreateReqVO reqVO) { | ||
| 26 | - return success(20L); | 32 | + public CommonResult<Integer> createBargainHelp(@RequestBody AppBargainHelpCreateReqVO reqVO) { |
| 33 | + BargainHelpDO help = bargainHelpService.createBargainHelp(getLoginUserId(), reqVO); | ||
| 34 | + return success(help.getReducePrice()); | ||
| 27 | } | 35 | } |
| 28 | 36 | ||
| 29 | @GetMapping("/list") | 37 | @GetMapping("/list") |
yudao-module-mall/yudao-module-promotion-biz/src/main/java/cn/iocoder/yudao/module/promotion/dal/dataobject/bargain/BargainAssistDO.java renamed to yudao-module-mall/yudao-module-promotion-biz/src/main/java/cn/iocoder/yudao/module/promotion/dal/dataobject/bargain/BargainHelpDO.java
| @@ -7,19 +7,19 @@ import com.baomidou.mybatisplus.annotation.TableName; | @@ -7,19 +7,19 @@ import com.baomidou.mybatisplus.annotation.TableName; | ||
| 7 | import lombok.*; | 7 | import lombok.*; |
| 8 | 8 | ||
| 9 | /** | 9 | /** |
| 10 | - * 砍价助力 DO TODO 芋艿:表结构 | 10 | + * 砍价助力 DO |
| 11 | * | 11 | * |
| 12 | * @author HUIHUI | 12 | * @author HUIHUI |
| 13 | */ | 13 | */ |
| 14 | -@TableName("promotion_bargain_assist") | ||
| 15 | -@KeySequence("promotion_bargain_assist_seq") // 用于 Oracle、PostgreSQL、Kingbase、DB2、H2 数据库的主键自增。如果是 MySQL 等数据库,可不写。 | 14 | +@TableName("promotion_bargain_help") |
| 15 | +@KeySequence("promotion_bargain_help_seq") // 用于 Oracle、PostgreSQL、Kingbase、DB2、H2 数据库的主键自增。如果是 MySQL 等数据库,可不写。 | ||
| 16 | @Data | 16 | @Data |
| 17 | @EqualsAndHashCode(callSuper = true) | 17 | @EqualsAndHashCode(callSuper = true) |
| 18 | @ToString(callSuper = true) | 18 | @ToString(callSuper = true) |
| 19 | @Builder | 19 | @Builder |
| 20 | @NoArgsConstructor | 20 | @NoArgsConstructor |
| 21 | @AllArgsConstructor | 21 | @AllArgsConstructor |
| 22 | -public class BargainAssistDO extends BaseDO { | 22 | +public class BargainHelpDO extends BaseDO { |
| 23 | 23 | ||
| 24 | /** | 24 | /** |
| 25 | * 编号 | 25 | * 编号 |
| @@ -29,11 +29,14 @@ public class BargainAssistDO extends BaseDO { | @@ -29,11 +29,14 @@ public class BargainAssistDO extends BaseDO { | ||
| 29 | 29 | ||
| 30 | /** | 30 | /** |
| 31 | * 砍价活动编号 | 31 | * 砍价活动编号 |
| 32 | + * | ||
| 33 | + * 关联 {@link BargainActivityDO#getId()} 字段 | ||
| 32 | */ | 34 | */ |
| 33 | private Long activityId; | 35 | private Long activityId; |
| 34 | - | ||
| 35 | /** | 36 | /** |
| 36 | * 砍价记录编号 | 37 | * 砍价记录编号 |
| 38 | + * | ||
| 39 | + * 关联 {@link BargainRecordDO#getId()} 字段 | ||
| 37 | */ | 40 | */ |
| 38 | private Long recordId; | 41 | private Long recordId; |
| 39 | 42 | ||
| @@ -41,9 +44,8 @@ public class BargainAssistDO extends BaseDO { | @@ -41,9 +44,8 @@ public class BargainAssistDO extends BaseDO { | ||
| 41 | * 用户编号 | 44 | * 用户编号 |
| 42 | */ | 45 | */ |
| 43 | private Long userId; | 46 | private Long userId; |
| 44 | - | ||
| 45 | /** | 47 | /** |
| 46 | - * 减少价格。单位分 | 48 | + * 减少价格,单位:分 |
| 47 | */ | 49 | */ |
| 48 | private Integer reducePrice; | 50 | private Integer reducePrice; |
| 49 | 51 |
yudao-module-mall/yudao-module-promotion-biz/src/main/java/cn/iocoder/yudao/module/promotion/dal/dataobject/bargain/BargainRecordDO.java
| @@ -61,6 +61,10 @@ public class BargainRecordDO extends BaseDO { | @@ -61,6 +61,10 @@ public class BargainRecordDO extends BaseDO { | ||
| 61 | /** | 61 | /** |
| 62 | * 砍价状态 | 62 | * 砍价状态 |
| 63 | * | 63 | * |
| 64 | + * 砍价成功的条件是:(2 选 1) | ||
| 65 | + * 1. 砍价到 {@link BargainActivityDO#getBargainMinPrice()} 底价 | ||
| 66 | + * 2. 助力人数到达 {@link BargainActivityDO#getUserSize()} 人 | ||
| 67 | + * | ||
| 64 | * 枚举 {@link BargainRecordStatusEnum} | 68 | * 枚举 {@link BargainRecordStatusEnum} |
| 65 | */ | 69 | */ |
| 66 | private Integer status; | 70 | private Integer status; |
yudao-module-mall/yudao-module-promotion-biz/src/main/java/cn/iocoder/yudao/module/promotion/dal/mysql/bargain/BargainHelpMapper.java
0 → 100644
| 1 | +package cn.iocoder.yudao.module.promotion.dal.mysql.bargain; | ||
| 2 | + | ||
| 3 | +import cn.iocoder.yudao.framework.mybatis.core.mapper.BaseMapperX; | ||
| 4 | +import cn.iocoder.yudao.module.promotion.dal.dataobject.bargain.BargainHelpDO; | ||
| 5 | +import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; | ||
| 6 | +import org.apache.ibatis.annotations.Mapper; | ||
| 7 | + | ||
| 8 | +@Mapper | ||
| 9 | +public interface BargainHelpMapper extends BaseMapperX<BargainHelpDO> { | ||
| 10 | + | ||
| 11 | + default Long selectCountByUserIdAndActivityId(Long userId, Long activityId) { | ||
| 12 | + return selectCount(new LambdaQueryWrapper<>(BargainHelpDO.class) | ||
| 13 | + .eq(BargainHelpDO::getUserId, userId) | ||
| 14 | + .eq(BargainHelpDO::getActivityId, activityId)); | ||
| 15 | + } | ||
| 16 | + | ||
| 17 | + default Long selectCountByRecordId(Long recordId) { | ||
| 18 | + return selectCount(BargainHelpDO::getRecordId, recordId); | ||
| 19 | + } | ||
| 20 | + | ||
| 21 | + default BargainHelpDO selectByUserIdAndRecordId(Long userId, Long recordId) { | ||
| 22 | + return selectOne(new LambdaQueryWrapper<>(BargainHelpDO.class) | ||
| 23 | + .eq(BargainHelpDO::getUserId, userId) | ||
| 24 | + .eq(BargainHelpDO::getRecordId, recordId)); | ||
| 25 | + } | ||
| 26 | + | ||
| 27 | +} |
yudao-module-mall/yudao-module-promotion-biz/src/main/java/cn/iocoder/yudao/module/promotion/dal/mysql/bargain/BargainRecordMapper.java
| @@ -36,4 +36,10 @@ public interface BargainRecordMapper extends BaseMapperX<BargainRecordDO> { | @@ -36,4 +36,10 @@ public interface BargainRecordMapper extends BaseMapperX<BargainRecordDO> { | ||
| 36 | .eq(BargainRecordDO::getStatus, status)); | 36 | .eq(BargainRecordDO::getStatus, status)); |
| 37 | } | 37 | } |
| 38 | 38 | ||
| 39 | + default int updateByIdAndBargainPrice(Long id, Integer whereBargainPrice, BargainRecordDO updateObj) { | ||
| 40 | + return update(updateObj, new LambdaQueryWrapper<>(BargainRecordDO.class) | ||
| 41 | + .eq(BargainRecordDO::getId, id) | ||
| 42 | + .eq(BargainRecordDO::getBargainPrice, whereBargainPrice)); | ||
| 43 | + } | ||
| 44 | + | ||
| 39 | } | 45 | } |
yudao-module-mall/yudao-module-promotion-biz/src/main/java/cn/iocoder/yudao/module/promotion/service/bargain/BargainActivityServiceImpl.java
| @@ -5,6 +5,7 @@ import cn.hutool.core.util.ObjectUtil; | @@ -5,6 +5,7 @@ import cn.hutool.core.util.ObjectUtil; | ||
| 5 | import cn.iocoder.yudao.framework.common.enums.CommonStatusEnum; | 5 | import cn.iocoder.yudao.framework.common.enums.CommonStatusEnum; |
| 6 | import cn.iocoder.yudao.framework.common.pojo.PageParam; | 6 | import cn.iocoder.yudao.framework.common.pojo.PageParam; |
| 7 | import cn.iocoder.yudao.framework.common.pojo.PageResult; | 7 | import cn.iocoder.yudao.framework.common.pojo.PageResult; |
| 8 | +import cn.iocoder.yudao.framework.common.util.date.LocalDateTimeUtils; | ||
| 8 | import cn.iocoder.yudao.module.product.api.sku.ProductSkuApi; | 9 | import cn.iocoder.yudao.module.product.api.sku.ProductSkuApi; |
| 9 | import cn.iocoder.yudao.module.product.api.sku.dto.ProductSkuRespDTO; | 10 | import cn.iocoder.yudao.module.product.api.sku.dto.ProductSkuRespDTO; |
| 10 | import cn.iocoder.yudao.module.promotion.controller.admin.bargain.vo.BargainActivityCreateReqVO; | 11 | import cn.iocoder.yudao.module.promotion.controller.admin.bargain.vo.BargainActivityCreateReqVO; |
| @@ -151,6 +152,9 @@ public class BargainActivityServiceImpl implements BargainActivityService { | @@ -151,6 +152,9 @@ public class BargainActivityServiceImpl implements BargainActivityService { | ||
| 151 | if (activity.getStock() <= 0) { | 152 | if (activity.getStock() <= 0) { |
| 152 | throw exception(BARGAIN_ACTIVITY_STOCK_NOT_ENOUGH); | 153 | throw exception(BARGAIN_ACTIVITY_STOCK_NOT_ENOUGH); |
| 153 | } | 154 | } |
| 155 | + if (LocalDateTimeUtils.isBetween(activity.getStartTime(), activity.getEndTime())) { | ||
| 156 | + throw exception(BARGAIN_ACTIVITY_TIME_END); | ||
| 157 | + } | ||
| 154 | return activity; | 158 | return activity; |
| 155 | } | 159 | } |
| 156 | 160 |
yudao-module-mall/yudao-module-promotion-biz/src/main/java/cn/iocoder/yudao/module/promotion/service/bargain/BargainHelpService.java
0 → 100644
| 1 | +package cn.iocoder.yudao.module.promotion.service.bargain; | ||
| 2 | + | ||
| 3 | +import cn.iocoder.yudao.module.promotion.controller.app.bargain.vo.help.AppBargainHelpCreateReqVO; | ||
| 4 | +import cn.iocoder.yudao.module.promotion.dal.dataobject.bargain.BargainHelpDO; | ||
| 5 | + | ||
| 6 | +/** | ||
| 7 | + * 砍价助力 Service 接口 | ||
| 8 | + * | ||
| 9 | + * @author 芋道源码 | ||
| 10 | + */ | ||
| 11 | +public interface BargainHelpService { | ||
| 12 | + | ||
| 13 | + /** | ||
| 14 | + * 创建砍价助力(帮人砍价) | ||
| 15 | + * | ||
| 16 | + * @param userId 用户编号 | ||
| 17 | + * @param reqVO 请求信息 | ||
| 18 | + * @return 砍价助力记录 | ||
| 19 | + */ | ||
| 20 | + BargainHelpDO createBargainHelp(Long userId, AppBargainHelpCreateReqVO reqVO); | ||
| 21 | + | ||
| 22 | +} |
yudao-module-mall/yudao-module-promotion-biz/src/main/java/cn/iocoder/yudao/module/promotion/service/bargain/BargainHelpServiceImpl.java
0 → 100644
| 1 | +package cn.iocoder.yudao.module.promotion.service.bargain; | ||
| 2 | + | ||
| 3 | +import cn.hutool.core.lang.Assert; | ||
| 4 | +import cn.hutool.core.util.ObjUtil; | ||
| 5 | +import cn.iocoder.yudao.module.promotion.controller.app.bargain.vo.help.AppBargainHelpCreateReqVO; | ||
| 6 | +import cn.iocoder.yudao.module.promotion.dal.dataobject.bargain.BargainActivityDO; | ||
| 7 | +import cn.iocoder.yudao.module.promotion.dal.dataobject.bargain.BargainHelpDO; | ||
| 8 | +import cn.iocoder.yudao.module.promotion.dal.dataobject.bargain.BargainRecordDO; | ||
| 9 | +import cn.iocoder.yudao.module.promotion.dal.mysql.bargain.BargainHelpMapper; | ||
| 10 | +import cn.iocoder.yudao.module.promotion.enums.bargain.BargainRecordStatusEnum; | ||
| 11 | +import jodd.util.MathUtil; | ||
| 12 | +import org.springframework.stereotype.Service; | ||
| 13 | +import org.springframework.transaction.annotation.Transactional; | ||
| 14 | +import org.springframework.validation.annotation.Validated; | ||
| 15 | + | ||
| 16 | +import javax.annotation.Resource; | ||
| 17 | + | ||
| 18 | +import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception; | ||
| 19 | +import static cn.iocoder.yudao.module.promotion.enums.ErrorCodeConstants.*; | ||
| 20 | + | ||
| 21 | +/** | ||
| 22 | + * 砍价助力 Service 实现类 | ||
| 23 | + * | ||
| 24 | + * @author 芋道源码 | ||
| 25 | + */ | ||
| 26 | +@Service | ||
| 27 | +@Validated | ||
| 28 | +public class BargainHelpServiceImpl implements BargainHelpService { | ||
| 29 | + | ||
| 30 | + @Resource | ||
| 31 | + private BargainHelpMapper bargainHelpMapper; | ||
| 32 | + | ||
| 33 | + @Resource | ||
| 34 | + private BargainRecordService bargainRecordService; | ||
| 35 | + @Resource | ||
| 36 | + private BargainActivityService bargainActivityService; | ||
| 37 | + | ||
| 38 | + @Override | ||
| 39 | + @Transactional(rollbackFor = Exception.class) | ||
| 40 | + public BargainHelpDO createBargainHelp(Long userId, AppBargainHelpCreateReqVO reqVO) { | ||
| 41 | + // 1.1 校验砍价记录存在,并且处于进行中 | ||
| 42 | + BargainRecordDO record = bargainRecordService.getBargainRecord(reqVO.getRecordId()); | ||
| 43 | + if (record == null) { | ||
| 44 | + throw exception(BARGAIN_RECORD_NOT_EXISTS); | ||
| 45 | + } | ||
| 46 | + if (ObjUtil.notEqual(record.getStatus(), BargainRecordStatusEnum.IN_PROGRESS.getStatus())) { | ||
| 47 | + throw exception(BARGAIN_HELP_CREATE_FAIL_RECORD_NOT_IN_PROCESS); | ||
| 48 | + } | ||
| 49 | + // 1.2 不能自己给自己砍价 | ||
| 50 | + if (ObjUtil.equal(record.getUserId(), userId)) { | ||
| 51 | + throw exception(BARGAIN_HELP_CREATE_FAIL_RECORD_SELF); | ||
| 52 | + } | ||
| 53 | + | ||
| 54 | + // 2.1 校验砍价活动 | ||
| 55 | + BargainActivityDO activity = bargainActivityService.getBargainActivity(record.getActivityId()); | ||
| 56 | + // 2.2 校验自己是否助力次数上限 | ||
| 57 | + if (bargainHelpMapper.selectCountByUserIdAndActivityId(userId, activity.getId()) | ||
| 58 | + >= activity.getBargainCount()) { | ||
| 59 | + throw exception(BARGAIN_HELP_CREATE_FAIL_LIMIT); | ||
| 60 | + } | ||
| 61 | + // 2.3 特殊情况:砍价已经砍到最低价,不能再砍了 | ||
| 62 | + if (record.getBargainPrice() <= activity.getBargainMinPrice()) { | ||
| 63 | + throw exception(BARGAIN_HELP_CREATE_FAIL_RECORD_NOT_IN_PROCESS); | ||
| 64 | + } | ||
| 65 | + | ||
| 66 | + // 3. 已经助力 | ||
| 67 | + if (bargainHelpMapper.selectByUserIdAndRecordId(userId, record.getId()) != null) { | ||
| 68 | + throw exception(BARGAIN_HELP_CREATE_FAIL_HELP_EXISTS); | ||
| 69 | + } | ||
| 70 | + | ||
| 71 | + // 4.1 计算砍价金额 | ||
| 72 | + Integer reducePrice = calculateReducePrice(activity, record); | ||
| 73 | + Assert.isTrue(reducePrice > 0, "砍价金额必须大于 0 元"); | ||
| 74 | + // 4.2 创建助力记录 | ||
| 75 | + BargainHelpDO help = BargainHelpDO.builder().userId(userId).activityId(activity.getId()) | ||
| 76 | + .recordId(record.getId()).reducePrice(reducePrice).build(); | ||
| 77 | + bargainHelpMapper.insert(help); | ||
| 78 | + | ||
| 79 | + // 5. 判断砍价记录是否完成 | ||
| 80 | + Boolean success = record.getBargainPrice() - reducePrice <= activity.getBargainMinPrice() // 情况一:砍价已经砍到最低价 | ||
| 81 | + || bargainHelpMapper.selectCountByRecordId(reqVO.getRecordId()) >= activity.getTotalLimitCount(); // 情况二:砍价助力已经达到上限 | ||
| 82 | + if (!bargainRecordService.updateBargainRecordBargainPrice( | ||
| 83 | + record.getId(), record.getBargainPrice(), reducePrice, success)) { | ||
| 84 | + // 多人一起砍价,需要重试 | ||
| 85 | + throw exception(BARGAIN_HELP_CREATE_FAIL_CONFLICT); | ||
| 86 | + } | ||
| 87 | + return help; | ||
| 88 | + } | ||
| 89 | + | ||
| 90 | + // TODO 芋艿:优化点:实现一个更随机的逻辑,可以按照你自己的业务; | ||
| 91 | + private Integer calculateReducePrice(BargainActivityDO activity, BargainRecordDO record) { | ||
| 92 | + // 1. 随机金额 | ||
| 93 | + Integer reducePrice = MathUtil.randomInt(activity.getBargainMinPrice(), | ||
| 94 | + activity.getRandomMaxPrice() + 1); // + 1 的原因是,randomInt 默认不包含第二个参数 | ||
| 95 | + // 2. 校验是否超过砍价上限 | ||
| 96 | + if (record.getBargainPrice() - reducePrice < activity.getBargainMinPrice()) { | ||
| 97 | + reducePrice = record.getBargainPrice() - activity.getBargainMinPrice(); | ||
| 98 | + } | ||
| 99 | + return reducePrice; | ||
| 100 | + } | ||
| 101 | + | ||
| 102 | +} |
yudao-module-mall/yudao-module-promotion-biz/src/main/java/cn/iocoder/yudao/module/promotion/service/bargain/BargainRecordService.java
| @@ -3,6 +3,7 @@ package cn.iocoder.yudao.module.promotion.service.bargain; | @@ -3,6 +3,7 @@ package cn.iocoder.yudao.module.promotion.service.bargain; | ||
| 3 | 3 | ||
| 4 | import cn.iocoder.yudao.module.promotion.api.bargain.dto.BargainValidateJoinRespDTO; | 4 | import cn.iocoder.yudao.module.promotion.api.bargain.dto.BargainValidateJoinRespDTO; |
| 5 | import cn.iocoder.yudao.module.promotion.controller.app.bargain.vo.record.AppBargainRecordCreateReqVO; | 5 | import cn.iocoder.yudao.module.promotion.controller.app.bargain.vo.record.AppBargainRecordCreateReqVO; |
| 6 | +import cn.iocoder.yudao.module.promotion.dal.dataobject.bargain.BargainRecordDO; | ||
| 6 | 7 | ||
| 7 | /** | 8 | /** |
| 8 | * 砍价记录 service 接口 | 9 | * 砍价记录 service 接口 |
| @@ -21,6 +22,20 @@ public interface BargainRecordService { | @@ -21,6 +22,20 @@ public interface BargainRecordService { | ||
| 21 | Long createBargainRecord(Long userId, AppBargainRecordCreateReqVO reqVO); | 22 | Long createBargainRecord(Long userId, AppBargainRecordCreateReqVO reqVO); |
| 22 | 23 | ||
| 23 | /** | 24 | /** |
| 25 | + * 更新砍价记录的砍价金额 | ||
| 26 | + * | ||
| 27 | + * 如果满足砍价成功的条件,则更新砍价记录的状态为成功 | ||
| 28 | + * | ||
| 29 | + * @param id 砍价记录编号 | ||
| 30 | + * @param whereBargainPrice 当前的砍价金额 | ||
| 31 | + * @param reducePrice 减少的砍价金额 | ||
| 32 | + * @param success 是否砍价成功 | ||
| 33 | + * @return 是否更新成功。注意,如果并发更新时,会更新失败 | ||
| 34 | + */ | ||
| 35 | + Boolean updateBargainRecordBargainPrice(Long id, Integer whereBargainPrice, | ||
| 36 | + Integer reducePrice, Boolean success); | ||
| 37 | + | ||
| 38 | + /** | ||
| 24 | * 【下单前】校验是否参与砍价活动 | 39 | * 【下单前】校验是否参与砍价活动 |
| 25 | * <p> | 40 | * <p> |
| 26 | * 如果校验失败,则抛出业务异常 | 41 | * 如果校验失败,则抛出业务异常 |
| @@ -32,4 +47,12 @@ public interface BargainRecordService { | @@ -32,4 +47,12 @@ public interface BargainRecordService { | ||
| 32 | */ | 47 | */ |
| 33 | BargainValidateJoinRespDTO validateJoinBargain(Long userId, Long bargainRecordId, Long skuId); | 48 | BargainValidateJoinRespDTO validateJoinBargain(Long userId, Long bargainRecordId, Long skuId); |
| 34 | 49 | ||
| 50 | + /** | ||
| 51 | + * 获得砍价记录 | ||
| 52 | + * | ||
| 53 | + * @param id 砍价记录编号 | ||
| 54 | + * @return 砍价记录 | ||
| 55 | + */ | ||
| 56 | + BargainRecordDO getBargainRecord(Long id); | ||
| 57 | + | ||
| 35 | } | 58 | } |
yudao-module-mall/yudao-module-promotion-biz/src/main/java/cn/iocoder/yudao/module/promotion/service/bargain/BargainRecordServiceImpl.java
| @@ -59,6 +59,16 @@ public class BargainRecordServiceImpl implements BargainRecordService { | @@ -59,6 +59,16 @@ public class BargainRecordServiceImpl implements BargainRecordService { | ||
| 59 | } | 59 | } |
| 60 | 60 | ||
| 61 | @Override | 61 | @Override |
| 62 | + public Boolean updateBargainRecordBargainPrice(Long id, Integer whereBargainPrice, | ||
| 63 | + Integer reducePrice, Boolean success) { | ||
| 64 | + BargainRecordDO updateObj = new BargainRecordDO().setBargainPrice(whereBargainPrice - reducePrice); | ||
| 65 | + if (success) { | ||
| 66 | + updateObj.setStatus(BargainRecordStatusEnum.SUCCESS.getStatus()); | ||
| 67 | + } | ||
| 68 | + return bargainRecordMapper.updateByIdAndBargainPrice(id, whereBargainPrice, updateObj) > 0; | ||
| 69 | + } | ||
| 70 | + | ||
| 71 | + @Override | ||
| 62 | public BargainValidateJoinRespDTO validateJoinBargain(Long userId, Long bargainRecordId, Long skuId) { | 72 | public BargainValidateJoinRespDTO validateJoinBargain(Long userId, Long bargainRecordId, Long skuId) { |
| 63 | // 1.1 拼团记录不存在 | 73 | // 1.1 拼团记录不存在 |
| 64 | BargainRecordDO record = bargainRecordMapper.selectByIdAndUserId(bargainRecordId, userId); | 74 | BargainRecordDO record = bargainRecordMapper.selectByIdAndUserId(bargainRecordId, userId); |
| @@ -77,4 +87,9 @@ public class BargainRecordServiceImpl implements BargainRecordService { | @@ -77,4 +87,9 @@ public class BargainRecordServiceImpl implements BargainRecordService { | ||
| 77 | .setBargainPrice(record.getBargainPrice()); | 87 | .setBargainPrice(record.getBargainPrice()); |
| 78 | } | 88 | } |
| 79 | 89 | ||
| 90 | + @Override | ||
| 91 | + public BargainRecordDO getBargainRecord(Long id) { | ||
| 92 | + return bargainRecordMapper.selectById(id); | ||
| 93 | + } | ||
| 94 | + | ||
| 80 | } | 95 | } |