KKSU
2024-12-23 8d326b853cd3f2d2ca5f9a70b343389da63d9af6
src/main/java/cc/mrbird/febs/mall/service/impl/ApiMallMemberWalletServiceImpl.java
@@ -1,14 +1,12 @@
package cc.mrbird.febs.mall.service.impl;
import cc.mrbird.febs.common.enumerates.FlowTypeEnum;
import cc.mrbird.febs.common.enumerates.MoneyFlowTypeEnum;
import cc.mrbird.febs.common.exception.FebsException;
import cc.mrbird.febs.common.utils.LoginUserUtil;
import cc.mrbird.febs.mall.dto.CommissionChangeDto;
import cc.mrbird.febs.mall.entity.MallMember;
import cc.mrbird.febs.mall.entity.MallMemberWallet;
import cc.mrbird.febs.mall.mapper.DataDictionaryCustomMapper;
import cc.mrbird.febs.mall.mapper.MallMemberWalletMapper;
import cc.mrbird.febs.mall.service.IApiMallMemberService;
import cc.mrbird.febs.mall.service.IApiMallMemberWalletService;
import cc.mrbird.febs.mall.service.IMallMoneyFlowService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
@@ -19,6 +17,7 @@
import java.lang.reflect.Field;
import java.math.BigDecimal;
import java.util.Map;
/**
 * @author wzy
@@ -30,6 +29,7 @@
public class ApiMallMemberWalletServiceImpl extends ServiceImpl<MallMemberWalletMapper, MallMemberWallet> implements IApiMallMemberWalletService {
    private final IMallMoneyFlowService moneyFlowService;
    private final DataDictionaryCustomMapper dataDictionaryCustomMapper;
    @Override
    public void addBalance(BigDecimal amount, Long memberId) {
@@ -46,7 +46,7 @@
                flag = false;
            } else {
                if (i > 2) {
                    throw new FebsException("余额增加失败");
                    throw new FebsException("操作失败");
                }
            }
        }
@@ -61,7 +61,7 @@
            i++;
            MallMemberWallet wallet = this.baseMapper.selectWalletByMemberId(memberId);
            if (amount.compareTo(wallet.getBalance()) > 0) {
                throw new FebsException("余额不足");
                throw new FebsException("碳币不足");
            }
            wallet.setBalance(wallet.getBalance().subtract(amount));
@@ -71,7 +71,53 @@
                flag = false;
            } else {
                if (i > 2) {
                    throw new FebsException("余额支付失败");
                    throw new FebsException("操作失败");
                }
            }
        }
    }
    @Override
    public void addScore(BigDecimal amount, Long memberId) {
        int i = 0;
        boolean flag = true;
        while (flag) {
            i++;
            MallMemberWallet wallet = this.baseMapper.selectWalletByMemberId(memberId);
            wallet.setScore(wallet.getScore().add(amount));
            int result = this.baseMapper.updateScoreWithVersion(wallet);
            if (result > 0) {
                flag = false;
            } else {
                if (i > 2) {
                    throw new FebsException("操作失败");
                }
            }
        }
    }
    @Override
    public void reduceScore(BigDecimal amount, Long memberId) {
        int i = 0;
        boolean flag = true;
        while (flag) {
            i++;
            MallMemberWallet wallet = this.baseMapper.selectWalletByMemberId(memberId);
            if (amount.compareTo(wallet.getScore()) > 0) {
                throw new FebsException("碳积分不足");
            }
            wallet.setScore(wallet.getScore().subtract(amount));
            int result = this.baseMapper.updateScoreWithVersion(wallet);
            if (result > 0) {
                flag = false;
            } else {
                if (i > 2) {
                    throw new FebsException("操作失败");
                }
            }
        }
@@ -154,7 +200,7 @@
                declaredField.set(update, balance.add(amount));
            } catch (NoSuchFieldException | IllegalAccessException e) {
                e.printStackTrace();
                throw new FebsException("金额新增失败");
                throw new FebsException("操作失败");
            }
            int result = this.baseMapper.updateAmountWithVersion(update);
@@ -162,7 +208,7 @@
                flag = false;
            } else {
                if (i > 2) {
                    throw new FebsException("金额新增失败");
                    throw new FebsException("操作失败");
                }
            }
        }
@@ -177,7 +223,7 @@
    }
    @Override
    public int reduce(BigDecimal amount, Long memberId, String field) {
    public int reduce(BigDecimal amount, Long memberId, String field, Map<String, BigDecimal> map) {
        int i = 0;
        boolean flag = true;
@@ -195,7 +241,21 @@
                BigDecimal balance = (BigDecimal) declaredField.get(wallet);
                if (amount.compareTo(balance) > 0) {
                    return 2;
                    if (map == null) {
                        return 2;
                    }
                    // 判断 赠送积分,如果剩下赠送积分不等于0且小于amount, 则扣除所有赠送积分
                    if ("score".equals(field)) {
                        if (balance.compareTo(BigDecimal.ZERO) == 0) {
                            return 2;
                        }
                        amount = balance;
                        map.put("amount", amount);
                    } else {
                        return 2;
                    }
                }
                declaredField.set(update, balance.subtract(amount));
@@ -208,7 +268,7 @@
                flag = false;
            } else {
                if (i > 2) {
                    throw new FebsException("余额修改失败");
                    throw new FebsException("操作失败");
                }
            }
        }
@@ -216,32 +276,16 @@
    }
    @Override
    public int reduce(BigDecimal amount, Long memberId, String field) {
        return reduce(amount, memberId, field, null);
    }
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void commissionChange(CommissionChangeDto commissionChange) {
        MallMember member = LoginUserUtil.getLoginUser();
        // TODO 判断是否开启了划转
        int type;
        int flowType;
        this.reduce(commissionChange.getAmount(), member.getId(), "commission");
        // 佣金转余额
        if (commissionChange.getType() == 1) {
            type = MoneyFlowTypeEnum.COMMISSION_TO_BALANCE.getValue();
            flowType = FlowTypeEnum.BALANCE.getValue();
            this.add(commissionChange.getAmount(), member.getId(), "balance");
            // 佣金转竞猜积分
        } else if (commissionChange.getType() == 2){
            type = MoneyFlowTypeEnum.COMMISSION_TO_PRIZESCORE.getValue();
            flowType = FlowTypeEnum.PRIZE_SCORE.getValue();
            this.add(commissionChange.getAmount(), member.getId(), "prizeScore");
        } else {
            throw new FebsException("参数错误");
        }
        moneyFlowService.addMoneyFlow(member.getId(), commissionChange.getAmount().negate(), type, null,  FlowTypeEnum.COMMISSION.getValue());
        moneyFlowService.addMoneyFlow(member.getId(), commissionChange.getAmount(), type, null,  flowType);
    }
}