wujiefeng 10 mesiacov pred
rodič
commit
d03660f1fa

+ 0 - 1
RewardClient/src/main/java/com/zhongshu/reward/client/model/wallet/WalletReceiptsModel.java

@@ -2,7 +2,6 @@ package com.zhongshu.reward.client.model.wallet;
 
 import com.zhongshu.reward.client.type.ReceiptsStatus;
 import com.zhongshu.reward.client.type.ReceiptsType;
-import com.zhongshu.vip.client.model.param.VipUserParam;
 import lombok.Data;
 
 import java.math.BigDecimal;

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

@@ -1,8 +1,11 @@
 package com.zhongshu.reward.server.core.dao.Impl;
 
+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.dao.extend.WalletReceiptsDaoExtend;
 import com.zhongshu.reward.server.core.domain.WalletReceipts;
+import org.apache.commons.lang3.StringUtils;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.data.domain.Sort;
 import org.springframework.data.mongodb.core.MongoTemplate;
@@ -31,7 +34,28 @@ public class WalletReceiptsDaoImpl implements WalletReceiptsDaoExtend {
             criteria.and("createTime").lte(endTime);
         }
 
-        criteria.and("inviteUserId").is(inviteUserId);
+        if (StringUtils.isNotEmpty(inviteUserId)){
+            criteria.and("inviteUserId").is(inviteUserId);
+        }
+        Query query = Query.query(criteria);
+        query.with(Sort.by(Sort.Order.desc("createTime")));
+
+        return mongoTemplate.find(query, WalletReceipts.class);
+    }
+
+    @Override
+    public List<WalletReceipts> listMonth(Long startTime, Long endTime) {
+        Criteria criteria = new Criteria();
+
+        criteria.and("createTime").gte(startTime);
+
+
+
+        criteria.and("createTime").lte(endTime);
+
+
+        criteria.and("ReceiptsType").is(ReceiptsType.COMMISSION);
+        criteria.and("status").is(ReceiptsStatus.WAIT);
         Query query = Query.query(criteria);
         query.with(Sort.by(Sort.Order.desc("createTime")));
 

+ 7 - 0
RewardServer/src/main/java/com/zhongshu/reward/server/core/dao/VipUserRecordDao.java

@@ -3,9 +3,16 @@ package com.zhongshu.reward.server.core.dao;
 import com.github.microservice.components.data.mongo.mongo.dao.MongoDao;
 import com.zhongshu.reward.server.core.domain.VipUserRecord;
 
+import java.util.List;
+
 /**
  * @author wjf
  * @date 2024/8/8
  */
 public interface VipUserRecordDao extends MongoDao<VipUserRecord> {
+
+    boolean existsByCpIdAndSucIn(String cpId, List<Integer> suc);
+    VipUserRecord findTop1ByCpIdAndSucOrderByOperateTime(String cpId, Integer suc);
+
+    VipUserRecord findTop1ByCpIdOrderByOperateTime(String cpId);
 }

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

@@ -17,4 +17,6 @@ public interface WalletReceiptsDao extends MongoDao<WalletReceipts>, WalletRecei
     WalletReceipts findTopByOutTradeNo(String outTradeNo);
 
     WalletReceipts findTop1ById(String id);
+
+    WalletReceipts findTop1ByVipUserRecord_IdAndVipUserRecord_First(String vipUserRecordId, boolean first);
 }

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

@@ -11,4 +11,6 @@ import java.util.List;
 public interface WalletReceiptsDaoExtend {
 
     List<WalletReceipts> inviteDetail(String inviteUserId, Long startTime, Long endTime);
+
+    List<WalletReceipts> listMonth(Long startTime, Long endTime);
 }

+ 2 - 0
RewardServer/src/main/java/com/zhongshu/reward/server/core/domain/VipUserRecord.java

@@ -54,4 +54,6 @@ public class VipUserRecord extends SuperEntity {
 
     @ApiModelProperty("5,6,7,8,9折扣")
     private Integer discount;
+
+    private boolean first;
 }

+ 67 - 36
RewardServer/src/main/java/com/zhongshu/reward/server/core/service/WalletReceiptsService.java

