/
issue_stream.go
101 lines (87 loc) · 3.14 KB
/
issue_stream.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
// 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/erda-project/erda/apistructs"
"github.com/erda-project/erda/pkg/database/dbengine"
)
// IssueStream 事件流水表
type IssueStream struct {
dbengine.BaseModel
IssueID int64 // issue id
Operator string // 操作人
StreamType apistructs.IssueStreamType // 通过事件流类型找到对应模板
StreamParams apistructs.ISTParam // 事件流模板值, 参数取值范围见: apistructs.ISTParam
}
// TableName 表名
func (IssueStream) TableName() string {
return "dice_issue_streams"
}
// Convert 事件流格式转换
func (stream *IssueStream) Convert(content string) apistructs.IssueStream {
return apistructs.IssueStream{
ID: int64(stream.ID),
IssueID: stream.IssueID,
Operator: stream.Operator,
StreamType: stream.StreamType,
Content: content,
CreatedAt: stream.CreatedAt,
UpdatedAt: stream.UpdatedAt,
}
}
// CreateIssueStream 创建 issue 事件流
func (client *DBClient) CreateIssueStream(is *IssueStream) error {
return client.Create(is).Error
}
// UpdateIssueStream 更新 issue 事件流
func (client *DBClient) UpdateIssueStream(is *IssueStream) error {
return client.Update(is).Error
}
// PagingIssueStream issue 分页查询事件流
func (client *DBClient) PagingIssueStream(req *apistructs.IssueStreamPagingRequest) (int64, []IssueStream, error) {
var (
total int64
issueStreams []IssueStream
)
if err := client.Where("issue_id = ?", req.IssueID).
Order("id DESC").
Offset((req.PageNo - 1) * req.PageSize).Limit(req.PageSize).Find(&issueStreams).
// reset offset & limit before count
Offset(0).Limit(-1).Count(&total).Error; err != nil {
return 0, nil, err
}
return total, issueStreams, nil
}
func (client *DBClient) FindIssueStream(issueId int) ([]IssueStream, error) {
var issueStreams []IssueStream
if err := client.Where("issue_id = ?", issueId).
Order("id DESC").Find(&issueStreams).Error; err != nil {
return nil, err
}
return issueStreams, nil
}
type IssueStreamExtra struct {
IssueStream
ProjectID uint64
IssueType apistructs.IssueType
}
func (client *DBClient) ListIssueStreamExtraForIssueStateTransMigration() ([]IssueStreamExtra, error) {
var issueStreamExtras []IssueStreamExtra
err := client.Table("dice_issue_streams AS stream").
Select("stream.*, issue.project_id, issue.type AS issue_type").
Joins("LEFT JOIN dice_issues issue ON stream.issue_id = issue.id").
Where("stream.stream_type = ?", apistructs.ISTTransferState).
Find(&issueStreamExtras).Error
return issueStreamExtras, err
}