Authored by LUOXC

结算

@@ -26,5 +26,7 @@ public interface BuyerOrderGoodsMapper { @@ -26,5 +26,7 @@ public interface BuyerOrderGoodsMapper {
26 26
27 BuyerOrderGoods selectOnlyByOrderCode(@Param("orderCode") long orderCode); 27 BuyerOrderGoods selectOnlyByOrderCode(@Param("orderCode") long orderCode);
28 28
  29 + List<BuyerOrderGoods> selectByOrderCodes(@Param("orderCodes") List<Long> orderCodes);
  30 +
29 31
30 } 32 }
@@ -151,4 +151,14 @@ @@ -151,4 +151,14 @@
151 limit 1 151 limit 1
152 </select> 152 </select>
153 153
  154 + <select id="selectByOrderCodes" resultMap="BaseResultMap">
  155 + select
  156 + <include refid="Base_Column_List" />
  157 + from buyer_order_goods
  158 + where order_code IN
  159 + <foreach collection="orderCodes" item="orderCode" open="(" separator="," close=")">
  160 + #{orderCode,jdbcType=BIGINT}
  161 + </foreach>
  162 + </select>
  163 +
154 </mapper> 164 </mapper>
1 package com.yohoufo.order.service.transfer; 1 package com.yohoufo.order.service.transfer;
2 2
  3 +import com.google.common.collect.Lists;
  4 +import com.google.common.collect.Sets;
3 import com.yoho.core.redis.cluster.annotation.Redis; 5 import com.yoho.core.redis.cluster.annotation.Redis;
4 import com.yoho.core.redis.cluster.operations.nosync.YHValueOperations; 6 import com.yoho.core.redis.cluster.operations.nosync.YHValueOperations;
5 import com.yoho.core.redis.cluster.operations.serializer.RedisKeyBuilder; 7 import com.yoho.core.redis.cluster.operations.serializer.RedisKeyBuilder;
6 import com.yohoufo.common.utils.DateUtil; 8 import com.yohoufo.common.utils.DateUtil;
  9 +import com.yohoufo.dal.order.BuyerOrderGoodsMapper;
7 import com.yohoufo.dal.order.TradeBillsMapper; 10 import com.yohoufo.dal.order.TradeBillsMapper;
  11 +import com.yohoufo.dal.order.model.BuyerOrderGoods;
8 import com.yohoufo.dal.order.model.TradeBills; 12 import com.yohoufo.dal.order.model.TradeBills;
  13 +import com.yohoufo.dal.product.ProductMapper;
  14 +import com.yohoufo.dal.product.StoragePriceMapper;
  15 +import com.yohoufo.dal.product.model.Product;
  16 +import com.yohoufo.dal.product.model.StoragePrice;
  17 +import lombok.Builder;
  18 +import lombok.Getter;
  19 +import lombok.Setter;
9 import lombok.extern.slf4j.Slf4j; 20 import lombok.extern.slf4j.Slf4j;
10 import org.springframework.beans.factory.annotation.Autowired; 21 import org.springframework.beans.factory.annotation.Autowired;
11 import org.springframework.stereotype.Service; 22 import org.springframework.stereotype.Service;
12 23
  24 +import java.math.BigDecimal;
13 import java.util.List; 25 import java.util.List;
  26 +import java.util.Optional;
  27 +import java.util.Set;
14 import java.util.concurrent.TimeUnit; 28 import java.util.concurrent.TimeUnit;
  29 +import java.util.stream.Collectors;
