|
|
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;
|
|
|
import com.yohoufo.dal.order.model.InviteSettlement;
|
|
|
import com.yohoufo.dal.order.model.InviteSettlementItem;
|
|
|
import com.yohoufo.dal.order.model.InviteSettlementItemStats;
|
|
|
import com.yohoufo.dal.order.model.Inviter;
|
|
|
import com.yohoufo.order.model.InviteSettlementItemListVO;
|
|
|
import com.yohoufo.order.model.InviteSettlementListVO;
|
|
|
import com.yohoufo.order.service.IInviteSettlementService;
|
|
|
import com.yohoufo.order.utils.IdUtils;
|
|
|
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.tuple.Pair;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
|
|
|
import java.math.RoundingMode;
|
|
|
import java.math.BigDecimal;
|
|
|
import java.time.LocalDateTime;
|
|
|
import java.time.ZoneOffset;
|
|
|
import java.time.format.DateTimeFormatter;
|
|
|
import java.util.Collections;
|
|
|
import java.util.List;
|
|
|
import java.util.Map;
|
|
|
import java.util.Objects;
|
|
|
import java.util.stream.Collectors;
|
|
|
|
|
|
import static com.yohoufo.order.utils.ServiceExceptions.throwServiceException;
|
|
|
|
|
|
/**
|
|
|
* @author LUOXC
|
|
|
* @date 2019/4/2 15:27
|
...
|
...
|
@@ -37,6 +51,9 @@ public class InviteSettlementServiceImpl implements IInviteSettlementService { |
|
|
private InviteSettlementMapper inviteSettlementMapper;
|
|
|
|
|
|
@Autowired
|
|
|
private InviteSettlementItemMapper inviteSettlementItemMapper;
|
|
|
|
|
|
@Autowired
|
|
|
private InviteSettlementItemCreator inviteSettlementItemCreator;
|
|
|
|
|
|
@Override
|
...
|
...
|
@@ -46,12 +63,12 @@ public class InviteSettlementServiceImpl implements IInviteSettlementService { |
|
|
List<InviteSettlement> inviteSettlementList = inviteSettlementMapper.selectByUidAndStatusWithLimit(uid, InviteSettlement.STATUS_PAID, 0, 100);
|
|
|
|
|
|
// build vo
|
|
|
ZoneOffset zoneOffset = ZoneOffset.of("+8");
|
|
|
|
|
|
List<InviteSettlementListVO.InviteSettlementVO> items = inviteSettlementList.stream()
|
|
|
.map(inviteSettlement -> {
|
|
|
InviteSettlementListVO.InviteSettlementVO item = new InviteSettlementListVO.InviteSettlementVO();
|
|
|
item.setSettleCode(String.format("%010d", inviteSettlement.getSettleCode()));
|
|
|
item.setSettleAmount("+" + inviteSettlement.getSettleAmount().setScale(1, RoundingMode.FLOOR));
|
|
|
item.setSettleAmount(InviteSettlementUtils.format("+%s", inviteSettlement.getSettleAmount()));
|
|
|
item.setSettleTime(DateUtil.formatYYMMddHHmmssPoint(inviteSettlement.getSettleTime()));
|
|
|
item.setPaidTime(DateUtil.formatYYMMddHHmmssPoint(inviteSettlement.getPaidTime()));
|
|
|
return item;
|
...
|
...
|
@@ -63,6 +80,73 @@ public class InviteSettlementServiceImpl implements IInviteSettlementService { |
|
|
}
|
|
|
|
|
|
@Override
|
|
|
public InviteSettlementItemListVO getInviteSettlementItemList(Integer uid, String month, Integer page, Integer limit) {
|
|
|
Pair<Integer, Integer> time = getTimeRangeOfMonth(month);
|
|
|
// 只查询结算规则1的记录
|
|
|
int settlementType = 1;
|
|
|
InviteSettlementItemStats stats = inviteSettlementItemMapper.selectStats(uid, 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();
|
|
|
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));
|
|
|
if (totalElements == 0) {
|
|
|
return builder.list(Lists.newArrayList()).build();
|
|
|
}
|
|
|
int start = (page - 1) * limit;
|
|
|
List<InviteSettlementItem> list = inviteSettlementItemMapper.select(uid, settlementType, time.getLeft(), time.getRight(), start, limit);
|
|
|
Map<Integer, Integer> settlementStatusMap = buildInviteSettlementIdStatusMap(uid, list);
|
|
|
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(item.getSellerUid().toString());
|
|
|
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()))
|
|
|
.build();
|
|
|
}
|
|
|
|
|
|
private Map<Integer, Integer> buildInviteSettlementIdStatusMap(Integer uid, List<InviteSettlementItem> list) {
|
|
|
val inviteSettlementIds = list.stream()
|
|
|
.map(InviteSettlementItem::getInviteSettlementId)
|
|
|
.filter(Objects::nonNull)
|
|
|
.distinct()
|
|
|
.collect(Collectors.toList());
|
|
|
if (CollectionUtils.isEmpty(inviteSettlementIds)) {
|
|
|
return Collections.emptyMap();
|
|
|
} else {
|
|
|
return inviteSettlementMapper.selectByUidAndIds(uid, inviteSettlementIds).stream()
|
|
|
.collect(Collectors.toMap(InviteSettlement::getId, InviteSettlement::getStatus));
|
|
|
}
|
|
|
}
|
|
|
|
|
|
@VisibleForTesting
|
|
|
Pair<Integer, Integer> getTimeRangeOfMonth(String month) {
|
|
|
LocalDateTime requestMonth = null;
|
|
|
try {
|
|
|
requestMonth = LocalDateTime.parse(month, DateTimeFormatter.ofPattern("yyyyMM"));
|
|
|
} 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);
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
public void settle() {
|
|
|
inviterMapper.selectAll().stream()
|
|
|
.map(Inviter::getUid)
|
...
|
...
|
|