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 }