|
|
@@ -3,6 +3,7 @@ package com.cyksj.service.order.impl;
|
|
|
import cn.hutool.core.collection.CollUtil;
|
|
|
import cn.hutool.core.date.DateTime;
|
|
|
import cn.hutool.core.date.DateUtil;
|
|
|
+import cn.hutool.core.util.ObjectUtil;
|
|
|
import cn.hutool.core.util.StrUtil;
|
|
|
import com.alipay.api.AlipayApiException;
|
|
|
import com.alipay.api.AlipayClient;
|
|
|
@@ -23,6 +24,7 @@ import com.cyksj.config.zfb.ZfbProductCode;
|
|
|
import com.cyksj.enums.GatewayApiCode;
|
|
|
import com.cyksj.mapper.*;
|
|
|
import com.cyksj.mapper.channel.ShopConfigMapper;
|
|
|
+import com.cyksj.mapper.manage.distribute.*;
|
|
|
import com.cyksj.mapper.sys.SysConfigMapper;
|
|
|
import com.cyksj.mapper.vip.VipGoodsSkuMapper;
|
|
|
import com.cyksj.mapper.vip.VipOrderDonDeductRelationMapper;
|
|
|
@@ -54,6 +56,7 @@ import org.springframework.dao.DuplicateKeyException;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
|
|
|
import java.math.BigDecimal;
|
|
|
+import java.math.RoundingMode;
|
|
|
import java.util.Date;
|
|
|
import java.util.List;
|
|
|
import java.util.Map;
|
|
|
@@ -116,6 +119,20 @@ public class VipOrderDonServiceImpl extends ServiceImpl<VipOrderDonMapper, VipOr
|
|
|
|
|
|
private final ClaudeService claudeService;
|
|
|
|
|
|
+ private final UserDistributeMapper userDistributeMapper;
|
|
|
+
|
|
|
+ private final UserDistributeSharedMapper userDistributeSharedMapper;
|
|
|
+
|
|
|
+ private final DistributePointsExchangeMoneyMapper distributePointsExchangeMoneyMapper;
|
|
|
+
|
|
|
+ private final DistributeWaitingSendPointsMapper distributeWaitingSendPointsMapper;
|
|
|
+
|
|
|
+ private final GoodsDonMapper goodsDonMapper;
|
|
|
+
|
|
|
+ private final UserPlatDistributeRateMapper userPlatDistributeRateMapper;
|
|
|
+
|
|
|
+ private final BusinessDistributeDefaultRateConfigMapper businessDistributeDefaultRateConfigMapper;
|
|
|
+
|
|
|
@Override
|
|
|
public VipOrderDon submit(VipOrderDonPayReq payRequest) {
|
|
|
Long userId = payRequest.getUserId();
|
|
|
@@ -364,6 +381,10 @@ public class VipOrderDonServiceImpl extends ServiceImpl<VipOrderDonMapper, VipOr
|
|
|
//续费订单
|
|
|
setTicketToVipExpiryTime(userIdList, vipUser.getUserId(), vipUser.getExpiryTime());
|
|
|
}
|
|
|
+ //金额大于0 是否是分销订单
|
|
|
+ if (orderDon.getMoney().compareTo(BigDecimal.ZERO) > 0) {
|
|
|
+ handlerDistributeOrder(orderDon);
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
private void sendVipTicketToUser(Long userId, Date vipExpiryTime, List<Long> userIds, Long orderId) {
|
|
|
@@ -578,4 +599,106 @@ public class VipOrderDonServiceImpl extends ServiceImpl<VipOrderDonMapper, VipOr
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 处理会员分销
|
|
|
+ */
|
|
|
+ private void handlerDistributeOrder(VipOrderDon orderDon) {
|
|
|
+ Long userId = orderDon.getUserId();
|
|
|
+ UserDistributeShared distributeShared = userDistributeSharedMapper.selectOne(Wrappers.lambdaQuery(UserDistributeShared.class)
|
|
|
+ .eq(UserDistributeShared::getUserId, userId)
|
|
|
+ .last("limit 1"));
|
|
|
+ if (distributeShared == null && !distributeShared.getIsBind()) {
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ if (!distributeShared.getIsBind()) {
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ Long sharedId = distributeShared.getSharedId();
|
|
|
+ UserDistribute userDistribute = userDistributeMapper.selectOne(Wrappers.lambdaQuery(UserDistribute.class)
|
|
|
+ .eq(UserDistribute::getUserId, sharedId)
|
|
|
+ .eq(UserDistribute::getDeleted, Boolean.TRUE)
|
|
|
+ .last("limit 1"));
|
|
|
+ if (userDistribute == null) {
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ Long goodsId = Constant.VIP_RELATE_GOODS_ID;
|
|
|
+ GoodsDon goodsDon = goodsDonMapper.selectById(goodsId);
|
|
|
+ if (goodsDon == null) {
|
|
|
+ log.info("会员分销关联平台不存在,不分销");
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ BigDecimal points = null;
|
|
|
+ BigDecimal returnMoney = null;
|
|
|
+ BigDecimal orderMoney = orderDon.getMoney();
|
|
|
+ DistributePointsExchangeMoney distributePointsExchangeMoney = distributePointsExchangeMoneyMapper.selectOne(Wrappers.lambdaQuery(DistributePointsExchangeMoney.class).orderByDesc(DistributePointsExchangeMoney::getPoints).last("limit 1"));
|
|
|
+ if (distributePointsExchangeMoney == null) {
|
|
|
+ log.info("分销兑换积分比例未配置");
|
|
|
+ } else {
|
|
|
+ //分销比例
|
|
|
+ Integer rate = 0;
|
|
|
+ if (userDistribute == null) {
|
|
|
+ //普通分销者 是否享受过该用户的首单分销
|
|
|
+ DistributeWaitingSendPoints isReceived = distributeWaitingSendPointsMapper.selectOne(Wrappers.lambdaQuery(DistributeWaitingSendPoints.class)
|
|
|
+ .eq(DistributeWaitingSendPoints::getSharedId, sharedId)
|
|
|
+ .eq(DistributeWaitingSendPoints::getUserId, userId)
|
|
|
+ .in(DistributeWaitingSendPoints::getSendStatus, List.of(DistributeWaitingSendPoints.Status.waiting.name(), DistributeWaitingSendPoints.Status.success.name()))
|
|
|
+ .last("limit 1"));
|
|
|
+ if (isReceived != null) {
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ rate = goodsDon.getRate();
|
|
|
+ //特定普通渠道 所有平台比例改成10%
|
|
|
+ if (ObjectUtil.equal(sharedId, Constant.SPECIAL_DISTRIBUTE_USER_ID) && rate > 0) {
|
|
|
+ rate = 10;
|
|
|
+ }
|
|
|
+ returnMoney = orderMoney.multiply(BigDecimal.valueOf(rate)).divide(BigDecimal.valueOf(100));
|
|
|
+ } else {
|
|
|
+ //固定推广分销 平台比例
|
|
|
+ UserPlatDistributeRate userPlatDistributeRate = userPlatDistributeRateMapper.selectOne(Wrappers.lambdaQuery(UserPlatDistributeRate.class)
|
|
|
+ .eq(UserPlatDistributeRate::getGoodsId, goodsId)
|
|
|
+ .eq(UserPlatDistributeRate::getDistributeId, userDistribute.getId())
|
|
|
+ .last("limit 1"));
|
|
|
+ ;
|
|
|
+ //未配置平台分销比例 寻找默认比例
|
|
|
+ if (userPlatDistributeRate == null) {
|
|
|
+ BusinessDistributeDefaultRateConfig defaultRateConfig = businessDistributeDefaultRateConfigMapper.selectOne(Wrappers.lambdaQuery(BusinessDistributeDefaultRateConfig.class)
|
|
|
+ .eq(BusinessDistributeDefaultRateConfig::getGoodsId, goodsId)
|
|
|
+ .last("limit 1"));
|
|
|
+ if (defaultRateConfig != null) {
|
|
|
+ userPlatDistributeRate = getUserPlatDistributeRate(defaultRateConfig.getRate(), defaultRateConfig.getSecondRate());
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if (userPlatDistributeRate != null) {
|
|
|
+ rate = userPlatDistributeRate.getRate();
|
|
|
+ returnMoney = orderMoney.multiply(BigDecimal.valueOf(rate)).divide(BigDecimal.valueOf(100));
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if (returnMoney != null) {
|
|
|
+ BigDecimal returnPoints = returnMoney.multiply(BigDecimal.valueOf(distributePointsExchangeMoney.getPoints())).divide(distributePointsExchangeMoney.getMoney(), 2, RoundingMode.HALF_DOWN);
|
|
|
+ points = returnPoints;
|
|
|
+ }
|
|
|
+
|
|
|
+ if (points != null && points.compareTo(BigDecimal.ZERO) > 0) {
|
|
|
+ log.info("用户{}邀请{},产生会员分销订单分发积分记录入库", sharedId, userId);
|
|
|
+ DistributeWaitingSendPoints distributeWaitingSendPoints = new DistributeWaitingSendPoints();
|
|
|
+ distributeWaitingSendPoints.setSharedId(sharedId);
|
|
|
+ distributeWaitingSendPoints.setUserId(userId);
|
|
|
+ distributeWaitingSendPoints.setIsVip(Boolean.TRUE);
|
|
|
+ distributeWaitingSendPoints.setOrderId(orderDon.getId());
|
|
|
+ distributeWaitingSendPoints.setPoints(points);
|
|
|
+ distributeWaitingSendPoints.setSendStatus(DistributeWaitingSendPoints.Status.waiting);
|
|
|
+ distributeWaitingSendPoints.setSendTime(DateUtil.offsetDay(orderDon.getPayTime(), 3));
|
|
|
+ distributeWaitingSendPoints.setRate(rate);
|
|
|
+ distributeWaitingSendPointsMapper.insert(distributeWaitingSendPoints);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ public UserPlatDistributeRate getUserPlatDistributeRate(Integer rate, Integer secondRate) {
|
|
|
+ UserPlatDistributeRate userPlatDistributeRate = new UserPlatDistributeRate();
|
|
|
+ userPlatDistributeRate.setRate(rate);
|
|
|
+ userPlatDistributeRate.setSecondRate(secondRate);
|
|
|
+ return userPlatDistributeRate;
|
|
|
+ }
|
|
|
}
|