pg_cooperation_mode_dao.go
3.5 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
package dao
import (
"fmt"
"github.com/go-pg/pg/v10"
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"
)
type CooperationModeDao struct {
transactionContext *pgTransaction.TransactionContext
}
// CheckModeNameAvailable 校验模式名称唯一性
func (dao *CooperationModeDao) CheckModeNameAvailable(queryOptions map[string]interface{}) (bool, error) {
tx := dao.transactionContext.PgTx
var cooperationModeModels []*models.CooperationMode
query := tx.Model(&cooperationModeModels)
if cooperationModeName, ok := queryOptions["cooperationModeName"]; ok && cooperationModeName != "" {
query = query.Where("cooperation_mode_name = ?", cooperationModeName)
}
if companyId, ok := queryOptions["companyId"]; ok && companyId.(int64) != 0 {
query = query.Where(`cooperation_mode.company @> '{"companyId":"?"}'`, companyId)
}
if orgId, ok := queryOptions["orgId"]; ok && orgId.(int64) != 0 {
query = query.Where(`cooperation_mode.org @> '{"orgId":"?"}'`, orgId)
}
ok, err := query.Exists()
return !ok, err
}
// CheckModeNumberAvailable 校验模式编码唯一性
func (dao *CooperationModeDao) CheckModeNumberAvailable(queryOptions map[string]interface{}) (bool, error) {
tx := dao.transactionContext.PgTx
var cooperationModeModels []*models.CooperationMode
query := tx.Model(&cooperationModeModels)
if cooperationModeNumber, ok := queryOptions["cooperationModeNumber"]; ok && cooperationModeNumber != "" {
query = query.Where("cooperation_mode_number = ?", cooperationModeNumber)
}
if companyId, ok := queryOptions["companyId"]; ok && companyId.(int64) != 0 {
query = query.Where(`cooperation_mode.company @> '{"companyId":"?"}'`, companyId)
}
if orgId, ok := queryOptions["orgId"]; ok && orgId.(int64) != 0 {
query = query.Where(`cooperation_mode.org @> '{"orgId":"?"}'`, orgId)
}
ok, err := query.Exists()
return !ok, err
}
// UpdateCooperationModeSlice 更新多个共创模式
func (dao *CooperationModeDao) UpdateCooperationModeSlice(modeIds []int64, status int32) ([]*domain.CooperationMode, error) {
tx := dao.transactionContext.PgTx
_, err := tx.QueryOne(
pg.Scan(),
"UPDATE cooperation_modes SET status=? WHERE cooperation_mode_id IN (?)",
status, pg.In(modeIds))
if err != nil {
return nil, err
} else {
var cooperationModeModels []*models.CooperationMode
cooperationModes := make([]*domain.CooperationMode, 0)
query := tx.Model(&cooperationModeModels)
query.Where("cooperation_mode_id IN (?)", pg.In(modeIds))
query.Order("cooperation_mode_id DESC")
if _, err := query.SelectAndCount(); err != nil {
return cooperationModes, err
} else {
for _, cooperationModeModel := range cooperationModeModels {
if cooperationMode, err := transform.TransformToCooperationModeDomainModelFromPgModels(cooperationModeModel); err != nil {
return cooperationModes, err
} else {
cooperationModes = append(cooperationModes, cooperationMode)
}
}
return cooperationModes, nil
}
}
}
func NewCooperationModeDao(transactionContext *pgTransaction.TransactionContext) (*CooperationModeDao, error) {
if transactionContext == nil {
return nil, fmt.Errorf("transactionContext参数不能为nil")
} else {
return &CooperationModeDao{
transactionContext: transactionContext,
}, nil
}
}