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:"其他"` 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 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), }) } } return } func (dbp *DbPersons) GetPersonsCompareCacheById(id string) (info *protomsg.Esinfo, err error) { sql := "select id,faceFeature,tableId,enable from dbtablepersons where id = \"" + id + "\"" var p DbPersons err = db.Raw(sql).First(&p).Error if err != nil { return nil, err } if p.FaceFeature != "" { info = &protomsg.Esinfo{ Id: p.Id, Tableid: p.TableId, FaceFeature: p.FaceFeature, Enable: int32(p.Enable), } } return }