pg_log_sms_repository.go
2.3 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
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)
}
count, err := query.SelectAndCount()
if err != nil {
return 0, nil, err
}
var datas []*domain.LogSms
for _, v := range m {
d := repo.TransformToDomain(v)
datas = append(datas, d)
}
return count, datas, 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,
}
}