package models type DeviceSdk struct { Id string `gorm:"column:id;primary_key;type:varchar(50);unique;not null;" json:"id"` DevId string `gorm:"column:devId" json:"devId"` MachineCode string `gorm:"column:machineCode" json:"machineCode"` ActivateCode string `gorm:"column:activateCode" json:"activateCode"` SdkId string `gorm:"column:sdkId" json:"sdkId"` ExpireTime string `gorm:"column:expireTime" json:"expireTime"` InstallTime string `gorm:"column:installTime" json:"installTime"` //安装时间 } func (DeviceSdk) TableName() string { return "t_device_sdk" } func (ds *DeviceSdk) Insert() bool { result := db.Table(ds.TableName()).Create(&ds) if result.Error == nil && result.RowsAffected > 0 { return true } return false } func (ds *DeviceSdk) Update() bool { result := db.Table(ds.TableName()).Where("id=?", ds.Id).Update(&ds) if result.Error ==nil && result.RowsAffected > 0 { return true } return false } func (ds *DeviceSdk) Exist(devId string, sdkId string) (int64, error) { result := db.Table(ds.TableName()).Where("devId=? and sdkId=?", devId, sdkId).First(&ds) return result.RowsAffected, result.Error } func (ds *DeviceSdk) DeleteById(id string) bool { result := db.Exec("delete from "+ds.TableName()+" where id='"+id+"'") if result.Error == nil && result.RowsAffected > 0 { return true } return false } func (ds *DeviceSdk) FindByActivateCode(activateCode string) (list []DeviceSdk, err error) { result := db.Table(ds.TableName()).Where("activateCode=?", activateCode).Find(&list) if result.Error != nil { return []DeviceSdk{},result.Error } return list, nil } func (ds *DeviceSdk) FindByMachineCode(machineCode string, serverId string) (list []DeviceSdk, err error) { result := db.Table(ds.TableName()).Where("machineCode=? or devId=?", machineCode, serverId).Order("installTime desc").Find(&list) if result.Error != nil { return []DeviceSdk{},result.Error } return list, nil } func (ds *DeviceSdk) FindByMacAndSdk(macCode, serverId, sdkId string) (list []DeviceSdk, err error) { result := db.Table(ds.TableName()).Where("(machineCode=? or devId=?) and sdkId=?", macCode, serverId, sdkId).Find(&list) if result.Error != nil { return []DeviceSdk{},result.Error } return list, nil } //按设备id统计算法数量 func (ds *DeviceSdk) CountByDevId(devId string) (int,error) { var count int if err := db.Raw("select count(1) as count from "+ds.TableName()+" where devId=?",devId).Count(&count).Error; err != nil { return 0, err } return count, nil } func (ds *DeviceSdk) FindAll() (list []DeviceSdk) { if err := db.Table(ds.TableName()).Find(&list).Error;err != nil { return []DeviceSdk{} } return }