|
|
@@ -20,6 +20,7 @@ import com.ruoyi.subsidy.enums.SubsidyCancelType;
|
|
|
import com.ruoyi.subsidy.enums.SubsidyStatus;
|
|
|
import com.ruoyi.subsidy.service.ISubsidyCutInService;
|
|
|
import com.ruoyi.subsidy.service.ISubsidyModeService;
|
|
|
+import com.ruoyi.subsidy.service.ISubsidySharedPoolService;
|
|
|
import com.ruoyi.user.domain.bo.UserChangeBalanceBo;
|
|
|
import com.ruoyi.user.enums.BalanceSourceType;
|
|
|
import com.ruoyi.user.service.IUserService;
|
|
|
@@ -67,6 +68,10 @@ public class SubsidyQueueServiceImpl implements ISubsidyQueueService {
|
|
|
@Resource
|
|
|
private IUserService userService;
|
|
|
|
|
|
+ @Lazy
|
|
|
+ @Resource
|
|
|
+ private ISubsidySharedPoolService subsidySharedPoolService;
|
|
|
+
|
|
|
/**
|
|
|
* 查询补贴排队分页
|
|
|
*
|
|
|
@@ -205,13 +210,15 @@ public class SubsidyQueueServiceImpl implements ISubsidyQueueService {
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
- public Long validQueueCount(Long businessId) {
|
|
|
+ public Long validQueueCount(Long businessId, Boolean excludeCutIn) {
|
|
|
return this.baseMapper.selectCount(new LambdaQueryWrapper<SubsidyQueue>()
|
|
|
.eq(SubsidyQueue::getCurrentBusinessId, businessId)
|
|
|
.eq(SubsidyQueue::getSubsidyStatus, SubsidyStatus.WAITING)
|
|
|
+ .eq(excludeCutIn, SubsidyQueue::getCutIn, false)
|
|
|
);
|
|
|
}
|
|
|
|
|
|
+
|
|
|
/**
|
|
|
* 获取商家当前排队人数
|
|
|
*
|
|
|
@@ -220,7 +227,11 @@ public class SubsidyQueueServiceImpl implements ISubsidyQueueService {
|
|
|
*/
|
|
|
@Override
|
|
|
public Long validQueueCountAndLock(Long businessId) {
|
|
|
- return this.baseMapper.validQueueCountAndLock(businessId);
|
|
|
+ return this.baseMapper.selectCount(new LambdaQueryWrapper<SubsidyQueue>()
|
|
|
+ .eq(SubsidyQueue::getCurrentBusinessId, businessId)
|
|
|
+ .eq(SubsidyQueue::getSubsidyStatus, SubsidyStatus.WAITING)
|
|
|
+ .last("for update")
|
|
|
+ );
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
@@ -274,14 +285,14 @@ public class SubsidyQueueServiceImpl implements ISubsidyQueueService {
|
|
|
|
|
|
//排队均摊补贴金额
|
|
|
BigDecimal sharedSubsidy = MathUtils.subtract(subsidyQueue, firstSubsidy);
|
|
|
- BigDecimal sharedSubsidyReturnAmount = sharedSubsidyHandle(order.getBusinessId(), sharedSubsidy);
|
|
|
+ BigDecimal sharedSubsidyReturnAmount = sharedSubsidyHandle(order, currentMode,sharedSubsidy);
|
|
|
if (sharedSubsidyReturnAmount.compareTo(BigDecimal.ZERO) > 0) {
|
|
|
shouldSubsidyAmount = MathUtils.subtract(shouldSubsidyAmount, sharedSubsidyReturnAmount);
|
|
|
}
|
|
|
}
|
|
|
|
|
|
if (ObjectUtil.isNotNull(firstSubsidyCutIn) && subsidyCutIn.compareTo(BigDecimal.ZERO) > 0) {
|
|
|
- BigDecimal returnAmount = subsidyCutInHandle(firstSubsidyCutIn, subsidyCutIn);
|
|
|
+ BigDecimal returnAmount = subsidyCutInService.subsidyCutInHandle(order,firstSubsidyCutIn, subsidyCutIn);
|
|
|
if (returnAmount.compareTo(BigDecimal.ZERO) > 0) {
|
|
|
shouldSubsidyAmount = MathUtils.subtract(shouldSubsidyAmount, returnAmount);
|
|
|
}
|
|
|
@@ -290,25 +301,121 @@ public class SubsidyQueueServiceImpl implements ISubsidyQueueService {
|
|
|
// 有了商家的实际补贴金额,就可以计算商家当前这个订单的实际所得
|
|
|
// 实际所得 = 订单金额- 商家实际补贴金额(让利)- 平台服务费(5%)
|
|
|
|
|
|
+ }
|
|
|
|
|
|
+ @Override
|
|
|
+ public void fillUpKickOut(Long businessId, Long originalQueueId, BigDecimal actualAddSubsidyAmount) {
|
|
|
+ SubsidyQueue subsidyQueue = loadById(originalQueueId, false);
|
|
|
+ if(subsidyQueue==null)
|
|
|
+ {
|
|
|
+ return;
|
|
|
+ }
|
|
|
|
|
|
+ subsidyQueue.setActualSubsidyAmount(subsidyQueue.getActualSubsidyAmount().add(actualAddSubsidyAmount));
|
|
|
+ subsidyQueue.setSubsidyStatus(SubsidyStatus.COMPLETE);
|
|
|
+ subsidyQueue.setQueueOrder(0);
|
|
|
+ subsidyQueue.setCompleteTime(new Date());
|
|
|
+ this.baseMapper.updateById(subsidyQueue);
|
|
|
+ //改变剩下对列的值
|
|
|
+ validQueueOrderRearrange(businessId,subsidyQueue.getQueueId());
|
|
|
}
|
|
|
|
|
|
- private BigDecimal subsidyCutInHandle(SubsidyCutIn firstSubsidyCutIn, BigDecimal subsidyCutIn) {
|
|
|
- return BigDecimal.ZERO;
|
|
|
- }
|
|
|
|
|
|
/**
|
|
|
* 均摊补贴金额
|
|
|
*
|
|
|
- * @param businessId 商家id
|
|
|
+ * @param order 订单
|
|
|
* @param sharedSubsidy 排队均摊补贴金额
|
|
|
* @return 结余
|
|
|
*/
|
|
|
- private BigDecimal sharedSubsidyHandle(Long businessId, BigDecimal sharedSubsidy) {
|
|
|
+ private BigDecimal sharedSubsidyHandle(OnlineOrder order ,SubsidyMode currentMode, BigDecimal sharedSubsidy) {
|
|
|
+ if(currentMode.getImmediateArrival())
|
|
|
+ {
|
|
|
+ if(sharedSubsidy.compareTo(BigDecimal.ZERO)==0)
|
|
|
+ {
|
|
|
+ return BigDecimal.ZERO;
|
|
|
+ }
|
|
|
+
|
|
|
+ while (true)
|
|
|
+ {
|
|
|
+ if(sharedSubsidy.compareTo(BigDecimal.ZERO)==0)
|
|
|
+ {
|
|
|
+ return BigDecimal.ZERO;
|
|
|
+ }
|
|
|
+
|
|
|
+ //立即到账人数
|
|
|
+ List<Long> validQueueIds = this.validQueueIds(order.getBusinessId());
|
|
|
+ int queueCount = validQueueIds.size();
|
|
|
+ if(queueCount==0)
|
|
|
+ {
|
|
|
+ return sharedSubsidy;
|
|
|
+ }
|
|
|
+ //均摊平均值
|
|
|
+ BigDecimal sharedSubsidyAverage = MathUtils.divide(sharedSubsidy,new BigDecimal(queueCount));
|
|
|
+ if(sharedSubsidyAverage.compareTo(BigDecimal.ZERO)==0)
|
|
|
+ {
|
|
|
+ //这里是为了避免 0.01/2 = 0.0
|
|
|
+ return sharedSubsidy;
|
|
|
+ }
|
|
|
+ for (Long validQueueId : validQueueIds)
|
|
|
+ {
|
|
|
+ SubsidyQueue firstQueue = this.loadById(validQueueId,false);
|
|
|
+ //应补贴金额
|
|
|
+ BigDecimal shouldSubsidyAmount = firstQueue.getShouldSubsidyAmount();
|
|
|
+ //已补贴金额
|
|
|
+ BigDecimal actualSubsidyAmount = firstQueue.getActualSubsidyAmount();
|
|
|
+ //当前排队用户的剩余补贴金额
|
|
|
+ BigDecimal currentQueueSubsidyAmount = MathUtils.subtract(shouldSubsidyAmount, actualSubsidyAmount);
|
|
|
+ //实际排队用户增加的补贴金额
|
|
|
+ BigDecimal actualAddSubsidyAmount = BigDecimal.ZERO;
|
|
|
+ if (sharedSubsidyAverage.compareTo(currentQueueSubsidyAmount) >= 0) {
|
|
|
+ //如果超出了应补贴金额=====================//
|
|
|
+ //计算剩剩余补贴金额
|
|
|
+
|
|
|
+ firstQueue.setActualSubsidyAmount(shouldSubsidyAmount);
|
|
|
+ firstQueue.setSubsidyStatus(SubsidyStatus.COMPLETE);
|
|
|
+ firstQueue.setQueueOrder(0);
|
|
|
+ firstQueue.setCompleteTime(new Date());
|
|
|
+ this.baseMapper.updateById(firstQueue);
|
|
|
+ actualAddSubsidyAmount = currentQueueSubsidyAmount;
|
|
|
+ sharedSubsidy = MathUtils.subtract(sharedSubsidy, currentQueueSubsidyAmount);
|
|
|
+ //改变剩下对列的值
|
|
|
+ validQueueOrderRearrange(order.getBusinessId(),firstQueue.getQueueId());
|
|
|
+ } else {
|
|
|
+ //如果小于应补贴金额=====================//
|
|
|
+ actualAddSubsidyAmount = sharedSubsidyAverage;
|
|
|
+ firstQueue.setActualSubsidyAmount(MathUtils.add(actualSubsidyAmount, sharedSubsidyAverage));
|
|
|
+ this.baseMapper.updateById(firstQueue);
|
|
|
+ sharedSubsidy = MathUtils.subtract(sharedSubsidy, sharedSubsidyAverage);
|
|
|
+ }
|
|
|
+
|
|
|
+ //用户余额增加
|
|
|
+ userService.changeUserBalance(UserChangeBalanceBo.builder()
|
|
|
+ .userId(firstQueue.getUserId())
|
|
|
+ .entryValue(actualAddSubsidyAmount)
|
|
|
+ .isAdd(true)
|
|
|
+ .sourceType(BalanceSourceType.SHARED_SUBSIDY)
|
|
|
+ .sourceId(firstQueue.getOrderId())
|
|
|
+ .sourceCode(firstQueue.getOrderNo())
|
|
|
+ .remark("消费排队均摊补贴")
|
|
|
+ .build());
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ //存到均摊池
|
|
|
+ subsidySharedPoolService.deposit(order, sharedSubsidy);
|
|
|
+
|
|
|
+ }
|
|
|
return BigDecimal.ZERO;
|
|
|
}
|
|
|
|
|
|
+ private List<Long> validQueueIds(Long businessId) {
|
|
|
+ return this.baseMapper.validQueueIds(businessId);
|
|
|
+ }
|
|
|
+
|
|
|
/**
|
|
|
* 创建自己的排队
|
|
|
*
|
|
|
@@ -384,9 +491,9 @@ public class SubsidyQueueServiceImpl implements ISubsidyQueueService {
|
|
|
} else {
|
|
|
//如果小于应补贴金额=====================//
|
|
|
actualAddSubsidyAmount = firstSubsidy;
|
|
|
- firstSubsidy = BigDecimal.ZERO;
|
|
|
- firstQueue.setActualSubsidyAmount(MathUtils.add(actualSubsidyAmount, firstSubsidy));
|
|
|
+ firstQueue.setActualSubsidyAmount(MathUtils.add(actualSubsidyAmount, actualAddSubsidyAmount));
|
|
|
this.baseMapper.updateById(firstQueue);
|
|
|
+ firstSubsidy = BigDecimal.ZERO;
|
|
|
}
|
|
|
|
|
|
//用户余额增加
|