pg_device_collection_repository.go
6.4 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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
package repository
import (
"fmt"
"github.com/go-pg/pg/v10"
"gitlab.fjmaimaimai.com/allied-creation/allied-creation-manufacture/pkg/infrastructure/utils"
"time"
"github.com/linmadan/egglib-go/persistent/pg/sqlbuilder"
pgTransaction "github.com/linmadan/egglib-go/transaction/pg"
"gitlab.fjmaimaimai.com/allied-creation/allied-creation-manufacture/pkg/domain"
"gitlab.fjmaimaimai.com/allied-creation/allied-creation-manufacture/pkg/infrastructure/pg/models"
"gitlab.fjmaimaimai.com/allied-creation/allied-creation-manufacture/pkg/infrastructure/pg/transform"
)
type DeviceCollectionRepository struct {
transactionContext *pgTransaction.TransactionContext
}
func (repository *DeviceCollectionRepository) nextIdentify() (int64, error) {
//IdWorker, err := snowflake.NewIdWorker(1)
//if err != nil {
// return 0, err
//}
//id, err := IdWorker.NextId()
return utils.NewSnowflakeId()
}
func (repository *DeviceCollectionRepository) Save(deviceCollection *domain.DeviceCollection) (*domain.DeviceCollection, error) {
sqlBuildFields := []string{
"device_collection_id",
"work_shop_name",
"device_sn",
"device_type",
"collection_time",
"startup_status",
"com_status",
"values",
}
insertFieldsSnippet := sqlbuilder.SqlFieldsSnippet(sqlBuildFields)
insertPlaceHoldersSnippet := sqlbuilder.SqlPlaceHoldersSnippet(sqlBuildFields)
returningFieldsSnippet := sqlbuilder.SqlFieldsSnippet(sqlBuildFields)
updateFields := sqlbuilder.RemoveSqlFields(sqlBuildFields, "device_collection_id")
updateFieldsSnippet := sqlbuilder.SqlUpdateFieldsSnippet(updateFields)
tx := repository.transactionContext.PgTx
if deviceCollection.Identify() == nil {
deviceCollectionId, err := repository.nextIdentify()
if err != nil {
return deviceCollection, err
} else {
deviceCollection.DeviceCollectionId = deviceCollectionId
}
if _, err := tx.QueryOne(
pg.Scan(
&deviceCollection.DeviceCollectionId,
&deviceCollection.WorkShopName,
&deviceCollection.DeviceSn,
&deviceCollection.DeviceType,
&deviceCollection.CollectionTime,
&deviceCollection.StartupStatus,
&deviceCollection.ComStatus,
&deviceCollection.Values,
),
fmt.Sprintf("INSERT INTO manufacture.device_collections (%s) VALUES (%s) RETURNING %s", insertFieldsSnippet, insertPlaceHoldersSnippet, returningFieldsSnippet),
deviceCollection.DeviceCollectionId,
deviceCollection.WorkShopName,
deviceCollection.DeviceSn,
deviceCollection.DeviceType,
deviceCollection.CollectionTime,
deviceCollection.StartupStatus,
deviceCollection.ComStatus,
deviceCollection.Values,
); err != nil {
return deviceCollection, err
}
} else {
if _, err := tx.QueryOne(
pg.Scan(
&deviceCollection.DeviceCollectionId,
&deviceCollection.WorkShopName,
&deviceCollection.DeviceSn,
&deviceCollection.DeviceType,
&deviceCollection.CollectionTime,
&deviceCollection.StartupStatus,
&deviceCollection.ComStatus,
&deviceCollection.Values,
),
fmt.Sprintf("UPDATE manufacture.device_collections SET %s WHERE device_collection_id=? RETURNING %s", updateFieldsSnippet, returningFieldsSnippet),
//deviceCollection.DeviceCollectionId,
deviceCollection.WorkShopName,
deviceCollection.DeviceSn,
deviceCollection.DeviceType,
deviceCollection.CollectionTime,
deviceCollection.StartupStatus,
deviceCollection.ComStatus,
deviceCollection.Values,
deviceCollection.Identify(),
); err != nil {
return deviceCollection, err
}
}
return deviceCollection, nil
}
func (repository *DeviceCollectionRepository) Remove(deviceCollection *domain.DeviceCollection) (*domain.DeviceCollection, error) {
tx := repository.transactionContext.PgTx
deviceCollectionModel := new(models.DeviceCollection)
deviceCollectionModel.DeviceCollectionId = deviceCollection.Identify().(int64)
if _, err := tx.Model(deviceCollectionModel).WherePK().Delete(); err != nil {
return deviceCollection, err
}
return deviceCollection, nil
}
func (repository *DeviceCollectionRepository) FindOne(queryOptions map[string]interface{}) (*domain.DeviceCollection, error) {
tx := repository.transactionContext.PgTx
deviceCollectionModel := new(models.DeviceCollection)
query := sqlbuilder.BuildQuery(tx.Model(deviceCollectionModel), queryOptions)
query.SetWhereByQueryOption("device_collection.device_collection_id = ?", "deviceCollectionId")
if err := query.First(); err != nil {
if err.Error() == "pg: no rows in result set" {
return nil, fmt.Errorf("没有此资源")
} else {
return nil, err
}
}
if deviceCollectionModel.DeviceCollectionId == 0 {
return nil, nil
} else {
return transform.TransformToDeviceCollectionDomainModelFromPgModels(deviceCollectionModel)
}
}
func (repository *DeviceCollectionRepository) Find(queryOptions map[string]interface{}) (int64, []*domain.DeviceCollection, error) {
tx := repository.transactionContext.PgTx
var deviceCollectionModels []*models.DeviceCollection
deviceCollections := make([]*domain.DeviceCollection, 0)
query := sqlbuilder.BuildQuery(tx.Model(&deviceCollectionModels), queryOptions)
query.SetOffsetAndLimit(20)
query.SetOrderDirect("device_collection_id", "DESC")
if deviceType, ok := queryOptions["deviceType"]; ok && deviceType != "" {
query.SetWhereByQueryOption("device_type = ?", "deviceType")
}
if workShopName, ok := queryOptions["workShopName"]; ok && workShopName.(string) != "" {
query.SetWhereByQueryOption("work_shop_name = ?", "workShopName")
}
if v, ok := queryOptions["productBeginTime"]; ok && !((v.(time.Time)).IsZero()) {
query.Where("collection_time>=?", v.(time.Time))
}
if v, ok := queryOptions["productEndTime"]; ok && !((v.(time.Time)).IsZero()) {
query.Where("collection_time<?", v.(time.Time))
}
if count, err := query.SelectAndCount(); err != nil {
return 0, deviceCollections, err
} else {
for _, deviceCollectionModel := range deviceCollectionModels {
if deviceCollection, err := transform.TransformToDeviceCollectionDomainModelFromPgModels(deviceCollectionModel); err != nil {
return 0, deviceCollections, err
} else {
deviceCollections = append(deviceCollections, deviceCollection)
}
}
return int64(count), deviceCollections, nil
}
}
func NewDeviceCollectionRepository(transactionContext *pgTransaction.TransactionContext) (*DeviceCollectionRepository, error) {
if transactionContext == nil {
return nil, fmt.Errorf("transactionContext参数不能为nil")
} else {
return &DeviceCollectionRepository{
transactionContext: transactionContext,
}, nil
}
}