2 Commit-ok b910e191db ... 6f7922478e

Szerző SHA1 Üzenet Dátum
  wujiefeng 6f7922478e 添加返利日志和购物返利 8 hónapja
  wujiefeng 2f5140fd6d 购物返利 8 hónapja

+ 16 - 2
RewardServer/pom.xml

@@ -32,32 +32,35 @@
 			<version>${project.version}</version>
 		</dependency>
 
-
+		<!--小程序用户端-->
 		<dependency>
 			<groupId>com.zswl.cloud.bdb</groupId>
 			<artifactId>zswl-cloud-bdb-client</artifactId>
 			<version>${project.version}</version>
 		</dependency>
 
+		<!--小程序商户端-->
 		<dependency>
 			<groupId>com.zswl.cloud.shop</groupId>
 			<artifactId>zswl-cloud-shop-client</artifactId>
 			<version>${project.version}</version>
 		</dependency>
 
+		<!--会员中心-->
 		<dependency>
 			<groupId>com.zhongshu</groupId>
 			<artifactId>VipClient</artifactId>
 			<version>${project.version}</version>
 		</dependency>
 
-
+		<!--mongoData-->
 		<dependency>
 			<groupId>com.github.microservice.components</groupId>
 			<artifactId>MongodbData</artifactId>
 			<version>${project.version}</version>
 		</dependency>
 
+		<!--redis-->
 		<dependency>
 			<groupId>com.github.microservice.components</groupId>
 			<artifactId>RedisData</artifactId>
@@ -76,6 +79,7 @@
             <version>3.14.0</version>
             <scope>compile</scope>
         </dependency>
+
         <dependency>
             <groupId>com.zhongshu</groupId>
             <artifactId>RewardClient</artifactId>
@@ -98,12 +102,14 @@
             <version>2.0.38</version>
         </dependency>
 
+		<!--推送中心-->
 		<dependency>
 			<groupId>com.github.microservice.components</groupId>
 			<artifactId>PushCenterClient</artifactId>
 			<version>${project.version}</version>
 		</dependency>
 
+		<!--jedis-->
 		<dependency>
 			<groupId>redis.clients</groupId>
 			<artifactId>jedis</artifactId>
@@ -116,12 +122,20 @@
 			<version>${project.version}</version>
 		</dependency>
 
+		<!--产品中心-->
 		<dependency>
 			<groupId>com.github.microservice.encryption</groupId>
 			<artifactId>ProductCenterClient</artifactId>
 			<version>${project.version}</version>
 		</dependency>
 
+		<!--支付中心-->
+		<dependency>
+			<groupId>com.zhongshu</groupId>
+			<artifactId>PaymentClient</artifactId>
+			<version>${project.version}</version>
+		</dependency>
+
 <!--		<dependency>-->
 <!--			<groupId>org.redisson</groupId>-->
 <!--			<artifactId>redisson-spring-boot-starter</artifactId>-->

+ 14 - 0
RewardServer/src/main/java/com/zhongshu/reward/server/core/config/PaymentConfig.java

@@ -0,0 +1,14 @@
+package com.zhongshu.reward.server.core.config;
+
+import com.zhongshu.payment.client.config.PaymentClientConfiguration;
+import org.springframework.context.annotation.Configuration;
+import org.springframework.context.annotation.Import;
+
+/**
+ * @author wjf
+ * @date 2024/9/14
+ */
+@Configuration
+@Import(PaymentClientConfiguration.class)
+public class PaymentConfig {
+}

+ 10 - 0
RewardServer/src/main/java/com/zhongshu/reward/server/core/dao/Impl/WalletReceiptsDaoImpl.java

@@ -129,4 +129,14 @@ public class WalletReceiptsDaoImpl implements WalletReceiptsDaoExtend {
         Query query = Query.query(criteria);
         return mongoTemplate.find(query, WalletReceipts.class);
     }
