...
|
...
|
@@ -20,10 +20,13 @@ import com.yohoufo.order.common.InviterType; |
|
|
import com.yohoufo.order.utils.IdUtils;
|
|
|
import com.yohoufo.order.utils.InviteSettlementUtils;
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
import org.apache.commons.collections.CollectionUtils;
|
|
|
import org.apache.commons.lang3.tuple.Pair;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
|
|
|
import java.math.BigDecimal;
|
|
|
import java.time.LocalDateTime;
|
|
|
import java.util.List;
|
|
|
import java.util.Map;
|
|
|
import java.util.Objects;
|
...
|
...
|
@@ -58,10 +61,10 @@ public class InviterSettleService { |
|
|
|
|
|
public void settle(Integer uid) {
|
|
|
inviterMapper.selectInviteCodeByUid(uid)
|
|
|
.forEach(this::settle);
|
|
|
.forEach(inviter -> this.settle(inviter, LocalDateTime.now()));
|
|
|
}
|
|
|
|
|
|
public void settle(Inviter inviter) {
|
|
|
public void settle(Inviter inviter, LocalDateTime settleTime) {
|
|
|
log.info("settle for {}", inviter);
|
|
|
int uid = inviter.getUid();
|
|
|
int now = DateUtil.getCurrentTimeSecond();
|
...
|
...
|
@@ -100,7 +103,7 @@ public class InviterSettleService { |
|
|
.withStopStrategy(StopStrategies.stopAfterAttempt(3))
|
|
|
.withWaitStrategy(WaitStrategies.fixedWait(50, TimeUnit.MILLISECONDS))
|
|
|
.build().call(() -> {
|
|
|
updateSettleAmount(uid, settleType, settlement);
|
|
|
updateSettleAmount(uid, settleTime, settleType, settlement);
|
|
|
return null;
|
|
|
});
|
|
|
} catch (ExecutionException | RetryException e) {
|
...
|
...
|
@@ -109,7 +112,7 @@ public class InviterSettleService { |
|
|
log.info("settle for {} end settle amount for settlement {} is {}.", uid, settlement.getId(), settlement.getSettleAmount());
|
|
|
}
|
|
|
|
|
|
private void updateSettleAmount(int uid, int settleType, InviteSettlement settlement) {
|
|
|
private void updateSettleAmount(int uid, LocalDateTime settleTime, int settleType, InviteSettlement settlement) {
|
|
|
int settlementId = settlement.getId();
|
|
|
log.info("settle for {} calculate settle amount for settlement {}.", uid, settlementId);
|
|
|
BigDecimal settleAmount;
|
...
|
...
|
@@ -120,14 +123,14 @@ public class InviterSettleService { |
|
|
uid, settlementId, settleAmount);
|
|
|
} else {
|
|
|
// type2方式结算金额
|
|
|
settleAmount = calculateSettleAmountOfType2(uid, settlementId);
|
|
|
settleAmount = calculateSettleAmountOfType2(uid, settleTime, settlementId);
|
|
|
log.info("settle for {} calculate settle amount for settlement {}, the amount of type2 is {}.",
|
|
|
uid, settlementId, settleAmount);
|
|
|
}
|
|
|
// 更新结算金额
|
|
|
settlement.setSettleCode(IdUtils.derangement(settlementId));
|
|
|
settlement.setSettleAmount(settleAmount);
|
|
|
settlement.setSettleTime(DateUtil.getCurrentTimeSecond());
|
|
|
settlement.setSettleTime((int) settleTime.toEpochSecond(InviteSettlementUtils.zoneOffset()));
|
|
|
settlement.setStatus(InviteSettlement.STATUS_WAIT_PAY);
|
|
|
inviteSettlementMapper.updateByUidAndId(settlement);
|
|
|
}
|
...
|
...
|
@@ -158,7 +161,7 @@ public class InviterSettleService { |
|
|
* @param settlementId
|
|
|
* @return
|
|
|
*/
|
|
|
private BigDecimal calculateSettleAmountOfType2(int uid, int settlementId) {
|
|
|
private BigDecimal calculateSettleAmountOfType2(int uid, LocalDateTime settleTime, int settlementId) {
|
|
|
int type = 2;
|
|
|
inviteSettlementItemMapper.updateToSettled(
|
|
|
uid,
|
...
|
...
|
@@ -173,10 +176,25 @@ public class InviterSettleService { |
|
|
.collect(Collectors.toMap(InviteRecord::getInviteeUid, InviteRecord::getCreateTime));
|
|
|
|
|
|
return statsList.stream()
|
|
|
.map(stats -> InviteSettlementUtils.calculateSettleAmountOfType2(
|
|
|
newLocalDateTimeOfEpochSecond(invitees.getOrDefault(stats.getSellerUid(), 0)),
|
|
|
inviteSettlementItemMapper.selectByUidTypeInviteSettlementIdAndSellerUid(uid, type, settlementId, stats.getSellerUid())
|
|
|
))
|
|
|
.map(stats -> {
|
|
|
Pair<BigDecimal, List<InviteSettlementItem>> settle = InviteSettlementUtils.calculateSettleAmountOfType2(
|
|
|
newLocalDateTimeOfEpochSecond(invitees.getOrDefault(stats.getSellerUid(), 0)),
|
|
|
settleTime,
|
|
|
inviteSettlementItemMapper.selectByUidTypeInviteSettlementIdAndSellerUid(uid, type, settlementId, stats.getSellerUid())
|
|
|
);
|
|
|
List<Integer> ids = settle.getRight().stream().map(InviteSettlementItem::getId).collect(Collectors.toList());
|
|
|
if (CollectionUtils.isNotEmpty(ids)) {
|
|
|
inviteSettlementItemMapper.updateToSettledRollback(
|
|
|
uid,
|
|
|
type,
|
|
|
InviteSettlementItem.STATUS_SETTLED,
|
|
|
InviteSettlementItem.STATUS_ENABLE,
|
|
|
0,
|
|
|
ids
|
|
|
);
|
|
|
}
|
|
|
return settle.getLeft();
|
|
|
})
|
|
|
.reduce(BigDecimal.ZERO, BigDecimal::add);
|
|
|
}
|
|
|
|
...
|
...
|
|