rentaiming
2024-07-15 5674594f06ec038a5ac1633b881518ffbd8c6c02
ruoyi-modules/ruoyi-member/src/main/java/com/ruoyi/member/service/impl/MemberServiceImpl.java
@@ -18,6 +18,7 @@
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.google.common.collect.Lists;
import com.ruoyi.common.core.enums.GenderEnum;
import com.ruoyi.common.core.exception.ServiceException;
import com.ruoyi.common.core.utils.StringUtils;
@@ -243,7 +244,6 @@
                Member member1=this.getOne(wrapper1);
                if (member1==null){
                        //创建新用户
                        String memberId = IdUtils.simpleUUID();
                        sysUser = new SysUser();
@@ -550,38 +550,32 @@
        this.updateById(byId);
        LambdaQueryWrapper<MemberLevel> wrapper= Wrappers.lambdaQuery();
        List<MemberLevel> list = memberLevelMapper.selectList(wrapper);
        for (int i=0;i<list.size();i++){
            if (i==0){
                int ia = byId.getMoney().compareTo(list.get(0).getCumulativeConsumption());
                if (ia<0){
                    byId.setLevel(0);
                    break;
                }
            }
            if (i==list.size()-1){
                BigDecimal cumulative = list.get(i - 1).getCumulativeConsumption();
                BigDecimal cumulative1 = list.get(i).getCumulativeConsumption();
                if (cumulative.compareTo(byId.getMoney()) <= 0 && cumulative1.compareTo(byId.getMoney()) >= 0) {
                    byId.setLevel(list.get(i).getLevel());
                }else{
                    if (cumulative1.compareTo(byId.getMoney())<=0){
                        byId.setLevel(list.get(i).getLevel());
                    }
                }
            }else {
                if (i!=0&&i!=list.size()-1){
                    BigDecimal cumulative = list.get(i - 1).getCumulativeConsumption();
                    BigDecimal cumulative1 = list.get(i).getCumulativeConsumption();
                    if (cumulative.compareTo(byId.getMoney()) <= 0 && cumulative1.compareTo(byId.getMoney()) >= 0) {
                        byId.setLevel(list.get(i).getLevel());
                    }
                }
            }
        }
        int appropriateLevel = findAppropriateLevel(list, byId.getMoney());
        byId.setLevel(appropriateLevel);
        this.updateById(byId);
    }
    private static int findAppropriateLevel(List<MemberLevel> memberLevels, BigDecimal money) {
        // 对于最开始的level,设置为0(假设0是最低等级)
        int level = 0;
        for (int i = 0; i < memberLevels.size(); i++) {
            MemberLevel currentLevel = memberLevels.get(i);
            BigDecimal currentCumulativeConsumption = currentLevel.getCumulativeConsumption();
            // 如果当前成员的消费额小于等于当前等级的累积消费额
            if (money.compareTo(currentCumulativeConsumption) < 0) {
                return level;
            }
            // 更新level为当前等级,准备下一次比较
            level = currentLevel.getLevel();
        }
        // 如果遍历完成后还没有返回,说明该成员的消费额高于所有已定义等级的累积消费额
        level = memberLevels.get(memberLevels.size() - 1).getLevel();
        return level;
    }
    /**
@@ -634,6 +628,8 @@
                .orderByDesc(Member::getLevel)
                .list();
    }
}