|
@@ -0,0 +1,322 @@
|
|
|
+package com.zhongshu.reward.server.core.service;
|
|
|
+
|
|
|
+import com.github.microservice.auth.security.helper.AuthHelper;
|
|
|
+import com.github.microservice.core.util.result.content.ResultContent;
|
|
|
+import com.github.microservice.core.util.result.content.ResultState;
|
|
|
+import com.zhongshu.reward.client.model.VipUserModel;
|
|
|
+import com.zhongshu.reward.client.model.wallet.WalletReceiptsModel;
|
|
|
+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.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;
|
|
|
+import org.springframework.beans.BeanUtils;
|
|
|
+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.ArrayList;
|
|
|
+import java.util.Date;
|
|
|
+import java.util.List;
|
|
|
+import java.util.Objects;
|
|
|
+import java.util.stream.Collectors;
|
|
|
+
|
|
|
+/**
|
|
|
+ * @author wjf
|
|
|
+ * @date 2024/8/6
|
|
|
+ */
|
|
|
+@Service
|
|
|
+@Slf4j
|
|
|
+public class WalletReceiptsService {
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ WalletDao walletDao;
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ WalletService walletService;
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ WalletReceiptsDao walletReceiptsDao;
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ VipUserRecordDao vipUserRecordDao;
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ InviteRecordDao inviteRecordDao;
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ AuthHelper authHelper;
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ InviteReceiptsRoleFeignService inviteReceiptsRoleFeignService;
|
|
|
+
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 邀请返利入账
|
|
|
+ * @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.setFirst(false);
|
|
|
+ //订阅
|
|
|
+ //查询邀请关系
|
|
|
+ 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);
|
|
|
+ //订阅入账
|
|
|
+ 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(rulerVo.getBaseTotal());
|
|
|
+ walletReceipts.setEstimatedTime(DateUtils.nextMonthDayStartTime(rulerVo.getDay()));
|
|
|
+ walletReceipts.setVipUserRecord(vipUserRecord);
|
|
|
+ //邀请人钱包
|
|
|
+ walletReceipts.setStatus(ReceiptsStatus.WAIT);
|
|
|
+ wallet.setWaitAmount(wallet.getWaitAmount().add(rulerVo.getBaseTotal()));
|
|
|
+ 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);
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 结算
|
|
|
+ */
|
|
|
+ public void settle(){
|
|
|
+ Long startTime = DateUtils.lastMonthStartTime();
|
|
|
+ Long endTime = DateUtils.lastMonthEndTime();
|
|
|
+ List<WalletReceipts> list = walletReceiptsDao.listMonth(startTime, endTime);
|
|
|
+
|
|
|
+ List<WalletReceipts> receipts = new ArrayList<>();
|
|
|
+ List<Wallet> wallets = new ArrayList<>();
|
|
|
+
|
|
|
+ //首次订购
|
|
|
+ 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());
|
|
|
+
|
|
|
+ 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);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ });
|
|
|
+ walletDao.saveAll(wallets);
|
|
|
+ walletReceiptsDao.saveAll(receipts);
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ 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();
|
|
|
+ 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 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()));
|
|
|
+ walletDao.save(wallet);
|
|
|
+ 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);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 取消返利入账
|
|
|
+ * @param
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ @Transactional
|
|
|
+ public Object cancelReceipts(String outTradeNo){
|
|
|
+
|
|
|
+ WalletReceipts walletReceipts = walletReceiptsDao.findTopByOutTradeNo(outTradeNo);
|
|
|
+ if (ObjectUtils.isEmpty(walletReceipts)){
|
|
|
+ return ResultContent.build(ResultState.Fail,"返利订单不存在或未入账");
|
|
|
+ }
|
|
|
+ if (!walletReceipts.getStatus().equals(ReceiptsStatus.WAIT)){
|
|
|
+ return ResultContent.build(ResultState.Fail,"订单不处于未结算状态");
|
|
|
+ }
|
|
|
+
|
|
|
+ walletReceipts.setStatus(ReceiptsStatus.CANCEL);
|
|
|
+ walletReceiptsDao.save(walletReceipts);
|
|
|
+ Wallet wallet = walletDao.findTop1ById(walletReceipts.getWallet().getId());
|
|
|
+ wallet.setWaitAmount(wallet.getWaitAmount().subtract(walletReceipts.getTotal()));
|
|
|
+ walletDao.save(wallet);
|
|
|
+ return ResultContent.build(ResultState.Success);
|
|
|
+ }
|
|
|
+
|
|
|
+ public Object listByWallet(){
|
|
|
+ String userId = authHelper.getCurrentUser().getUserId();
|
|
|
+ Wallet wallet = walletService.getWalletByUserId(userId);
|
|
|
+ List<WalletReceipts> list = walletReceiptsDao.findByWallet_IdOrderByCreateTimeDesc(wallet.getId());
|
|
|
+ return ResultContent.buildContent(list.stream().map(this::toModel).collect(Collectors.toList()));
|
|
|
+ }
|
|
|
+
|
|
|
+ public Object receiptsDetail(String receiptsId){
|
|
|
+ WalletReceipts walletReceipts = walletReceiptsDao.findTop1ById(receiptsId);
|
|
|
+ if (ObjectUtils.isEmpty(walletReceipts)){
|
|
|
+ return ResultContent.build(ResultState.Fail, "记录不存在");
|
|
|
+ }
|
|
|
+ VipUserRecord vipUserRecord = walletReceipts.getVipUserRecord();
|
|
|
+ VipUserModel vipUserModel = new VipUserModel();
|
|
|
+ BeanUtils.copyProperties(vipUserRecord, vipUserModel);
|
|
|
+ BeanUtils.copyProperties(walletReceipts, vipUserModel);
|
|
|
+ InviteRecord inviteRecord = inviteRecordDao.findTopByUserIdOrderByCreateTime(walletReceipts.getUserId());
|
|
|
+ if (ObjectUtils.isNotEmpty(inviteRecord)){
|
|
|
+ vipUserModel.setReceiptsTime(inviteRecord.getRegisterTime());
|
|
|
+ vipUserModel.setPhone(inviteRecord.getPhone());
|
|
|
+ }
|
|
|
+ return ResultContent.buildContent(vipUserModel);
|
|
|
+ }
|
|
|
+
|
|
|
+ public Object rewardDetail(Integer year, Integer month){
|
|
|
+ String userId = authHelper.getCurrentUser().getUserId();
|
|
|
+ Long monthStartTime = DateUtils.getMonthStartTime(year, month);
|
|
|
+ Long monthEndTime = DateUtils.getMonthEndTime(year, month);
|
|
|
+ List<WalletReceipts> walletReceipts = walletReceiptsDao.inviteDetail(userId, monthStartTime, monthEndTime);
|
|
|
+ return ResultContent.buildContent(walletReceipts.stream().map(this::toVipUserModel).collect(Collectors.toList()));
|
|
|
+ }
|
|
|
+
|
|
|
+ private VipUserModel toVipUserModel(WalletReceipts walletReceipts) {
|
|
|
+ VipUserModel vipUserModel = new VipUserModel();
|
|
|
+ if (ObjectUtils.isNotEmpty(walletReceipts)){
|
|
|
+ VipUserRecord vipUserRecord = walletReceipts.getVipUserRecord();
|
|
|
+ BeanUtils.copyProperties(vipUserRecord, vipUserModel);
|
|
|
+ BeanUtils.copyProperties(walletReceipts, vipUserModel);
|
|
|
+ }
|
|
|
+ return vipUserModel;
|
|
|
+ }
|
|
|
+
|
|
|
+ WalletReceiptsModel toModel(WalletReceipts walletReceipts){
|
|
|
+ WalletReceiptsModel model = new WalletReceiptsModel();
|
|
|
+ if (null != walletReceipts){
|
|
|
+ BeanUtils.copyProperties(walletReceipts, model, "wallet");
|
|
|
+ model.setWalletId(walletReceipts.getWallet().getId());
|
|
|
+ }
|
|
|
+ return model;
|
|
|
+ }
|
|
|
+}
|