| | |
| | | "strings" |
| | | ) |
| | | |
| | | // 已弃用 |
| | | // 生成一个包含楼层的人脸id,解析楼层 |
| | | // 使用48位源id, 其中前41位是imageid, 不可以修改 41-43位填 06 代表图像, +99 + 3位楼层(第一位0表示正,1表示负) |
| | | func GenerateFaceIdContainFloor(srcId, floorStr string) string { |
| | | floorNum, _ := ParseFloor(floorStr) |
| | | floorNum, _ := ParseFloor("0", floorStr) |
| | | newId := srcId[0:43] + "99" + floorNum |
| | | //newId := srcId[0:43] + "99" + floorNum + snowflake.CreateRandomNumber(1) |
| | | |
| | | return newId |
| | | } |
| | | |
| | | func ParseFloorFromId(srcId string) (string, error) { |
| | | func ParseFloorFromId(srcId string) (string, string, error) { |
| | | if len(srcId) != 48 { |
| | | return "", fmt.Errorf("invalid id %s", srcId) |
| | | return "", "", fmt.Errorf("invalid id %s", srcId) |
| | | } |
| | | |
| | | if srcId[43:45] != "99" { |
| | | return "", fmt.Errorf("invalid flag %s", srcId[43:45]) |
| | | return "", "", fmt.Errorf("invalid flag %s", srcId[43:45]) |
| | | } |
| | | |
| | | return RestoreFloor(srcId[45:48]) |
| | | } |
| | | |
| | | // ParseFloor parses the floor string and returns a three-character string |
| | | func ParseFloor(floor string) (string, error) { |
| | | func ParseFloor(direction, floor string) (string, error) { |
| | | var sign string |
| | | var number string |
| | | |
| | |
| | | // Format the number to be two digits |
| | | formattedNumber := fmt.Sprintf("%02s", number) |
| | | |
| | | return sign + formattedNumber, nil |
| | | return direction + sign + formattedNumber, nil |
| | | } |
| | | |
| | | // RestoreFloor restores the three-character string back to the original floor string |
| | | func RestoreFloor(encoded string) (string, error) { |
| | | if len(encoded) != 3 { |
| | | return "", fmt.Errorf("encoded string must be 3 characters long") |
| | | func RestoreFloor(encoded string) (string, string, error) { |
| | | if len(encoded) != 4 { |
| | | return "", "", fmt.Errorf("encoded string must be 3 characters long") |
| | | } |
| | | |
| | | sign := encoded[0] |
| | | number := encoded[1:] |
| | | direction := encoded[0] |
| | | sign := encoded[1] |
| | | number := encoded[2:] |
| | | |
| | | // Convert the number back to integer to remove any leading zeros |
| | | parsedNumber, err := strconv.Atoi(number) |
| | | if err != nil { |
| | | return "", err |
| | | return "", "", err |
| | | } |
| | | |
| | | var restoredFloor string |
| | | var floorStr, directionStr string |
| | | if sign == '1' { |
| | | restoredFloor = fmt.Sprintf("-%dF", parsedNumber) |
| | | floorStr = fmt.Sprintf("-%dF", parsedNumber) |
| | | } else if sign == '0' { |
| | | restoredFloor = fmt.Sprintf("%dF", parsedNumber) |
| | | floorStr = fmt.Sprintf("%dF", parsedNumber) |
| | | } else { |
| | | return "", fmt.Errorf("invalid sign character in encoded string") |
| | | return "", "", fmt.Errorf("invalid sign character in encoded string") |
| | | } |
| | | |
| | | return restoredFloor, nil |
| | | if direction == '1' { |
| | | directionStr = "in" |
| | | } else if direction == '2' { |
| | | directionStr = "out" |
| | | } |
| | | |
| | | return directionStr, floorStr, nil |
| | | } |