/
publisher.go
152 lines (135 loc) · 4.64 KB
/
publisher.go
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
// Copyright (c) 2021 Terminus, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package dao
import (
"github.com/jinzhu/gorm"
"github.com/erda-project/erda/apistructs"
"github.com/erda-project/erda/modules/dop/model"
"github.com/erda-project/erda/pkg/strutil"
)
// CreatePublisher 创建Publisher
func (client *DBClient) CreatePublisher(publisher *model.Publisher) error {
return client.Create(publisher).Error
}
// UpdatePublisher 更新Publisher
func (client *DBClient) UpdatePublisher(publisher *model.Publisher) error {
return client.Save(publisher).Error
}
// DeletePublisher 删除Publisher
func (client *DBClient) DeletePublisher(publisherID int64) error {
return client.Where("id = ?", publisherID).Delete(&model.Publisher{}).Error
}
// GetPublisherByID 根据publisherID获取Publisher信息
func (client *DBClient) GetPublisherByID(publisherID int64) (model.Publisher, error) {
var publisher model.Publisher
if err := client.Where("id = ?", publisherID).Find(&publisher).Error; err != nil {
if gorm.IsRecordNotFoundError(err) {
return publisher, ErrNotFoundPublisher
}
return publisher, err
}
return publisher, nil
}
// GetPublishersByOrgIDAndName 根据orgID与名称获取Publisher列表
func (client *DBClient) GetPublishersByOrgIDAndName(orgID int64, params *apistructs.PublisherListRequest) (
int, []model.Publisher, error) {
var (
publishers []model.Publisher
total int
)
db := client.Where("org_id = ?", orgID)
if params.Name != "" {
db = db.Where("name = ?", params.Name)
}
if params.Query != "" {
db = db.Where("name LIKE ?", strutil.Concat("%", params.Query, "%"))
}
db = db.Order("updated_at DESC")
if err := db.Offset((params.PageNo - 1) * params.PageSize).Limit(params.PageSize).
Find(&publishers).Error; err != nil {
return 0, nil, err
}
// 获取总量
db = client.Model(&model.Publisher{}).Where("org_id = ?", orgID)
if params.Name != "" {
db = db.Where("name = ?", params.Name)
}
if params.Query != "" {
db = db.Where("name LIKE ?", strutil.Concat("%", params.Query, "%"))
}
if err := db.Count(&total).Error; err != nil {
return 0, nil, err
}
return total, publishers, nil
}
// GetPublishersByIDs 根据publisherIDs获取Publisher列表
func (client *DBClient) GetPublishersByIDs(publisherIDs []int64, params *apistructs.PublisherListRequest) (
int, []model.Publisher, error) {
var (
total int
publishers []model.Publisher
)
db := client.Where("id in (?)", publisherIDs)
if params.Name != "" {
db = db.Where("name = ?", params.Name)
}
if params.Query != "" {
db = db.Where("name LIKE ?", strutil.Concat("%", params.Query, "%"))
}
db = db.Order("updated_at DESC")
if err := db.Offset((params.PageNo - 1) * params.PageSize).Limit(params.PageSize).
Find(&publishers).Error; err != nil {
return 0, nil, err
}
// 获取总量
db = client.Model(&model.Publisher{}).Where("id in (?)", publisherIDs)
if params.Name != "" {
db = db.Where("name = ?", params.Name)
}
if params.Query != "" {
db = db.Where("name LIKE ?", strutil.Concat("%", params.Query, "%"))
}
if err := db.Count(&total).Error; err != nil {
return 0, nil, err
}
return total, publishers, nil
}
// GetPublisherByOrgAndName 根据orgID & Publisher名称 获取Publisher
func (client *DBClient) GetPublisherByOrgAndName(orgID int64, name string) (*model.Publisher, error) {
var publisher model.Publisher
if err := client.Where("org_id = ?", orgID).
Where("name = ?", name).Find(&publisher).Error; err != nil {
return nil, err
}
return &publisher, nil
}
// GetPublishersByIDs 获取企业的Publisher个数
func (client *DBClient) GetOrgPublishersCount(orgID uint64) (int64, error) {
var count int64
if err := client.Model(&model.Publisher{}).Where("org_id = ?", orgID).Count(&count).Error; err != nil {
return 0, err
}
return count, nil
}
// GetPublisherByOrgID 根据orgID 获取Publisher
func (client *DBClient) GetPublisherByOrgID(orgID int64) (*model.Publisher, error) {
var publisher model.Publisher
if err := client.Where("org_id = ?", orgID).First(&publisher).Error; err != nil {
if gorm.IsRecordNotFoundError(err) {
return nil, ErrNotFoundPublisher
}
return nil, err
}
return &publisher, nil
}