15 30
16 import static com.yohoufo.order.common.BillTradeStatus.HK_AMOUNT_PAYING; 31 import static com.yohoufo.order.common.BillTradeStatus.HK_AMOUNT_PAYING;
17 import static com.yohoufo.order.common.BillTradeStatus.HK_AMOUNT_WAIT_PAYMENT; 32 import static com.yohoufo.order.common.BillTradeStatus.HK_AMOUNT_WAIT_PAYMENT;
@@ -23,6 +38,15 @@ public class HkAccountSettlement { @@ -23,6 +38,15 @@ public class HkAccountSettlement {
23 @Autowired 38 @Autowired
24 private TradeBillsMapper tradeBillsMapper; 39 private TradeBillsMapper tradeBillsMapper;
25 40
  41 + @Autowired
  42 + private BuyerOrderGoodsMapper buyerOrderGoodsMapper;
  43 +
  44 + @Autowired
  45 + private StoragePriceMapper storagePriceMapper;
  46 +
  47 + @Autowired
  48 + private ProductMapper productMapper;
  49 +
26 @Redis("gwNoSyncRedis") 50 @Redis("gwNoSyncRedis")
27 private YHValueOperations valueOperations; 51 private YHValueOperations valueOperations;
28 52
@@ -51,6 +75,14 @@ public class HkAccountSettlement { @@ -51,6 +75,14 @@ public class HkAccountSettlement {
51 if (sumIncome > 100) { 75 if (sumIncome > 100) {
52 try { 76 try {
53 log.info("{} settle, send email", uid, sumIncome); 77 log.info("{} settle, send email", uid, sumIncome);
  78 + List<TradeBillResult> tradeBillResults = getTradeBillResults(uid, tradeBills);
  79 + // 差异订单
  80 + Set<Long> tradeBillOrderCodes = tradeBills.stream().map(TradeBills::getOrderCode).collect(Collectors.toSet());
  81 + Set<Long> tradeBillResultOrderCodes = tradeBillResults.stream().map(TradeBillResult::getOrderCode).collect(Collectors.toSet());
  82 + Sets.SetView<Long> difference = Sets.difference(tradeBillOrderCodes, tradeBillResultOrderCodes);
  83 + log.info("{} settle, difference is {}", uid, difference);
  84 + // 通知财务打款
  85 + tradeBillResults.forEach(tradeBillResult -> log.info("{} settle, item {}", uid, tradeBillResult));
54 } catch (Exception e) { 86 } catch (Exception e) {
55 // 通知失败 87 // 通知失败
56 log.info("{} settle, send email fail", uid, sumIncome); 88 log.info("{} settle, send email fail", uid, sumIncome);
@@ -63,6 +95,78 @@ public class HkAccountSettlement { @@ -63,6 +95,78 @@ public class HkAccountSettlement {
63 } 95 }
64 } 96 }
65 97
  98 + private List<TradeBillResult> getTradeBillResults(Integer uid, List<TradeBills> tradeBills) {
  99 + List<Long> orderCodes = tradeBills.stream().map(TradeBills::getOrderCode).collect(Collectors.toList());
  100 + List<BuyerOrderGoods> buyerOrderGoodsList = buyerOrderGoodsMapper.selectByOrderCodes(orderCodes);
  101 + List<Integer> skups = buyerOrderGoodsList.stream().map(BuyerOrderGoods::getSkup).collect(Collectors.toList());
  102 + List<StoragePrice> storagePrices = storagePriceMapper.selectBySkupList(skups);
  103 + List<Integer> productIds = storagePrices.stream().map(StoragePrice::getProductId).collect(Collectors.toList());
  104 + List<Product> products = productMapper.selectByIds(productIds);
  105 + List<TradeBillResult> tradeBillResults = Lists.newArrayList();
  106 + tradeBills.forEach(tradeBill -> {
  107 + getBuyerOrderGoods(tradeBill, buyerOrderGoodsList)
  108 + .ifPresent(buyerOrderGoods -> {
  109 + getStoragePrice(buyerOrderGoods, storagePrices).ifPresent(storagePrice -> {
  110 + getProduct(storagePrice, products).ifPresent(product -> {
  111 + TradeBillResult billResult = TradeBillResult.builder()
  112 + .uid(uid)
  113 + .orderCode(buyerOrderGoods.getOrderCode())
  114 + .productNo(product.getProductCode())
  115 + .productName(product.getProductName())
  116 + .rate(BigDecimal.ZERO)
  117 + .platformServiceAmount(tradeBill.getSystemAmount())
  118 + .payAmount(BigDecimal.valueOf(tradeBill.getIncomeOutcome()))
  119 + .payType(tradeBill.getTradeType() == 1 ? "保证金"
  120 + : tradeBill.getTradeType() == 2 ? "货款"
  121 + : tradeBill.getTradeType() == 3 ? "补偿款" : "")
  122 + .build();
  123 + tradeBillResults.add(billResult);
  124 + });
  125 + });
  126 + });
  127 + });
  128 + return tradeBillResults;
  129 + }
  130 +
  131 + @Getter
  132 + @Setter
  133 + @Builder
  134 + private static class TradeBillResult {
  135 + private Integer uid;
  136 + // 订单编号
  137 + private Long orderCode;
  138 + // 商品货号
  139 + private String productNo;
  140 + // 商品名称
  141 + private String productName;
  142 + // 税费
  143 + private BigDecimal rate;
  144 + //平台服务费
  145 + private BigDecimal platformServiceAmount;
  146 + // 打款金额
  147 + private BigDecimal payAmount;
  148 + // 金额类型:货款
  149 + private String payType;
  150 + }
  151 +
  152 + private Optional<BuyerOrderGoods> getBuyerOrderGoods(TradeBills tradeBills, List<BuyerOrderGoods> buyerOrderGoodsList) {
  153 + return buyerOrderGoodsList.stream()
  154 + .filter(e -> e.getOrderCode().equals(tradeBills.getOrderCode()))
  155 + .findAny();
  156 + }
  157 +
  158 + private Optional<StoragePrice> getStoragePrice(BuyerOrderGoods buyerOrderGoods, List<StoragePrice> storagePrices) {
  159 + return storagePrices.stream()
  160 + .filter(e -> e.getSkup().equals(buyerOrderGoods.getSkup()))
  161 + .findAny();
  162 + }
  163 +
  164 + private Optional<Product> getProduct(StoragePrice storagePrice, List<Product> products) {
  165 + return products.stream()
  166 + .filter(e -> e.getId().equals(storagePrice.getProductId()))
  167 + .findAny();
  168 + }
  169 +
66 private boolean tryLock(Integer uid, int dealTime) { 170 private boolean tryLock(Integer uid, int dealTime) {
67 return tradeBillsMapper.updateLockOfUidTradeStatusAndDealTime( 171 return tradeBillsMapper.updateLockOfUidTradeStatusAndDealTime(
68 uid, 172 uid,