pg_sys_message_produce_repository.go
3.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
package repository
import (
"fmt"
"github.com/tiptok/gocomm/common"
. "github.com/tiptok/gocomm/pkg/orm/pgx"
"gitlab.fjmaimaimai.com/mmm-go/partner/pkg/domain"
"gitlab.fjmaimaimai.com/mmm-go/partner/pkg/infrastructure/pg/models"
"gitlab.fjmaimaimai.com/mmm-go/partner/pkg/infrastructure/pg/transaction"
)
type SysMessageProduceRepository struct {
transactionContext *transaction.TransactionContext
}
func (repository *SysMessageProduceRepository) Save(dm *domain.SysMessageProduce) (*domain.SysMessageProduce, error) {
var (
err error
m = &models.SysMessageProduce{}
tx = repository.transactionContext.PgTx
)
if err = common.GobModelTransform(m, dm); err != nil {
return nil, err
}
if _, err = tx.Model(m).Returning("id").Insert(m); err != nil {
return nil, err
}
return dm, nil
}
func (repository *SysMessageProduceRepository) Remove(SysMessageProduce *domain.SysMessageProduce) (*domain.SysMessageProduce, error) {
var (
tx = repository.transactionContext.PgTx
SysMessageProduceModel = &models.SysMessageProduce{Id: SysMessageProduce.Identify().(int64)}
)
if _, err := tx.Model(SysMessageProduceModel).Where("id = ?", SysMessageProduce.Id).Delete(); err != nil {
return SysMessageProduce, err
}
return SysMessageProduce, nil
}
func (repository *SysMessageProduceRepository) FindOne(queryOptions map[string]interface{}) (*domain.SysMessageProduce, error) {
tx := repository.transactionContext.PgTx
SysMessageProduceModel := new(models.SysMessageProduce)
query := NewQuery(tx.Model(SysMessageProduceModel), queryOptions)
query.SetWhere("id = ?", "id")
if err := query.First(); err != nil {
return nil, fmt.Errorf("query row not found")
}
if SysMessageProduceModel.Id == 0 {
return nil, fmt.Errorf("query row not found")
}
return repository.transformPgModelToDomainModel(SysMessageProduceModel)
}
func (repository *SysMessageProduceRepository) Find(queryOptions map[string]interface{}) (int64, []*domain.SysMessageProduce, error) {
tx := repository.transactionContext.PgDd
var SysMessageProduceModels []*models.SysMessageProduce
SysMessageProduces := make([]*domain.SysMessageProduce, 0)
query := NewQuery(tx.Model(&SysMessageProduceModels), queryOptions).
SetWhere("status = ?", "status").
SetOrder("update_time", "sortByUpdateTime")
var err error
if query.AffectRow, err = query.SelectAndCount(); err != nil {
return 0, SysMessageProduces, err
}
for _, SysMessageProduceModel := range SysMessageProduceModels {
if SysMessageProduce, err := repository.transformPgModelToDomainModel(SysMessageProduceModel); err != nil {
return 0, SysMessageProduces, err
} else {
SysMessageProduces = append(SysMessageProduces, SysMessageProduce)
}
}
return int64(query.AffectRow), SysMessageProduces, nil
}
func (repository *SysMessageProduceRepository) transformPgModelToDomainModel(SysMessageProduceModel *models.SysMessageProduce) (*domain.SysMessageProduce, error) {
m := &domain.SysMessageProduce{}
err := common.GobModelTransform(m, SysMessageProduceModel)
return m, err
}
func NewSysMessageProduceRepository(transactionContext *transaction.TransactionContext) (*SysMessageProduceRepository, error) {
if transactionContext == nil {
return nil, fmt.Errorf("transactionContext参数不能为nil")
}
return &SysMessageProduceRepository{transactionContext: transactionContext}, nil
}