byte_notice_logic.go
6.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
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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
package consumer
import (
"context"
"encoding/json"
"fmt"
"gitlab.fjmaimaimai.com/allied-creation/sumifcc-bchart/cmd/chart-server/api/internal/svc"
"gitlab.fjmaimaimai.com/allied-creation/sumifcc-bchart/cmd/chart-server/api/internal/types"
"gitlab.fjmaimaimai.com/allied-creation/sumifcc-bchart/cmd/chart-server/interanl/pkg/db/transaction"
"gitlab.fjmaimaimai.com/allied-creation/sumifcc-bchart/cmd/chart-server/interanl/pkg/domain"
"gitlab.fjmaimaimai.com/allied-creation/sumifcc-bchart/cmd/chart-server/interanl/pkg/gateway/bytelib"
)
type ByteNoticeLogic struct {
ctx context.Context
svcCtx *svc.ServiceContext
conn transaction.Conn
}
func NewByteNoticeLogic(svcCtx *svc.ServiceContext) *ByteNoticeLogic {
return &ByteNoticeLogic{
ctx: context.Background(),
svcCtx: svcCtx,
conn: svcCtx.DefaultDBConn(),
}
}
func (logic *ByteNoticeLogic) Consume(key, value string) error {
fmt.Println(key, value)
notice := &domain.ObjectNotice{}
err := json.Unmarshal([]byte(value), notice)
if err != nil {
return err
}
//保存推送消息
_, err = logic.svcCtx.ObjectNoticeRepository.Insert(logic.ctx, logic.conn, notice)
if err != nil {
return err
}
//处理消息
err = transaction.UseTrans(logic.ctx, logic.conn.DB(), func(ctx context.Context, conn transaction.Conn) error {
return logic.handleNotice(conn, notice)
}, true)
//更新处理结果
if err != nil {
notice.Status = domain.ObjectNoticeStatusError
notice.Message = err.Error()
} else {
notice.Status = domain.ObjectNoticeStatusDone
notice.Message = "OK"
}
_, _ = logic.svcCtx.ObjectNoticeRepository.Update(logic.ctx, logic.conn, notice)
return err
}
// handleNotice 处理消息
func (logic *ByteNoticeLogic) handleNotice(conn transaction.Conn, notice *domain.ObjectNotice) error {
//是否删除消息
if notice.IsDeletedEvent() {
return logic.handleDelete(conn, notice)
}
accessToken, _ := types.TableAccessToken{CompanyId: notice.CompanyId}.GenerateToken()
//结构变更
if notice.StructChanged {
request := bytelib.ObjectTableSearchRequest{
Token: accessToken,
Module: bytelib.ModuleDigitalCenter,
}
if notice.ObjectType == "导入模块" {
request.TableTypes = []string{bytelib.MainTable, bytelib.SubTable, bytelib.SideTable}
}
if notice.ObjectType == "拆解模块" {
request.TableTypes = []string{bytelib.SchemaTable}
request.ReturnGroupItem = true
}
if notice.ObjectType == "计算模块" {
request.TableTypes = []string{bytelib.CalculateItem, bytelib.CalculateSet}
request.ReturnGroupItem = true
request.ExcludeTables = []int{0}
}
list, err := logic.svcCtx.ByteMetadataService.ObjectTableSearch(logic.ctx, request)
if err != nil {
return err
}
tableInfo, err := logic.svcCtx.ByteMetadataService.TableInfo(logic.ctx, &bytelib.TableInfoRequest{
Token: accessToken,
TableId: notice.TableId,
})
if err != nil {
return nil
}
if len(list.List) > 0 {
objectTables := make([]*domain.ObjectTable, 0)
for _, item := range list.List {
if item.TableId == notice.TableId {
objectTables = append(objectTables, &domain.ObjectTable{
Id: item.Id,
TableId: item.TableId,
Name: item.Name,
TableType: item.TableType,
CompanyId: notice.CompanyId,
ParentId: item.ParentId,
Flag: item.Flag,
})
//父级节点
objectTables = append(objectTables, logic.getParents(notice.CompanyId, item.ParentId, list.List)...)
}
}
err = logic.saveTables(conn, objectTables)
if err != nil {
return err
}
}
//保存字段
_, err = logic.saveFields(conn, &domain.ObjectField{
Id: int64(tableInfo.TableId),
Name: tableInfo.Name,
Fields: tableInfo.Fields,
})
if err != nil {
return err
}
}
//数据变更
if notice.DataChanged {
data := &types.SyncTableDataPusher{
CompanyId: notice.CompanyId,
ObjectId: notice.TableId,
}
mBytes, _ := json.Marshal(data)
_, err := logic.svcCtx.Redis.LpushCtx(logic.ctx, logic.svcCtx.Config.Name+":table_data", string(mBytes))
return err
}
return nil
}
func (logic *ByteNoticeLogic) getParents(companyId int64, parentId int, list []*bytelib.Table) []*domain.ObjectTable {
result := make([]*domain.ObjectTable, 0)
for _, item := range list {
if item.Id == parentId {
result = append(result, &domain.ObjectTable{
Id: item.Id,
TableId: item.TableId,
Name: item.Name,
TableType: item.TableType,
CompanyId: companyId,
ParentId: item.ParentId,
Flag: item.Flag,
})
if item.ParentId > 0 {
result = append(result, logic.getParents(companyId, item.ParentId, list)...)
}
}
}
return result
}
// handleDelete 删除
func (logic *ByteNoticeLogic) handleDelete(conn transaction.Conn, notice *domain.ObjectNotice) error {
objectTable, err := logic.svcCtx.ObjectTableRepository.FindOneByTableId(logic.ctx, conn, notice.TableId)
if err == nil && objectTable.Id > 0 {
objectTable.RemoteDeleted = 1
_, err := logic.svcCtx.ObjectTableRepository.Update(logic.ctx, conn, objectTable)
if err != nil {
return err
}
}
//是否有使用数据源
used, err := logic.svcCtx.ChartSettingRepository.CheckUseDataSource(logic.ctx, conn, notice.TableId)
if err == nil && !used {
err = logic.svcCtx.ObjectTableDataRepository.DropTable(logic.ctx, conn, notice.TableId)
if err != nil {
return err
}
}
return nil
}
// saveTables 保存表结构
func (logic *ByteNoticeLogic) saveTables(conn transaction.Conn, tables []*domain.ObjectTable) error {
if len(tables) > 0 {
for _, item := range tables {
objectTable, err := logic.svcCtx.ObjectTableRepository.FindOne(logic.ctx, conn, item.Id)
if err == nil && objectTable.Id > 0 {
item.Id = objectTable.Id
item.Version = objectTable.Version + 1
_, err := logic.svcCtx.ObjectTableRepository.Update(logic.ctx, conn, item)
if err != nil {
return err
}
} else {
_, err := logic.svcCtx.ObjectTableRepository.Insert(logic.ctx, conn, item)
if err != nil {
return err
}
}
}
}
return nil
}
// saveFields 保存表字段
func (logic *ByteNoticeLogic) saveFields(conn transaction.Conn, objectField *domain.ObjectField) (*domain.ObjectField, error) {
mField, err := logic.svcCtx.ObjectFieldRepository.FindOne(logic.ctx, conn, objectField.Id)
if err == nil && mField.Id > 0 { //已存在 - 更新
objectField.Version = mField.Version + 1
return logic.svcCtx.ObjectFieldRepository.Update(logic.ctx, conn, objectField)
} else {
return logic.svcCtx.ObjectFieldRepository.Insert(logic.ctx, conn, objectField)
}
}