zhangzengfei
2024-12-09 366e2ff546092d9be26411fb698b3ddd8e834a11
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
package db
 
import (
    "basic.com/valib/logger.git"
    "encoding/base64"
    "sdkCompare/util"
    "strconv"
)
 
type DbPersons struct {
    BaseEntity
    TableId     string `gorm:"column:table_id"`
    FaceFeature string `gorm:"column:face_feature"`
    CommunityID string `gorm:"column:community_id"` // 常住小区 domain unit ID
    OrgID       string `gorm:"column:org_id"`       // 常住派出所 domain unit ID
    OrgName     string `gorm:"column:community"`
}
 
func (dbp *DbPersons) GetPersonTotal() (total int64, err error) {
    sql := "select id from person where is_delete = 0 and table_id = 'system'"
 
    err = db.Raw(sql).Count(&total).Error
 
    return
}
 
func (dbp *DbPersons) GetPersonsCacheBase(from int, size int) (arr []*FeatureCacheBase, err error) {
    var persons []DbPersons
    sql := "select id, table_id, face_feature, community_id from person where is_delete = 0 and table_id = 'system'"
    sql += " order by id asc limit " + strconv.Itoa(from) + "," + strconv.Itoa(size)
 
    err = db.Raw(sql).Find(&persons).Error
    if err != nil {
        return nil, err
    }
 
    for _, p := range persons {
        if p.FaceFeature != "" {
            byteFeat, err := base64.StdEncoding.DecodeString(p.FaceFeature)
            if err != nil {
                logger.Errorf("Person %s feature is invalid", p.ID)
                continue
            }
 
            arr = append(arr, &FeatureCacheBase{
                Id:          p.ID,
                TableId:     p.TableId,
                AreaId:      p.CommunityID,
                FaceFeature: util.ByteSlice2float32Slice(byteFeat),
            })
        } else {
            logger.Warnf("Person %s feature is empty", p.ID)
        }
    }
 
    return
}
 
func (dbp *DbPersons) GetPersonsById(id string) (info *FeatureCacheBase, err error) {
    var p DbPersons
 
    err = db.Table("person").
        Select("id", "table_id", "face_feature", "community_id").
        First(&p, "id = ?", id).Error
    if err != nil {
        return nil, err
    }
 
    if p.FaceFeature != "" {
        byteFeat, err := base64.StdEncoding.DecodeString(p.FaceFeature)
        if err != nil {
            return nil, err
        }
        info = &FeatureCacheBase{
            Id:          p.ID,
            TableId:     p.TableId,
            AreaId:      p.CommunityID,
            FaceFeature: util.ByteSlice2float32Slice(byteFeat),
        }
    }
 
    return
}