|
@@ -0,0 +1,436 @@
|
|
|
+package com.zhongshu.reward.server.core.service;
|
|
|
+
|
|
|
+import com.github.microservice.auth.security.helper.AuthHelper;
|
|
|
+import com.zhongshu.reward.client.type.ReceiptsStatus;
|
|
|
+import com.zhongshu.reward.client.type.ReceiptsType;
|
|
|
+import com.zhongshu.reward.client.type.UserType;
|
|
|
+import com.zhongshu.reward.server.core.dao.InviteRecordDao;
|
|
|
+import com.zhongshu.reward.server.core.dao.VipUserRecordDao;
|
|
|
+import com.zhongshu.reward.server.core.dao.WalletDao;
|
|
|
+import com.zhongshu.reward.server.core.dao.WalletReceiptsDao;
|
|
|
+import com.zhongshu.reward.server.core.domain.InviteRecord;
|
|
|
+import com.zhongshu.reward.server.core.domain.VipUserRecord;
|
|
|
+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.zswl.cloud.bdb.client.ret.ResultState;
|
|
|
+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.bson.types.ObjectId;
|
|
|
+import org.jetbrains.annotations.NotNull;
|
|
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
|
+import org.springframework.stereotype.Service;
|
|
|
+import org.springframework.transaction.annotation.Transactional;
|
|
|
+
|
|
|
+import java.math.BigDecimal;
|
|
|
+import java.util.*;
|
|
|
+import java.util.jar.JarFile;
|
|
|
+import java.util.stream.Collectors;
|
|
|
+
|
|
|
+/**
|
|
|
+ * @author wjf
|
|
|
+ * @date 2024/8/23
|
|
|
+ */
|
|
|
+
|
|
|
+@Service
|
|
|
+@Slf4j
|
|
|
+public class InviteReceiptsService {
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ WalletDao walletDao;
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ WalletService walletService;
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ WalletReceiptsDao walletReceiptsDao;
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ VipUserRecordDao vipUserRecordDao;
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ InviteRecordDao inviteRecordDao;
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ AuthHelper authHelper;
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ InviteReceiptsRoleFeignService inviteReceiptsRoleFeignService;
|
|
|
+
|
|
|
+
|
|
|
+ public void receipts(VipUserRecord vipUserRecord){
|
|
|
+ //查询邀请关系
|
|
|
+ InviteRecord inviteRecord = inviteRecordDao.findTopByUserIdOrderByCreateTime(vipUserRecord.getCpId());
|
|
|
+ if (ObjectUtils.isEmpty(inviteRecord)){
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ //校验对应套餐返利规则
|
|
|
+ com.zswl.cloud.bdb.client.ret.ResultContent<InviteReceiptsRoleVo> rulerResultContent = inviteReceiptsRoleFeignService.getOne(vipUserRecord.getPlanningId());
|
|
|
+ if (!ResultState.Success.equals(rulerResultContent.getState())){
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ //获取套餐返利规则
|
|
|
+ InviteReceiptsRoleVo ruler = rulerResultContent.getContent();
|
|
|
+
|
|
|
+ if (vipUserRecord.getSuc().equals(1) || vipUserRecord.getSuc().equals(4)){//订阅
|
|
|
+
|
|
|
+ if (isFirstSuc(ruler, vipUserRecord.getCpId(), vipUserRecord.getOperateTime(), inviteRecord.getRegisterTime())){ //首次订购,且时间小于48小时
|
|
|
+
|
|
|
+ sucToReceipts(ruler, vipUserRecord, inviteRecord.getInviteUserId());
|
|
|
+ vipUserRecord.setFirst(true);
|
|
|
+ }else if (isSucLastMonth(vipUserRecord.getCpId())){
|
|
|
+ sucToReceipts(ruler, vipUserRecord, inviteRecord.getInviteUserId());
|
|
|
+ vipUserRecord.setFirst(false);
|
|
|
+ }
|
|
|
+
|
|
|
+ }else if (vipUserRecord.getSuc().equals(-1) || vipUserRecord.getSuc().equals(5)){//退订
|
|
|
+
|
|
|
+ if (isFirstCancel(vipUserRecord.getCpId())){
|
|
|
+ firstCancel(vipUserRecord, ruler);
|
|
|
+ }else {
|
|
|
+ VipUserRecord currentRecord = vipUserRecordDao.findTop1ByCpIdOrderByOperateTime(vipUserRecord.getCpId());
|
|
|
+ if (ObjectUtils.isNotEmpty(currentRecord)){
|
|
|
+ WalletReceipts currentWalletReceipts = walletReceiptsDao.findTop1ByVipUserRecord_Id(currentRecord.getId());
|
|
|
+ if (ObjectUtils.isNotEmpty(currentWalletReceipts) && Math.abs(vipUserRecord.getOperateTime() - currentWalletReceipts.getCreateTime()) < 24L *60*60*1000*currentWalletReceipts.getRuler().getFirstPurchaseDay()){
|
|
|
+ currentWalletReceipts.setStatus(ReceiptsStatus.CANCEL);
|
|
|
+ walletReceiptsDao.save(currentWalletReceipts);
|
|
|
+ Wallet wallet = walletService.getWalletByUserId(currentWalletReceipts.getInviteUserId());
|
|
|
+ walletDao.updateIncWaitAmount(wallet.getId(), ruler.getBaseTotal().negate());
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ vipUserRecordDao.save(vipUserRecord);
|
|
|
+ }
|
|
|
+
|
|
|
+ private void firstCancel(VipUserRecord vipUserRecord, InviteReceiptsRoleVo ruler) {
|
|
|
+ WalletReceipts currentMonth = walletReceiptsDao.findNoCancelByCreateTime(vipUserRecord.getCpId(), DateUtils.getCurrentMonthStartTime(), new Date().getTime());
|
|
|
+ if (Objects.nonNull(currentMonth) && Math.abs(vipUserRecord.getOperateTime() - currentMonth.getCreateTime()) < 24L *60*60*1000*currentMonth.getRuler().getFirstPurchaseDay()){
|
|
|
+ //本月有返利且退订时间不满足规则 取消
|
|
|
+ currentMonth.setStatus(ReceiptsStatus.CANCEL);
|
|
|
+ walletReceiptsDao.save(currentMonth);
|
|
|
+ Wallet wallet = walletService.getWalletByUserId(currentMonth.getInviteUserId());
|
|
|
+ walletDao.updateIncWaitAmount(wallet.getId(), ruler.getBaseTotal().negate());
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ private void sucToReceipts(InviteReceiptsRoleVo rulerVo, VipUserRecord vipUserRecord, String inviteUserId){
|
|
|
+ Wallet wallet = walletService.getWalletByUserId(inviteUserId);
|
|
|
+ WalletReceipts walletReceipts = buildReceipts(vipUserRecord, rulerVo, inviteUserId, wallet);
|
|
|
+ walletDao.updateIncWaitAmount(wallet.getId(), rulerVo.getBaseTotal());
|
|
|
+ walletReceiptsDao.save(walletReceipts);
|
|
|
+ }
|
|
|
+
|
|
|
+ private boolean isFirstCancel(String userId){
|
|
|
+ VipUserRecord topVipUserRecord = vipUserRecordDao.findTop1ByCpIdOrderByOperateTime(userId);
|
|
|
+ return ObjectUtils.isNotEmpty(topVipUserRecord) && topVipUserRecord.isFirst();
|
|
|
+ }
|
|
|
+
|
|
|
+ private boolean isFirstSuc(InviteReceiptsRoleVo rulerVo, String userId, Long operateTime, Long registerTime){
|
|
|
+ return rulerVo.getFirstPurchaseDay()!=null && !vipUserRecordDao.existsByCpIdAndSucIn(userId, List.of(1)) && Math.abs(operateTime - registerTime) <= 48L*60*60*1000;
|
|
|
+ }
|
|
|
+
|
|
|
+ private boolean isSucLastMonth(String userId){
|
|
|
+ WalletReceipts noCancelByLastMonth = walletReceiptsDao.findNoCancelByCreateTime(userId, DateUtils.lastMonthStartTime(), DateUtils.lastMonthEndTime());
|
|
|
+ return ObjectUtils.isNotEmpty(noCancelByLastMonth);
|
|
|
+ }
|
|
|
+
|
|
|
+// /**
|
|
|
+// * 邀请返利入账
|
|
|
+// * @param
|
|
|
+// * @return
|
|
|
+// */
|
|
|
+// @Transactional
|
|
|
+// 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 (rulerVo.isFirst() && (vipUserRecord.getSuc().equals(1) || vipUserRecord.getSuc().equals(4))){
|
|
|
+// vipUserRecord.setFirst(false);
|
|
|
+// //订阅
|
|
|
+// //查询邀请关系
|
|
|
+// InviteRecord inviteRecord = inviteRecordDao.findTopByUserIdOrderByCreateTime(vipUserRecord.getCpId());
|
|
|
+// if (Objects.nonNull(inviteRecord) && inviteRecord.getUserType().equals(UserType.NEW)){
|
|
|
+// //首次订购且订购时间小于注册时间48小时
|
|
|
+// if (rulerVo.getFirstPurchaseDay()!=null && !vipUserRecordDao.existsByCpIdAndSucIn(vipUserRecord.getCpId(), List.of(1)) && Math.abs(vipUserRecord.getOperateTime() - inviteRecord.getRegisterTime()) <= 48L*60*60*1000) {
|
|
|
+// vipUserRecord.setFirst(true);
|
|
|
+// //订阅入账
|
|
|
+// Wallet wallet = walletService.getWalletByUserId(inviteRecord.getInviteUserId());
|
|
|
+// WalletReceipts walletReceipts = buildReceipts(vipUserRecord, rulerVo, inviteRecord.getInviteUserId(), wallet);
|
|
|
+// walletDao.updateIncWaitAmount(wallet.getId(), rulerVo.getBaseTotal());
|
|
|
+// walletReceiptsDao.save(walletReceipts);
|
|
|
+// }else if (rulerVo.getFirstPurchaseDay()!=null && vipUserRecordDao.existsByCpIdAndSucIn(vipUserRecord.getCpId(), List.of(1))){//不是首次订购、
|
|
|
+//
|
|
|
+// WalletReceipts lastMoth = walletReceiptsDao.findNoCancelByCreateTime(vipUserRecord.getCpId(), DateUtils.lastMonthStartTime(), DateUtils.lastMonthEndTime());
|
|
|
+//
|
|
|
+// if (ObjectUtils.isNotEmpty(lastMoth)){//上月有未取消返利订单
|
|
|
+// WalletReceipts currentMonth = walletReceiptsDao.findNoCancelByCreateTime(vipUserRecord.getCpId(), DateUtils.getCurrentMonthStartTime(), new Date().getTime());
|
|
|
+//
|
|
|
+// if (ObjectUtils.isEmpty(currentMonth)){//本月没有未取消返利订单
|
|
|
+// Wallet wallet = walletService.getWalletByUserId(inviteRecord.getInviteUserId());
|
|
|
+// WalletReceipts walletReceipts = buildReceipts(vipUserRecord, rulerVo, inviteRecord, wallet);
|
|
|
+// walletDao.updateIncWaitAmount(wallet.getId(), rulerVo.getBaseTotal());
|
|
|
+// 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()){
|
|
|
+// WalletReceipts walletReceipts = walletReceiptsDao.findTop1ByVipUserRecord_IdAndVipUserRecord_First(lastOne.getId(), true);
|
|
|
+// if (ObjectUtils.isNotEmpty(walletReceipts) && Math.abs(vipUserRecord.getOperateTime() - lastOne.getOperateTime()) < 24L *60*60*1000*walletReceipts.getRuler().getFirstPurchaseDay()){
|
|
|
+// walletReceipts.setStatus(ReceiptsStatus.CANCEL);
|
|
|
+// Wallet wallet = walletService.getWalletByUserId(walletReceipts.getInviteUserId());
|
|
|
+// walletDao.updateIncWaitAmount(wallet.getId(), rulerVo.getBaseTotal().negate());
|
|
|
+// walletReceiptsDao.save(walletReceipts);
|
|
|
+// }
|
|
|
+// }else {
|
|
|
+// InviteRecord inviteRecord = inviteRecordDao.findTopByUserIdOrderByCreateTime(vipUserRecord.getCpId());
|
|
|
+// if (Objects.nonNull(inviteRecord) && inviteRecord.getUserType().equals(UserType.NEW)){
|
|
|
+// WalletReceipts lastMoth = walletReceiptsDao.findNoCancelByCreateTime(vipUserRecord.getCpId(), DateUtils.lastMonthStartTime(), DateUtils.lastMonthEndTime());
|
|
|
+// if (ObjectUtils.isEmpty(lastMoth)){//上月有返利
|
|
|
+// WalletReceipts currentMonth = walletReceiptsDao.findNoCancelByCreateTime(vipUserRecord.getCpId(), DateUtils.getCurrentMonthStartTime(), new Date().getTime());
|
|
|
+// if (Objects.nonNull(currentMonth) && Math.abs(vipUserRecord.getOperateTime() - currentMonth.getCreateTime()) < 24L *60*60*1000*currentMonth.getRuler().getFirstPurchaseDay()){
|
|
|
+// //本月有返利且退订时间不满足规则 取消
|
|
|
+// currentMonth.setStatus(ReceiptsStatus.CANCEL);
|
|
|
+// walletReceiptsDao.save(currentMonth);
|
|
|
+// Wallet wallet = walletService.getWalletByUserId(currentMonth.getInviteUserId());
|
|
|
+// walletDao.updateIncWaitAmount(wallet.getId(), rulerVo.getBaseTotal().negate());
|
|
|
+// }
|
|
|
+// }
|
|
|
+// }
|
|
|
+// }
|
|
|
+// }
|
|
|
+// }
|
|
|
+// vipUserRecordDao.save(vipUserRecord);
|
|
|
+// }
|
|
|
+
|
|
|
+ @NotNull
|
|
|
+ private static WalletReceipts buildReceipts(VipUserRecord vipUserRecord, InviteReceiptsRoleVo rulerVo, String inviteUserId, Wallet wallet) {
|
|
|
+ WalletReceipts walletReceipts = new WalletReceipts();
|
|
|
+ walletReceipts.setWallet(wallet);
|
|
|
+ walletReceipts.setInviteUserId(inviteUserId);
|
|
|
+ walletReceipts.setUserId(vipUserRecord.getCpId());
|
|
|
+ walletReceipts.setReceiptsType(ReceiptsType.COMMISSION);
|
|
|
+ walletReceipts.setOutTradeNo(vipUserRecord.getPlanningId());
|
|
|
+ walletReceipts.setTotal(rulerVo.getBaseTotal());
|
|
|
+ walletReceipts.setEstimatedTime(DateUtils.nextMonthDayStartTime(rulerVo.getDay()));
|
|
|
+ vipUserRecord.setId(new ObjectId().toHexString());
|
|
|
+ walletReceipts.setVipUserRecord(vipUserRecord);
|
|
|
+ walletReceipts.setRuler(rulerVo);
|
|
|
+ //邀请人钱包
|
|
|
+ walletReceipts.setStatus(ReceiptsStatus.WAIT);
|
|
|
+ return walletReceipts;
|
|
|
+ }
|
|
|
+
|
|
|
+ public void settle(Integer day){
|
|
|
+ Long startTime = DateUtils.lastMonthStartTime();
|
|
|
+ Long endTime = DateUtils.lastMonthEndTime();
|
|
|
+ List<WalletReceipts> list = walletReceiptsDao.listMonth(startTime, endTime, day);
|
|
|
+
|
|
|
+
|
|
|
+ if (list==null || list.isEmpty()){
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
+ //首次订购
|
|
|
+ list.forEach(this::addAmount);
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+// Map<String, List<WalletReceipts>> map = list.stream().collect(Collectors.groupingBy(WalletReceipts::getInviteUserId));
|
|
|
+//
|
|
|
+// for (String key : map.keySet()){
|
|
|
+// BigDecimal amountTotal = BigDecimal.ZERO;
|
|
|
+// List<WalletReceipts> walletReceipts = map.get(key);
|
|
|
+// walletReceipts.forEach(it->{
|
|
|
+// amountTotal = amountTotal.add(it.getTotal());
|
|
|
+// });
|
|
|
+// }
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+// /**
|
|
|
+// * 结算邀请返利
|
|
|
+// * @param day
|
|
|
+// */
|
|
|
+// @Transactional
|
|
|
+// public void settle(Integer day){
|
|
|
+//
|
|
|
+// Long startTime = DateUtils.lastMonthStartTime();
|
|
|
+// Long endTime = DateUtils.lastMonthEndTime();
|
|
|
+// List<WalletReceipts> list = walletReceiptsDao.listMonth(startTime, endTime, day);
|
|
|
+//
|
|
|
+//// List<WalletReceipts> receipts = new ArrayList<>();
|
|
|
+//// List<Wallet> wallets = new ArrayList<>();
|
|
|
+//
|
|
|
+// if (list==null || list.isEmpty()){
|
|
|
+// return;
|
|
|
+// }
|
|
|
+//
|
|
|
+// //首次订购
|
|
|
+// list.forEach(it -> {
|
|
|
+// if (it.getVipUserRecord().isFirst()) {
|
|
|
+// addAmount(it);
|
|
|
+// }
|
|
|
+// });
|
|
|
+//
|
|
|
+// //持续订购
|
|
|
+// List<WalletReceipts> keep = list.stream().map(it -> {
|
|
|
+// if (!it.getVipUserRecord().isFirst()) {
|
|
|
+// return it;
|
|
|
+// }
|
|
|
+// return null;
|
|
|
+// }).collect(Collectors.toList());
|
|
|
+//
|
|
|
+// if (keep.isEmpty()){
|
|
|
+// return;
|
|
|
+// }
|
|
|
+//
|
|
|
+// keep.forEach(it->{
|
|
|
+// boolean cancel = vipUserRecordDao.existsByCpIdAndSucIn(it.getUserId(), List.of(-1, 5));
|
|
|
+// if (!cancel){//无退订消息
|
|
|
+// 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(it);
|
|
|
+// }else {
|
|
|
+// cancel(it);
|
|
|
+// }
|
|
|
+// }
|
|
|
+// });
|
|
|
+//
|
|
|
+// }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 生成持续订购返利单
|
|
|
+ */
|
|
|
+ @Transactional
|
|
|
+ public void autoReceipts(){
|
|
|
+ //获取设置持续订购返利的套餐规则
|
|
|
+ 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()+5*24*60*60*1000;
|
|
|
+ List<WalletReceipts> walletReceiptsList = walletReceiptsDao.listMonthBySetMealCode(startTime, endTime, setMealCodeList);
|
|
|
+ if (walletReceiptsList.isEmpty()){
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
+ rulerList.forEach(ruler->{
|
|
|
+ walletReceiptsList.forEach(walletReceipts -> {
|
|
|
+ //匹配规则
|
|
|
+ if (walletReceipts.getOutTradeNo().equals(ruler.getSetMealCode())){
|
|
|
+ //上月至本月5号最新消息
|
|
|
+ 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)){
|
|
|
+ //上月最后消息为订购,生成入账(若本月已生成持续返利则不自动生成)
|
|
|
+ WalletReceipts noCancelByCreateTime = walletReceiptsDao.findNoCancelByCreateTime(walletReceipts.getUserId(), DateUtils.getCurrentMonthStartTime(), new Date().getTime());
|
|
|
+ if (ObjectUtils.isEmpty(noCancelByCreateTime)){
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ insertReceipts(ruler, walletReceipts);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ });
|
|
|
+ });
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ public void testAutoReceipts(Integer month){
|
|
|
+ //获取设置持续订购返利的套餐规则
|
|
|
+ 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.getMonthStartTime(2024, month);
|
|
|
+ Long endTime = DateUtils.lastMonthEndTime()+5*24*60*60*1000;
|
|
|
+ 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)){
|
|
|
+ //上月最后消息为订购,生成入账
|
|
|
+ WalletReceipts noCancelByCreateTime = walletReceiptsDao.findNoCancelByCreateTime(walletReceipts.getUserId(), DateUtils.getCurrentMonthStartTime(), new Date().getTime());
|
|
|
+ if (ObjectUtils.isEmpty(noCancelByCreateTime)){
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ insertReceipts(ruler, walletReceipts);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ });
|
|
|
+ });
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ 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());
|
|
|
+ walletReceipts.setRuler(ruler);
|
|
|
+ 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());
|
|
|
+ walletDao.updateIncAmount(it.getWallet().getId(), it.getTotal());
|
|
|
+ walletDao.updateIncWaitAmount(it.getWallet().getId(), it.getTotal().negate());
|
|
|
+ walletReceiptsDao.save(it);
|
|
|
+ }
|
|
|
+
|
|
|
+ private void cancel(WalletReceipts it){
|
|
|
+ it.setStatus(ReceiptsStatus.CANCEL);
|
|
|
+ Wallet wallet = it.getWallet();
|
|
|
+ wallet.setWaitAmount(wallet.getWaitAmount().subtract(it.getTotal()));
|
|
|
+ walletDao.save(wallet);
|
|
|
+ walletReceiptsDao.save(it);
|
|
|
+ }
|
|
|
+}
|