...
|
...
|
@@ -2,7 +2,6 @@ package com.yohoufo.order.service.impl; |
|
|
|
|
|
import com.google.common.annotations.VisibleForTesting;
|
|
|
import com.google.common.collect.Lists;
|
|
|
import com.yohoufo.common.utils.DateUtil;
|
|
|
import com.yohoufo.dal.order.InviteSettlementItemMapper;
|
|
|
import com.yohoufo.dal.order.InviteSettlementMapper;
|
|
|
import com.yohoufo.dal.order.InviterMapper;
|
...
|
...
|
@@ -18,6 +17,7 @@ import com.yohoufo.order.utils.InviteSettlementUtils; |
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
import lombok.val;
|
|
|
import org.apache.commons.collections.CollectionUtils;
|
|
|
import org.apache.commons.lang3.ObjectUtils;
|
|
|
import org.apache.commons.lang3.tuple.Pair;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
import org.springframework.stereotype.Service;
|
...
|
...
|
@@ -30,8 +30,13 @@ import java.util.Collections; |
|
|
import java.util.List;
|
|
|
import java.util.Map;
|
|
|
import java.util.Objects;
|
|
|
import java.util.function.Predicate;
|
|
|
import java.util.stream.Collectors;
|
|
|
|
|
|
import static com.yohoufo.common.utils.DateUtil.formatYYMMddHHmmssPoint;
|
|
|
import static com.yohoufo.dal.order.model.InviteSettlement.STATUS_WAIT_SETTLE;
|
|
|
import static com.yohoufo.order.utils.InviteSettlementUtils.RMB_FLAG;
|
|
|
import static com.yohoufo.order.utils.InviteSettlementUtils.formatAmount;
|
|
|
import static com.yohoufo.order.utils.ServiceExceptions.throwServiceException;
|
|
|
|
|
|
/**
|
...
|
...
|
@@ -72,9 +77,9 @@ public class InviteSettlementServiceImpl implements IInviteSettlementService { |
|
|
.map(inviteSettlement -> {
|
|
|
InviteSettlementListVO.InviteSettlementVO item = new InviteSettlementListVO.InviteSettlementVO();
|
|
|
item.setSettleCode(String.format("%010d", inviteSettlement.getSettleCode()));
|
|
|
item.setSettleAmount(InviteSettlementUtils.format("+%s", inviteSettlement.getSettleAmount()));
|
|
|
item.setSettleTime(DateUtil.formatYYMMddHHmmssPoint(inviteSettlement.getSettleTime()));
|
|
|
item.setPaidTime(DateUtil.formatYYMMddHHmmssPoint(inviteSettlement.getPaidTime()));
|
|
|
item.setSettleAmount(formatAmount("+%s", inviteSettlement.getSettleAmount()));
|
|
|
item.setSettleTime(formatYYMMddHHmmssPoint(inviteSettlement.getSettleTime()));
|
|
|
item.setPaidTime(formatYYMMddHHmmssPoint(inviteSettlement.getPaidTime()));
|
|
|
return item;
|
|
|
})
|
|
|
.collect(Collectors.toList());
|
...
|
...
|
@@ -84,47 +89,80 @@ public class InviteSettlementServiceImpl implements IInviteSettlementService { |
|
|
}
|
|
|
|
|
|
@Override
|
|
|
public InviteSettlementItemListVO getInviteSettlementItemList(Integer uid, String month, Integer page, Integer limit) {
|
|
|
public InviteSettlementItemListVO getInviteSettlementItemList(Integer uid, Integer tab, String month, Integer page, Integer limit) {
|
|
|
if (!ObjectUtils.allNotNull(uid, tab, month, page, limit) || uid <= 0 || page <= 0 || limit <= 0) {
|
|
|
throwServiceException("参数不正确");
|
|
|
}
|
|
|
Pair<Integer, Integer> time = getTimeRangeOfMonth(month);
|
|
|
List<Integer> statusList = getStatusList(tab);
|
|
|
// 只查询结算规则1的记录
|
|
|
int settlementType = 1;
|
|
|
InviteSettlementItemStats stats = inviteSettlementItemMapper.selectStats(uid, settlementType, time.getLeft(), time.getRight());
|
|
|
|
|
|
InviteSettlementItemStats stats = inviteSettlementItemMapper.selectStats(uid, statusList, settlementType, time.getLeft(), time.getRight());
|
|
|
int totalElements = Objects.isNull(stats.getTotalElements()) ? 0 : stats.getTotalElements();
|
|
|
BigDecimal totalOrderAmount = Objects.isNull(stats.getTotalOrderAmount()) ? BigDecimal.ZERO : stats.getTotalOrderAmount();
|
|
|
BigDecimal totalSettleAmount = Objects.isNull(stats.getTotalSettleAmount()) ? BigDecimal.ZERO : stats.getTotalSettleAmount();
|
|
|
InviteSettlementItemStats statsAll;
|
|
|
if (tab != 0) {
|
|
|
statsAll = inviteSettlementItemMapper.selectStats(uid, getStatusList(0), settlementType, time.getLeft(), time.getRight());
|
|
|
} else {
|
|
|
statsAll = stats;
|
|
|
}
|
|
|
BigDecimal totalSettleAmount = Objects.isNull(statsAll.getTotalSettleAmount()) ? BigDecimal.ZERO : statsAll.getTotalSettleAmount();
|
|
|
BigDecimal totalPaidAmount = Objects.isNull(statsAll.getTotalPaidAmount()) ? BigDecimal.ZERO : statsAll.getTotalPaidAmount();
|
|
|
BigDecimal totalWaitPayAmount = Objects.isNull(statsAll.getTotalWaitPayAmount()) ? BigDecimal.ZERO : statsAll.getTotalWaitPayAmount();
|
|
|
InviteSettlementItemListVO.InviteSettlementItemListVOBuilder builder = InviteSettlementItemListVO.builder();
|
|
|
builder.page(page)
|
|
|
.pageSize(limit)
|
|
|
.pageTotal((totalElements % limit == 0) ? (totalElements / limit) : (totalElements / limit + 1))
|
|
|
.totalElements(totalElements)
|
|
|
.totalOrderAmount(InviteSettlementUtils.format(totalOrderAmount))
|
|
|
.totalSettleAmount(InviteSettlementUtils.format(totalSettleAmount));
|
|
|
.totalSettleAmount(formatAmount("%s%s", RMB_FLAG, totalSettleAmount))
|
|
|
.totalPaidAmount(formatAmount("%s%s", RMB_FLAG, totalPaidAmount))
|
|
|
.totalWaitPayAmount(formatAmount("%s%s", RMB_FLAG, totalWaitPayAmount));
|
|
|
if (totalElements == 0) {
|
|
|
return builder.list(Lists.newArrayList()).build();
|
|
|
return builder
|
|
|
.list(Lists.newArrayList())
|
|
|
.build();
|
|
|
}
|
|
|
int start = (page - 1) * limit;
|
|
|
List<InviteSettlementItem> list = inviteSettlementItemMapper.select(uid, settlementType, time.getLeft(), time.getRight(), start, limit);
|
|
|
List<InviteSettlementItem> list = inviteSettlementItemMapper.select(uid, statusList, settlementType, time.getLeft(), time.getRight(), start, limit);
|
|
|
Map<Integer, Integer> settlementStatusMap = buildInviteSettlementIdStatusMap(uid, list);
|
|
|
Map<Integer, String> sellerUidNickNameMap = userProxyService.getNickNameByUids(list.stream()
|
|
|
.map(InviteSettlementItem::getSellerUid)
|
|
|
.filter(Objects::nonNull)
|
|
|
.distinct()
|
|
|
.collect(Collectors.toList()));
|
|
|
|
|
|
Predicate<InviteSettlementItem> itemHasPaid = item -> {
|
|
|
int status = settlementStatusMap.getOrDefault(item.getInviteSettlementId(), STATUS_WAIT_SETTLE).intValue();
|
|
|
return InviteSettlement.STATUS_PAID == status;
|
|
|
};
|
|
|
|
|
|
return builder
|
|
|
.list(list.stream().map(item -> {
|
|
|
Integer settlementStatus = settlementStatusMap.getOrDefault(item.getInviteSettlementId(), InviteSettlement.STATUS_WAIT_SETTLE);
|
|
|
InviteSettlementItemListVO.InviteSettlementItemVO vo = new InviteSettlementItemListVO.InviteSettlementItemVO();
|
|
|
vo.setBuyerOrderCode(item.getBuyerOrderCode().toString());
|
|
|
vo.setSellerName(sellerUidNickNameMap.get(item.getSellerUid()));
|
|
|
vo.setOrderAmount(InviteSettlementUtils.format(item.getOrderAmount()));
|
|
|
vo.setOrderCreateTime(DateUtil.formatYYMMddHHmmssPoint(item.getOrderCreateTime()));
|
|
|
vo.setSettleAmount(InviteSettlementUtils.format(item.getSettleAmount()));
|
|
|
vo.setStatusDesc(settlementStatus.equals(InviteSettlement.STATUS_PAID) ? "已付款" : "待付款");
|
|
|
return vo;
|
|
|
}).collect(Collectors.toList()))
|
|
|
.list(list.stream()
|
|
|
.map(item -> new InviteSettlementItemListVO.InviteSettlementItemVO()
|
|
|
.setBuyerOrderCode(item.getBuyerOrderCode().toString())
|
|
|
.setSellerName(sellerUidNickNameMap.get(item.getSellerUid()))
|
|
|
.setOrderAmount(formatAmount("%s%s", RMB_FLAG, item.getOrderAmount()))
|
|
|
.setOrderCreateTime(formatYYMMddHHmmssPoint(item.getOrderCreateTime()))
|
|
|
.setSettleAmount(formatAmount("%s%s", RMB_FLAG, item.getSettleAmount()))
|
|
|
.setStatusDesc(itemHasPaid.test(item) ? "已付款" : "待付款"))
|
|
|
.collect(Collectors.toList()))
|
|
|
.build();
|
|
|
}
|
|
|
|
|
|
private List<Integer> getStatusList(Integer tab) {
|
|
|
if (tab == 0) {
|
|
|
return Lists.newArrayList(InviteSettlementItem.STATUS_ENABLE, InviteSettlementItem.STATUS_SETTLED, InviteSettlementItem.STATUS_PAID);
|
|
|
}
|
|
|
if (tab == 1) {
|
|
|
return Lists.newArrayList(InviteSettlementItem.STATUS_ENABLE, InviteSettlementItem.STATUS_SETTLED);
|
|
|
}
|
|
|
if (tab == 2) {
|
|
|
return Lists.newArrayList(InviteSettlementItem.STATUS_PAID);
|
|
|
}
|
|
|
throwServiceException("参数不正确");
|
|
|
return Lists.newArrayList();
|
|
|
}
|
|
|
|
|
|
private Map<Integer, Integer> buildInviteSettlementIdStatusMap(Integer uid, List<InviteSettlementItem> list) {
|
|
|
val inviteSettlementIds = list.stream()
|
|
|
.map(InviteSettlementItem::getInviteSettlementId)
|
...
|
...
|
@@ -141,18 +179,14 @@ public class InviteSettlementServiceImpl implements IInviteSettlementService { |
|
|
|
|
|
@VisibleForTesting
|
|
|
Pair<Integer, Integer> getTimeRangeOfMonth(String month) {
|
|
|
LocalDateTime requestMonth = null;
|
|
|
LocalDateTime start = null;
|
|
|
ZoneOffset zoneOffset = InviteSettlementUtils.zoneOffset();
|
|
|
try {
|
|
|
requestMonth = LocalDateTime.parse(month, DateTimeFormatter.ofPattern("yyyyMM"));
|
|
|
start = LocalDateTime.parse(month + "01 00:00:00", DateTimeFormatter.ofPattern("yyyyMMdd HH:mm:ss"));
|
|
|
} catch (Exception e) {
|
|
|
throwServiceException("日期格式不正确");
|
|
|
}
|
|
|
ZoneOffset zoneOffset = ZoneOffset.of("+8");
|
|
|
int startTime = (int) LocalDateTime.of(requestMonth.getYear(), requestMonth.getMonthValue(), 1, 0, 0, 0)
|
|
|
.toEpochSecond(zoneOffset);
|
|
|
int endTime = (int) LocalDateTime.of(requestMonth.getYear(), requestMonth.getMonthValue() + 1, 1, 0, 0, 0)
|
|
|
.toEpochSecond(zoneOffset) - 1;
|
|
|
return Pair.of(startTime, endTime);
|
|
|
return Pair.of((int) start.toEpochSecond(zoneOffset), (int) start.plusMonths(1).toEpochSecond(zoneOffset) - 1);
|
|
|
}
|
|
|
|
|
|
@Override
|
...
|
...
|
@@ -164,8 +198,18 @@ public class InviteSettlementServiceImpl implements IInviteSettlementService { |
|
|
}
|
|
|
|
|
|
@Override
|
|
|
public void settle(int uid) {
|
|
|
inviterSettleService.settle(uid);
|
|
|
public void paid(List<Integer> ids) {
|
|
|
if (CollectionUtils.isEmpty(ids)) {
|
|
|
return;
|
|
|
}
|
|
|
List<InviteSettlement> settlements = inviteSettlementMapper.selectByIds(ids);
|
|
|
settlements.stream().forEach(settlement -> {
|
|
|
log.info("mark settlement to paid, settle id is {}", settlement.getId());
|
|
|
settlement.setStatus(InviteSettlement.STATUS_PAID);
|
|
|
inviteSettlementMapper.updateByUidAndId(settlement);
|
|
|
inviteSettlementItemMapper.updateStatusByUidAndInviteSettlementId(settlement.getUid(), settlement.getId(), InviteSettlementItem.STATUS_PAID);
|
|
|
});
|
|
|
|
|
|
}
|
|
|
|
|
|
@Override
|
...
|
...
|
|