admin
2025-02-25 30d8e227e8d823b6c38c3b9c90ac2df03b63befe
fanli/src/main/java/com/yeshi/fanli/service/impl/order/OrderMoneySettleServiceImpl.java
@@ -13,6 +13,9 @@
import javax.annotation.Resource;
import com.yeshi.fanli.util.mq.rabbit.RabbitmqManager;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
@@ -64,6 +67,8 @@
@Service
public class OrderMoneySettleServiceImpl implements OrderMoneySettleService {
    Logger logger = LoggerFactory.getLogger(OrderMoneySettleService.class);
    @Resource
    private HongBaoV2Mapper hongBaoV2Mapper;
@@ -111,9 +116,20 @@
    @Resource
    private RocketMQManager rocketMQManager;
    @Resource
    private HongBaoRecieveCMQManager hongBaoRecieveCMQManager;
    // 下级被封禁,红包失效
    private void invalidHongBaoForbidden(Long id) {
        HongBaoV2 updateHongBaoV2 = new HongBaoV2(id);
    private void invalidHongBaoForbidden(HongBaoV2 hongBaoV2) throws TeamRewardDebtException {
        if (hongBaoV2.getState() == HongBaoV2.STATE_SHIXIAO || hongBaoV2.getState() == HongBaoV2.STATE_YILINGQU || hongBaoV2.getState() == HongBaoV2.STATE_LINGQUING)
            return;
        //如果在偿还里面就先偿还,然后再失效
        TeamRewardDebt debt = teamRewardDebtService.selectBySourceId(hongBaoV2.getId());
        if (debt != null &&debt.getLeftMoney().compareTo(new BigDecimal(0))>0  && debt.getLeftMoney().compareTo(hongBaoV2.getMoney()) <= 0) {
            teamRewardDebtService.repayDebt(debt.getId(), hongBaoV2.getMoney(), "封禁失效,先偿还再失效");
        }
        HongBaoV2 updateHongBaoV2 = new HongBaoV2(hongBaoV2.getId());
        updateHongBaoV2.setState(HongBaoV2.STATE_SHIXIAO);
        updateHongBaoV2.setBeizhu("封禁失效");
        updateHongBaoV2.setUpdateTime(new Date());
@@ -125,7 +141,7 @@
    public void inviteSettleTB(Long uid, Date maxPregetTime) throws OrderMoneySettleException {
        // 查询UID的一二级邀请赚
        int sourceType = Constant.SOURCE_TYPE_TAOBAO;
        inviteSettle(uid,sourceType,maxPregetTime);
        inviteSettle(uid, sourceType, maxPregetTime);
    }
    @Transactional(rollbackFor = Exception.class)
@@ -133,7 +149,7 @@
    public void inviteSettleJD(Long uid, Date maxPregetTime) throws OrderMoneySettleException {
        // 查询UID的一二级邀请赚
        int sourceType = Constant.SOURCE_TYPE_JD;
        inviteSettle(uid,sourceType,maxPregetTime);
        inviteSettle(uid, sourceType, maxPregetTime);
    }
    @Transactional(rollbackFor = Exception.class)
@@ -141,7 +157,7 @@
    public void inviteSettlePDD(Long uid, Date maxPregetTime) throws OrderMoneySettleException {
        // 查询UID的一二级邀请赚
        int sourceType = Constant.SOURCE_TYPE_PDD;
        inviteSettle(uid,sourceType,maxPregetTime);
        inviteSettle(uid, sourceType, maxPregetTime);
    }
    @Transactional(rollbackFor = Exception.class)
@@ -149,7 +165,14 @@
    public void inviteSettleVipShop(Long uid, Date maxPregetTime) throws OrderMoneySettleException {
        // 查询UID的一二级邀请赚
        int sourceType = Constant.SOURCE_TYPE_VIP;
        inviteSettle(uid,sourceType,maxPregetTime);
        inviteSettle(uid, sourceType, maxPregetTime);
    }
    @Override
    public void inviteSettleDY(Long uid, Date maxPregetTime) throws OrderMoneySettleException {
        // 查询UID的一二级邀请赚
        int sourceType = Constant.SOURCE_TYPE_DY;
        inviteSettle(uid, sourceType, maxPregetTime);
    }
@@ -158,7 +181,7 @@
    public void inviteSettleSuning(Long uid, Date maxPregetTime) throws OrderMoneySettleException {
        // 查询UID的一二级邀请赚
        int sourceType = Constant.SOURCE_TYPE_SUNING;
        inviteSettle(uid,sourceType,maxPregetTime);
        inviteSettle(uid, sourceType, maxPregetTime);
    }
@@ -190,7 +213,12 @@
                        // 上级用户不是正常用户,订单均不能到账
                        UserInfo userInfo = userInfoMapper.selectAvailableByPrimaryKey(co.getUserInfo().getId());
                        if (userInfo == null || userInfo.getState() != UserInfo.STATE_NORMAL) {
                            invalidHongBaoForbidden(item.getId());
                            try {
                                invalidHongBaoForbidden(item);
                            } catch (TeamRewardDebtException e) {
                                e.printStackTrace();
                                throw new OrderMoneySettleException(10, "偿还出错");
                            }
                            hongBaoList.remove(i);
                            i--;
                        }
@@ -260,7 +288,7 @@
                @Override
                public TransactionStatus execute(Message arg0, Object arg1) {
                    try {
                        fanLiShareTB(hongBaoList, uid, taskKey);
                        fanLiShareTB(hongBaoList, uid, taskKey,maxPregetTime);
                    } catch (TaoBaoWeiQuanException e) {
                        return TransactionStatus.RollbackTransaction;
                    }
@@ -304,7 +332,7 @@
            orderTransactionProducer.send(msg, new LocalTransactionExecuter() {
                @Override
                public TransactionStatus execute(Message arg0, Object arg1) {
                    fanLiShareOther(hongBaoList, uid, sourceType, taskKey);
                    fanLiShareOther(hongBaoList, uid, sourceType, taskKey,maxPregetTime);
                    return TransactionStatus.CommitTransaction;
                }
            }, null);
@@ -329,6 +357,11 @@
    @Override
    public void shareSettleSuning(Long uid, Date maxPregetTime) throws OrderMoneySettleException {
        shareSettle(Constant.SOURCE_TYPE_SUNING, uid, maxPregetTime);
    }
    @Override
    public void shareSettleDY(Long uid, Date maxPregetTime) throws OrderMoneySettleException {
        shareSettle(Constant.SOURCE_TYPE_DY, uid, maxPregetTime);
    }
    private void shareSettle(int sourceType, Long uid, Date maxPregetTime) throws OrderMoneySettleException {
@@ -360,7 +393,7 @@
            orderTransactionProducer.send(msg, new LocalTransactionExecuter() {
                @Override
                public TransactionStatus execute(Message arg0, Object arg1) {
                    fanLiShareOther(hongBaoList, uid, sourceType, taskKey);
                    fanLiShareOther(hongBaoList, uid, sourceType, taskKey,maxPregetTime);
                    return TransactionStatus.CommitTransaction;
                }
            }, null);
@@ -380,6 +413,9 @@
        Map<Long, Integer> userGoodsCount = new HashMap<>();
        for (CommonOrder co : orderList) {
            HongBaoOrder hongBaoOrder = hongBaoOrderMapper.selectByCommonOrderId(co.getId());
            if (hongBaoOrder == null) {
                continue;
            }
            hongBaoList.add(hongBaoOrder.getHongBaoV2());
            Long uid = co.getUserInfo().getId();
            if (userGoodsCount.get(uid) == null)
@@ -465,7 +501,7 @@
                hbIdList.add(v2.getId());
                accountDetailsHongBaoMapService.saveAccountDetailsHongBaoMap(v2.getId(), userMoneyDetail.getId());
                try {
                    HongBaoRecieveCMQManager.getInstance().addHongBaoRecieveMsg(v2.getId());
                    hongBaoRecieveCMQManager.addHongBaoRecieveMsg(v2.getId());
                } catch (Exception e) {
                    LogHelper.errorDetailInfo(e);
                }
@@ -518,6 +554,7 @@
    private void fanliInvite(List<HongBaoV2> hongBaoList, Long uid, int sourceType, String key, Date maxPreGetTime)
            throws TeamEincomeRecordException, ParamsException, TeamRewardDebtException {
        logger.info("邀请奖金预到账:uid-{} sourceType-{} key-{}", new Object[]{uid, sourceType, key});
        List<Long> hbIdList = new ArrayList<>();
        BigDecimal invitemoney = new BigDecimal(0);
        Set<String> inviteOrders = new HashSet<>();
@@ -529,15 +566,17 @@
                continue;
            hongBao = hongBaoV2Mapper.selectByPrimaryKeyForUpdate(hongBao.getId());
            if (hongBao.getState() == HongBaoV2.STATE_BUKELINGQU || hongBao.getState() == HongBaoV2.STATE_KELINGQU) {
                logger.info("邀请奖金预到账-每个红包-到账开始:hongbaoid-{}", hongBao.getId());
                TeamRewardDebt debt = teamRewardDebtService.selectBySourceId(hongBao.getId());
                BigDecimal money = hongBao.getMoney();
                if (debt != null && debt.getLeftMoney().compareTo(new BigDecimal(0)) > 0
                        && money.compareTo(debt.getLeftMoney()) >= 0) {
                    money = money.subtract(debt.getLeftMoney());
                    // 还钱
                    teamRewardDebtService.repayDebt(debt.getId(), debt.getLeftMoney());
                    teamRewardDebtService.repayDebt(debt.getId(), debt.getLeftMoney(), null);
                    logger.info("邀请奖金预到账-每个红包-还款:hongbaoid-{} debtId-{} debtMoney-{}", new Object[]{hongBao.getId(), debt.getId(), debt.getLeftMoney()});
                }
                logger.info("邀请奖金预到账-每个红包:hongbaoid-{} money-{}", new Object[]{hongBao.getId(), money});
                invitemoney = invitemoney.add(money);
                HongBaoV2 updateHongBao = new HongBaoV2();
                updateHongBao.setId(hongBao.getId());
@@ -549,6 +588,7 @@
                hbIdList.add(hongBao.getId());
                HongBaoOrder hongBaoOrder = hongBaoOrderMapper.selectByHongBaoId(hongBao.getParent().getId());
                inviteOrders.add(hongBaoOrder.getCommonOrder().getOrderNo());
                logger.info("邀请奖金预到账-每个红包-到账成功:hongbaoid-{}", hongBao.getId());
            }
        }
@@ -556,6 +596,7 @@
        if (invitemoney.compareTo(new BigDecimal(0)) > 0) {
            // 获取当前的月份
            teamRewardManager.addToEincome(uid, maxPreGetTime, invitemoney, sourceType);
            logger.info("邀请奖金预到账-添加到团队收益成功:uid-{} day-{} money-{} sourceType-{}", new Object[]{uid, maxPreGetTime, invitemoney, sourceType});
        }
        hongBaoV2SettleTempService.addTemp(hbIdList, key);
@@ -564,7 +605,7 @@
        for (Long hongBaoId : hbIdList) {
            try {
                if (Constant.ENABLE_MQ)
                    HongBaoRecieveCMQManager.getInstance().addHongBaoRecieveMsg(hongBaoId);
                    hongBaoRecieveCMQManager.addHongBaoRecieveMsg(hongBaoId);
            } catch (Exception e) {
                LogHelper.errorDetailInfo(e);
            }
@@ -578,7 +619,7 @@
     * @param uid
     * @throws TaoBaoWeiQuanException
     */
    private void fanLiShareTB(List<HongBaoV2> hongBaoList, Long uid, String key) throws TaoBaoWeiQuanException {
    private void fanLiShareTB(List<HongBaoV2> hongBaoList, Long uid, String key,Date recieveMonth) throws TaoBaoWeiQuanException {
        BigDecimal sharemoney = new BigDecimal(0);
        List<Long> hbIdList = new ArrayList<>();
        Set<String> drawBackOrders = new HashSet<String>();
@@ -623,7 +664,7 @@
            // 添加新版详情记录
            try {
                UserMoneyDetail userMoneyDetail = UserMoneyDetailFactory.createShare(uid, Constant.SOURCE_TYPE_TAOBAO,
                        sharemoney, new Date());
                        sharemoney,recieveMonth);
                // 添加资金
                userMoneyService.addUserMoney(uid, sharemoney, userMoneyDetail);
@@ -653,7 +694,7 @@
        for (Long hongBaoId : recieveHongBaoIds) {
            try {
                if (Constant.ENABLE_MQ)
                    HongBaoRecieveCMQManager.getInstance().addHongBaoRecieveMsg(hongBaoId);
                    hongBaoRecieveCMQManager.addHongBaoRecieveMsg(hongBaoId);
            } catch (Exception e) {
                LogHelper.errorDetailInfo(e);
            }
@@ -668,7 +709,7 @@
     * @param uid
     * @param sourceType
     */
    private void fanLiShareOther(List<HongBaoV2> hongBaoList, Long uid, int sourceType, String key) {
    private void fanLiShareOther(List<HongBaoV2> hongBaoList, Long uid, int sourceType, String key,Date recieveMonth) {
        BigDecimal sharemoney = new BigDecimal(0);
        List<Long> hbIdList = new ArrayList<>();
        List<Long> recieveHongBaoIds = new ArrayList<>();
@@ -704,7 +745,7 @@
            // 添加新版详情记录
            try {
                UserMoneyDetail userMoneyDetail = UserMoneyDetailFactory.createShare(uid, sourceType, sharemoney,
                        new Date());
                        recieveMonth);
                // 添加资金
                userMoneyService.addUserMoney(uid, sharemoney, userMoneyDetail);
@@ -730,7 +771,7 @@
        for (Long hongBaoId : recieveHongBaoIds) {
            try {
                if (Constant.ENABLE_MQ)
                    HongBaoRecieveCMQManager.getInstance().addHongBaoRecieveMsg(hongBaoId);
                    hongBaoRecieveCMQManager.addHongBaoRecieveMsg(hongBaoId);
            } catch (Exception e) {
                LogHelper.errorDetailInfo(e);
            }