|
|
1
|
+package repository
|
|
|
2
|
+
|
|
|
3
|
+import (
|
|
|
4
|
+ "fmt"
|
|
|
5
|
+
|
|
|
6
|
+ "github.com/linmadan/egglib-go/persistent/pg/sqlbuilder"
|
|
|
7
|
+ pgTransaction "github.com/linmadan/egglib-go/transaction/pg"
|
|
|
8
|
+ "github.com/linmadan/egglib-go/utils/snowflake"
|
|
|
9
|
+ "gitlab.fjmaimaimai.com/allied-creation/allied-creation-cooperation/pkg/domain"
|
|
|
10
|
+ "gitlab.fjmaimaimai.com/allied-creation/allied-creation-cooperation/pkg/infrastructure/pg/models"
|
|
|
11
|
+ "gitlab.fjmaimaimai.com/allied-creation/allied-creation-cooperation/pkg/infrastructure/pg/transform"
|
|
|
12
|
+)
|
|
|
13
|
+
|
|
|
14
|
+type CooperationContractUndertakerRepository struct {
|
|
|
15
|
+ transactionContext *pgTransaction.TransactionContext
|
|
|
16
|
+}
|
|
|
17
|
+
|
|
|
18
|
+func (repository *CooperationContractUndertakerRepository) nextIdentify() (int64, error) {
|
|
|
19
|
+ IdWorker, err := snowflake.NewIdWorker(1)
|
|
|
20
|
+ if err != nil {
|
|
|
21
|
+ return 0, err
|
|
|
22
|
+ }
|
|
|
23
|
+ id, err := IdWorker.NextId()
|
|
|
24
|
+ return id, err
|
|
|
25
|
+}
|
|
|
26
|
+func (repository *CooperationContractUndertakerRepository) Save(cooperationContractUndertaker *domain.CooperationContractUndertaker) (*domain.CooperationContractUndertaker, error) {
|
|
|
27
|
+ sqlBuildFields := []string{}
|
|
|
28
|
+ insertFieldsSnippet := sqlbuilder.SqlFieldsSnippet(sqlBuildFields)
|
|
|
29
|
+ insertPlaceHoldersSnippet := sqlbuilder.SqlPlaceHoldersSnippet(sqlBuildFields)
|
|
|
30
|
+ returningFieldsSnippet := sqlbuilder.SqlFieldsSnippet(sqlBuildFields)
|
|
|
31
|
+ updateFields := sqlbuilder.RemoveSqlFields(sqlBuildFields, "cooperationContractUndertaker_id")
|
|
|
32
|
+ updateFieldsSnippet := sqlbuilder.SqlUpdateFieldsSnippet(updateFields)
|
|
|
33
|
+ tx := repository.transactionContext.PgTx
|
|
|
34
|
+ if cooperationContractUndertaker.Identify() == nil {
|
|
|
35
|
+ cooperationContractUndertakerId, err := repository.nextIdentify()
|
|
|
36
|
+ if err != nil {
|
|
|
37
|
+ return cooperationContractUndertaker, err
|
|
|
38
|
+ } else {
|
|
|
39
|
+ cooperationContractUndertaker.CooperationContractUndertakerId = cooperationContractUndertakerId
|
|
|
40
|
+ }
|
|
|
41
|
+ if _, err := tx.QueryOne(
|
|
|
42
|
+ pg.Scan(),
|
|
|
43
|
+ fmt.Sprintf("INSERT INTO cooperation_contract_undertakers (%s) VALUES (%s) RETURNING %s", insertFieldsSnippet, insertPlaceHoldersSnippet, returningFieldsSnippet),
|
|
|
44
|
+ ); err != nil {
|
|
|
45
|
+ return cooperationContractUndertaker, err
|
|
|
46
|
+ }
|
|
|
47
|
+ } else {
|
|
|
48
|
+ if _, err := tx.QueryOne(
|
|
|
49
|
+ pg.Scan(),
|
|
|
50
|
+ fmt.Sprintf("UPDATE cooperation_contract_undertakers SET %s WHERE cooperation_contract_undertaker_id=? RETURNING %s", updateFieldsSnippet, returningFieldsSnippet),
|
|
|
51
|
+ cooperationContractUndertaker.Identify(),
|
|
|
52
|
+ ); err != nil {
|
|
|
53
|
+ return cooperationContractUndertaker, err
|
|
|
54
|
+ }
|
|
|
55
|
+ }
|
|
|
56
|
+ return cooperationContractUndertaker, nil
|
|
|
57
|
+}
|
|
|
58
|
+func (repository *CooperationContractUndertakerRepository) Remove(cooperationContractUndertaker *domain.CooperationContractUndertaker) (*domain.CooperationContractUndertaker, error) {
|
|
|
59
|
+ tx := repository.transactionContext.PgTx
|
|
|
60
|
+ cooperationContractUndertakerModel := new(models.CooperationContractUndertaker)
|
|
|
61
|
+ cooperationContractUndertakerModel.CooperationContractUndertakerId = cooperationContractUndertaker.Identify().(int64)
|
|
|
62
|
+ if _, err := tx.Model(cooperationContractUndertakerModel).WherePK().Delete(); err != nil {
|
|
|
63
|
+ return cooperationContractUndertaker, err
|
|
|
64
|
+ }
|
|
|
65
|
+ return cooperationContractUndertaker, nil
|
|
|
66
|
+}
|
|
|
67
|
+func (repository *CooperationContractUndertakerRepository) FindOne(queryOptions map[string]interface{}) (*domain.CooperationContractUndertaker, error) {
|
|
|
68
|
+ tx := repository.transactionContext.PgTx
|
|
|
69
|
+ cooperationContractUndertakerModel := new(models.CooperationContractUndertaker)
|
|
|
70
|
+ query := sqlbuilder.BuildQuery(tx.Model(cooperationContractUndertakerModel), queryOptions)
|
|
|
71
|
+ query.SetWhereByQueryOption("cooperation_contract_undertaker.cooperation_contract_undertaker_id = ?", "cooperationContractUndertakerId")
|
|
|
72
|
+ if err := query.First(); err != nil {
|
|
|
73
|
+ if err.Error() == "pg: no rows in result set" {
|
|
|
74
|
+ return nil, fmt.Errorf("没有此资源")
|
|
|
75
|
+ } else {
|
|
|
76
|
+ return nil, err
|
|
|
77
|
+ }
|
|
|
78
|
+ }
|
|
|
79
|
+ if cooperationContractUndertakerModel.CooperationContractUndertakerId == 0 {
|
|
|
80
|
+ return nil, nil
|
|
|
81
|
+ } else {
|
|
|
82
|
+ return transform.TransformToCooperationContractUndertakerDomainModelFromPgModels(cooperationContractUndertakerModel)
|
|
|
83
|
+ }
|
|
|
84
|
+}
|
|
|
85
|
+func (repository *CooperationContractUndertakerRepository) Find(queryOptions map[string]interface{}) (int64, []*domain.CooperationContractUndertaker, error) {
|
|
|
86
|
+ tx := repository.transactionContext.PgTx
|
|
|
87
|
+ var cooperationContractUndertakerModels []*models.CooperationContractUndertaker
|
|
|
88
|
+ cooperationContractUndertakers := make([]*domain.CooperationContractUndertaker, 0)
|
|
|
89
|
+ query := sqlbuilder.BuildQuery(tx.Model(&cooperationContractUndertakerModels), queryOptions)
|
|
|
90
|
+ query.SetOffsetAndLimit(20)
|
|
|
91
|
+ query.SetOrderDirect("cooperation_contract_undertaker_id", "DESC")
|
|
|
92
|
+ if count, err := query.SelectAndCount(); err != nil {
|
|
|
93
|
+ return 0, cooperationContractUndertakers, err
|
|
|
94
|
+ } else {
|
|
|
95
|
+ for _, cooperationContractUndertakerModel := range cooperationContractUndertakerModels {
|
|
|
96
|
+ if cooperationContractUndertaker, err := transform.TransformToCooperationContractUndertakerDomainModelFromPgModels(cooperationContractUndertakerModel); err != nil {
|
|
|
97
|
+ return 0, cooperationContractUndertakers, err
|
|
|
98
|
+ } else {
|
|
|
99
|
+ cooperationContractUndertakers = append(cooperationContractUndertakers, cooperationContractUndertaker)
|
|
|
100
|
+ }
|
|
|
101
|
+ }
|
|
|
102
|
+ return int64(count), cooperationContractUndertakers, nil
|
|
|
103
|
+ }
|
|
|
104
|
+}
|
|
|
105
|
+func NewCooperationContractUndertakerRepository(transactionContext *pgTransaction.TransactionContext) (*CooperationContractUndertakerRepository, error) {
|
|
|
106
|
+ if transactionContext == nil {
|
|
|
107
|
+ return nil, fmt.Errorf("transactionContext参数不能为nil")
|
|
|
108
|
+ } else {
|
|
|
109
|
+ return &CooperationContractUndertakerRepository{
|
|
|
110
|
+ transactionContext: transactionContext,
|
|
|
111
|
+ }, nil
|
|
|
112
|
+ }
|
|
|
113
|
+} |