Demand is briefly

For a due diligence operation of credit granting, multiple signers need to be selected to confirm the signings. According to the different amount of credit granted, the level of the selected signer should meet the corresponding rules to perform the signer operation. Therefore, we need to check the selected signer and the corresponding amount. The lowest level of the casting personnel is 10, and the smaller the number, the higher the level.

  1. The following are specific rules for the different amount ranges.
Amount (unit: 100 million) Level (level of casting personnel)
6<X Grade 3 or above
3 < X 6 or less Grade 4 or above
1 < X 3 or less Grade 5 or above
0.5 < X 1 or less Grade 6 or above
0.3 “X 0.5 or less Grade 7 or above
X 0.3 or less Grade 8 or above
  1. The permissions of two identical low-level people can be combined to form a single high-level person. For example, two level 7 personnel castings are equal to one level 6 personnel castings

  2. A detailed example

Of situation Authority enjoyed
7 + 7 6 level authority
Level 6 +2 level 7 5 level authority
2 levels 7 +2 levels 7 5 level authority

Demand analysis

The implementation of this requirement has two points to address:

  1. How to combine and convert the levels of multiple personnel
  2. Determine whether the level permission after conversion meets the check permission of the corresponding amount.

Obviously, the first point is the key and difficult point. As long as the first conversion, the second point can be judged step by step.

Design ideas

Two identical grades can be added as one higher grade. Based on this property, we can use base 2 addition to simulate the process and structure. Because the secondary system is 1+1=2, but the full 2 carries the 1 is like adding two low points to make one high point. For example, the binary number 01+01=10. The requirement level is from low to high, and the number is from large to small. There are 10 levels, so based on a 10 bit binary number 0B1000000000, shift (level-1) bits to the right to get the level 2 number.

Formula: (level) = 0 F b1000000000 > > (level 1);

The following table shows the process and results of mapping a hierarchy to a binary number

Level (level) F(level)=0B1000000000>>(level-1); Number of secondary system
1 0B1000000000>>0; 0B1000000000
2 0B1000000000>>1; 0B0100000000
3 0B1000000000>>2; 0B0010000000
4 0B1000000000>>3; 0B0001000000
5 0B1000000000>>4; 0B0000100000
6 0B1000000000>>5; 0B0000010000
7 0B1000000000>>6; 0B0000001000
8 0B1000000000>>7; 0B0000000100
9 0B1000000000>>8; 0B0000000010
10 0B1000000000>>9; 0B0000000001

For example, determine whether three level 6 and two level 7 casters have a level 4 authority.

0B0000001000(Level 7)+0B0000001000(Level 7)+0B0000010000(Level 6)+0B0000010000(Level 6)=0B0001000000(Level 4)

The core code

private boolean checkLevel(Long amount,List<Integer> signLevelList) {
    LOGGER.info("Start checking the grade of A corner.");
    Integer maxLevel = 0B1000000000;
    Integer totalLevel = 0;
    for (int i = 0; i < signLevelList.size(); i++) {
        Integer signUserLevel = signLevelList.get(i);
        Integer refLevel = maxLevel >> (signUserLevel - 1);
        totalLevel += refLevel;
    }
    Long W = 10000*100L;/ / unit
    LOGGER.info("Amount of the project: {}", amount);
    if (amount <= 3000 * W) {
        return totalLevel >= (maxLevel >> (8 - 1));// Less than 30 million and above grade 8
    } else if (amount <= 5000 * W) {
        return totalLevel >= (maxLevel >> (7 - 1));// Less than 50 million and above grade 7
    } else if (amount <= 10000 * W) {
        return totalLevel >= (maxLevel >> (6 - 1));// Less than 100 million or above grade 6
    } else if (amount <= 30000 * W) {
        return totalLevel >= (maxLevel >> (5 - 1));// Less than 300 million and above grade 5
    } else if (amount <= 60000 * W) {
        return totalLevel >= (maxLevel >> (4 - 1));// Less than 500 million or above grade 4
    } else if (amount > 60000 * W) {
        return totalLevel >= (maxLevel >> (3 - 1));// Less than 60,000,000 and above grade 3
    }
    return false;
}
Copy the code

Expand the thinking

If the rule became 3 low level can be converted into a high level, can you also use the same idea?