+
+    @Override
+    public List<WalletReceipts> findByWaitSettle(ReceiptsStatus status, ReceiptsType receiptsType, Long currentTime) {
+        Criteria criteria = new Criteria();
+        criteria.and("status").in(status);
+        criteria.and("receiptsType").is(receiptsType);
+        criteria.and("estimatedTime").lte(currentTime);
+        Query query = Query.query(criteria);
+        return mongoTemplate.find(query, WalletReceipts.class);
+    }
 }

+ 1 - 1
RewardServer/src/main/java/com/zhongshu/reward/server/core/dao/WalletReceiptsDao.java

@@ -23,6 +23,6 @@ public interface WalletReceiptsDao extends MongoDao<WalletReceipts>, WalletRecei
 
     WalletReceipts findTop1ByVipUserRecord_Id(String vipUserRecordId);
 
-    List<WalletReceipts> findByOutTradeNoAndGoodsIdInAndStatus(String outTradeNo, List<String> goodsIds, ReceiptsStatus status);
+    WalletReceipts findByOutTradeNoAndPaymentGoodsIdAndStatus(String outTradeNo, String paymentGoodsId, ReceiptsStatus status);
 
 }

+ 4 - 0
RewardServer/src/main/java/com/zhongshu/reward/server/core/dao/extend/WalletReceiptsDaoExtend.java

@@ -1,6 +1,8 @@
 package com.zhongshu.reward.server.core.dao.extend;
 
 import com.zhongshu.reward.client.model.InviteSuccessModel;
+import com.zhongshu.reward.client.type.ReceiptsStatus;
+import com.zhongshu.reward.client.type.ReceiptsType;
 import com.zhongshu.reward.server.core.dao.WalletReceiptsDao;
 import com.zhongshu.reward.server.core.domain.WalletReceipts;
 
@@ -23,4 +25,6 @@ public interface WalletReceiptsDaoExtend {
     WalletReceipts findNoCancelByCreateTime(String userId, Long startTime, Long endTime);
 
     List<WalletReceipts> findByEstimatedTime(Long currentTime);
+
+    List<WalletReceipts> findByWaitSettle(ReceiptsStatus status, ReceiptsType receiptsType, Long currentTime);
 }

+ 7 - 1
RewardServer/src/main/java/com/zhongshu/reward/server/core/domain/WalletReceipts.java

