qixiaoning
2025-07-08 84d2ef9760af0a4a4aa933937294400b3caa291d
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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
package models
 
import (
    "basic.com/pubsub/protomsg.git"
    "basic.com/valib/logger.git"
    "errors"
    "github.com/jinzhu/gorm"
    "github.com/satori/go.uuid"
    "strconv"
    "strings"
    "time"
    "vamicro/config"
)
 
type Dbtablepersons 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 DbPersonsCompVo struct {
    Dbtablepersons
    CompareScore float64 `json:"compareScore"`
}
 
func (Dbtablepersons) TableName() string {
    return "dbtablepersons"
}
 
func (dbp *Dbtablepersons) Add() (bool, error) {
    var dtTmp Dbtables
    rows, _ := dtTmp.SelectById(dbp.TableId)
    logger.Debug("addDbPerson dtTmp.rows:", rows)
    if rows > 0 {
        if dtTmp.AnalyServerId != "" {
            db.LogMode(false)
            defer db.LogMode(true)
        }
        result := db.Table("dbtablepersons").Create(&dbp)
        if result.Error != nil {
            return false, result.Error
        }
        if result.RowsAffected > 0 {
            go SyncSdkCompareCache(dtTmp.AnalyServerId, protomsg.EsCacheChanged_T_DbTablePerson,
                dtTmp.Id, dbp.Id, dbp.FaceFeature, protomsg.DbAction_Insert, int32(dbp.Enable), dbp.CarNo)
            return true, nil
        }
    }
 
    return false, errors.New("dbtablepersons create err")
}
 
func (dbp *Dbtablepersons) Update() (bool, error) {
    var dbpTmp Dbtablepersons
    pRow, _ := dbpTmp.SelectById(dbp.Id)
    if pRow > 0 {
        var dtTmp Dbtables
        rows, _ := dtTmp.SelectById(dbpTmp.TableId)
        if rows > 0 {
            if dtTmp.AnalyServerId != "" {
                db.LogMode(false)
                defer db.LogMode(true)
            }
            var result *gorm.DB
            if dbp.TableId == "" { //更新启用停用状态
                result = db.Exec("update dbtablepersons set enable=" + strconv.Itoa(dbp.Enable) + " where id='" + dbp.Id + "'")
            } else {
                result = db.Table("dbtablepersons").Save(&dbp)
            }
 
            if result.Error != nil {
                return false, result.Error
            }
            if result.RowsAffected > 0 {
 
                go SyncSdkCompareCache(dtTmp.AnalyServerId, protomsg.EsCacheChanged_T_DbTablePerson,
                    dbp.TableId, dbp.Id, dbpTmp.FaceFeature, protomsg.DbAction_Update, int32(dbpTmp.Enable), dbp.CarNo)
                return true, nil
            }
        }
    }
 
    return false, errors.New("dbtablepersons update err")
}
 
func (dbp Dbtablepersons) UpdateFace(id string, faceFeature string, pic string) (bool, error) {
    var dbpTmp Dbtablepersons
    pRow, _ := dbpTmp.SelectById(id)
    if pRow > 0 {
        var dtTmp Dbtables
        rows, _ := dtTmp.SelectById(dbpTmp.TableId)
        if rows > 0 {
            if dtTmp.AnalyServerId != "" {
                db.LogMode(false)
                defer db.LogMode(true)
            }
 
            result := db.Exec("update dbtablepersons set faceFeature='" + faceFeature + "',personPicUrl='" + pic + "' where id='" + id + "'")
 
            if result.Error != nil {
                return false, result.Error
            }
            if result.RowsAffected > 0 {
                go SyncSdkCompareCache(dtTmp.AnalyServerId, protomsg.EsCacheChanged_T_DbTablePerson,
                    dtTmp.Id, id, faceFeature, protomsg.DbAction_Update, int32(dbpTmp.Enable), dbpTmp.CarNo)
                return true, nil
            }
        }
    }
 
    return false, errors.New("dbtablepersons update err")
}
 
func (dbp *Dbtablepersons) SelectById(id string) (rows int64, err error) {
    dbselect := db.Table("dbtablepersons").Where("id = ?", id).First(&dbp)
    if dbselect.Error != nil || dbselect.RowsAffected == 0 {
        return 0, err
    }
    return dbselect.RowsAffected, nil
}
 
func (dbp *Dbtablepersons) SelectByCarNo(tableId string, carNo string) (rows int64, err error) {
    dbselect := db.Table("dbtablepersons").Where("tableId=? and carNo = ?", tableId, carNo).First(&dbp)
    if dbselect.Error != nil || dbselect.RowsAffected == 0 {
        return 0, err
    }
    return dbselect.RowsAffected, nil
}
 
