package models //已授权的设备 type AuthDevice struct { Id string `gorm:"primary_key;column:id" json:"id"` DevId string `gorm:"column:devId;unique;not null;" json:"devId"` DevName string `gorm:"column:devName" json:"devName"` DevIp string `gorm:"column:devIp" json:"devIp"` ApplyKey string `gorm:"column:applyKey" json:"applyKey"` AuthTime string `gorm:"column:authTime" json:"authTime"` //授权时间或解除授权时间 Status int `gorm:"column:status" json:"status"` //授权状态:0:待审核,1:已授权,-1:已解除授权 CreateTime string `gorm:"column:createTime" json:"createTime"` //创建时间 } const ( AuthStatus_AuthCanceled = -2 //已解除授权 AuthStatus_Rejected = -1 //已拒绝 AuthStatus_Waiting = 0 //待审核 AuthStatus_Agreed = 1 //已授权 ) func (AuthDevice) TableName() string { return "auth_device" } func (ad *AuthDevice) Insert() bool { result := db.Table(ad.TableName()).Create(&ad) if result.Error !=nil { return false } return result.RowsAffected>0 } func (ad *AuthDevice) Update() bool { result := db.Table(ad.TableName()).Update(&ad) if result.Error !=nil { return false } return result.RowsAffected>0 } func (ad *AuthDevice) SelectById(id string) (int64, error){ result := db.Table(ad.TableName()).Where("id=?",id).First(&ad) if result.Error != nil || result.RowsAffected == 0 { return 0, err } return result.RowsAffected, nil } func (ad *AuthDevice) FindByDevId(devId string) (int64, error){ result := db.Table(ad.TableName()).Where("devId=?",devId).First(&ad) if result.Error != nil || result.RowsAffected == 0 { return 0, err } return result.RowsAffected, nil } func (ad *AuthDevice) FindByStatus(status int) (list []AuthDevice){ if err:=db.Table(ad.TableName()).Where("status=?", status).Scan(&list).Error;err !=nil{ return nil } return list } func (ad *AuthDevice) UpdateStatus(id string, status int) bool { return db.Exec("update "+ad.TableName()+" set status=? where id=?", status, id).RowsAffected >0 }