pg_cooperation_contract_undertaker_dao.go
3.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
package dao
import (
"fmt"
"github.com/go-pg/pg/v10"
"github.com/go-pg/pg/v10/orm"
"github.com/linmadan/egglib-go/persistent/pg/sqlbuilder"
pgTransaction "github.com/linmadan/egglib-go/transaction/pg"
"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"
"gitlab.fjmaimaimai.com/allied-creation/allied-creation-cooperation/pkg/infrastructure/utils"
)
type CooperationContractUndertakerDao struct {
transactionContext *pgTransaction.TransactionContext
}
func (repository *CooperationContractUndertakerDao) 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.WhereGroup(func(query *orm.Query) (*orm.Query, error) {
query.WhereOr("user_base_id = ? ", userBaseId)
query.WhereOr("referrer->>'userBaseId' = '?' ", userBaseId)
query.WhereOr("salesman->>'userBaseId' = '?' ", userBaseId)
return query, nil
})
}
if userId, ok := queryOptions["userId"]; ok && userId.(int64) != 0 {
query.WhereGroup(func(query *orm.Query) (*orm.Query, error) {
query.WhereOr("user_id = ? ", userId)
query.WhereOr("referrer->>'userId' = '?' ", userId)
query.WhereOr("salesman->>'userId' = '?' ", userId)
return query, nil
})
}
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 NewCooperationContractUndertakerDao(transactionContext *pgTransaction.TransactionContext) (*CooperationContractUndertakerDao, error) {
if transactionContext == nil {
return nil, fmt.Errorf("transactionContext参数不能为nil")
} else {
return &CooperationContractUndertakerDao{
transactionContext: transactionContext,
}, nil
}
}