作者 庄敏学

获取数据加入本地存储

1 package main 1 package main
2 2
3 import ( 3 import (
  4 + "encoding/json"
4 "flag" 5 "flag"
  6 + "fmt"
5 "github.com/golang-jwt/jwt/v4/request" 7 "github.com/golang-jwt/jwt/v4/request"
6 "github.com/zeromicro/go-queue/kq" 8 "github.com/zeromicro/go-queue/kq"
7 "github.com/zeromicro/go-zero/core/logx" 9 "github.com/zeromicro/go-zero/core/logx"
@@ -10,6 +12,7 @@ import ( @@ -10,6 +12,7 @@ import (
10 "gitlab.fjmaimaimai.com/allied-creation/sumifcc-bchart/cmd/chart-server/interanl/pkg/domain" 12 "gitlab.fjmaimaimai.com/allied-creation/sumifcc-bchart/cmd/chart-server/interanl/pkg/domain"
11 "net/http" 13 "net/http"
12 "strings" 14 "strings"
  15 + "time"
13 16
14 "gitlab.fjmaimaimai.com/allied-creation/sumifcc-bchart/cmd/chart-server/api/internal/config" 17 "gitlab.fjmaimaimai.com/allied-creation/sumifcc-bchart/cmd/chart-server/api/internal/config"
15 "gitlab.fjmaimaimai.com/allied-creation/sumifcc-bchart/cmd/chart-server/api/internal/handler" 18 "gitlab.fjmaimaimai.com/allied-creation/sumifcc-bchart/cmd/chart-server/api/internal/handler"
@@ -67,26 +70,26 @@ func main() { @@ -67,26 +70,26 @@ func main() {
67 func startConsume(c config.Config) { 70 func startConsume(c config.Config) {
68 svcCtx := svc.NewServiceContext(c) 71 svcCtx := svc.NewServiceContext(c)
69 go func() { 72 go func() {
70 - //for {  
71 - // notice := &domain.ObjectNotice{  
72 - // CompanyId: 1594869884284571648,  
73 - // TableId: 1573,  
74 - // TableType: "主表",  
75 - // ObjectType: "导入模块",  
76 - // Event: "table.data.edit",  
77 - // TableAffectedList: []int{1573},  
78 - // DataChanged: true,  
79 - // StructChanged: true,  
80 - // MetaData: domain.ObjectNoticeMetaData{  
81 - // Module: 0,  
82 - // Status: 0,  
83 - // },  
84 - // }  
85 - // mBytes, _ := json.Marshal(notice)  
86 - // err := kq.NewPusher(c.KqConsumerConf.Brokers, c.KqConsumerConf.Topic).Push(string(mBytes))  
87 - // fmt.Println(err)  
88 - // time.Sleep(10 * 10 * time.Second)  
89 - //} 73 + for {
  74 + notice := &domain.ObjectNotice{
  75 + CompanyId: 1594869884284571648,
  76 + TableId: 1573,
  77 + TableType: "主表",
  78 + ObjectType: "导入模块",
  79 + Event: "table.data.edit",
  80 + TableAffectedList: []int{1573},
  81 + DataChanged: true,
  82 + StructChanged: true,
  83 + MetaData: domain.ObjectNoticeMetaData{
  84 + Module: 0,
  85 + Status: 0,
  86 + },
  87 + }
  88 + mBytes, _ := json.Marshal(notice)
  89 + err := kq.NewPusher(c.KqConsumerConf.Brokers, c.KqConsumerConf.Topic).Push(string(mBytes))
  90 + fmt.Println(err)
  91 + time.Sleep(10 * 10 * time.Second)
  92 + }
90 }() 93 }()
91 go func() { 94 go func() {
92 95
@@ -4,6 +4,7 @@ import ( @@ -4,6 +4,7 @@ import (
4 "context" 4 "context"
5 "encoding/json" 5 "encoding/json"
6 "fmt" 6 "fmt"
  7 + "github.com/pkg/errors"
7 "github.com/zeromicro/go-zero/core/stores/redis" 8 "github.com/zeromicro/go-zero/core/stores/redis"
8 "gitlab.fjmaimaimai.com/allied-creation/sumifcc-bchart/cmd/chart-server/api/internal/svc" 9 "gitlab.fjmaimaimai.com/allied-creation/sumifcc-bchart/cmd/chart-server/api/internal/svc"
9 "gitlab.fjmaimaimai.com/allied-creation/sumifcc-bchart/cmd/chart-server/api/internal/types" 10 "gitlab.fjmaimaimai.com/allied-creation/sumifcc-bchart/cmd/chart-server/api/internal/types"
@@ -39,7 +40,9 @@ func (logic *ByteNoticeLogic) Consume(key, value string) error { @@ -39,7 +40,9 @@ func (logic *ByteNoticeLogic) Consume(key, value string) error {
39 return err 40 return err
40 } 41 }
41 //处理消息 42 //处理消息
42 - err = logic.handleNotice(notice) 43 + err = transaction.UseTrans(logic.ctx, logic.conn.DB(), func(ctx context.Context, conn transaction.Conn) error {
  44 + return logic.handleNotice(conn, notice)
  45 + }, true)
43 //更新处理结果 46 //更新处理结果
44 if err != nil { 47 if err != nil {
45 notice.Status = domain.ObjectNoticeStatusError 48 notice.Status = domain.ObjectNoticeStatusError
@@ -53,10 +56,10 @@ func (logic *ByteNoticeLogic) Consume(key, value string) error { @@ -53,10 +56,10 @@ func (logic *ByteNoticeLogic) Consume(key, value string) error {
53 } 56 }
54 57
55 // handleNotice 处理消息 58 // handleNotice 处理消息
56 -func (logic *ByteNoticeLogic) handleNotice(notice *domain.ObjectNotice) error { 59 +func (logic *ByteNoticeLogic) handleNotice(conn transaction.Conn, notice *domain.ObjectNotice) error {
57 //是否删除消息 60 //是否删除消息
58 if notice.IsDeletedEvent() { 61 if notice.IsDeletedEvent() {
59 - return logic.handleDelete(notice) 62 + return logic.handleDelete(conn, notice)
60 } 63 }
61 accessToken, _ := types.TableAccessToken{CompanyId: notice.CompanyId}.GenerateToken() 64 accessToken, _ := types.TableAccessToken{CompanyId: notice.CompanyId}.GenerateToken()
62 //结构变更 65 //结构变更
@@ -100,17 +103,25 @@ func (logic *ByteNoticeLogic) handleNotice(notice *domain.ObjectNotice) error { @@ -100,17 +103,25 @@ func (logic *ByteNoticeLogic) handleNotice(notice *domain.ObjectNotice) error {
100 CompanyId: notice.CompanyId, 103 CompanyId: notice.CompanyId,
101 ParentId: item.ParentId, 104 ParentId: item.ParentId,
102 Flag: item.Flag, 105 Flag: item.Flag,
103 - Fields: tableInfo.Fields,  
104 }) 106 })
105 //父级节点 107 //父级节点
106 objectTables = append(objectTables, logic.getParents(notice.CompanyId, item.ParentId, list.List)...) 108 objectTables = append(objectTables, logic.getParents(notice.CompanyId, item.ParentId, list.List)...)
107 } 109 }
108 } 110 }
109 - err = logic.saveTables(objectTables) 111 + err = logic.saveTables(conn, objectTables)
110 if err != nil { 112 if err != nil {
111 return err 113 return err
112 } 114 }
113 } 115 }
  116 + //保存字段
  117 + _, err = logic.saveFields(conn, &domain.ObjectField{
  118 + Id: int64(tableInfo.TableId),
  119 + Name: tableInfo.Name,
  120 + Fields: tableInfo.Fields,
  121 + })
  122 + if err != nil {
  123 + return err
  124 + }
114 } 125 }
115 //数据变更 126 //数据变更
116 if notice.DataChanged { 127 if notice.DataChanged {
@@ -136,10 +147,15 @@ func (logic *ByteNoticeLogic) handleNotice(notice *domain.ObjectNotice) error { @@ -136,10 +147,15 @@ func (logic *ByteNoticeLogic) handleNotice(notice *domain.ObjectNotice) error {
136 acquire, err := lock.Acquire() 147 acquire, err := lock.Acquire()
137 fmt.Println(acquire, err) 148 fmt.Println(acquire, err)
138 defer lock.Release() 149 defer lock.Release()
139 - err = transaction.UseTrans(logic.ctx, logic.conn.DB(), func(ctx context.Context, conn transaction.Conn) error {  
140 - return logic.svcCtx.ObjectTableDataRepository.InsertWithTableData(logic.ctx, conn, bytelib.TableData(tableDataPreview))  
141 - }, true)  
142 - fmt.Println(err) 150 + err = logic.svcCtx.ObjectTableDataRepository.InsertWithTableData(logic.ctx, conn, bytelib.TableData(tableDataPreview))
  151 + if err != nil {
  152 + return err
  153 + }
  154 + //更新标记本地存储
  155 + err = logic.updateTableWithLocal(conn, int(tableDataPreview.ObjectId))
  156 + if err != nil {
  157 + return err
  158 + }
143 } 159 }
144 return nil 160 return nil
145 } 161 }
@@ -156,7 +172,6 @@ func (logic *ByteNoticeLogic) getParents(companyId int64, parentId int, list []* @@ -156,7 +172,6 @@ func (logic *ByteNoticeLogic) getParents(companyId int64, parentId int, list []*
156 CompanyId: companyId, 172 CompanyId: companyId,
157 ParentId: item.ParentId, 173 ParentId: item.ParentId,
158 Flag: item.Flag, 174 Flag: item.Flag,
159 - Fields: item.Fields,  
160 }) 175 })
161 if item.ParentId > 0 { 176 if item.ParentId > 0 {
162 result = append(result, logic.getParents(companyId, item.ParentId, list)...) 177 result = append(result, logic.getParents(companyId, item.ParentId, list)...)
@@ -167,11 +182,11 @@ func (logic *ByteNoticeLogic) getParents(companyId int64, parentId int, list []* @@ -167,11 +182,11 @@ func (logic *ByteNoticeLogic) getParents(companyId int64, parentId int, list []*
167 } 182 }
168 183
169 // handleDelete 删除 184 // handleDelete 删除
170 -func (logic *ByteNoticeLogic) handleDelete(notice *domain.ObjectNotice) error {  
171 - objectTable, err := logic.svcCtx.ObjectTableRepository.FindOneByTableId(logic.ctx, logic.conn, notice.TableId) 185 +func (logic *ByteNoticeLogic) handleDelete(conn transaction.Conn, notice *domain.ObjectNotice) error {
  186 + objectTable, err := logic.svcCtx.ObjectTableRepository.FindOneByTableId(logic.ctx, conn, notice.TableId)
172 if err == nil && objectTable.Id > 0 { 187 if err == nil && objectTable.Id > 0 {
173 objectTable.RemoteDeleted = 1 188 objectTable.RemoteDeleted = 1
174 - _, err := logic.svcCtx.ObjectTableRepository.Update(logic.ctx, logic.conn, objectTable) 189 + _, err := logic.svcCtx.ObjectTableRepository.Update(logic.ctx, conn, objectTable)
175 if err != nil { 190 if err != nil {
176 return err 191 return err
177 } 192 }
@@ -180,26 +195,46 @@ func (logic *ByteNoticeLogic) handleDelete(notice *domain.ObjectNotice) error { @@ -180,26 +195,46 @@ func (logic *ByteNoticeLogic) handleDelete(notice *domain.ObjectNotice) error {
180 } 195 }
181 196
182 // saveTables 保存表结构 197 // saveTables 保存表结构
183 -func (logic *ByteNoticeLogic) saveTables(tables []*domain.ObjectTable) error {  
184 - err := transaction.UseTrans(logic.ctx, logic.conn.DB(), func(ctx context.Context, conn transaction.Conn) error {  
185 - if len(tables) > 0 {  
186 - for _, item := range tables {  
187 - objectTable, err := logic.svcCtx.ObjectTableRepository.FindOne(logic.ctx, conn, item.Id)  
188 - if err == nil && objectTable.Id > 0 {  
189 - item.Id = objectTable.Id  
190 - _, err := logic.svcCtx.ObjectTableRepository.Update(logic.ctx, conn, item)  
191 - if err != nil {  
192 - return err  
193 - }  
194 - } else {  
195 - _, err := logic.svcCtx.ObjectTableRepository.Insert(logic.ctx, conn, item)  
196 - if err != nil {  
197 - return err  
198 - } 198 +func (logic *ByteNoticeLogic) saveTables(conn transaction.Conn, tables []*domain.ObjectTable) error {
  199 + if len(tables) > 0 {
  200 + for _, item := range tables {
  201 + objectTable, err := logic.svcCtx.ObjectTableRepository.FindOne(logic.ctx, conn, item.Id)
  202 + if err == nil && objectTable.Id > 0 {
  203 + item.Id = objectTable.Id
  204 + item.Version = objectTable.Version + 1
  205 + _, err := logic.svcCtx.ObjectTableRepository.Update(logic.ctx, conn, item)
  206 + if err != nil {
  207 + return err
  208 + }
  209 + } else {
  210 + _, err := logic.svcCtx.ObjectTableRepository.Insert(logic.ctx, conn, item)
  211 + if err != nil {
  212 + return err
199 } 213 }
200 } 214 }
201 } 215 }
202 - return nil  
203 - }, true)  
204 - return err 216 + }
  217 + return nil
  218 +}
  219 +
  220 +// saveFields 保存表字段
  221 +func (logic *ByteNoticeLogic) saveFields(conn transaction.Conn, objectField *domain.ObjectField) (*domain.ObjectField, error) {
  222 + mField, err := logic.svcCtx.ObjectFieldRepository.FindOne(logic.ctx, conn, objectField.Id)
  223 + if err == nil && mField.Id > 0 { //已存在 - 更新
  224 + objectField.Version = mField.Version + 1
  225 + return logic.svcCtx.ObjectFieldRepository.Update(logic.ctx, conn, objectField)
  226 + } else {
  227 + return logic.svcCtx.ObjectFieldRepository.Insert(logic.ctx, conn, objectField)
  228 + }
  229 +}
  230 +
  231 +// updateTableWithLocal 更新表标记本地存储
  232 +func (logic *ByteNoticeLogic) updateTableWithLocal(conn transaction.Conn, tableId int) error {
  233 + objectTable, err := logic.svcCtx.ObjectTableRepository.FindOneByTableId(logic.ctx, conn, tableId)
  234 + if err == nil && objectTable.Id > 0 {
  235 + objectTable.IsLocal = true
  236 + _, err = logic.svcCtx.ObjectTableRepository.Update(logic.ctx, conn, objectTable)
  237 + return err
  238 + }
  239 + return errors.New("表不存在")
205 } 240 }
@@ -8,6 +8,7 @@ import ( @@ -8,6 +8,7 @@ import (
8 "gitlab.fjmaimaimai.com/allied-creation/sumifcc-bchart/cmd/chart-server/api/internal/svc" 8 "gitlab.fjmaimaimai.com/allied-creation/sumifcc-bchart/cmd/chart-server/api/internal/svc"
9 "gitlab.fjmaimaimai.com/allied-creation/sumifcc-bchart/cmd/chart-server/api/internal/types" 9 "gitlab.fjmaimaimai.com/allied-creation/sumifcc-bchart/cmd/chart-server/api/internal/types"
10 "gitlab.fjmaimaimai.com/allied-creation/sumifcc-bchart/cmd/chart-server/interanl/pkg/gateway/bytelib" 10 "gitlab.fjmaimaimai.com/allied-creation/sumifcc-bchart/cmd/chart-server/interanl/pkg/gateway/bytelib"
  11 + "gitlab.fjmaimaimai.com/allied-creation/sumifcc-bchart/pkg/contextdata"
11 "gitlab.fjmaimaimai.com/allied-creation/sumifcc-bchart/pkg/xerr" 12 "gitlab.fjmaimaimai.com/allied-creation/sumifcc-bchart/pkg/xerr"
12 ) 13 )
13 14
@@ -29,50 +30,70 @@ func (l *SearchTableByModuleLogic) SearchTableByModule(req *types.SearchTableByM @@ -29,50 +30,70 @@ func (l *SearchTableByModuleLogic) SearchTableByModule(req *types.SearchTableByM
29 var result = make(map[string]interface{}) 30 var result = make(map[string]interface{})
30 var batchError errorx.BatchError 31 var batchError errorx.BatchError
31 fx.Parallel(func() { 32 fx.Parallel(func() {
32 - list, err := l.svcCtx.ByteMetadataService.ObjectTableSearch(l.ctx, bytelib.ObjectTableSearchRequest{  
33 - Token: req.Token,  
34 - TableTypes: []string{bytelib.MainTable, bytelib.SubTable, bytelib.SideTable},  
35 - Module: bytelib.ModuleDigitalCenter | bytelib.ModuleChartTemplate, // TODO:字库更新完改为只查 bytelib.ModuleChartTemplate  
36 - })  
37 - if err != nil {  
38 - batchError.Add(err) 33 + mResp, err := l.getTableByLocal([]string{bytelib.MainTable, bytelib.SubTable, bytelib.SideTable})
  34 + if err == nil {
  35 + result["导入模块"] = mResp
  36 + } else {
  37 + list, err := l.svcCtx.ByteMetadataService.ObjectTableSearch(l.ctx, bytelib.ObjectTableSearchRequest{
  38 + Token: req.Token,
  39 + TableTypes: []string{bytelib.MainTable, bytelib.SubTable, bytelib.SideTable},
  40 + Module: bytelib.ModuleDigitalCenter | bytelib.ModuleChartTemplate, // TODO:字库更新完改为只查 bytelib.ModuleChartTemplate
  41 + })
  42 + if err != nil {
  43 + batchError.Add(err)
  44 + }
  45 + result["导入模块"] = newList(list)
39 } 46 }
40 - result["导入模块"] = newList(list)  
41 }, func() { 47 }, func() {
42 - list, err := l.svcCtx.ByteMetadataService.ObjectTableSearch(l.ctx, bytelib.ObjectTableSearchRequest{  
43 - Token: req.Token,  
44 - TableTypes: []string{bytelib.SchemaTable},  
45 - Module: bytelib.ModuleQuerySetCenter,  
46 - ReturnGroupItem: true,  
47 - })  
48 - if err != nil {  
49 - batchError.Add(err) 48 + mResp, err := l.getTableByLocal([]string{bytelib.SchemaTable})
  49 + if err == nil {
  50 + result["拆解模块"] = mResp
  51 + } else {
  52 + list, err := l.svcCtx.ByteMetadataService.ObjectTableSearch(l.ctx, bytelib.ObjectTableSearchRequest{
  53 + Token: req.Token,
  54 + TableTypes: []string{bytelib.SchemaTable},
  55 + Module: bytelib.ModuleQuerySetCenter,
  56 + ReturnGroupItem: true,
  57 + })
  58 + if err != nil {
  59 + batchError.Add(err)
  60 + }
  61 + result["拆解模块"] = newList(list)
50 } 62 }
51 - result["拆解模块"] = newList(list)  
52 }, func() { 63 }, func() {
53 - list, err := l.svcCtx.ByteMetadataService.ObjectTableSearch(l.ctx, bytelib.ObjectTableSearchRequest{  
54 - Token: req.Token,  
55 - TableTypes: []string{bytelib.CalculateItem},  
56 - Module: bytelib.ModuleCalculateCenter,  
57 - ReturnGroupItem: true,  
58 - ExcludeTables: []int{0},  
59 - })  
60 - if err != nil {  
61 - batchError.Add(err) 64 + mResp, err := l.getTableByLocal([]string{bytelib.CalculateItem})
  65 + if err == nil {
  66 + result["计算项"] = mResp
  67 + } else {
  68 + list, err := l.svcCtx.ByteMetadataService.ObjectTableSearch(l.ctx, bytelib.ObjectTableSearchRequest{
  69 + Token: req.Token,
  70 + TableTypes: []string{bytelib.CalculateItem},
  71 + Module: bytelib.ModuleCalculateCenter,
  72 + ReturnGroupItem: true,
  73 + ExcludeTables: []int{0},
  74 + })
  75 + if err != nil {
  76 + batchError.Add(err)
  77 + }
  78 + result["计算项"] = newList(list)
62 } 79 }
63 - result["计算项"] = newList(list)  
64 }, func() { 80 }, func() {
65 - list, err := l.svcCtx.ByteMetadataService.ObjectTableSearch(l.ctx, bytelib.ObjectTableSearchRequest{  
66 - Token: req.Token,  
67 - TableTypes: []string{bytelib.CalculateSet},  
68 - Module: bytelib.ModuleCalculateCenter,  
69 - ReturnGroupItem: true,  
70 - ExcludeTables: []int{0},  
71 - })  
72 - if err != nil {  
73 - batchError.Add(err) 81 + mResp, err := l.getTableByLocal([]string{bytelib.CalculateSet})
  82 + if err == nil {
  83 + result["计算集"] = mResp
  84 + } else {
  85 + list, err := l.svcCtx.ByteMetadataService.ObjectTableSearch(l.ctx, bytelib.ObjectTableSearchRequest{
  86 + Token: req.Token,
  87 + TableTypes: []string{bytelib.CalculateSet},
  88 + Module: bytelib.ModuleCalculateCenter,
  89 + ReturnGroupItem: true,
  90 + ExcludeTables: []int{0},
  91 + })
  92 + if err != nil {
  93 + batchError.Add(err)
  94 + }
  95 + result["计算集"] = newList(list)
74 } 96 }
75 - result["计算集"] = newList(list)  
76 }) 97 })
77 if err = batchError.Err(); err != nil { 98 if err = batchError.Err(); err != nil {
78 logx.Error(err) 99 logx.Error(err)
@@ -88,3 +109,30 @@ func newList(r bytelib.ObjectTableSearchResponse) interface{} { @@ -88,3 +109,30 @@ func newList(r bytelib.ObjectTableSearchResponse) interface{} {
88 "list": r.List, 109 "list": r.List,
89 } 110 }
90 } 111 }
  112 +
  113 +func (l *SearchTableByModuleLogic) getTableByLocal(modules []string) (types.SearchTableByModuleResponse, error) {
  114 + tenantId := contextdata.GetTenantFromCtx(l.ctx)
  115 + response := types.SearchTableByModuleResponse{
  116 + Count: 0,
  117 + List: make([]types.SearchTableByModuleItem, 0),
  118 + }
  119 + total, list, err := l.svcCtx.ObjectTableRepository.Find(l.ctx, l.svcCtx.DefaultDBConn(), map[string]interface{}{
  120 + "companyId": tenantId,
  121 + "tableType": modules,
  122 + })
  123 + if err != nil {
  124 + return response, err
  125 + }
  126 + response.Count = int(total)
  127 + for _, item := range list {
  128 + response.List = append(response.List, types.SearchTableByModuleItem{
  129 + Id: item.Id,
  130 + TableId: item.TableId,
  131 + TableType: item.TableType,
  132 + Name: item.Name,
  133 + ParentId: item.ParentId,
  134 + Flag: item.Flag,
  135 + })
  136 + }
  137 + return response, nil
  138 +}
@@ -2,6 +2,8 @@ package table @@ -2,6 +2,8 @@ package table
2 2
3 import ( 3 import (
4 "context" 4 "context"
  5 + "github.com/jinzhu/copier"
  6 + "gitlab.fjmaimaimai.com/allied-creation/sumifcc-bchart/cmd/chart-server/interanl/pkg/domain"
5 "gitlab.fjmaimaimai.com/allied-creation/sumifcc-bchart/cmd/chart-server/interanl/pkg/gateway/bytelib" 7 "gitlab.fjmaimaimai.com/allied-creation/sumifcc-bchart/cmd/chart-server/interanl/pkg/gateway/bytelib"
6 "gitlab.fjmaimaimai.com/allied-creation/sumifcc-bchart/pkg/xerr" 8 "gitlab.fjmaimaimai.com/allied-creation/sumifcc-bchart/pkg/xerr"
7 9
@@ -32,9 +34,55 @@ func (l *SearchTableDataLogic) SearchTableData(req *types.SearchTableDataRequest @@ -32,9 +34,55 @@ func (l *SearchTableDataLogic) SearchTableData(req *types.SearchTableDataRequest
32 "fields": nil, 34 "fields": nil,
33 "total": 0, 35 "total": 0,
34 "data": make([]string, 0), 36 "data": make([]string, 0),
  37 + "local": false,
35 } 38 }
36 return 39 return
37 } 40 }
  41 + resp, err = l.getLocal(req)
  42 + if err == nil {
  43 + return resp, nil
  44 + }
  45 + return l.getRemote(req)
  46 +}
  47 +
  48 +// getLocal 获取本地数据
  49 +func (l *SearchTableDataLogic) getLocal(req *types.SearchTableDataRequest) (interface{}, error) {
  50 + conn := l.svcCtx.DefaultDBConn()
  51 + conditions := make([]*domain.TableDataCondition, 0)
  52 + _ = copier.Copy(&conditions, req.Condition)
  53 + //查询表数据
  54 + objectTable, err := l.svcCtx.ObjectTableRepository.FindOneByTableId(l.ctx, conn, req.ObjectId)
  55 + if err != nil || objectTable.Id <= 0 {
  56 + return nil, xerr.NewErrMsg("表不存在")
  57 + }
  58 + if !objectTable.IsLocal {
  59 + return nil, xerr.NewErrMsg("未保存到本地存储")
  60 + }
  61 + //查询表字段
  62 + objectField, err := l.svcCtx.ObjectFieldRepository.FindOne(l.ctx, conn, int64(req.ObjectId))
  63 + if err != nil || objectField.Id <= 0 {
  64 + return nil, xerr.NewErrMsg("表字段不存在")
  65 + }
  66 + //表数据
  67 + total, list, err := l.svcCtx.ObjectTableDataRepository.Find(l.ctx, conn, req.ObjectId, &domain.ObjectTableDataQuery{
  68 + Page: req.PageNumber,
  69 + Size: req.PageSize,
  70 + Conditions: conditions,
  71 + })
  72 + if err != nil {
  73 + return nil, xerr.NewErrMsg("查询表数据失败")
  74 + }
  75 + return map[string]interface{}{
  76 + "objectId": req.ObjectId,
  77 + "fields": removeIdField(objectField.Fields),
  78 + "total": total,
  79 + "data": list,
  80 + "local": true,
  81 + }, nil
  82 +}
  83 +
  84 +// getRemote 获取远程字库数据
  85 +func (l *SearchTableDataLogic) getRemote(req *types.SearchTableDataRequest) (resp interface{}, err error) {
38 tableDataPreviewRequest := &bytelib.TableDataPreviewRequest{ 86 tableDataPreviewRequest := &bytelib.TableDataPreviewRequest{
39 Token: req.Token, 87 Token: req.Token,
40 ObjectType: bytelib.ObjectMetaTable, 88 ObjectType: bytelib.ObjectMetaTable,
@@ -53,6 +101,7 @@ func (l *SearchTableDataLogic) SearchTableData(req *types.SearchTableDataRequest @@ -53,6 +101,7 @@ func (l *SearchTableDataLogic) SearchTableData(req *types.SearchTableDataRequest
53 "fields": removeIdField(response.Fields), 101 "fields": removeIdField(response.Fields),
54 "total": response.Grid.Total, 102 "total": response.Grid.Total,
55 "data": response.Grid.List, 103 "data": response.Grid.List,
  104 + "local": false,
56 } 105 }
57 return 106 return
58 } 107 }
@@ -24,6 +24,7 @@ type ServiceContext struct { @@ -24,6 +24,7 @@ type ServiceContext struct {
24 AppPageRepository domain.AppPageRepository 24 AppPageRepository domain.AppPageRepository
25 ObjectNoticeRepository domain.ObjectNoticeRepository 25 ObjectNoticeRepository domain.ObjectNoticeRepository
26 ObjectTableRepository domain.ObjectTableRepository 26 ObjectTableRepository domain.ObjectTableRepository
  27 + ObjectFieldRepository domain.ObjectFieldRepository
27 ObjectTableDataRepository domain.ObjectTableDataRepository 28 ObjectTableDataRepository domain.ObjectTableDataRepository
28 29
29 ByteMetadataService bytelib.ByteMetadataService 30 ByteMetadataService bytelib.ByteMetadataService
@@ -45,6 +46,7 @@ func NewServiceContext(c config.Config) *ServiceContext { @@ -45,6 +46,7 @@ func NewServiceContext(c config.Config) *ServiceContext {
45 AppPageRepository: repository.NewAppPageRepository(cache.NewCachedRepository(mlCache)), 46 AppPageRepository: repository.NewAppPageRepository(cache.NewCachedRepository(mlCache)),
46 ObjectNoticeRepository: repository.NewObjectNoticeRepository(cache.NewCachedRepository(mlCache)), 47 ObjectNoticeRepository: repository.NewObjectNoticeRepository(cache.NewCachedRepository(mlCache)),
47 ObjectTableRepository: repository.NewObjectTableRepository(cache.NewCachedRepository(mlCache)), 48 ObjectTableRepository: repository.NewObjectTableRepository(cache.NewCachedRepository(mlCache)),
  49 + ObjectFieldRepository: repository.NewObjectFieldRepository(cache.NewCachedRepository(mlCache)),
48 ObjectTableDataRepository: repository.NewObjectTableDataRepository(), 50 ObjectTableDataRepository: repository.NewObjectTableDataRepository(),
49 51
50 ByteMetadataService: bytelib.ByteMetadataService{ 52 ByteMetadataService: bytelib.ByteMetadataService{
@@ -189,6 +189,18 @@ type SearchTableByModuleRequest struct { @@ -189,6 +189,18 @@ type SearchTableByModuleRequest struct {
189 } 189 }
190 190
191 type SearchTableByModuleResponse struct { 191 type SearchTableByModuleResponse struct {
  192 + Count int `json:"count"`
  193 + List []SearchTableByModuleItem `json:"list"`
  194 +}
  195 +
  196 +type SearchTableByModuleItem struct {
  197 + Id int `json:"id"` //ID
  198 + TableId int `json:"tableId"` //表ID
  199 + Name string `json:"name"` //表名
  200 + TableType string `json:"tableType"` //表类型
  201 + ParentId int `json:"parentId"` //父级ID
  202 + Flag string `json:"flag"` //分组:Group 集合:Set
  203 + IsLocal bool `json:"isLocal"` //是否本地存储
192 } 204 }
193 205
194 type SearchTableFieldOptionalValuesRequest struct { 206 type SearchTableFieldOptionalValuesRequest struct {
@@ -12,5 +12,6 @@ func Migrate(db *gorm.DB) { @@ -12,5 +12,6 @@ func Migrate(db *gorm.DB) {
12 &models.AppPage{}, 12 &models.AppPage{},
13 &models.ObjectNotice{}, 13 &models.ObjectNotice{},
14 &models.ObjectTable{}, 14 &models.ObjectTable{},
  15 + &models.ObjectField{},
15 ) 16 )
16 } 17 }
@@ -12,7 +12,7 @@ import ( @@ -12,7 +12,7 @@ import (
12 type ObjectField struct { 12 type ObjectField struct {
13 Id int64 `json:"id" gorm:"primaryKey"` // ID 13 Id int64 `json:"id" gorm:"primaryKey"` // ID
14 Name string `json:"name"` //表名 14 Name string `json:"name"` //表名
15 - Fields []*bytelib.Field `json:"fields"` //表字段 15 + Fields []*bytelib.Field `json:"fields" gorm:"serializer:json"` //表字段
16 Version int `json:",omitempty"` //版本 16 Version int `json:",omitempty"` //版本
17 IsDel soft_delete.DeletedAt `gorm:"softDelete:flag,DeletedAtField:DeletedAt"` //删除标记 17 IsDel soft_delete.DeletedAt `gorm:"softDelete:flag,DeletedAtField:DeletedAt"` //删除标记
18 CreatedAt int64 `json:",omitempty"` //创建时间 18 CreatedAt int64 `json:",omitempty"` //创建时间
@@ -3,7 +3,6 @@ package models @@ -3,7 +3,6 @@ package models
3 import ( 3 import (
4 "fmt" 4 "fmt"
5 "gitlab.fjmaimaimai.com/allied-creation/sumifcc-bchart/cmd/chart-server/interanl/pkg/domain" 5 "gitlab.fjmaimaimai.com/allied-creation/sumifcc-bchart/cmd/chart-server/interanl/pkg/domain"
6 - "gitlab.fjmaimaimai.com/allied-creation/sumifcc-bchart/cmd/chart-server/interanl/pkg/gateway/bytelib"  
7 "gorm.io/gorm" 6 "gorm.io/gorm"
8 "gorm.io/plugin/soft_delete" 7 "gorm.io/plugin/soft_delete"
9 "time" 8 "time"
@@ -18,7 +17,7 @@ type ObjectTable struct { @@ -18,7 +17,7 @@ type ObjectTable struct {
18 ParentId int `json:"parentId" gorm:"default:0"` //父id 17 ParentId int `json:"parentId" gorm:"default:0"` //父id
19 Flag string `json:"flag"` //分组:Group 集合:Set 18 Flag string `json:"flag"` //分组:Group 集合:Set
20 Version int `json:",omitempty"` //版本 19 Version int `json:",omitempty"` //版本
21 - Fields []*bytelib.Field `json:"fields" gorm:"serializer:json"` //表字段 20 + IsLocal bool `json:"isLocal" gorm:"default:false"` //是否有本地存储
22 RemoteDeleted int `json:"remoteDeleted"` //远端删除 21 RemoteDeleted int `json:"remoteDeleted"` //远端删除
23 IsDel soft_delete.DeletedAt `gorm:"softDelete:flag,DeletedAtField:DeletedAt"` //删除标记 22 IsDel soft_delete.DeletedAt `gorm:"softDelete:flag,DeletedAtField:DeletedAt"` //删除标记
24 CreatedAt int64 `json:",omitempty"` //创建时间 23 CreatedAt int64 `json:",omitempty"` //创建时间
@@ -120,9 +120,28 @@ func (repository *ObjectTableDataRepository) InsertWithTableData(ctx context.Con @@ -120,9 +120,28 @@ func (repository *ObjectTableDataRepository) InsertWithTableData(ctx context.Con
120 return nil 120 return nil
121 } 121 }
122 122
123 -// FindAll 获取表内所有数据  
124 -func (repository *ObjectTableDataRepository) FindAll(ctx context.Context, conn transaction.Conn, tableId int) {  
125 - 123 +// Find 获取表内所有数据
  124 +func (repository *ObjectTableDataRepository) Find(ctx context.Context, conn transaction.Conn, tableId int, query *domain.ObjectTableDataQuery) (int64, []map[string]interface{}, error) {
  125 + tx := conn.DB().Table(fmt.Sprintf("data.%v", tableId))
  126 + if len(query.Conditions) > 0 {
  127 + for _, item := range query.Conditions {
  128 + if item.FieldName == "" {
  129 + continue
  130 + }
  131 + if item.Like != "" {
  132 + tx = tx.Where(item.FieldName+" like ?", item.Like)
  133 + }
  134 + if len(item.In) > 0 {
  135 + tx = tx.Where(item.FieldName+" in ?", item.In)
  136 + }
  137 + if item.Order != "" {
  138 + tx = tx.Order(item.FieldName + " " + item.Order)
  139 + }
  140 + }
  141 + }
  142 + list := make([]map[string]interface{}, 0)
  143 + total, tx := transaction.PaginationAndCount(ctx, tx, domain.NewQueryOptions().WithOffsetLimit(query.Page, query.Size), &list)
  144 + return total, list, tx.Error
126 } 145 }
127 146
128 func NewObjectTableDataRepository() domain.ObjectTableDataRepository { 147 func NewObjectTableDataRepository() domain.ObjectTableDataRepository {
@@ -113,19 +113,12 @@ func (repository *ObjectTableRepository) FindOne(ctx context.Context, conn trans @@ -113,19 +113,12 @@ func (repository *ObjectTableRepository) FindOne(ctx context.Context, conn trans
113 113
114 func (repository *ObjectTableRepository) FindOneByTableId(ctx context.Context, conn transaction.Conn, tableId int) (*domain.ObjectTable, error) { 114 func (repository *ObjectTableRepository) FindOneByTableId(ctx context.Context, conn transaction.Conn, tableId int) (*domain.ObjectTable, error) {
115 var ( 115 var (
116 - err error  
117 - tx = conn.DB()  
118 - m = new(models.ObjectTable) 116 + tx = conn.DB()
  117 + m = new(models.ObjectTable)
119 ) 118 )
120 - queryFunc := func() (interface{}, error) {  
121 - tx = tx.Model(m).Where("table_id = ?", tableId).First(m)  
122 - if errors.Is(tx.Error, gorm.ErrRecordNotFound) {  
123 - return nil, domain.ErrNotFound  
124 - }  
125 - return m, tx.Error  
126 - }  
127 - if _, err = repository.Query(queryFunc); err != nil {  
128 - return nil, err 119 + tx = tx.Model(m).Where("table_id = ?", tableId).First(m)
  120 + if errors.Is(tx.Error, gorm.ErrRecordNotFound) {
  121 + return nil, domain.ErrNotFound
129 } 122 }
130 return repository.ModelToDomainModel(m) 123 return repository.ModelToDomainModel(m)
131 } 124 }
@@ -139,6 +132,12 @@ func (repository *ObjectTableRepository) Find(ctx context.Context, conn transact @@ -139,6 +132,12 @@ func (repository *ObjectTableRepository) Find(ctx context.Context, conn transact
139 ) 132 )
140 queryFunc := func() (interface{}, error) { 133 queryFunc := func() (interface{}, error) {
141 tx = tx.Model(&ms).Order("id desc") 134 tx = tx.Model(&ms).Order("id desc")
  135 + if v, ok := queryOptions["companyId"]; ok {
  136 + tx = tx.Where("company_id = ?", v)
  137 + }
  138 + if v, ok := queryOptions["tableTypeIn"]; ok {
  139 + tx = tx.Where("table_type in ?", v)
  140 + }
142 if total, tx = transaction.PaginationAndCount(ctx, tx, queryOptions, &ms); tx.Error != nil { 141 if total, tx = transaction.PaginationAndCount(ctx, tx, queryOptions, &ms); tx.Error != nil {
143 return dms, tx.Error 142 return dms, tx.Error
144 } 143 }
@@ -8,20 +8,20 @@ import ( @@ -8,20 +8,20 @@ import (
8 ) 8 )
9 9
10 type ObjectTable struct { 10 type ObjectTable struct {
11 - Id int `json:"id"` // ID  
12 - TableId int `json:"tableId" ` //表ID  
13 - Name string `json:"name"` //表名  
14 - TableType string `json:"tableType" ` //模块  
15 - CompanyId int64 `json:"companyId"` //公司id  
16 - ParentId int `json:"parentId"` //父id  
17 - Flag string `json:"flag"` //分组:Group 集合:Set  
18 - Fields []*bytelib.Field `json:"fields" gorm:"serializer:json"` //表字段  
19 - Version int `json:"version,omitempty"` //版本  
20 - RemoteDeleted int `json:"remoteDeleted"` //远端删除  
21 - IsDel soft_delete.DeletedAt `json:"isDel,omitempty"` //删除标记  
22 - CreatedAt int64 `json:"createdAt,omitempty"` //创建时间  
23 - UpdatedAt int64 `json:"updatedAt,omitempty"` //更新时间  
24 - DeletedAt int64 `json:"deletedAt,omitempty"` //删除时间 11 + Id int `json:"id"` // ID
  12 + TableId int `json:"tableId" ` //表ID
  13 + Name string `json:"name"` //表名
  14 + TableType string `json:"tableType" ` //模块
  15 + CompanyId int64 `json:"companyId"` //公司id
  16 + ParentId int `json:"parentId"` //父id
  17 + Flag string `json:"flag"` //分组:Group 集合:Set
  18 + Version int `json:"version,omitempty"` //版本
  19 + IsLocal bool `json:"isLocal"` //是否有本地存储
  20 + RemoteDeleted int `json:"remoteDeleted"` //远端删除
  21 + IsDel soft_delete.DeletedAt `json:"isDel,omitempty"` //删除标记
  22 + CreatedAt int64 `json:"createdAt,omitempty"` //创建时间
  23 + UpdatedAt int64 `json:"updatedAt,omitempty"` //更新时间
  24 + DeletedAt int64 `json:"deletedAt,omitempty"` //删除时间
25 } 25 }
26 26
27 type ObjectTableRepository interface { 27 type ObjectTableRepository interface {
@@ -34,8 +34,22 @@ type ObjectTableRepository interface { @@ -34,8 +34,22 @@ type ObjectTableRepository interface {
34 Find(ctx context.Context, conn transaction.Conn, queryOptions map[string]interface{}) (int64, []*ObjectTable, error) 34 Find(ctx context.Context, conn transaction.Conn, queryOptions map[string]interface{}) (int64, []*ObjectTable, error)
35 } 35 }
36 36
  37 +type ObjectTableDataQuery struct {
  38 + Page int
  39 + Size int
  40 + Conditions []*TableDataCondition
  41 +}
  42 +
  43 +type TableDataCondition struct {
  44 + FieldName string `json:"field"` // 条件字段
  45 + Like string `json:"like"` // 模糊匹配
  46 + In []string `json:"in"` // 匹配多个值
  47 + Order string `json:"order"` // 排序 ASC DESC 默认ASC
  48 +}
  49 +
37 type ObjectTableDataRepository interface { 50 type ObjectTableDataRepository interface {
38 InsertWithTableData(ctx context.Context, conn transaction.Conn, tableDataPreview bytelib.TableData) error 51 InsertWithTableData(ctx context.Context, conn transaction.Conn, tableDataPreview bytelib.TableData) error
  52 + Find(ctx context.Context, conn transaction.Conn, tableId int, query *ObjectTableDataQuery) (int64, []map[string]interface{}, error)
39 } 53 }
40 54
41 func (m *ObjectTable) Identify() interface{} { 55 func (m *ObjectTable) Identify() interface{} {
1 syntax = "v1" 1 syntax = "v1"
2 2
3 info( 3 info(
4 - title: "天联字库图表模板"  
5 - desc: "图表模板"  
6 - author: "小火箭"  
7 - email: "email"  
8 - version: "v1" 4 + title: "天联字库图表模板"
  5 + desc: "图表模板"
  6 + author: "小火箭"
  7 + email: "email"
  8 + version: "v1"
9 ) 9 )
10 10
11 @server( 11 @server(
12 - prefix: v1  
13 - group: table  
14 - //jwt: JwtAuth  
15 - //middleware: Authority 12 + prefix: v1
  13 + group: table
  14 + //jwt: JwtAuth
  15 + //middleware: Authority
16 ) 16 )
17 service Core { 17 service Core {
18 - @doc "源数据表-字段可选值"  
19 - @handler searchTableFieldOptionalValues  
20 - post /table/field-optional-values (SearchTableByModuleRequest) returns (SearchTableByModuleResponse) 18 + @doc "源数据表-字段可选值"
  19 + @handler searchTableFieldOptionalValues
  20 + post /table/field-optional-values (SearchTableByModuleRequest) returns (SearchTableByModuleResponse)
21 21
22 - @doc "源数据表-详情"  
23 - @handler getTableDetail  
24 - get /table/:tableId (GetTableDetailRequest) returns (GetTableDetailResponse) 22 + @doc "源数据表-详情"
  23 + @handler getTableDetail
  24 + get /table/:tableId (GetTableDetailRequest) returns (GetTableDetailResponse)
25 25
26 - @doc "源数据表-数据"  
27 - @handler searchTableData  
28 - post /table/data (SearchTableDataRequest) returns (SearchTableDataResponse) 26 + @doc "源数据表-数据"
  27 + @handler searchTableData
  28 + post /table/data (SearchTableDataRequest) returns (SearchTableDataResponse)
29 } 29 }
30 30
31 @server( 31 @server(
32 - prefix: v1  
33 - group: table  
34 - jwt: JwtAuth  
35 - //middleware: Authority 32 + prefix: v1
  33 + group: table
  34 + jwt: JwtAuth
  35 + //middleware: Authority
36 ) 36 )
37 service Core { 37 service Core {
38 - @doc "源数据表-按模块搜索"  
39 - @handler searchTableByModule  
40 - post /table/search-by-module (SearchTableByModuleRequest) returns (SearchTableByModuleResponse) 38 + @doc "源数据表-按模块搜索"
  39 + @handler searchTableByModule
  40 + post /table/search-by-module (SearchTableByModuleRequest) returns (SearchTableByModuleResponse)
41 } 41 }
42 42
43 type ( 43 type (
44 - SearchTableByModuleRequest struct{  
45 - Token string `header:"x-mmm-accesstoken,optional"`  
46 - }  
47 - SearchTableByModuleResponse struct{ 44 + SearchTableByModuleRequest {
  45 + Token string `header:"x-mmm-accesstoken,optional"`
  46 + }
  47 + SearchTableByModuleResponse {
  48 + Count int `json:"count"`
  49 + List []SearchTableByModuleItem `json:"list"`
  50 + }
  51 + SearchTableByModuleItem {
  52 + Id int `json:"id"` //ID
  53 + TableId int `json:"tableId"` //表ID
  54 + Name string `json:"name"` //表名
  55 + TableType string `json:"tableType"` //表类型
  56 + ParentId int `json:"parentId"` //父级ID
  57 + Flag string `json:"flag"` //分组:Group 集合:Set
  58 + IsLocal bool `json:"isLocal"` //是否本地存储
  59 + }
48 60
49 - } 61 + SearchTableFieldOptionalValuesRequest {
  62 + Token string `header:"x-mmm-accesstoken,optional"`
  63 + ObjectId int `json:"objectId"` // 对象ID
  64 + Field string `json:"field"` // 当前选择的字段
  65 + //SqlName string `json:"sqlName,optional"` // 字段SqlName
  66 + // Match string `json:"match"`
  67 + //PageNumber int `json:"pageNumber,optional"` // 分页数
  68 + //PageSize int `json:"pageSize,optional"` // 页码
  69 + Condition []*Condition `json:"conditions,optional"` // 条件
  70 + }
  71 + SearchTableFieldOptionalValuesResponse {
  72 + Values []string `json:"values"`
  73 + Total int64 `json:"total"`
  74 + }
  75 + Condition {
  76 + FieldName string `json:"field"` // 条件字段
  77 + //SqlName string `json:"sqlName,optional"` // 字段SqlName
  78 + Like string `json:"like,optional"` // 模糊匹配
  79 + In []string `json:"in,optional"` // 匹配多个值
  80 + Order string `json:"order,optional,options=ASC||DESC"` // 排序 ASC DESC 默认ASC
  81 + }
50 82
51 - SearchTableFieldOptionalValuesRequest struct{  
52 - Token string `header:"x-mmm-accesstoken,optional"`  
53 - ObjectId int `json:"objectId"` // 对象ID  
54 - Field string `json:"field"` // 当前选择的字段  
55 - //SqlName string `json:"sqlName,optional"` // 字段SqlName  
56 - // Match string `json:"match"`  
57 - //PageNumber int `json:"pageNumber,optional"` // 分页数  
58 - //PageSize int `json:"pageSize,optional"` // 页码  
59 - Condition []*Condition `json:"conditions,optional"` // 条件  
60 - }  
61 - SearchTableFieldOptionalValuesResponse struct{  
62 - Values []string `json:"values"`  
63 - Total int64 `json:"total"`  
64 - }  
65 - Condition struct {  
66 - FieldName string `json:"field"` // 条件字段  
67 - //SqlName string `json:"sqlName,optional"` // 字段SqlName  
68 - Like string `json:"like,optional"` // 模糊匹配  
69 - In []string `json:"in,optional"` // 匹配多个值  
70 - Order string `json:"order,optional,options=ASC||DESC"` // 排序 ASC DESC 默认ASC  
71 - } 83 + GetTableDetailRequest {
  84 + Token string `header:"x-mmm-accesstoken,optional"`
  85 + TableId int `path:"tableId"` // 表ID
  86 + }
  87 + GetTableDetailResponse {
  88 + }
72 89
73 - GetTableDetailRequest struct {  
74 - Token string `header:"x-mmm-accesstoken,optional"`  
75 - TableId int `path:"tableId"` // 表ID  
76 - }  
77 - GetTableDetailResponse struct{  
78 -  
79 - }  
80 -  
81 - SearchTableDataRequest struct{  
82 - Token string `header:"x-mmm-accesstoken,optional"`  
83 - ObjectId int `json:"objectId,optional"` // 对象ID  
84 - PageNumber int `json:"page,optional"` // 分页数  
85 - PageSize int `json:"size,optional"` // 页码  
86 - Condition []*Condition `json:"conditions,optional"` // 条件  
87 - }  
88 - SearchTableDataResponse struct{  
89 -  
90 - } 90 + SearchTableDataRequest {
  91 + Token string `header:"x-mmm-accesstoken,optional"`
  92 + ObjectId int `json:"objectId,optional"` // 对象ID
  93 + PageNumber int `json:"page,optional"` // 分页数
  94 + PageSize int `json:"size,optional"` // 页码
  95 + Condition []*Condition `json:"conditions,optional"` // 条件
  96 + }
  97 + SearchTableDataResponse {
  98 + }
91 ) 99 )