|  | @@ -76,7 +76,7 @@ public class WalletReceiptsService {
 | 
	
		
			
				|  |  |          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)){
 | 
	
		
			
				|  |  | +            if (rulerVo.isFirst() && vipUserRecord.getSuc().equals(1)){
 | 
	
		
			
				|  |  |                  vipUserRecord.setFirst(false);
 | 
	
		
			
				|  |  |                  //订阅
 | 
	
		
			
				|  |  |                  //查询邀请关系
 | 
	
	
		
			
				|  | @@ -84,7 +84,6 @@ public class WalletReceiptsService {
 | 
	
		
			
				|  |  |                  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
 | 
	
	
		
			
				|  | @@ -95,12 +94,12 @@ public class WalletReceiptsService {
 | 
	
		
			
				|  |  |                          walletReceipts.setUserId(vipUserRecord.getCpId());
 | 
	
		
			
				|  |  |                          walletReceipts.setReceiptsType(ReceiptsType.COMMISSION);
 | 
	
		
			
				|  |  |                          walletReceipts.setOutTradeNo(vipUserRecord.getPlanningId());
 | 
	
		
			
				|  |  | -                        walletReceipts.setTotal(total);
 | 
	
		
			
				|  |  | +                        walletReceipts.setTotal(rulerVo.getBaseTotal());
 | 
	
		
			
				|  |  |                          walletReceipts.setEstimatedTime(DateUtils.nextMonthDayStartTime(rulerVo.getDay()));
 | 
	
		
			
				|  |  |                          walletReceipts.setVipUserRecord(vipUserRecord);
 | 
	
		
			
				|  |  |                          //邀请人钱包
 | 
	
		
			
				|  |  |                          walletReceipts.setStatus(ReceiptsStatus.WAIT);
 | 
	
		
			
				|  |  | -                        wallet.setWaitAmount(wallet.getWaitAmount().add(total));
 | 
	
		
			
				|  |  | +                        wallet.setWaitAmount(wallet.getWaitAmount().add(rulerVo.getBaseTotal()));
 | 
	
		
			
				|  |  |                          walletDao.save(wallet);
 | 
	
		
			
				|  |  |                          walletReceiptsDao.save(walletReceipts);
 | 
	
		
			
				|  |  |                  }
 | 
	
	
		
			
				|  | @@ -138,7 +137,7 @@ public class WalletReceiptsService {
 | 
	
		
			
				|  |  |          //首次订购
 | 
	
		
			
				|  |  |          list.forEach(it -> {
 | 
	
		
			
				|  |  |              if (it.getVipUserRecord().isFirst()) {
 | 
	
		
			
				|  |  | -                addAmount(receipts, wallets, it);
 | 
	
		
			
				|  |  | +                addAmount(it);
 | 
	
		
			
				|  |  |              }
 | 
	
		
			
				|  |  |          });
 | 
	
		
			
				|  |  |  
 | 
	
	
		
			
				|  | @@ -153,41 +152,97 @@ public class WalletReceiptsService {
 | 
	
		
			
				|  |  |          keep.forEach(it->{
 | 
	
		
			
				|  |  |              boolean cancel = vipUserRecordDao.existsByCpIdAndSucIn(it.getUserId(), List.of(-1, 5));
 | 
	
		
			
				|  |  |              if (!cancel){//无退订消息
 | 
	
		
			
				|  |  | -                addAmount(receipts, wallets, it);
 | 
	
		
			
				|  |  | +                addAmount(it);
 | 
	
		
			
				|  |  |              }else {
 | 
	
		
			
				|  |  |                  VipUserRecord cancelRecord = vipUserRecordDao.findTopOneByOperateTime(it.getUserId(), DateUtils.lastMonthDayStartTime(1), DateUtils.lastMonthDayStartTime(6));
 | 
	
		
			
				|  |  |                  if (ObjectUtils.isEmpty(cancelRecord) || cancelRecord.getSuc().equals(1) || cancelRecord.getSuc().equals(4)){
 | 
	
		
			
				|  |  |                      //上月1-5号无退订消息或消息为订购:默认扣款成功。。。
 | 
	
		
			
				|  |  | -                    addAmount(receipts, wallets, it);
 | 
	
		
			
				|  |  | +                    addAmount(it);
 | 
	
		
			
				|  |  |                  }else {
 | 
	
		
			
				|  |  | -                    cancel(receipts, wallets, it);
 | 
	
		
			
				|  |  | +                    cancel(it);
 | 
	
		
			
				|  |  |                  }
 | 
	
		
			
				|  |  |              }
 | 
	
		
			
				|  |  |          });
 | 
	
		
			
				|  |  |          walletDao.saveAll(wallets);
 | 
	
		
			
				|  |  |          walletReceiptsDao.saveAll(receipts);
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  |      }
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  |      public void autoReceipts(){
 | 
	
		
			
				|  |  | -//        inviteReceiptsRoleFeignService.getOne()
 | 
	
		
			
				|  |  | +        //获取设置持续订购返利的套餐规则
 | 
	
		
			
				|  |  | +        com.zswl.cloud.bdb.client.ret.ResultContent<List<InviteReceiptsRoleVo>> rulerResult = inviteReceiptsRoleFeignService.listKeep();
 | 
	
		
			
				|  |  | +        if (rulerResult.getState().equals(com.zswl.cloud.bdb.client.ret.ResultState.Success)){
 | 
	
		
			
				|  |  | +            List<InviteReceiptsRoleVo> rulerList = rulerResult.getContent();
 | 
	
		
			
				|  |  | +            if (rulerList.isEmpty()){
 | 
	
		
			
				|  |  | +                return;
 | 
	
		
			
				|  |  | +            }
 | 
	
		
			
				|  |  | +            List<String> setMealCodeList = rulerList.stream().map(InviteReceiptsRoleVo::getSetMealCode).collect(Collectors.toList());
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +            //获取上月所有入帐单(待结算和已结算的)
 | 
	
		
			
				|  |  | +            Long startTime = DateUtils.lastMonthStartTime();
 | 
	
		
			
				|  |  | +            Long endTime = DateUtils.lastMonthEndTime();
 | 
	
		
			
				|  |  | +            List<WalletReceipts> walletReceiptsList = walletReceiptsDao.listMonthBySetMealCode(startTime, endTime, setMealCodeList);
 | 
	
		
			
				|  |  | +            if (walletReceiptsList.isEmpty()){
 | 
	
		
			
				|  |  | +                return;
 | 
	
		
			
				|  |  | +            }
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +            rulerList.forEach(ruler->{
 | 
	
		
			
				|  |  | +                walletReceiptsList.forEach(walletReceipts -> {
 | 
	
		
			
				|  |  | +                    //匹配规则
 | 
	
		
			
				|  |  | +                    if (walletReceipts.getOutTradeNo().equals(ruler.getSetMealCode())){
 | 
	
		
			
				|  |  | +                        //上月最新消息
 | 
	
		
			
				|  |  | +                        VipUserRecord topOne = vipUserRecordDao.findTopOneByOperateTime(walletReceipts.getUserId(), startTime, endTime);
 | 
	
		
			
				|  |  | +                        if (Objects.isNull(topOne)){
 | 
	
		
			
				|  |  | +                            //上月无消息,生成入账
 | 
	
		
			
				|  |  | +                            insertReceipts(ruler, walletReceipts);
 | 
	
		
			
				|  |  | +                        }else if (topOne.getSuc().equals(1) || topOne.getSuc().equals(4)){
 | 
	
		
			
				|  |  | +                            //上月最后消息为订购,生成入账
 | 
	
		
			
				|  |  | +                            insertReceipts(ruler, walletReceipts);
 | 
	
		
			
				|  |  | +                        }
 | 
	
		
			
				|  |  | +                    }
 | 
	
		
			
				|  |  | +                });
 | 
	
		
			
				|  |  | +            });
 | 
	
		
			
				|  |  | +        }
 | 
	
		
			
				|  |  |      }
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  | -    private static void addAmount(List<WalletReceipts> receipts, List<Wallet> wallets, WalletReceipts it) {
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +    private void insertReceipts(InviteReceiptsRoleVo ruler, WalletReceipts it) {
 | 
	
		
			
				|  |  | +        WalletReceipts walletReceipts = new WalletReceipts();
 | 
	
		
			
				|  |  | +        walletReceipts.setWallet(it.getWallet());
 | 
	
		
			
				|  |  | +        walletReceipts.setInviteUserId(it.getInviteUserId());
 | 
	
		
			
				|  |  | +        walletReceipts.setUserId(it.getUserId());
 | 
	
		
			
				|  |  | +        walletReceipts.setEstimatedTime(DateUtils.nextMonthDayStartTime(ruler.getDay()));
 | 
	
		
			
				|  |  | +        walletReceipts.setReceiptsType(ReceiptsType.COMMISSION);
 | 
	
		
			
				|  |  | +        walletReceipts.setTotal(ruler.getBaseTotal());
 | 
	
		
			
				|  |  | +        walletReceipts.setStatus(ReceiptsStatus.WAIT);
 | 
	
		
			
				|  |  | +        walletReceipts.setOutTradeNo(ruler.getSetMealCode());
 | 
	
		
			
				|  |  | +        VipUserRecord vipUserRecord = it.getVipUserRecord();
 | 
	
		
			
				|  |  | +        vipUserRecord.setId(null);
 | 
	
		
			
				|  |  | +        vipUserRecord.setFirst(false);
 | 
	
		
			
				|  |  | +        vipUserRecord.setPlanningId(ruler.getSetMealCode());
 | 
	
		
			
				|  |  | +        walletReceipts.setVipUserRecord(vipUserRecord);
 | 
	
		
			
				|  |  | +        Wallet wallet = walletReceipts.getWallet();
 | 
	
		
			
				|  |  | +        wallet.setWaitAmount(wallet.getWaitAmount().add(ruler.getBaseTotal()));
 | 
	
		
			
				|  |  | +        walletDao.save(wallet);
 | 
	
		
			
				|  |  | +        walletReceiptsDao.save(walletReceipts);
 | 
	
		
			
				|  |  | +    }
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +    private void addAmount(WalletReceipts it) {
 | 
	
		
			
				|  |  |          it.setStatus(ReceiptsStatus.RECEIPTS);
 | 
	
		
			
				|  |  |          it.setReceiptsTime(new Date().getTime());
 | 
	
		
			
				|  |  |          Wallet wallet = it.getWallet();
 | 
	
		
			
				|  |  |          wallet.setWaitAmount(wallet.getWaitAmount().subtract(it.getTotal()));
 | 
	
		
			
				|  |  |          wallet.setAmount(wallet.getAmount().add(it.getTotal()));
 | 
	
		
			
				|  |  | -        receipts.add(it);
 | 
	
		
			
				|  |  | -        wallets.add(wallet);
 | 
	
		
			
				|  |  | +        walletDao.save(wallet);
 | 
	
		
			
				|  |  | +        walletReceiptsDao.save(it);
 | 
	
		
			
				|  |  |      }
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  | -    private static void cancel(List<WalletReceipts> receipts, List<Wallet> wallets, WalletReceipts it){
 | 
	
		
			
				|  |  | +    private void cancel(WalletReceipts it){
 | 
	
		
			
				|  |  |          it.setStatus(ReceiptsStatus.CANCEL);
 | 
	
		
			
				|  |  |          Wallet wallet = it.getWallet();
 | 
	
		
			
				|  |  |          wallet.setWaitAmount(wallet.getWaitAmount().subtract(it.getTotal()));
 | 
	
		
			
				|  |  | -        receipts.add(it);
 | 
	
		
			
				|  |  | -        wallets.add(wallet);
 | 
	
		
			
				|  |  | +        walletDao.save(wallet);
 | 
	
		
			
				|  |  | +        walletReceiptsDao.save(it);
 | 
	
		
			
				|  |  |      }
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  |      /**
 |