@@ -18,6 +18,8 @@ import com.zhongshu.reward.server.core.domain.Wallet;
 import com.zhongshu.reward.server.core.domain.WalletReceipts;
 import com.zhongshu.reward.server.core.util.DateUtils;
 import com.zhongshu.vip.client.model.param.VipUserParam;
+import com.zswl.cloud.bdb.client.service.InviteReceiptsRoleFeignService;
+import com.zswl.cloud.bdb.client.vo.InviteReceiptsRoleVo;
 import lombok.extern.slf4j.Slf4j;
 import org.apache.commons.lang3.ObjectUtils;
 import org.jetbrains.annotations.NotNull;
@@ -57,6 +59,9 @@ public class WalletReceiptsService {
     @Autowired
     AuthHelper authHelper;
 
+    @Autowired
+    InviteReceiptsRoleFeignService inviteReceiptsRoleFeignService;
+
 
     /**
      * 邀请返利入账
@@ -64,45 +69,71 @@ public class WalletReceiptsService {
      * @return
      */
     @Transactional
-    public void receipts(VipUserParam vipUserParam){
-
-        VipUserRecord vipUserRecord = new VipUserRecord();
-        BeanUtils.copyProperties(vipUserParam, vipUserRecord, "createTime");
-        vipUserRecord.setOperateTime(DateUtils.timeToLong(vipUserParam.getCreateTime(), DateUtils.FORMAT_LONG));
-        VipUserRecord save = vipUserRecordDao.save(vipUserRecord);
-        //查询邀请关系
-        InviteRecord inviteRecord = inviteRecordDao.findTopByUserIdOrderByCreateTime(vipUserParam.getCpId());
-        if (Objects.isNull(inviteRecord)){
-            return;
-        }
-        if(inviteRecord.getUserType().equals(UserType.OLD)){//是否老用户
-            return;
+    public void receipts(VipUserRecord vipUserRecord){
+        //校验对应套餐返利规则
+        com.zswl.cloud.bdb.client.ret.ResultContent<InviteReceiptsRoleVo> rulerResultContent = inviteReceiptsRoleFeignService.getOne(vipUserRecord.getPlanningId());
+        if (rulerResultContent.getState().equals(com.zswl.cloud.bdb.client.ret.ResultState.Success)){
+            InviteReceiptsRoleVo rulerVo = rulerResultContent.getContent();
+            if (vipUserRecord.getSuc().equals(1)){
+                //订阅
+                //查询邀请关系
+                InviteRecord inviteRecord = inviteRecordDao.findTopByUserIdOrderByCreateTime(vipUserRecord.getCpId());
+                if (Objects.nonNull(inviteRecord) && inviteRecord.getUserType().equals(UserType.NEW)){
+                    if (rulerVo.getFirstPurchaseDay()!=null && !vipUserRecordDao.existsByCpIdAndSucIn(vipUserRecord.getCpId(), List.of(1))) {
+                        vipUserRecord.setFirst(true);
+                        BigDecimal total = BigDecimal.ZERO;
+                        //订阅入账
+                        WalletReceipts walletReceipts = new WalletReceipts();
+                        //获取邀请人userid
+                        String inviteId = inviteRecord.getInviteUserId();
+                        Wallet wallet = walletService.getWalletByUserId(inviteId);
+                        walletReceipts.setWallet(wallet);
+                        walletReceipts.setInviteUserId(inviteId);
+                        walletReceipts.setUserId(vipUserRecord.getCpId());
+                        walletReceipts.setReceiptsType(ReceiptsType.COMMISSION);
+                        walletReceipts.setOutTradeNo(vipUserRecord.getPlanningId());
+                        walletReceipts.setTotal(total);
+                        walletReceipts.setEstimatedTime(DateUtils.nextMonthDayStartTime(rulerVo.getDay()));
+                        walletReceipts.setVipUserRecord(vipUserRecord);
+                        //邀请人钱包
+                        walletReceipts.setStatus(ReceiptsStatus.WAIT);
+                        wallet.setWaitAmount(wallet.getWaitAmount().add(total));
+                        walletDao.save(wallet);
+                        walletReceiptsDao.save(walletReceipts);
+                }
+            }
+        }else if (vipUserRecord.getSuc().equals(-1) || vipUserRecord.getSuc().equals(5)){
+                VipUserRecord lastOne = vipUserRecordDao.findTop1ByCpIdAndSucOrderByOperateTime(vipUserRecord.getCpId(), 1);
+                //上一单首购
+                if (ObjectUtils.isNotEmpty(lastOne) && lastOne.isFirst() && Math.abs(vipUserRecord.getOperateTime() - lastOne.getOperateTime()) < 24L *60*60*1000*rulerVo.getFirstPurchaseDay()){
+                    WalletReceipts walletReceipts = walletReceiptsDao.findTop1ByVipUserRecord_IdAndVipUserRecord_First(lastOne.getId(), true);
+                    if (ObjectUtils.isNotEmpty(walletReceipts)){
+                        walletReceipts.setStatus(ReceiptsStatus.CANCEL);
+                        Wallet wallet = walletService.getWalletByUserId(walletReceipts.getInviteUserId());
+                        wallet.setWaitAmount(wallet.getWaitAmount().subtract(walletReceipts.getTotal()));
+                        walletDao.save(wallet);
+                        walletReceiptsDao.save(walletReceipts);
+                    }
+                }
+            }
         }
+        vipUserRecordDao.save(vipUserRecord);
+    }
 
-        //todo 校验对应套餐返利规则
-        BigDecimal total = BigDecimal.ZERO;
-
-        if (vipUserRecord.getSuc().equals(1) || vipUserRecord.getSuc().equals(4)){
-            //订阅入账
-            WalletReceipts walletReceipts = new WalletReceipts();
-            //获取邀请人userid
-            String inviteId = inviteRecord.getInviteUserId();
-            Wallet wallet = walletService.getWalletByUserId(inviteId);
-            walletReceipts.setWallet(wallet);
-            walletReceipts.setUserId(vipUserParam.getCpId());
-            walletReceipts.setReceiptsType(ReceiptsType.COMMISSION);
-            walletReceipts.setOutTradeNo(vipUserParam.getPlanningId());
-            walletReceipts.setTotal(total);
-            walletReceipts.setEstimatedTime(DateUtils.nextMonthDayStartTime(25));
-            walletReceipts.setVipUserRecord(save);
-            //邀请人钱包
-            if (walletReceipts.getReceiptsType()==null){
-                walletReceipts.setStatus(ReceiptsStatus.WAIT);
-            }
-            wallet.setWaitAmount(wallet.getWaitAmount().add(total));
-            walletDao.save(wallet);
-            walletReceiptsDao.save(walletReceipts);
+
+    /**
+     * 结算
+     */
+    public void settle(){
+        Long startTime = DateUtils.lastMonthStartTime();
+        Long endTime = DateUtils.lastMonthEndTime();
+        List<WalletReceipts> list = walletReceiptsDao.listMonth(startTime, endTime);
+
+        for (WalletReceipts walletReceipts : list){
+            String inviteUserId = walletReceipts.getInviteUserId();
+            String userId = walletReceipts.getUserId();
         }
+        return;
     }
 
     /**

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

@@ -1,9 +1,14 @@
 package com.zhongshu.reward.server.core.stream;
 
 import com.github.microservice.app.stream.StreamConsumer;
+import com.zhongshu.reward.server.core.dao.VipUserRecordDao;
+import com.zhongshu.reward.server.core.domain.VipUserRecord;
 import com.zhongshu.reward.server.core.service.InviteReceiptsRulerService;
+import com.zhongshu.reward.server.core.service.WalletReceiptsService;
+import com.zhongshu.reward.server.core.util.DateUtils;
 import com.zhongshu.vip.client.model.param.VipUserParam;
 import lombok.extern.log4j.Log4j2;
+import org.springframework.beans.BeanUtils;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Component;
 
@@ -12,10 +17,17 @@ import org.springframework.stereotype.Component;
 public class VipUserStream extends StreamConsumer<VipUserParam> {
 
     @Autowired
-    InviteReceiptsRulerService inviteReceiptsRulerService;
+    WalletReceiptsService walletReceiptsService;
+
+    @Autowired
+    VipUserRecordDao vipUserRecordDao;
 
     @Override
     public void accept(VipUserParam vipUserParam) {
         log.info("收到会员套餐消息:{}",vipUserParam);
+        VipUserRecord vipUserRecord = new VipUserRecord();
+        BeanUtils.copyProperties(vipUserParam, vipUserRecord, "createTime");
+        vipUserRecord.setOperateTime(DateUtils.timeToLong(vipUserParam.getCreateTime(), DateUtils.FORMAT_LONG));
+        walletReceiptsService.receipts(vipUserRecord);
     }
 }

+ 52 - 12
RewardServer/src/main/java/com/zhongshu/reward/server/core/util/DateUtils.java

@@ -66,11 +66,49 @@ public class DateUtils {
     public static Long nextMonthDayStartTime(Integer day) {
         Calendar c = Calendar.getInstance();
         c.add(Calendar.MONTH, 1);
-        c.set(Calendar.DATE, day);
+
+        if (day >c.getActualMaximum(Calendar.DAY_OF_MONTH)){
+            c.set(Calendar.DATE, c.getActualMaximum(Calendar.DAY_OF_MONTH));
+        }else {
+            c.set(Calendar.DATE, day);
+        }
+        c.set(Calendar.HOUR_OF_DAY, 0);
+        c.set(Calendar.MINUTE, 0);
+        c.set(Calendar.SECOND, 0);
+        return c.getTimeInMillis();
+    }
+
+    /**
+     * 上月开始时间
+     *
+     * @return
+     */
+    public static Long lastMonthStartTime() {
+        Calendar c = Calendar.getInstance();
+        c.add(Calendar.MONTH, -1);
+        c.set(Calendar.DATE, 1);
 
         c.set(Calendar.HOUR_OF_DAY, 0);
         c.set(Calendar.MINUTE, 0);
         c.set(Calendar.SECOND, 0);
+        return c.getTimeInMillis();
+    }
+
+    /**
+     * 上月结束时间
+     *
+     * @return
+     */
+    public static Long lastMonthEndTime() {
+        Calendar c = Calendar.getInstance();
+
+        c.add(Calendar.MONTH, 0);
+        c.set(Calendar.DATE, 1);
+        c.set(Calendar.DAY_OF_MONTH, 0);
+
+        c.set(Calendar.HOUR_OF_DAY, 23);
+        c.set(Calendar.MINUTE, 59);
+        c.set(Calendar.SECOND, 59);
 
         return c.getTimeInMillis();
     }
@@ -80,7 +118,7 @@ public class DateUtils {
      *
      * @return
      */
-    public static Long getDayOfMonthStartTime(Integer day) {
+    public static Long getDayOfMonthEndTime(Integer day) {
         Calendar c = Calendar.getInstance();
 
         c.set(Calendar.DATE, 1);
@@ -98,7 +136,7 @@ public class DateUtils {
      *
      * @return
      */
-    public static Long getDayOfMonthEndTime(Integer day) {
+    public static Long getDayOfMonthStartTime(Integer day) {
         Calendar c = Calendar.getInstance();
 
         c.set(Calendar.DATE, 1);
@@ -178,15 +216,17 @@ public class DateUtils {
         return c.getTimeInMillis();
     }
 
-    public static void main(String[] args) {
-        Calendar c = Calendar.getInstance();
-        c.add(Calendar.MONTH, 1);
-        c.set(Calendar.DATE, 1);
-
-        c.set(Calendar.HOUR_OF_DAY, 0);
-        c.set(Calendar.MINUTE, 0);
-        c.set(Calendar.SECOND, 0);
+    public static String paresTime(Long time, String pattern) {
+        if (time == null || time <= 0) {
+            return "";
+        }
+        Date date = new Date(time);
+        SimpleDateFormat formatter = new SimpleDateFormat(pattern);
+        return formatter.format(date);
+    }
 
-        ;
+    public static void main(String[] args) {
+        log.info(paresTime(lastMonthStartTime(), FORMAT_LONG));
+        log.info(paresTime(lastMonthEndTime(), FORMAT_LONG));
     }
 }