pg_cooperation_contract_undertaker_repository.go
10.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
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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
package repository
import (
"fmt"
"github.com/go-pg/pg/v10"
"gitlab.fjmaimaimai.com/allied-creation/allied-creation-cooperation/pkg/infrastructure/utils"
"github.com/linmadan/egglib-go/persistent/pg/sqlbuilder"
pgTransaction "github.com/linmadan/egglib-go/transaction/pg"
"github.com/linmadan/egglib-go/utils/snowflake"
"gitlab.fjmaimaimai.com/allied-creation/allied-creation-cooperation/pkg/domain"
"gitlab.fjmaimaimai.com/allied-creation/allied-creation-cooperation/pkg/infrastructure/pg/models"
"gitlab.fjmaimaimai.com/allied-creation/allied-creation-cooperation/pkg/infrastructure/pg/transform"
)
type CooperationContractUndertakerRepository struct {
transactionContext *pgTransaction.TransactionContext
}
func (repository *CooperationContractUndertakerRepository) nextIdentify() (int64, error) {
IdWorker, err := snowflake.NewIdWorker(1)
if err != nil {
return 0, err
}
id, err := IdWorker.NextId()
return id, err
}
func (repository *CooperationContractUndertakerRepository) Save(cooperationContractUndertaker *domain.CooperationContractUndertaker) (*domain.CooperationContractUndertaker, error) {
sqlBuildFields := []string{
"cooperation_contract_undertaker_id",
"cooperation_contract_number",
"user_id",
"user_base_id",
"org",
"orgs",
"department",
"role",
"user_info",
"user_type",
"referrer",
"salesman",
"status",
"company",
"contract_attachment",
"created_at",
"updated_at",
"deleted_at",
}
insertFieldsSnippet := sqlbuilder.SqlFieldsSnippet(sqlBuildFields)
insertPlaceHoldersSnippet := sqlbuilder.SqlPlaceHoldersSnippet(sqlBuildFields)
returningFieldsSnippet := sqlbuilder.SqlFieldsSnippet(sqlBuildFields)
updateFields := sqlbuilder.RemoveSqlFields(sqlBuildFields, "cooperationContractUndertaker_id")
updateFieldsSnippet := sqlbuilder.SqlUpdateFieldsSnippet(updateFields)
tx := repository.transactionContext.PgTx
if cooperationContractUndertaker.Identify() == nil {
cooperationContractUndertakerId, err := repository.nextIdentify()
if err != nil {
return cooperationContractUndertaker, err
} else {
cooperationContractUndertaker.CooperationContractUndertakerId = cooperationContractUndertakerId
}
if _, err := tx.QueryOne(
pg.Scan(
&cooperationContractUndertaker.CooperationContractUndertakerId,
&cooperationContractUndertaker.CooperationContractNumber,
&cooperationContractUndertaker.Undertaker.UserId,
&cooperationContractUndertaker.Undertaker.UserBaseId,
&cooperationContractUndertaker.Undertaker.Org,
&cooperationContractUndertaker.Undertaker.Orgs,
&cooperationContractUndertaker.Undertaker.Department,
&cooperationContractUndertaker.Undertaker.Roles,
&cooperationContractUndertaker.Undertaker.UserInfo,
&cooperationContractUndertaker.Undertaker.UserType,
&cooperationContractUndertaker.Undertaker.Referrer,
&cooperationContractUndertaker.Undertaker.Salesman,
&cooperationContractUndertaker.Undertaker.Status,
&cooperationContractUndertaker.Undertaker.Company,
&cooperationContractUndertaker.Undertaker.ContractAttachment,
&cooperationContractUndertaker.CreatedAt,
&cooperationContractUndertaker.UpdatedAt,
&cooperationContractUndertaker.DeletedAt,
),
fmt.Sprintf("INSERT INTO cooperation_contract_undertakers (%s) VALUES (%s) RETURNING %s", insertFieldsSnippet, insertPlaceHoldersSnippet, returningFieldsSnippet),
cooperationContractUndertaker.CooperationContractUndertakerId,
cooperationContractUndertaker.CooperationContractNumber,
&cooperationContractUndertaker.Undertaker.UserId,
&cooperationContractUndertaker.Undertaker.UserBaseId,
&cooperationContractUndertaker.Undertaker.Org,
&cooperationContractUndertaker.Undertaker.Orgs,
&cooperationContractUndertaker.Undertaker.Department,
&cooperationContractUndertaker.Undertaker.Roles,
&cooperationContractUndertaker.Undertaker.UserInfo,
&cooperationContractUndertaker.Undertaker.UserType,
&cooperationContractUndertaker.Undertaker.Referrer,
&cooperationContractUndertaker.Undertaker.Salesman,
&cooperationContractUndertaker.Undertaker.Status,
&cooperationContractUndertaker.Undertaker.Company,
&cooperationContractUndertaker.Undertaker.ContractAttachment,
cooperationContractUndertaker.CreatedAt,
cooperationContractUndertaker.UpdatedAt,
cooperationContractUndertaker.DeletedAt,
); err != nil {
return cooperationContractUndertaker, err
}
} else {
if _, err := tx.QueryOne(
pg.Scan(
&cooperationContractUndertaker.CooperationContractUndertakerId,
&cooperationContractUndertaker.CooperationContractNumber,
&cooperationContractUndertaker.Undertaker.UserId,
&cooperationContractUndertaker.Undertaker.UserBaseId,
&cooperationContractUndertaker.Undertaker.Org,
&cooperationContractUndertaker.Undertaker.Orgs,
&cooperationContractUndertaker.Undertaker.Department,
&cooperationContractUndertaker.Undertaker.Roles,
&cooperationContractUndertaker.Undertaker.UserInfo,
&cooperationContractUndertaker.Undertaker.UserType,
&cooperationContractUndertaker.Undertaker.Referrer,
&cooperationContractUndertaker.Undertaker.Salesman,
&cooperationContractUndertaker.Undertaker.Status,
&cooperationContractUndertaker.Undertaker.Company,
&cooperationContractUndertaker.Undertaker.ContractAttachment,
&cooperationContractUndertaker.CreatedAt,
&cooperationContractUndertaker.UpdatedAt,
&cooperationContractUndertaker.DeletedAt,
),
fmt.Sprintf("UPDATE cooperation_contract_undertakers SET %s WHERE cooperation_contract_undertaker_id=? RETURNING %s", updateFieldsSnippet, returningFieldsSnippet),
cooperationContractUndertaker.CooperationContractUndertakerId,
cooperationContractUndertaker.CooperationContractNumber,
cooperationContractUndertaker.Undertaker.UserId,
cooperationContractUndertaker.Undertaker.UserBaseId,
cooperationContractUndertaker.Undertaker.Org,
cooperationContractUndertaker.Undertaker.Orgs,
cooperationContractUndertaker.Undertaker.Department,
cooperationContractUndertaker.Undertaker.Roles,
cooperationContractUndertaker.Undertaker.UserInfo,
cooperationContractUndertaker.Undertaker.UserType,
cooperationContractUndertaker.Undertaker.Referrer,
cooperationContractUndertaker.Undertaker.Salesman,
cooperationContractUndertaker.Undertaker.Status,
cooperationContractUndertaker.Undertaker.Company,
cooperationContractUndertaker.Undertaker.ContractAttachment,
cooperationContractUndertaker.CreatedAt,
cooperationContractUndertaker.UpdatedAt,
cooperationContractUndertaker.DeletedAt,
cooperationContractUndertaker.Identify(),
); err != nil {
return cooperationContractUndertaker, err
}
}
return cooperationContractUndertaker, nil
}
func (repository *CooperationContractUndertakerRepository) Remove(cooperationContractUndertaker *domain.CooperationContractUndertaker) (*domain.CooperationContractUndertaker, error) {
tx := repository.transactionContext.PgTx
cooperationContractUndertakerModel := new(models.CooperationContractUndertaker)
cooperationContractUndertakerModel.CooperationContractUndertakerId = cooperationContractUndertaker.Identify().(int64)
if _, err := tx.Model(cooperationContractUndertakerModel).WherePK().Delete(); err != nil {
return cooperationContractUndertaker, err
}
return cooperationContractUndertaker, nil
}
func (repository *CooperationContractUndertakerRepository) FindOne(queryOptions map[string]interface{}) (*domain.CooperationContractUndertaker, error) {
tx := repository.transactionContext.PgTx
cooperationContractUndertakerModel := new(models.CooperationContractUndertaker)
query := sqlbuilder.BuildQuery(tx.Model(cooperationContractUndertakerModel), queryOptions)
query.SetWhereByQueryOption("cooperation_contract_undertaker.cooperation_contract_undertaker_id = ?", "cooperationContractUndertakerId")
if err := query.First(); err != nil {
if err.Error() == "pg: no rows in result set" {
return nil, fmt.Errorf("合约承接人不存在")
} else {
return nil, err
}
}
if cooperationContractUndertakerModel.CooperationContractUndertakerId == 0 {
return nil, nil
} else {
return transform.TransformToCooperationContractUndertakerDomainModelFromPgModels(cooperationContractUndertakerModel)
}
}
func (repository *CooperationContractUndertakerRepository) Find(queryOptions map[string]interface{}) (int64, []*domain.CooperationContractUndertaker, error) {
tx := repository.transactionContext.PgTx
var cooperationContractUndertakerModels []*models.CooperationContractUndertaker
cooperationContractUndertakers := make([]*domain.CooperationContractUndertaker, 0)
query := sqlbuilder.BuildQuery(tx.Model(&cooperationContractUndertakerModels), queryOptions)
if companyId, ok := queryOptions["companyId"]; ok && companyId.(int64) != 0 {
query.Where("company->>'companyId' = '?'", companyId)
}
if orgId, ok := queryOptions["orgId"]; ok && orgId.(int64) != 0 {
query.Where("org->>'orgId' = '?'", orgId)
}
if orgIds, ok := queryOptions["orgIds"]; ok && len(orgIds.([]int64)) > 0 {
newOrgIds := utils.SliceItoa(orgIds.([]int64))
query.Where("org->>'orgId' in (?)", pg.In(newOrgIds))
}
if userBaseId, ok := queryOptions["userBaseId"]; ok && userBaseId.(int64) != 0 {
query.Where("user_base_id = ? ", userBaseId)
}
offsetLimitFlag := true
if offsetLimit, ok := queryOptions["offsetLimit"]; ok {
offsetLimitFlag = offsetLimit.(bool)
}
if offsetLimitFlag {
query.SetOffsetAndLimit(20)
}
query.SetOrderDirect("cooperation_contract_undertaker_id", "DESC")
if count, err := query.SelectAndCount(); err != nil {
return 0, cooperationContractUndertakers, err
} else {
for _, cooperationContractUndertakerModel := range cooperationContractUndertakerModels {
if cooperationContractUndertaker, err := transform.TransformToCooperationContractUndertakerDomainModelFromPgModels(cooperationContractUndertakerModel); err != nil {
return 0, cooperationContractUndertakers, err
} else {
cooperationContractUndertakers = append(cooperationContractUndertakers, cooperationContractUndertaker)
}
}
return int64(count), cooperationContractUndertakers, nil
}
}
func NewCooperationContractUndertakerRepository(transactionContext *pgTransaction.TransactionContext) (*CooperationContractUndertakerRepository, error) {
if transactionContext == nil {
return nil, fmt.Errorf("transactionContext参数不能为nil")
} else {
return &CooperationContractUndertakerRepository{
transactionContext: transactionContext,
}, nil
}
}