From a331c3be6000d3c81f88e0a564e2db63da99018b Mon Sep 17 00:00:00 2001
From: Administrator <15274802129@163.com>
Date: Wed, 21 May 2025 10:33:42 +0800
Subject: [PATCH] feat(score): 初始化积分设置页面的默认值

---
 src/main/java/cc/mrbird/febs/mall/service/impl/ApiMallMemberWalletServiceImpl.java |  255 +++++++++++++++++++++++++++++++++++++++++++++++++++
 1 files changed, 255 insertions(+), 0 deletions(-)

diff --git a/src/main/java/cc/mrbird/febs/mall/service/impl/ApiMallMemberWalletServiceImpl.java b/src/main/java/cc/mrbird/febs/mall/service/impl/ApiMallMemberWalletServiceImpl.java
index e2c2815..4ea020d 100644
--- a/src/main/java/cc/mrbird/febs/mall/service/impl/ApiMallMemberWalletServiceImpl.java
+++ b/src/main/java/cc/mrbird/febs/mall/service/impl/ApiMallMemberWalletServiceImpl.java
@@ -1,15 +1,35 @@
 package cc.mrbird.febs.mall.service.impl;
 
+import cc.mrbird.febs.common.enumerates.DataDictionaryEnum;
+import cc.mrbird.febs.common.enumerates.FlowTypeEnum;
+import cc.mrbird.febs.common.enumerates.MoneyFlowTypeEnum;
+import cc.mrbird.febs.common.enumerates.ScoreFlowTypeEnum;
 import cc.mrbird.febs.common.exception.FebsException;
+import cc.mrbird.febs.common.utils.LoginUserUtil;
+import cc.mrbird.febs.common.utils.MallUtils;
+import cc.mrbird.febs.mall.dto.CommissionChangeDto;
+import cc.mrbird.febs.mall.dto.ScoreSettingDto;
+import cc.mrbird.febs.mall.entity.DataDictionaryCustom;
+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 cn.hutool.core.util.ObjectUtil;
+import cn.hutool.core.util.StrUtil;
+import com.alibaba.fastjson.JSONObject;
 import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
 import lombok.RequiredArgsConstructor;
 import lombok.extern.slf4j.Slf4j;
 import org.springframework.stereotype.Service;
+import org.springframework.transaction.annotation.Transactional;
 
+import java.lang.reflect.Field;
 import java.math.BigDecimal;