func (dbp *Dbtablepersons) InsertCarNo(tableId string, carNo string) bool {
    var dtTmp Dbtables
    rows, _ := dtTmp.SelectById(tableId)
    if rows > 0 {
        if dtTmp.AnalyServerId != "" {
            db.LogMode(false)
            defer db.LogMode(true)
        }
        id := uuid.NewV4().String()
        t := time.Now().Format("2006-01-02 15:04:05")
        result := db.Exec("insert into dbtablepersons(id,tableId,carNo,createTime,fromServerId,enable,isDelete) values (?,?,?,?,?,?,?)", id, tableId, carNo, t, config.Server.AnalyServerId, 1, 0)
        if result.Error == nil && result.RowsAffected > 0 {
            go SyncSdkCompareCache(dtTmp.AnalyServerId, protomsg.EsCacheChanged_T_DbTablePerson,
                dtTmp.Id, id, "", protomsg.DbAction_Update, int32(dbp.Enable), dbp.CarNo)
            return true
        }
    }
 
    return false
}
 
func (dbp *Dbtablepersons) DeleteById(id string) (b bool, err error) {
    var dbpTmp Dbtablepersons
    r1, _ := dbpTmp.SelectById(id)
    if r1 > 0 {
        var dtTmp Dbtables
        r2, _ := dtTmp.SelectById(dbpTmp.TableId)
        if r2 > 0 {
            if dtTmp.AnalyServerId != "" {
                db.LogMode(false)
                defer db.LogMode(true)
            }
            result := db.Exec("update dbtablepersons set isDelete=1 where id=?", id)
            if result.Error != nil {
                return false, result.Error
            }
            if result.RowsAffected > 0 {
                go SyncSdkCompareCache(dtTmp.AnalyServerId, protomsg.EsCacheChanged_T_DbTablePerson,
                    dtTmp.Id, id, "", protomsg.DbAction_Delete, 0, "")
                return true, nil
            }
        }
    }
    return false, errors.New("dbtablepersons delete err")
}
 
func (dbp *Dbtablepersons) FindByPersonIds(ids []string) (arr []Dbtablepersons, err error) {
    str := ""
    for _, id := range ids {
        str += `'` + id + `',`
    }
    str = str[:len(str)-1]
    sql := "select * from dbtablepersons where id in (" + str + ")"
    err = db.Raw(sql).Scan(&arr).Error
    if err != nil {
        return nil, err
    }
    return
}
 
//func (dbp *Dbtablepersons) DeleteByPersonIds(ids []string) (bool,error) {
//    str := ""
//    for _,id := range ids {
//        str += `'`+id+`',`
//    }
//    str = str[:len(str)-1]
//    sql := "update dbtablepersons set isDelete=1 where id in ("+str+")"
//    result := db.Exec(sql)
//    if result.Error !=nil {
//        return false,result.Error
//    }
//    return result.RowsAffected >0,nil
//}
 
func (dbp *Dbtablepersons) DeletePersonsByTbId(tableId string) (bool, error) {
    var dtTmp Dbtables
    rows, _ := dtTmp.SelectById(tableId)
    if rows > 0 {
        if dtTmp.AnalyServerId != "" {
            db.LogMode(false)
            defer db.LogMode(true)
        }
        result := db.Exec("update dbtablepersons set isDelete=1 where tableId='" + tableId + "'")
        if result.Error != nil {
            return false, result.Error
        }
        if result.RowsAffected > 0 {
            go SyncSdkCompareCache(dtTmp.AnalyServerId, protomsg.EsCacheChanged_T_DbTable,
                dtTmp.Id, "", "", protomsg.DbAction_Delete, 0, "")
            return true, nil
        } else {
            return false, errors.New("update dbtablepersons err")
        }
    }
    return false, errors.New("dbtable not exist")
}
 
func (dbp *Dbtablepersons) FindPersons(tableId string, orderName string, orderType string, contentValue string, from int, size int) (arr []Dbtablepersons, err error) {
    sql := "select * from dbtablepersons where tableId='" + tableId + "' and isDelete=0"
    if contentValue != "" {
        sql += " and (personName like '%" + contentValue + "%' or sex like '%" + contentValue + "%' or idCard like '%" + contentValue + "%' or phoneNum like '%" + contentValue + "%')"
    }
    sql += " order by " + orderName + " " + orderType + " limit " + strconv.Itoa(from) + "," + strconv.Itoa(size)
    err = db.Raw(sql).Scan(&arr).Error
    if err != nil {
        return nil, err
    }
    return
}
 
func (dbp *Dbtablepersons) 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
}
 
type PersonId struct {
    Id string `json:"id"`
}
 
func (dbp *Dbtablepersons) FindLikePersonIds(tableIds []string, contentValue string) (personIds []PersonId, err error) {
    tIds := ""
    if tableIds != nil && len(tableIds) > 0 {
        for _, tId := range tableIds {
            tIds = tIds + "'" + tId + "',"
        }
    }
    tIds = strings.Trim(tIds, ",")
    sql := "select id from dbtablepersons where 1=1"
    if tIds != "" {
        sql += " and tableId in (" + tIds + ")"
    }
    if contentValue != "" {
        sql += " and (personName like '%" + contentValue + "%' or sex like '%" + contentValue + "%' or idCard like '%" + contentValue + "%' or phoneNum like '%" + contentValue + "%')"
    }
    logger.Debug("FindLikePersonIds sql:", sql)
    err = db.Raw(sql).Scan(&personIds).Error
    if err != nil {
        return nil, err
    }
    return personIds, nil
}