push_device_info_repository.go
3.6 KB
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
package repository
import (
"github.com/astaxie/beego/orm"
"openapi/pkg/domain"
"openapi/pkg/infrastructure/bgorm/models"
"openapi/pkg/infrastructure/utils"
"strings"
"time"
)
type PushDeviceRepository struct {
}
func (repository *PushDeviceRepository) Save(device *domain.UpdateDeviceRequest) error {
o := orm.NewOrm()
m := &models.PushDeviceInfo{
Uid: device.Muid,
ClientId: strings.TrimSpace(device.ClientId),
Phone: device.Phone,
DeviceToken: strings.TrimSpace(device.DeviceToken),
CreateAt: time.Now(),
UpdateAt: time.Now(),
ProjectMasterKey: device.ProjectKey,
IsActive: 1,
}
_, err := o.Insert(m)
return err
}
func (repository *PushDeviceRepository) FindOne(queryOptions map[string]interface{}) (*domain.Device, error) {
o := orm.NewOrm()
model := new(models.PushDeviceInfo)
qs := o.QueryTable(model.TableName()).
Filter("uid", queryOptions["uid"]).
Filter("project_master_key", queryOptions["project_master_key"])
err := qs.One(model)
if err != nil {
if err == orm.ErrNoRows {
err = domain.ERR_DB_NOT_FOUND
}
return nil, err
}
return repository.transformBgormModelToDomainModel(model)
}
func (repository *PushDeviceRepository) Find(queryOptions map[string]interface{}) (rsp []*domain.Device, err error) {
o := orm.NewOrm()
model := new(models.PushDeviceInfo)
var ms []*models.PushDeviceInfo
qs := o.QueryTable(model.TableName()).Filter("uid__in", queryOptions["receivers"]).Filter("is_active", 1).Filter("project_master_key", queryOptions["project_master_key"])
qs.All(&ms)
if len(ms) == 0 {
return
}
for i := range ms {
v, _ := repository.transformBgormModelToDomainModel(ms[i])
rsp = append(rsp, v)
}
return
}
func (repository *PushDeviceRepository) UpdateDevice(uid int64, clientId, deviceToken string, projectKey string, phone string) error {
o := orm.NewOrm()
o.Begin()
//更新(这个项目)其他绑定这个client_id的设备 is_active=0
_, err := o.Raw("UPDATE push_device_info SET update_at=now(),is_active=0 where client_id=? and is_active=1 and project_master_key=?", clientId, projectKey).Exec()
if err != nil {
o.Rollback()
return err
}
_, err = o.Raw("UPDATE push_device_info SET client_id=?,device_token = ?,update_at=now(),is_active=1,phone=? where uid=? and project_master_key=?", clientId, deviceToken, phone, uid, projectKey).Exec()
if err != nil {
o.Rollback()
return err
}
o.Commit()
return nil
}
func (repository *PushDeviceRepository) SaveLog(request *domain.PushInfoRequest, extData map[string]interface{}, err error) error {
var (
result int
errmsg string
)
if err != nil {
result = 1
errmsg = err.Error()
if v, ok := err.(*domain.ErrWithMessage); ok {
result = v.Errno
}
}
sql := `INSERT INTO push_log(msg_type,project,receivers,message,result,err_msg,create_at,ext_data)VALUES(?,?,?,?,?,?,now(),?)`
o := orm.NewOrm()
_, err = o.Raw(sql,
request.Type,
request.ProjectKey,
utils.JsonAssertString(request.ActualReceivers),
utils.JsonAssertString(request),
result,
errmsg,
utils.JsonAssertString(extData),
).Exec()
if err != nil {
return err
}
return nil
}
func (repository *PushDeviceRepository) transformBgormModelToDomainModel(model *models.PushDeviceInfo) (*domain.Device, error) {
return &domain.Device{
Uid: model.Uid,
ClientId: model.ClientId,
DeviceToken: model.DeviceToken,
IsActive: model.IsActive,
}, nil
}
func NewPushDeviceRepository(transactionContext interface{}) (*PushDeviceRepository, error) {
if transactionContext == nil {
return &PushDeviceRepository{}, nil
} else {
return &PushDeviceRepository{}, nil
}
}