zhangzengfei
2023-09-05 1b34d7bacad94933ad63fc0e199bd32ac49d9fa5
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 DeviceApply struct {
    Id                 string         `gorm:"column:id;primary_key;type:varchar(50);unique;not null;" json:"id"`
    DevId             string         `gorm:"column:devId;unique;not null;" json:"devId"`
    DevName         string         `gorm:"column:devName" json:"devName"`
    ApplyKey         string         `gorm:"column:applyKey" json:"applyKey"`
    Ip                 string         `gorm:"column:ip" json:"ip"`
    CreateTime         string         `gorm:"column:createTime" json:"createTime"`
    Status             int         `gorm:"column:status;default:0;" json:"status"`  //状态 0:已添加,待发送给对方 1:已成功发送请求到对方,等待反馈。 2:对方已同意 -2:对方已拒绝
}
 
const (
    ApplyStatus_KeyErr = -2   //密钥错误
    ApplyStatus_Reject = -1  //已拒绝
    ApplyStatus_Sending = 0  //等待发送给对方
    ApplyStatus_Waiting = 1  //等待响应
    ApplyStatus_Agreed = 2   //已同意
    ApplyStatus_Managed = 3  //已处于管理状态
)
 
func (DeviceApply) TableName() string {
    return "t_device_apply"
}
 
func (da *DeviceApply) Insert() bool {
    result := db.Table(da.TableName()).Create(&da)
    if result.Error == nil && result.RowsAffected > 0 {
        return true
    }
    return false
}
 
func (da *DeviceApply) FindByDevId(devId string) (int64,error) {
    result := db.Table(da.TableName()).Where("devId=?", devId).First(&da)
    if result.Error != nil || result.RowsAffected == 0 {
        return 0, result.Error
    }
    return result.RowsAffected, nil
}
 
func (da *DeviceApply) Update() bool {
    result := db.Table(da.TableName()).Where("id=?", da.Id).Update(&da)
    if result.Error ==nil && result.RowsAffected > 0 {
        return true
    }
    return false
}
 
func (da *DeviceApply) DeleteById(id string) bool {
    result := db.Exec("delete from "+da.TableName()+" where id='"+id+"'")
    if result.Error == nil && result.RowsAffected > 0 {
        return true
    }
    return false
}
 
func (da *DeviceApply) FindByStatus(status int) (list []DeviceApply) {
    err := db.Table(da.TableName()).Where("status=?", status).Find(&list).Error
    if err != nil {
        return nil
    }
    return list
}
 
func (da *DeviceApply) UpdateStatus(status int, id string) bool {
    return db.Exec("update "+da.TableName()+" set status=? where id=?", status, id).RowsAffected >0
}