Authored by LUOXC

结算

... ... @@ -26,5 +26,7 @@ public interface BuyerOrderGoodsMapper {
BuyerOrderGoods selectOnlyByOrderCode(@Param("orderCode") long orderCode);
List<BuyerOrderGoods> selectByOrderCodes(@Param("orderCodes") List<Long> orderCodes);
}
\ No newline at end of file
... ...
... ... @@ -151,4 +151,14 @@
limit 1
</select>
<select id="selectByOrderCodes" resultMap="BaseResultMap">
select
<include refid="Base_Column_List" />
from buyer_order_goods
where order_code IN
<foreach collection="orderCodes" item="orderCode" open="(" separator="," close=")">
#{orderCode,jdbcType=BIGINT}
</foreach>
</select>
</mapper>
\ No newline at end of file
... ...
package com.yohoufo.order.service.transfer;
import com.google.common.collect.Lists;
import com.google.common.collect.Sets;
import com.yoho.core.redis.cluster.annotation.Redis;
import com.yoho.core.redis.cluster.operations.nosync.YHValueOperations;
import com.yoho.core.redis.cluster.operations.serializer.RedisKeyBuilder;
import com.yohoufo.common.utils.DateUtil;
import com.yohoufo.dal.order.BuyerOrderGoodsMapper;
import com.yohoufo.dal.order.TradeBillsMapper;
import com.yohoufo.dal.order.model.BuyerOrderGoods;
import com.yohoufo.dal.order.model.TradeBills;
import com.yohoufo.dal.product.ProductMapper;
import com.yohoufo.dal.product.StoragePriceMapper;
import com.yohoufo.dal.product.model.Product;
import com.yohoufo.dal.product.model.StoragePrice;
import lombok.Builder;
import lombok.Getter;
import lombok.Setter;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.math.BigDecimal;
import java.util.List;
import java.util.Optional;
import java.util.Set;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;
import static com.yohoufo.order.common.BillTradeStatus.HK_AMOUNT_PAYING;
import static com.yohoufo.order.common.BillTradeStatus.HK_AMOUNT_WAIT_PAYMENT;
... ... @@ -23,6 +38,15 @@ public class HkAccountSettlement {
@Autowired
private TradeBillsMapper tradeBillsMapper;
@Autowired
private BuyerOrderGoodsMapper buyerOrderGoodsMapper;
@Autowired
private StoragePriceMapper storagePriceMapper;
@Autowired
private ProductMapper productMapper;
@Redis("gwNoSyncRedis")
private YHValueOperations valueOperations;
... ... @@ -51,6 +75,14 @@ public class HkAccountSettlement {
if (sumIncome > 100) {
try {
log.info("{} settle, send email", uid, sumIncome);
List<TradeBillResult> tradeBillResults = getTradeBillResults(uid, tradeBills);
// 差异订单
Set<Long> tradeBillOrderCodes = tradeBills.stream().map(TradeBills::getOrderCode).collect(Collectors.toSet());
Set<Long> tradeBillResultOrderCodes = tradeBillResults.stream().map(TradeBillResult::getOrderCode).collect(Collectors.toSet());
Sets.SetView<Long> difference = Sets.difference(tradeBillOrderCodes, tradeBillResultOrderCodes);
log.info("{} settle, difference is {}", uid, difference);
// 通知财务打款
tradeBillResults.forEach(tradeBillResult -> log.info("{} settle, item {}", uid, tradeBillResult));
} catch (Exception e) {
// 通知失败
log.info("{} settle, send email fail", uid, sumIncome);
... ... @@ -63,6 +95,78 @@ public class HkAccountSettlement {
}
}
private List<TradeBillResult> getTradeBillResults(Integer uid, List<TradeBills> tradeBills) {
List<Long> orderCodes = tradeBills.stream().map(TradeBills::getOrderCode).collect(Collectors.toList());
List<BuyerOrderGoods> buyerOrderGoodsList = buyerOrderGoodsMapper.selectByOrderCodes(orderCodes);
List<Integer> skups = buyerOrderGoodsList.stream().map(BuyerOrderGoods::getSkup).collect(Collectors.toList());
List<StoragePrice> storagePrices = storagePriceMapper.selectBySkupList(skups);
List<Integer> productIds = storagePrices.stream().map(StoragePrice::getProductId).collect(Collectors.toList());
List<Product> products = productMapper.selectByIds(productIds);
List<TradeBillResult> tradeBillResults = Lists.newArrayList();
tradeBills.forEach(tradeBill -> {
getBuyerOrderGoods(tradeBill, buyerOrderGoodsList)
.ifPresent(buyerOrderGoods -> {
getStoragePrice(buyerOrderGoods, storagePrices).ifPresent(storagePrice -> {
getProduct(storagePrice, products).ifPresent(product -> {
TradeBillResult billResult = TradeBillResult.builder()
.uid(uid)
.orderCode(buyerOrderGoods.getOrderCode())
.productNo(product.getProductCode())
.productName(product.getProductName())
.rate(BigDecimal.ZERO)
.platformServiceAmount(tradeBill.getSystemAmount())
.payAmount(BigDecimal.valueOf(tradeBill.getIncomeOutcome()))
.payType(tradeBill.getTradeType() == 1 ? "保证金"
: tradeBill.getTradeType() == 2 ? "货款"
: tradeBill.getTradeType() == 3 ? "补偿款" : "")
.build();
tradeBillResults.add(billResult);
});
});
});
});
return tradeBillResults;
}
@Getter
@Setter
@Builder
private static class TradeBillResult {
private Integer uid;
// 订单编号
private Long orderCode;
// 商品货号
private String productNo;
// 商品名称
private String productName;
// 税费
private BigDecimal rate;
//平台服务费
private BigDecimal platformServiceAmount;
// 打款金额
private BigDecimal payAmount;
// 金额类型:货款
private String payType;
}
private Optional<BuyerOrderGoods> getBuyerOrderGoods(TradeBills tradeBills, List<BuyerOrderGoods> buyerOrderGoodsList) {
return buyerOrderGoodsList.stream()
.filter(e -> e.getOrderCode().equals(tradeBills.getOrderCode()))
.findAny();
}
private Optional<StoragePrice> getStoragePrice(BuyerOrderGoods buyerOrderGoods, List<StoragePrice> storagePrices) {
return storagePrices.stream()
.filter(e -> e.getSkup().equals(buyerOrderGoods.getSkup()))
.findAny();
}
private Optional<Product> getProduct(StoragePrice storagePrice, List<Product> products) {
return products.stream()
.filter(e -> e.getId().equals(storagePrice.getProductId()))
.findAny();
}
private boolean tryLock(Integer uid, int dealTime) {
return tradeBillsMapper.updateLockOfUidTradeStatusAndDealTime(
uid,
... ...