+import java.math.RoundingMode;
+import java.util.Map;
 
 /**
  * @author wzy
@@ -20,6 +40,8 @@
 @RequiredArgsConstructor
 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) {
@@ -66,4 +88,237 @@
             }
         }
     }
+
+    @Override
+    public void addCommission(BigDecimal amount, Long memberId) {
+        int i = 0;
+        boolean flag = true;
+
+        while (flag) {
+            i++;
+            MallMemberWallet wallet = this.baseMapper.selectWalletByMemberId(memberId);
+            wallet.setCommission(wallet.getCommission().add(amount));
+
+            int result = this.baseMapper.updateCommissionWithVersion(wallet);
+            if (result > 0) {
+                flag = false;
+            } else {
+                if (i > 2) {
+                    throw new FebsException("余额增加失败");
+                }
+            }
+        }
+    }
+
+    public static void main(String[] args) throws NoSuchFieldException, IllegalAccessException {
+        Field commission = MallMemberWallet.class.getDeclaredField("commission");
+        MallMemberWallet wallet = new MallMemberWallet();
+        commission.setAccessible(true);
+        commission.set(wallet, BigDecimal.valueOf(12.34d));
+
+        System.out.println(1);
+    }
+
+    @Override
+    public void reduceCommission(BigDecimal amount, Long memberId) {
+        int i = 0;
+        boolean flag = true;
+
+        while (flag) {
+            i++;
+            MallMemberWallet wallet = this.baseMapper.selectWalletByMemberId(memberId);
+            if (amount.compareTo(wallet.getCommission()) > 0) {
+                throw new FebsException("佣金不足");
+            }
+
+            wallet.setCommission(wallet.getCommission().subtract(amount));
+
+            int result = this.baseMapper.updateCommissionWithVersion(wallet);
+            if (result > 0) {
+                flag = false;
+            } else {
+                if (i > 2) {
+                    throw new FebsException("佣金支付失败");
+                }
+            }
+        }
+    }
+
+    @Override
+    public void add(BigDecimal amount, Long memberId, String field) {
+        int i = 0;
+        boolean flag = true;
+
+        while (flag) {
+            i++;
+            MallMemberWallet wallet = this.baseMapper.selectWalletByMemberId(memberId);
+
+            MallMemberWallet update = new MallMemberWallet();
+            update.setId(wallet.getId());
+            update.setRevision(wallet.getRevision());
+
+            Field declaredField = null;
+            try {
+                declaredField = MallMemberWallet.class.getDeclaredField(field);
+                declaredField.setAccessible(true);
+
+                BigDecimal balance = (BigDecimal) declaredField.get(wallet);
+                declaredField.set(update, balance.add(amount));
+            } catch (NoSuchFieldException | IllegalAccessException e) {
+                e.printStackTrace();
+                throw new FebsException("金额新增失败");
+            }
+
+            int result = this.baseMapper.updateAmountWithVersion(update);
+            if (result > 0) {
+                flag = false;
+            } else {
+                if (i > 2) {
+                    throw new FebsException("金额新增失败");
+                }
+            }
+        }
+    }
+
+    @Override
+    public void add(BigDecimal amount, Long memberId, String... field) {
+        String[] fields = field.clone();
+        for (String s : fields) {
+            add(amount, memberId, s);
+        }
+    }
+
+    @Override
+    public int reduce(BigDecimal amount, Long memberId, String field, Map<String, BigDecimal> map) {
+        int i = 0;
+        boolean flag = true;
+
+        while (flag) {
+            i++;
+            MallMemberWallet wallet = this.baseMapper.selectWalletByMemberId(memberId);
+
+            MallMemberWallet update = new MallMemberWallet();
+            update.setId(wallet.getId());
+            update.setRevision(wallet.getRevision());
+
+            try {
+                Field declaredField = MallMemberWallet.class.getDeclaredField(field);
+                declaredField.setAccessible(true);
+                BigDecimal balance = (BigDecimal) declaredField.get(wallet);
+
+                if (amount.compareTo(balance) > 0) {
+                    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));
+
+            } catch (NoSuchFieldException | IllegalAccessException e) {
+                e.printStackTrace();
+            }
+
+            int result = this.baseMapper.updateAmountWithVersion(update);
+            if (result > 0) {
+                flag = false;
+            } else {
+                if (i > 2) {
+                    throw new FebsException("操作失败");
+                }
+            }
+        }
+        return 1;
+    }
+
+    @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();
+
+        BigDecimal amount = commissionChange.getAmount();
+        if (amount.compareTo(BigDecimal.ZERO) <= 0) {
+            throw new FebsException("参数错误");
+        }
+        MallMemberWallet mallMemberWallet = this.baseMapper.selectWalletByMemberId(member.getId());
+        if (mallMemberWallet.getCommission().compareTo(amount) < 0) {
+            throw new FebsException("佣金不足");
+        }
+
+        BigDecimal scorePercent = BigDecimal.ONE;
+        DataDictionaryCustom dic = dataDictionaryCustomMapper.selectDicDataByTypeAndCode(
+                DataDictionaryEnum.SCORE_SIGN_SETTING.getType(),
+                DataDictionaryEnum.SCORE_SIGN_SETTING.getCode());
+        if (dic != null) {
+            ScoreSettingDto scoreSettingDto = JSONObject.parseObject(dic.getValue(), ScoreSettingDto.class);
+            BigDecimal bigDecimal = ObjectUtil.isEmpty(scoreSettingDto.getScorePercent()) ? BigDecimal.ONE : new BigDecimal(scoreSettingDto.getScorePercent());
+            if (bigDecimal.compareTo(BigDecimal.ZERO) > 0) {
+                scorePercent = bigDecimal;
+            }
+        }
+
+        // TODO 判断是否开启了划转
+
+        int type;
+        int flowType;
+        String desc = null;
+        BigDecimal addAmount = BigDecimal.ZERO;
+        this.reduce(commissionChange.getAmount(), member.getId(), "commission");
+
+        // 佣金转余额
+        if (commissionChange.getType() == 1) {
+            type = ScoreFlowTypeEnum.COMMISSION_TO_BALANCE.getValue();
+            flowType = FlowTypeEnum.BALANCE.getValue();
+            addAmount = commissionChange.getAmount();
+            desc = StrUtil.format(ScoreFlowTypeEnum.COMMISSION_TO_BALANCE.getDesc(), commissionChange.getAmount());
+
+            this.add(addAmount, member.getId(), "balance");
+
+        // 佣金转积分
+        } else if (commissionChange.getType() == 2){
+            type = ScoreFlowTypeEnum.COMMISSION_TO_PRIZE_SCORE.getValue();
+            flowType = FlowTypeEnum.PRIZE_SCORE.getValue();
+            desc = StrUtil.format(ScoreFlowTypeEnum.COMMISSION_TO_PRIZE_SCORE.getDesc(), commissionChange.getAmount());
+            addAmount = commissionChange.getAmount().multiply(scorePercent).setScale(0, RoundingMode.HALF_DOWN);
+
+            this.add(addAmount, member.getId(), "prizeScore");
+        } else {
+            throw new FebsException("操作失败");
+        }
+
+        moneyFlowService.addMoneyFlow(
+                member.getId(),
+                commissionChange.getAmount().negate(),
+                type,
+                MallUtils.getOrderNum(),
+                FlowTypeEnum.COMMISSION.getValue(),
+                desc,
+                2
+        );
+
+        moneyFlowService.addMoneyFlow(
+                member.getId(),
+                addAmount,
+                type,
+                MallUtils.getOrderNum(),
+                flowType,
+                StrUtil.format(ScoreFlowTypeEnum.getDescByValue(type), commissionChange.getAmount()),
+                2
+        );
+    }
 }

--
Gitblit v1.9.1