新客立减 金额问题
This commit is contained in:
@@ -65,7 +65,7 @@ public class MkShopConsumeDiscountRecordServiceImpl extends ServiceImpl<MkShopCo
|
||||
|
||||
// 随机金额(概率加起来是100)
|
||||
List<MkConsumeDiscountRandom> randomList = consumeDiscountVO.getRandomDiscountList();
|
||||
if ("RANDOM".equals(consumeDiscountVO.getDiscountType()) && (randomList == null || randomList.isEmpty()) ){
|
||||
if ("RANDOM".equals(consumeDiscountVO.getDiscountType()) && (randomList == null || randomList.isEmpty())) {
|
||||
throw new CzgException("随机立减配置错误");
|
||||
}
|
||||
ShopUser shopUser = shopUserService.getShopUserInfo(shopId, userId);
|
||||
@@ -84,8 +84,8 @@ public class MkShopConsumeDiscountRecordServiceImpl extends ServiceImpl<MkShopCo
|
||||
|
||||
@Override
|
||||
public void useDiscount(Long shopUserId, Long orderId, BigDecimal amount) {
|
||||
AssertUtil.isTrue( shopUserId == null || orderId == null || amount == null, "参数不能为空");
|
||||
ShopUser shopUser = shopUserService.getById(shopUserId);
|
||||
AssertUtil.isTrue(shopUserId == null || orderId == null || amount == null, "参数不能为空");
|
||||
// ShopUser shopUser = shopUserService.getById(shopUserId);
|
||||
|
||||
MkShopConsumeDiscountRecord discountRecord = getOne(new QueryWrapper()
|
||||
.eq(MkShopConsumeDiscountRecord::getShopUserId, shopUserId));
|
||||
@@ -125,7 +125,7 @@ public class MkShopConsumeDiscountRecordServiceImpl extends ServiceImpl<MkShopCo
|
||||
MkConsumeDiscountVO consumeDiscountVO = canUse(shopId, userId);
|
||||
|
||||
if (consumeDiscountVO.getIsEnable() != 1) {
|
||||
return null;
|
||||
return null;
|
||||
}
|
||||
|
||||
DateTime now = DateUtil.date();
|
||||
@@ -139,8 +139,17 @@ public class MkShopConsumeDiscountRecordServiceImpl extends ServiceImpl<MkShopCo
|
||||
.eq(MkShopConsumeDiscountRecord::getIsUse, 0)
|
||||
.eq(MkShopConsumeDiscountRecord::getShopUserId, shopUser.getId()));
|
||||
if (discountRecord != null) {
|
||||
boolean isUp = false;
|
||||
if (consumeDiscountVO.getUpdateTime() != null && consumeDiscountVO.getUpdateTime().isBefore(discountRecord.getUpdateTime())) {
|
||||
BigDecimal deductionAmount = deductionAmount(consumeDiscountVO);
|
||||
discountRecord.setAmount(deductionAmount);
|
||||
isUp = true;
|
||||
}
|
||||
if (discountRecord.getOrderId() == null && orderId != null) {
|
||||
discountRecord.setOrderId(orderId);
|
||||
isUp = true;
|
||||
}
|
||||
if (isUp) {
|
||||
updateById(discountRecord);
|
||||
}
|
||||
return discountRecord;
|
||||
@@ -152,38 +161,43 @@ public class MkShopConsumeDiscountRecordServiceImpl extends ServiceImpl<MkShopCo
|
||||
shopConsumeDiscountRecord.setOrderId(orderId);
|
||||
shopConsumeDiscountRecord.setConsumeDiscountId(consumeDiscountVO.getId());
|
||||
|
||||
// 固定金额
|
||||
if (TableValueConstant.ConsumeDiscount.DiscountType.FIXED.getCode().equals(consumeDiscountVO.getDiscountType())) {
|
||||
shopConsumeDiscountRecord.setAmount(consumeDiscountVO.getDiscountAmount());
|
||||
save(shopConsumeDiscountRecord);
|
||||
return shopConsumeDiscountRecord;
|
||||
BigDecimal deductionAmount = deductionAmount(consumeDiscountVO);
|
||||
if (deductionAmount.compareTo(BigDecimal.ZERO) == 0) {
|
||||
return null;
|
||||
}
|
||||
|
||||
// 随机金额(概率加起来是100)
|
||||
List<MkConsumeDiscountRandom> randomList = consumeDiscountVO.getRandomDiscountList();
|
||||
if (randomList == null || randomList.isEmpty()) {
|
||||
throw new CzgException("随机立减配置错误");
|
||||
}
|
||||
|
||||
// 生成 1~100 的随机数
|
||||
int rand = new Random().nextInt(100) + 1;
|
||||
int current = 0;
|
||||
|
||||
for (MkConsumeDiscountRandom item : randomList) {
|
||||
int prob = item.getProbability().intValue();
|
||||
current += prob;
|
||||
if (rand <= current) {
|
||||
shopConsumeDiscountRecord.setAmount(item.getAmount());
|
||||
save(shopConsumeDiscountRecord);
|
||||
return shopConsumeDiscountRecord;
|
||||
}
|
||||
}
|
||||
|
||||
throw new CzgException("随机立减计算失败");
|
||||
shopConsumeDiscountRecord.setAmount(deductionAmount);
|
||||
save(shopConsumeDiscountRecord);
|
||||
return shopConsumeDiscountRecord;
|
||||
} catch (Exception e) {
|
||||
log.info(e.getMessage());
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
private BigDecimal deductionAmount(MkConsumeDiscountVO consumeDiscountVO) {
|
||||
// 固定金额
|
||||
if (TableValueConstant.ConsumeDiscount.DiscountType.FIXED.getCode().equals(consumeDiscountVO.getDiscountType())) {
|
||||
return consumeDiscountVO.getDiscountAmount();
|
||||
}
|
||||
|
||||
// 随机金额(概率加起来是100)
|
||||
List<MkConsumeDiscountRandom> randomList = consumeDiscountVO.getRandomDiscountList();
|
||||
if (randomList == null || randomList.isEmpty()) {
|
||||
throw new CzgException("随机立减配置错误");
|
||||
}
|
||||
|
||||
// 生成 1~100 的随机数
|
||||
int rand = new Random().nextInt(100) + 1;
|
||||
int current = 0;
|
||||
|
||||
for (MkConsumeDiscountRandom item : randomList) {
|
||||
int prob = item.getProbability().intValue();
|
||||
current += prob;
|
||||
if (rand <= current) {
|
||||
return item.getAmount();
|
||||
}
|
||||
}
|
||||
return BigDecimal.ZERO;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user