zhangzengfei
2024-10-22 a254bc563003a9e7b3a8f1307df38b8ae4274a4f
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
package pkg
 
import (
    "fmt"
    "strconv"
    "strings"
)
 
// 已弃用
// 生成一个包含楼层的人脸id,解析楼层
// 使用48位源id, 其中前41位是imageid, 不可以修改 41-43位填 06 代表图像, +99 + 3位楼层(第一位0表示正,1表示负)
func GenerateFaceIdContainFloor(srcId, floorStr string) string {
    floorNum, _ := BuildFloorString("0", floorStr)
    newId := srcId[0:43] + "99" + floorNum
    //newId := srcId[0:43] + "99" + floorNum + snowflake.CreateRandomNumber(1)
 
    return newId
}
 
func ParseFloorFromId(srcId string) (string, string, error) {
    if len(srcId) != 48 {
        return "", "", fmt.Errorf("invalid id %s", srcId)
    }
 
    if srcId[43:45] != "99" {
        return "", "", fmt.Errorf("invalid flag %s", srcId[43:45])
    }
 
    return RestoreFloor(srcId[45:48])
}
 
// BuildFloorString parses the floor string and returns a string
func BuildFloorString(direction, floor string) (string, error) {
    var dire string
    var sign string
    var number string
 
    switch direction {
    case "in":
        dire = "1"
    case "out":
        dire = "2"
    default:
        dire = "0"
    }
 
    // Check if the floor is negative
    if strings.HasPrefix(floor, "-") {
        sign = "1"
        number = floor[1 : len(floor)-1]
    } else {
        sign = "0"
        number = floor[:len(floor)-1]
    }
 
    // Convert the number to an integer to validate and ensure it is a valid number
    if _, err := strconv.Atoi(number); err != nil {
        return "", err
    }
 
    // Format the number to be two digits
    formattedNumber := fmt.Sprintf("%02s", number)
 
    return dire + sign + formattedNumber, nil
}
 
// RestoreFloor restores the three-character string back to the original floor string
func RestoreFloor(encoded string) (string, string, error) {
    var direction, sign uint8
    var floorNumber, floorStr, directionStr string
 
    // 3位是旧的格式, 为了兼容, 暂时保留
    if len(encoded) == 3 {
        sign = encoded[0]
        floorNumber = encoded[1:]
    } else if len(encoded) == 4 {
        direction = encoded[0]
        sign = encoded[1]
        floorNumber = encoded[2:]
    } else {
        return "", "", fmt.Errorf("encoded string must be 3 characters long")
    }
 
    // Convert the number back to integer to remove any leading zeros
    parsedNumber, err := strconv.Atoi(floorNumber)
    if err != nil {
        return "", "", err
    }
 
    if sign == '1' {
        floorStr = fmt.Sprintf("-%dF", parsedNumber)
    } else if sign == '0' {
        floorStr = fmt.Sprintf("%dF", parsedNumber)
    } else {
        return "", "", fmt.Errorf("invalid sign character in encoded string")
    }
 
    if direction == '1' {
        directionStr = "in"
    } else if direction == '2' {
        directionStr = "out"
    }
 
    return directionStr, floorStr, nil
}
 
func CheckDirection(str1, str2 string) string {
    // 去掉字符串最后一个字符 'F'
    numStr1 := strings.TrimSuffix(str1, "F")
    numStr2 := strings.TrimSuffix(str2, "F")
 
    // 转换为 int 类型
    num1, err1 := strconv.Atoi(numStr1)
    num2, err2 := strconv.Atoi(numStr2)
 
    // 检查转换是否成功
    if err1 != nil || err2 != nil {
        return ""
    }
 
    // 比较两个整数的大小
    if num1 > num2 {
        return "out"
    } else {
        return "in"
    }
}