|
|
1
|
+package dao
|
|
|
2
|
+
|
|
|
3
|
+import (
|
|
|
4
|
+ "fmt"
|
|
|
5
|
+ "github.com/go-pg/pg/v10"
|
|
|
6
|
+ "github.com/go-pg/pg/v10/orm"
|
|
|
7
|
+ "github.com/linmadan/egglib-go/persistent/pg/sqlbuilder"
|
|
|
8
|
+ pgTransaction "github.com/linmadan/egglib-go/transaction/pg"
|
|
|
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
|
+ "gitlab.fjmaimaimai.com/allied-creation/allied-creation-cooperation/pkg/infrastructure/utils"
|
|
|
13
|
+)
|
|
|
14
|
+
|
|
|
15
|
+type CooperationContractUndertakerDao struct {
|
|
|
16
|
+ transactionContext *pgTransaction.TransactionContext
|
|
|
17
|
+}
|
|
|
18
|
+
|
|
|
19
|
+func (repository *CooperationContractUndertakerDao) Find(queryOptions map[string]interface{}) (int64, []*domain.CooperationContractUndertaker, error) {
|
|
|
20
|
+ tx := repository.transactionContext.PgTx
|
|
|
21
|
+ var cooperationContractUndertakerModels []*models.CooperationContractUndertaker
|
|
|
22
|
+ cooperationContractUndertakers := make([]*domain.CooperationContractUndertaker, 0)
|
|
|
23
|
+ query := sqlbuilder.BuildQuery(tx.Model(&cooperationContractUndertakerModels), queryOptions)
|
|
|
24
|
+ if companyId, ok := queryOptions["companyId"]; ok && companyId.(int64) != 0 {
|
|
|
25
|
+ query.Where("company->>'companyId' = '?'", companyId)
|
|
|
26
|
+ }
|
|
|
27
|
+ if orgId, ok := queryOptions["orgId"]; ok && orgId.(int64) != 0 {
|
|
|
28
|
+ query.Where("org->>'orgId' = '?'", orgId)
|
|
|
29
|
+ }
|
|
|
30
|
+ if orgIds, ok := queryOptions["orgIds"]; ok && len(orgIds.([]int64)) > 0 {
|
|
|
31
|
+ newOrgIds := utils.SliceItoa(orgIds.([]int64))
|
|
|
32
|
+ query.Where("org->>'orgId' in (?)", pg.In(newOrgIds))
|
|
|
33
|
+ }
|
|
|
34
|
+ if userBaseId, ok := queryOptions["userBaseId"]; ok && userBaseId.(int64) != 0 {
|
|
|
35
|
+ query.WhereGroup(func(query *orm.Query) (*orm.Query, error) {
|
|
|
36
|
+ query.WhereOr("user_base_id = ? ", userBaseId)
|
|
|
37
|
+ query.WhereOr("referrer->>'userBaseId' = '?' ", userBaseId)
|
|
|
38
|
+ query.WhereOr("salesman->>'userBaseId' = '?' ", userBaseId)
|
|
|
39
|
+ return query, nil
|
|
|
40
|
+ })
|
|
|
41
|
+ }
|
|
|
42
|
+ offsetLimitFlag := true
|
|
|
43
|
+ if offsetLimit, ok := queryOptions["offsetLimit"]; ok {
|
|
|
44
|
+ offsetLimitFlag = offsetLimit.(bool)
|
|
|
45
|
+ }
|
|
|
46
|
+ if offsetLimitFlag {
|
|
|
47
|
+ query.SetOffsetAndLimit(20)
|
|
|
48
|
+ }
|
|
|
49
|
+ query.SetOrderDirect("cooperation_contract_undertaker_id", "DESC")
|
|
|
50
|
+ if count, err := query.SelectAndCount(); err != nil {
|
|
|
51
|
+ return 0, cooperationContractUndertakers, err
|
|
|
52
|
+ } else {
|
|
|
53
|
+ for _, cooperationContractUndertakerModel := range cooperationContractUndertakerModels {
|
|
|
54
|
+ if cooperationContractUndertaker, err := transform.TransformToCooperationContractUndertakerDomainModelFromPgModels(cooperationContractUndertakerModel); err != nil {
|
|
|
55
|
+ return 0, cooperationContractUndertakers, err
|
|
|
56
|
+ } else {
|
|
|
57
|
+ cooperationContractUndertakers = append(cooperationContractUndertakers, cooperationContractUndertaker)
|
|
|
58
|
+ }
|
|
|
59
|
+ }
|
|
|
60
|
+ return int64(count), cooperationContractUndertakers, nil
|
|
|
61
|
+ }
|
|
|
62
|
+}
|
|
|
63
|
+
|
|
|
64
|
+func NewCooperationContractUndertakerDao(transactionContext *pgTransaction.TransactionContext) (*CooperationContractUndertakerDao, error) {
|
|
|
65
|
+ if transactionContext == nil {
|
|
|
66
|
+ return nil, fmt.Errorf("transactionContext参数不能为nil")
|
|
|
67
|
+ } else {
|
|
|
68
|
+ return &CooperationContractUndertakerDao{
|
|
|
69
|
+ transactionContext: transactionContext,
|
|
|
70
|
+ }, nil
|
|
|
71
|
+ }
|
|
|
72
|
+} |