pg_log_sms_repository.go
3.1 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
129
130
131
132
133
134
package repository
import (
pgTransaction "github.com/linmadan/egglib-go/transaction/pg"
"gitlab.fjmaimaimai.com/allied-creation/performance/pkg/domain"
"gitlab.fjmaimaimai.com/allied-creation/performance/pkg/infrastructure/pg/models"
)
type LogSmsRepository struct {
transactionContext *pgTransaction.TransactionContext
}
func NewLogSmsRepository(transactionContext *pgTransaction.TransactionContext) *LogSmsRepository {
return &LogSmsRepository{transactionContext: transactionContext}
}
var _ domain.LogSmsRepository = (*LogSmsRepository)(nil)
func (repo *LogSmsRepository) Save(param *domain.LogSms) error {
m := models.LogSms{
Id: param.Id,
Phone: param.Phone,
TemplateId: param.TemplateId,
Template: param.Template,
Value: param.Value,
CreatedAt: param.CreatedAt,
Result: param.Result,
Status: string(param.Status),
From: param.From,
Index: param.Index,
ExecuteAt: param.ExecuteAt,
}
tx := repo.transactionContext.PgTx
if m.Id == 0 {
_, err := tx.Model(&m).Insert()
if err != nil {
return err
}
return nil
}
_, err := tx.Model(&m).WherePK().Update()
if err != nil {
return err
}
return nil
}
func (repo *LogSmsRepository) Find(queryOptions map[string]interface{}) (int, []*domain.LogSms, error) {
tx := repo.transactionContext.PgTx
var m []*models.LogSms
query := tx.Model(&m)
if v, ok := queryOptions["limit"].(int); ok {
query.Limit(v)
}
if v, ok := queryOptions["offset"].(int); ok {
query.Offset(v)
}
if v, ok := queryOptions["status"]; ok {
query.Where("status=?", v)
}
if v, ok := queryOptions["executeAtBegin"]; ok {
query.Where("execute_at>=?", v)
}
if v, ok := queryOptions["executeAtEnd"]; ok {
query.Where("execute_at<=?", v)
}
if v, ok := queryOptions["from"]; ok {
query.Where(`"from"=?`, v)
}
if v, ok := queryOptions["index"]; ok {
query.Where("index=?", v)
}
count, err := query.SelectAndCount()
if err != nil {
return 0, nil, err
}
arrays := make([]*domain.LogSms, 0)
for _, v := range m {
d := repo.TransformToDomain(v)
arrays = append(arrays, d)
}
return count, arrays, nil
}
func (repo *LogSmsRepository) TransformToDomain(d *models.LogSms) *domain.LogSms {
return &domain.LogSms{
Id: d.Id,
Phone: d.Phone,
TemplateId: d.TemplateId,
Template: d.Template,
Value: d.Value,
Result: d.Result,
Status: domain.SmsStatus(d.Status),
From: d.From,
Index: d.Index,
ExecuteAt: d.ExecuteAt,
CreatedAt: d.CreatedAt,
}
}
//批量添加
func (repo *LogSmsRepository) BatchInsert(params []*domain.LogSms) error {
smsList := []models.LogSms{}
for _, param := range params {
m := models.LogSms{
Id: param.Id,
Phone: param.Phone,
TemplateId: param.TemplateId,
Template: param.Template,
Value: param.Value,
CreatedAt: param.CreatedAt,
Result: param.Result,
Status: string(param.Status),
From: param.From,
Index: param.Index,
ExecuteAt: param.ExecuteAt,
}
smsList = append(smsList, m)
}
tx := repo.transactionContext.PgTx
_, err := tx.Model(&smsList).Insert()
if err != nil {
return err
}
return nil
}