zhangzengfei
2024-02-05 78b0ffc543d78769580fa91842c8d635fe8c9405
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
128
129
130
131
132
133
134
135
package cache
 
import (
    "fmt"
    "strconv"
 
    "sdkCompare/config"
 
    "basic.com/pubsub/protomsg.git"
    "basic.com/valib/logger.git"
    "gorm.io/driver/mysql"
    "gorm.io/gorm"
)
 
var db *gorm.DB
 
func ConnectDB() error {
    var err error
 
    dsn := fmt.Sprintf("%s:%s@tcp(%s)/%s?charset=utf8mb4",
        config.DbPersonCompInfo.Username,
        config.DbPersonCompInfo.Password,
        config.DbPersonCompInfo.MysqlAddr,
        config.DbPersonCompInfo.Database)
 
    db, err = gorm.Open(mysql.Open(dsn), &gorm.Config{
        // 禁用外键(指定外键时不会在mysql创建真实的外键约束)
        DisableForeignKeyConstraintWhenMigrating: true,
        //// 指定表前缀
        //NamingStrategy: schema.NamingStrategy{
        //    TablePrefix: config.Conf.Mysql.TablePrefix + "_",
        //},
    })
 
    if err != nil {
        logger.Error("mysql database open err: %s", err.Error())
        return err
    }
 
    return nil
}
 
type BaseEntity struct {
    Id         string `gorm:"primary_key;column:id" json:"id" example:""`
    CreateTime string `gorm:"column:createTime" json:"createTime,omitempty" example:""`
    UpdateTime string `gorm:"column:updateTime" json:"updateTime,omitempty" example:""`
    CreateBy   string `gorm:"column:createBy" json:"createBy,omitempty" example:""`
    IsDelete   int    `gorm:"column:isDelete" json:"isDelete" example:"0 未删除 1已删除"`
    Enable     int    `gorm:"column:enable" json:"enable" example:" 1生效 0未生效"`
}
 
type DbTables struct {
    BaseEntity
    TableName     string `gorm:"column:tableName" json:"tableName" example:"底库123"`
    TableDesc     string `gorm:"column:tableDesc" json:"tableDesc" example:"底库描述"`
    TableType     string `gorm:"column:tableType" json:"tableType" example:"person,car"`
    BwType        string `gorm:"column:bwType" json:"bwType" example:"黑名单:1,白名单:0"`
    StartTime     string `gorm:"column:startTime" json:"startTime" example:"2019-01-12 12:14:56"`
    EndTime       string `gorm:"column:endTime" json:"endTime" example:"2019-10-12 12:14:56"`
    IsSync        string `gorm:"column:isSync" json:"isSync" example:"1:同步到管理平台,2:不同步"`
    AnalyServerId string `gorm:"column:analyServerId" json:"analyServerId" example:"本地库所属的分析设备id,如果是同步库就不需要记录"`
}
 
type DbPersons struct {
    BaseEntity
    TableId      string `gorm:"column:tableId" json:"tableId" example:"库表id"`
    FaceFeature  string `gorm:"column:faceFeature" json:"faceFeature" example:"人脸特征值(车主特征)"`
    PersonPicUrl string `gorm:"column:personPicUrl" json:"personPicUrl" example:"图片路径,(车主照片)"`
    PersonName   string `gorm:"column:personName" json:"personName" example:"人员姓名,(车主姓名)"`
    Age          string `gorm:"column:age" json:"age"  example:"年龄"`
    Sex          string `gorm:"column:sex" json:"sex" example:"性别 男 女(车主性别)"`
    IdCard       string `gorm:"column:idCard" json:"idCard" example:"身份证(车主身份证)"`
    PhoneNum     string `gorm:"column:phoneNum" json:"phoneNum" example:"手机号码"`
    MonitorLevel string `gorm:"column:monitorLevel" json:"monitorLevel" example:"等级"`
    PicDesc      string `gorm:"column:picDesc" json:"picDesc" example:"照片标识"`
    Reserved     string `gorm:"column:reserved" json:"reserved" example:"其他"`
 
    CarNo      string `gorm:"column:carNo" json:"carNo" example:"车牌号"`
    CarPicUrls string `gorm:"column:carPicUrls" json:"carPicUrls" example:"车辆照片"`
    CarType    int    `gorm:"column:carType" json:"carType" example:"车辆类型"`
    CarBrand   int    `gorm:"column:carBrand" json:"carBrand" example:"车辆品牌"`
    CarColor   int    `gorm:"column:carColor" json:"carColor" example:"车辆颜色"`
 
    FromServerId string `gorm:"column:fromServerId" json:"fromServerId,omitempty" example:"入库serverId"`
}
 
type FeatureCacheBase struct {
    Id          string `json:"id"`
    TableId     string `json:"tableid"`
    FaceFeature string `json:"faceFeature"`
    Enable      int32  `json:"enable"`
 
    CarNo string `json:"carNo"`
}
 
func (dt *DbTables) FindAllDbTablesByCurServer() (arr []DbTables, err error) {
    sql := "select * from dbtables where isDelete = 0"
    err = db.Raw(sql).Find(&arr).Error
    if err != nil {
        return nil, err
    }
 
    return
}
 
func (dbp *DbPersons) GetPersonTotal(tableId string) (total int64, err error) {
    sql := "select count(1) as total from dbtablepersons where isDelete=0 and tableId in (select id from dbtables where isDelete=0)"
    if tableId != "" {
        sql += " and tableId='" + tableId + "'"
    }
    err = db.Raw(sql).Count(&total).Error
    return
}
 
func (dbp *DbPersons) GetPersonsCompareCacheBase(from int, size int) (arr []*protomsg.Esinfo, err error) {
    var persons []DbPersons
    sql := "select id,faceFeature,tableId,enable,carNo from dbtablepersons where isDelete=0 and tableId in (select id from dbtables where isDelete=0)"
    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 != "" {
            arr = append(arr, &protomsg.Esinfo{
                Id:          p.Id,
                Tableid:     p.TableId,
                FaceFeature: p.FaceFeature,
                Enable:      int32(p.Enable),
                CarNo:       p.CarNo,
            })
        }
    }
    return
}