使用Redis生成具有一定规则且不重复的的编号

private String getCheckCode(String remark, String checkCode) {
        String key = "";
        String parentCode = "";
        String result = "";
        if (Constants.ONE_SUBJECT.equals(remark)) {
            key = Constants.ONE_SUBJECT_CODE;
        } else if (Constants.SECOND_SUBJECT.equals(remark)) {
            key = Constants.SECOND_SUBJECT_CODE + ":" + checkCode;
            parentCode = StringUtils.leftPad(checkCode, 2, "0");
        } else if (Constants.DETAIL_SUBJECT.equals(remark)) {
            key = Constants.SUBJECT_DETAIL_CODE + ":" + checkCode;
            parentCode = StringUtils.leftPad(checkCode, 2, "0");
        }
        Long generateId = iCache.getIncr(key);
        if (generateId == null || generateId.intValue() == 0) {
            generateId = iCache.getIncr(key);
        }
        Asserts.assertTrue(generateId != null, "编号生成失败");

        if (Constants.ONE_SUBJECT.equals(remark)) {
            result = String.valueOf(generateId);
        } else {
            result = parentCode + StringUtils.leftPad(String.valueOf(generateId), 2, "0");
        }
        return result;
    }
}

 

你可能感兴趣的:(工作总结)