• 获取唯一的短邀请码


    
    /**
         * 获取唯一的邀请码
         *
         * @return the string
         */
        private String generateUserUniqueShareCode() {
            Set<String> arr = getSetArr();
    
            String code;
            do {
                code = generateCode(arr);
            } while (isCodeUserExists(code));
    
            return code;
        }
    
        /**
         * Gets set arr.
         *
         * @return the set arr
         */
        @NotNull
        private static Set<String> getSetArr() {
            return new HashSet<>(Arrays.asList(
                    "0", "1", "2", "3", "4", "5", "6", "7", "8", "9",
                    "a", "b", "c", "d", "e", "f", "g", "h", "j", "k", "m",
                    "n", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y",
                    "z", "A", "B", "C", "D", "E", "F", "G", "H", "J", "K",
                    "M", "N", "P", "Q", "R", "S", "T", "U", "V", "W", "X",
                    "Y", "Z"
            ));
        }
    
    
        /**
         * 获取随机邀请码
         *
         * @param arr the arr
         * @return the string
         */
        private String generateCode(Set<String> arr) {
            StringBuilder code = new StringBuilder();
            Random random = new Random();
            for (int i = 0; i < 6; i++) {
                code.append(new ArrayList<>(arr).get(random.nextInt(arr.size())));
            }
            return code.toString();
        }
    
    
        /**
         * 判断邀请码是否存在
         *
         * @param code the code
         * @return the boolean
         */
        private boolean isCodeUserExists(String code) {
            LambdaQueryWrapper<BusinessUser> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(BusinessUser::getShareCode, code);
            return businessUserService.getOne(queryWrapper) != null;
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62

    并且需要在parent的pom.xml中添加notnull引用

          <dependency>
                <groupId>org.jetbrainsgroupId>
                <artifactId>annotationsartifactId>
                <version>RELEASEversion>
                <scope>compilescope>
            dependency>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
  • 相关阅读:
    protobuf语法之proto2简述
    maven通过maven repository添加依赖包
    速看|期待已久的2022年广州助理检测工程师真题解析终于出炉
    MyBatis入门案例
    drf_day04
    2022最新版Redis入门到精通(云课堂视频学习笔记)
    java遍历Map的方式
    沁恒CH9101是一个USB总线的转接芯片
    C++自增/减运算符的原理以及前后缀形式的本质区别
    研发管理和项目管理有哪些痛点
  • 原文地址:https://blog.csdn.net/weixin_39973810/article/details/133162475