zhangzengfei
2023-09-04 e8e536d1cb52d2126c8c7ce2ba1c7a76f7208678
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
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
}