@@ -80,11 +80,17 @@ public class WalletReceipts extends SuperEntity {
     private String outTradeNo;
 
     /**
-     * 外部订单号(商品返利:商品id)
+     * 外部订单号(商品返利:商品id:对应产品库id)
      */
     @Indexed
     private String goodsId;
 
+    /**
+     * 对应支付中心业务id
+     */
+    @Indexed
+    private String paymentGoodsId;
+
     /**
      * 产生入账的订阅消息
      */

+ 49 - 29
RewardServer/src/main/java/com/zhongshu/reward/server/core/service/GoodsReceiptsService.java

@@ -1,9 +1,11 @@
 package com.zhongshu.reward.server.core.service;
 
+import com.github.microservice.auth.client.content.ResultState;
 import com.github.microservice.productcenter.client.model.ProductRuleModel;
 import com.github.microservice.productcenter.client.ret.ResultContent;
-import com.github.microservice.productcenter.client.ret.ResultState;
 import com.github.microservice.productcenter.client.service.ProductRuleService;
+import com.zhongshu.payment.client.model.order.v2.GoodsResultModel;
+import com.zhongshu.payment.client.service.v2.OrderService2;
 import com.zhongshu.reward.client.type.ReceiptsStatus;
 import com.zhongshu.reward.client.type.ReceiptsType;
 import com.zhongshu.reward.server.core.dao.WalletDao;
@@ -11,6 +13,8 @@ import com.zhongshu.reward.server.core.dao.WalletReceiptsDao;
 import com.zhongshu.reward.server.core.domain.Wallet;
 import com.zhongshu.reward.server.core.domain.WalletReceipts;
 import lombok.extern.slf4j.Slf4j;
+import org.apache.commons.lang3.ObjectUtils;
+import org.apache.commons.lang3.StringUtils;
 import org.jetbrains.annotations.NotNull;
 import org.jetbrains.annotations.Nullable;
 import org.springframework.beans.factory.annotation.Autowired;
@@ -20,7 +24,6 @@ import org.springframework.transaction.annotation.Transactional;
 import java.math.BigDecimal;
 import java.util.Date;
 import java.util.List;
-import java.util.stream.Collectors;
 
 /**
  * @author wjf
@@ -42,32 +45,47 @@ public class GoodsReceiptsService {
     @Autowired
     ProductRuleService productRuleService;
 
+    @Autowired
+    OrderService2 orderService2;
+
+
     /**
      * 购物返利 todo// 1、获取产品库 返利规则 2、获取订单详情、获取商品详情
-     * @param userId 用户id
-     * @param goodsTotal 商品金额
-     * @param orderNo 订单号
-     * @param goodsId 商品id
      */
     @Transactional
-    public void receipts(String userId, BigDecimal goodsTotal, String orderNo, String goodsId){
+    public void receipts(String userId, String orderNo, String payGoodsId, Long operationTime){
 
-        ProductRuleModel productRule = getRule(goodsId);
+        com.github.microservice.auth.client.content.ResultContent<GoodsResultModel> goodsResultModelResultContent = orderService2.qrGoods(payGoodsId);
+        if (!ResultState.Success.equals(goodsResultModelResultContent.getState())){
+            log.info("查询商品信息失败:id:{}", payGoodsId);
+            return;
+        }
+        GoodsResultModel goodsResultModel = goodsResultModelResultContent.getContent();
+        if (StringUtils.isEmpty(goodsResultModel.getGoodsId())){
+            log.info("商品无对应产品库id:{}", payGoodsId);
+            return;
+        }
+        ProductRuleModel productRule = getRule(goodsResultModel.getGoodsId());
         if (null == productRule){
             return;
         }
 
-        BigDecimal total = getReceiptsTotal(goodsTotal, productRule);
+        BigDecimal total = getReceiptsTotal(goodsResultModel.getPayAmount(), productRule);
+        if (BigDecimal.ZERO.compareTo(total) == 0){
+            log.info("商品返利金额为0, id:{}, productId:{}", payGoodsId, goodsResultModel.getGoodsId());
+            return;
+        }
 
         Wallet wallet = walletService.getWalletByUserId(userId);
 
         WalletReceipts walletReceipts = new WalletReceipts();
         walletReceipts.setWallet(wallet);
         walletReceipts.setReceiptsType(ReceiptsType.REBATE);
-        walletReceipts.setEstimatedTime(new Date().getTime() + 2*24*60*60*1000);
+        walletReceipts.setEstimatedTime(operationTime + 2*24*60*60*1000);
         walletReceipts.setTotal(total);
         walletReceipts.setOutTradeNo(orderNo);
-        walletReceipts.setGoodsId(goodsId);
+        walletReceipts.setGoodsId(goodsResultModel.getGoodsId());
+        walletReceipts.setPaymentGoodsId(payGoodsId);
         walletReceipts.setStatus(ReceiptsStatus.WAIT);
 
         walletReceiptsDao.save(walletReceipts);
@@ -104,21 +122,21 @@ public class GoodsReceiptsService {
 
     /**
      * 取消购物返利
-     * @param userId 用户id
      * @param orderNo 订单号
-     * @param goodsIds 商品id
      */
     @Transactional
-    public void cancel(String userId, String orderNo, List<String> goodsIds){
-        BigDecimal total = BigDecimal.ZERO;
-        Wallet wallet = walletService.getWalletByUserId(userId);
-        List<WalletReceipts> walletReceiptsList = walletReceiptsDao.findByOutTradeNoAndGoodsIdInAndStatus(orderNo, goodsIds, ReceiptsStatus.WAIT);
-        for (WalletReceipts it : walletReceiptsList){
-            total = total.add(it.getTotal());
-            it.setStatus(ReceiptsStatus.CANCEL);
+    public void cancel(String orderNo, String payGoodsId, Long operateTime){
+        WalletReceipts walletReceipts = walletReceiptsDao.findByOutTradeNoAndPaymentGoodsIdAndStatus(orderNo, payGoodsId, ReceiptsStatus.WAIT);
+        if (ObjectUtils.isEmpty(walletReceipts)){
+            log.info("无购物返利订单:{}", payGoodsId);
+            return;
+        }
+        if (operateTime > walletReceipts.getEstimatedTime()){
+            return;
         }
-        walletReceiptsDao.saveAll(walletReceiptsList);
-        walletDao.updateIncWaitAmount(wallet.getId(), total.negate());
+        walletReceipts.setStatus(ReceiptsStatus.CANCEL);
+        walletReceiptsDao.save(walletReceipts);
+        walletDao.updateIncWaitAmount(walletReceipts.getWallet().getId(), walletReceipts.getTotal().negate());
     }
 
     /**
@@ -127,12 +145,14 @@ public class GoodsReceiptsService {
     @Transactional
     public void settle(){
         //todo 查询到期入账的订单
-        WalletReceipts walletReceipts = walletReceiptsDao.findTop1ById("");
-
-        walletReceipts.setReceiptsTime(new Date().getTime());
-        walletReceipts.setStatus(ReceiptsStatus.RECEIPTS);
-        walletReceiptsDao.save(walletReceipts);
-        walletDao.updateIncWaitAmount(walletReceipts.getWallet().getId(), walletReceipts.getTotal().negate());
-        walletDao.updateIncAmount(walletReceipts.getWallet().getId(), walletReceipts.getTotal());
+        List<WalletReceipts> waitSettle = walletReceiptsDao.findByWaitSettle(ReceiptsStatus.WAIT, ReceiptsType.REBATE, new Date().getTime());
+        waitSettle.forEach(walletReceipts->{
+            walletReceipts.setReceiptsTime(new Date().getTime());
+            walletReceipts.setStatus(ReceiptsStatus.RECEIPTS);
+            walletReceiptsDao.save(walletReceipts);
+            walletDao.updateIncWaitAmount(walletReceipts.getWallet().getId(), walletReceipts.getTotal().negate());
+            walletDao.updateIncAmount(walletReceipts.getWallet().getId(), walletReceipts.getTotal());
+            log.info("结算商品返利:{}", walletReceipts.getId());
+        });
     }
 }

+ 4 - 1
RewardServer/src/main/java/com/zhongshu/reward/server/core/service/InviteReceiptsService.java

@@ -236,7 +236,10 @@ public class InviteReceiptsService {
         Long startTime = DateUtils.lastMonthStartTime();
         Long endTime = DateUtils.lastMonthEndTime();
         List<WalletReceipts> list = walletReceiptsDao.listMonth(startTime, endTime, day);
-
+        log.info("结算日期:{}", day);
+        log.info("结算开始时间:{}", startTime);
+        log.info("结算结束时间:{}", endTime);
+        log.info("结算返利订单数量:{}", list);
 
         if (list==null || list.isEmpty()){
             return;

+ 22 - 1
RewardServer/src/main/java/com/zhongshu/reward/server/core/stream/OrderStream.java

@@ -2,14 +2,35 @@ package com.zhongshu.reward.server.core.stream;
 
 import com.github.microservice.app.stream.StreamConsumer;
 import com.zhongshu.payment.client.model.order.v2.OrderStremModel;
+import com.zhongshu.payment.client.type.OrderType;
+import com.zhongshu.reward.server.core.service.GoodsReceiptsService;
+import lombok.extern.log4j.Log4j2;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.stereotype.Component;
+
+import java.math.BigDecimal;
 
 /**
  * @author wjf
  * @date 2024/8/22
  */
+@Log4j2
+@Component("orderStreamConsumer")
 public class OrderStream extends StreamConsumer<OrderStremModel> {
+
+    @Autowired
+    GoodsReceiptsService goodsReceiptsService;
+
     @Override
     public void accept(OrderStremModel orderStremModel) {
-        
+//        switch (orderStremModel.getOrderType()) {
+//            case USED:
+//                goodsReceiptsService.receipts(orderStremModel.getUserId(), orderStremModel.getOrderNo(), orderStremModel.getGoodsId(), orderStremModel.getOperationTime());
+//                break;
+//            case REFUNDED:
+//                goodsReceiptsService.cancel(orderStremModel.getOrderNo(), orderStremModel.getGoodsId(), orderStremModel.getOperationTime());
+//                break;
+//        }
     }
+
 }

+ 1 - 1
RewardServer/src/main/java/com/zhongshu/reward/server/core/stream/VipUserStream.java

@@ -40,7 +40,7 @@ public class VipUserStream extends StreamConsumer<VipUserParam> {
 
         VipUserRecord vipUserRecord = new VipUserRecord();
         BeanUtils.copyProperties(vipUserParam, vipUserRecord, "createTime");
-        vipUserRecord.setOperateTime(DateUtils.timeToLong(vipUserParam.getCreateTime(), DateUtils.FORMAT_LONG));
+        vipUserRecord.setOperateTime(DateUtils.timeToLong(vipUserParam.getCreateTime(), DateUtils.patternyyyyMis));
         vipUserRecord.setFirst(false);
         vipUserRecordDao.save(vipUserRecord);
         inviteReceiptsService.receipts(vipUserRecord);

+ 18 - 1
RewardServer/src/main/java/com/zhongshu/reward/server/core/timer/WalletTimer.java

@@ -6,6 +6,7 @@ import com.zhongshu.reward.client.type.TransferStatus;
 import com.zhongshu.reward.server.core.dao.WalletDao;
 import com.zhongshu.reward.server.core.dao.WxTransferBatchDao;
 import com.zhongshu.reward.server.core.domain.WxTransferBatch;
+import com.zhongshu.reward.server.core.service.GoodsReceiptsService;
 import com.zhongshu.reward.server.core.service.InviteReceiptsService;
 import com.zhongshu.reward.server.core.service.WalletReceiptsService;
 import com.zhongshu.reward.server.core.service.WxTransferService;
@@ -45,6 +46,9 @@ public class WalletTimer {
     @Autowired
     ResourceTokenService resourceTokenService;
 
+    @Autowired
+    GoodsReceiptsService goodsReceiptsService;
+
 
 //    @Scheduled(fixedRate = 5000)
 //    public void updateTransferStatus(){
@@ -66,7 +70,7 @@ public class WalletTimer {
         @Cleanup ResourceTokenService.Token token =  resourceTokenService.token("settleInviteReceipts");
         try {
             LocalDate now = LocalDate.now();
-
+            log.info("-----------定时任务:开始结算邀请返利--------------");
             if (DateUtils.checkLastDayOfMonth() &&  now.getMonthValue() == 2){
                 for (int i = now.getDayOfMonth(); i <= 31; i++) {
                     inviteReceiptsService.settle(i);
@@ -78,6 +82,19 @@ public class WalletTimer {
         }
     }
 
+//    /**
+//     * 定时结算商品返利
+//     */
+//    @Scheduled(fixedRate = 5*60*1000)
+//    public void goodsSettle(){
+//        @Cleanup ResourceTokenService.Token token =  resourceTokenService.token("settleGoodsReceipts");
+//        try {
+//            goodsReceiptsService.settle();
+//        }catch (Exception e){
+//            e.printStackTrace();
+//        }
+//    }
+
     /**
      * 定时生成持续返利订单
      */