作者 yangfu

feat: table operate

正在显示 100 个修改的文件 包含 3365 行增加74 行删除

要显示太多修改。

为保证性能只显示 100 of 100+ 个文件。

@@ -107,4 +107,37 @@ @@ -107,4 +107,37 @@
107 }, 107 },
108 "msg": "ok" 108 "msg": "ok"
109 } 109 }
110 -```  
  110 +```
  111 +
  112 +
  113 +## 表关联关系
  114 +
  115 +- [x] 可追加数据的表列表 /tables/search-appended-list
  116 +- [x] 校验文件列表 /files/search-verified-file
  117 +- [x] 匹配方案列表 /mapping-rule-config/search
  118 +- [x] 匹配方案主表 /mapping-rule-config/prepare //主表 校验表 主表字段 校验文件表字段
  119 +- [x] 匹配方案添加 /mapping-rule-config/
  120 +- [x] 匹配方案删除 /mapping-rule-config/:id
  121 +- [ ] 追加数据到表格 /append-data-to-table // 验证是否追加过
  122 +
  123 +- [x] 表结构更新 /tables/update-table-struct
  124 +- [x] 表结构添加 /tables/add-table-struct
  125 +- [x] 分表列表 /tables/search
  126 +- [x] 表复制 /tables/copy-data-table
  127 +- [x] 表删除 /tables/:id // 若是删除主表,需级联删除关联的分表,删除内容包括表数据及表结构;? 分表的副表是否要删除
  128 +- [x] 表详情 /tables/:id // 表结构
  129 +- [x] 表更新 /tables/:id // 表结构、分表才可以编辑
  130 +
  131 +- [x] 日志搜索 /log/search
  132 +- [x] 校验步骤日志 /log/verified-step-Log
  133 +
  134 +## 数据预览
  135 +- [ ] 表数据预览(格式) /table/preview
  136 +- [ ] 表数据自定义查询 /table/preview where conditions 升序、降序 包含、不包含
  137 +- [ ] 表数据字段可选值搜索 /table/field-optional 文本匹配
  138 +- [ ] 表数据更新、添加、删除 /table/row-data-mutation
  139 +- [ ] 表数据导出
  140 +
  141 +## 数据验证
  142 +
  143 +- [ ] 文件验证 /data/edit-data-table
  1 +version: v1
  2 +kind: HttpApi
  3 +metadata:
  4 + service: mappingRule
  5 + path: /mapping-rules
  6 + endpoints:
  7 + - method: createMappingRule
  8 + route:
  9 + post: /
  10 + - method: updateMappingRule
  11 + route:
  12 + put: /{mappingRuleId}
  13 + - method: getMappingRule
  14 + route:
  15 + get: /{mappingRuleId}
  16 + - method: removeMappingRule
  17 + route:
  18 + delete: /{mappingRuleId}
  19 + - method: listMappingRule
  20 + route:
  21 + get: /
  22 + params:
  23 + - name: offset
  24 + - name: limit
  25 + - method: search
  26 + route:
  27 + post: /search
  28 + - method: prepare
  29 + route:
  30 + post: /prepare
  1 +version: v1
  2 +kind: HttpApi
  3 +metadata:
  4 + service: table
  5 + path: /tables
  6 + endpoints:
  7 + - method: createTable
  8 + route:
  9 + post: /
  10 + - method: updateTable
  11 + route:
  12 + put: /{tableId}
  13 + - method: getTable
  14 + route:
  15 + get: /{tableId}
  16 + - method: removeTable
  17 + route:
  18 + delete: /{tableId}
  19 + - method: listTable
  20 + route:
  21 + get: /
  22 + params:
  23 + - name: offset
  24 + - name: limit
  25 + - method: splitDataTable
  26 + route:
  27 + post: /split-data-table
  28 + - method: batchEditSubTable
  29 + route:
  30 + post: /batch-edit-sub-table
  31 + - method: copyDataTable
  32 + route:
  33 + post: /copy-data-table
  34 + - method: search
  35 + route:
  36 + post: /search
  1 +version: v1
  2 +kind: Attribute
  3 +metadata:
  4 + name: mainTableFields
  5 + description: 主表列
  6 + type:
  7 + primitive: string
  1 +version: v1
  2 +kind: Attribute
  3 +metadata:
  4 + name: mappingFields
  5 + description: 校验文件列
  6 + type:
  7 + primitive: string
  1 +version: v1
  2 +kind: Attribute
  3 +metadata:
  4 + name: mappingRuleId
  5 + description: 匹配规则ID
  6 + type:
  7 + primitive: int
  1 +version: v1
  2 +kind: Attribute
  3 +metadata:
  4 + name: verifiedFileFields
  5 + description: 校验文件列
  6 + type:
  7 + primitive: string
  1 +version: v1
  2 +kind: Schema
  3 +metadata:
  4 + name: mappingRule
  5 + description: 匹配规则配置
  6 + attributes:
  7 + - ref: mappingRuleId
  8 + required: true
  9 + - ref: name
  10 + required: true
  11 + - ref: tableId
  12 + required: true
  13 + - ref: fileId
  14 + required: true
  15 + - ref: mainTableFields
  16 + required: true
  17 + - ref: verifiedFileFields
  18 + required: true
  19 + - ref: mappingFields
  20 + required: true
  21 + - ref: createdAt
  22 + required: true
  23 + - ref: updatedAt
  24 + required: true
  25 + - ref: deletedAt
  26 + required: true
  1 +version: v1
  2 +kind: Method
  3 +metadata:
  4 + name: createMappingRule
  5 + type: command
  6 + description: 创建匹配规则服务
  7 + payload:
  8 + - ref: name
  9 + required: true
  10 + result:
  11 + - name: mappingRule
  12 + type:
  13 + schema: mappingRule
  14 + required: true
  1 +version: v1
  2 +kind: Method
  3 +metadata:
  4 + name: getMappingRule
  5 + type: query
  6 + description: 返回匹配规则服务
  7 + payload:
  8 + - ref: mappingRuleId
  9 + required: true
  10 + result:
  11 + - name: mappingRule
  12 + type:
  13 + schema: mappingRule
  14 + required: true
  1 +version: v1
  2 +kind: Method
  3 +metadata:
  4 + name: listMappingRule
  5 + type: query
  6 + description: 返回匹配规则服务列表
  7 + payload:
  8 + - ref: offset
  9 + required: true
  10 + - ref: limit
  11 + required: true
  12 + result:
  13 + - ref: count
  14 + required: true
  15 + - name: mappingRules
  16 + type:
  17 + array: mappingRule
  18 + required: true
  1 +version: v1
  2 +kind: Method
  3 +metadata:
  4 + name: prepare
  5 + type: command
  6 + description: 匹配规则预准备(新建规则)
  7 + payload:
  8 + - ref: tableId
  9 + required: true
  10 + - ref: fileId
  11 + required: true
  12 + result:
  13 + - name: mappingRule
  14 + type:
  15 + schema: mappingRule
  16 + required: true
  1 +version: v1
  2 +kind: Method
  3 +metadata:
  4 + name: removeMappingRule
  5 + type: command
  6 + description: 移除匹配规则服务
  7 + payload:
  8 + - ref: mappingRuleId
  9 + required: true
  10 + result:
  11 + - name: mappingRule
  12 + type:
  13 + schema: mappingRule
  14 + required: true
  1 +version: v1
  2 +kind: Method
  3 +metadata:
  4 + name: search
  5 + type: command
  6 + description: 搜索规则
  7 + payload:
  8 + - ref: name
  9 + required: false
  10 + result:
  11 + - name: mappingRule
  12 + type:
  13 + schema: mappingRule
  14 + required: true
  1 +version: v1
  2 +kind: Method
  3 +metadata:
  4 + name: updateMappingRule
  5 + type: command
  6 + description: 更新匹配规则服务
  7 + payload:
  8 + - ref: mappingRuleId
  9 + required: true
  10 + result:
  11 + - name: mappingRule
  12 + type:
  13 + schema: mappingRule
  14 + required: true
  1 +version: v1
  2 +kind: Service
  3 +metadata:
  4 + name: mappingRule
  5 + description: 匹配规则服务
  1 +version: v1
  2 +kind: Method
  3 +metadata:
  4 + name: batchEditSubTable
  5 + type: command
  6 + description: 批量编辑分表
  7 + payload:
  8 + - ref: tableId
  9 + required: true
  10 + result:
  11 + - name: table
  12 + type:
  13 + schema: table
  14 + required: true
  1 +version: v1
  2 +kind: Method
  3 +metadata:
  4 + name: copyDataTable
  5 + type: command
  6 + description: 表复制
  7 + payload:
  8 + - ref: tableId
  9 + required: true
  10 + result:
  11 + - name: table
  12 + type:
  13 + schema: table
  14 + required: true
  1 +version: v1
  2 +kind: Method
  3 +metadata:
  4 + name: createTable
  5 + type: command
  6 + description: 创建表服务
  7 + payload:
  8 + - ref: name
  9 + required: true
  10 + result:
  11 + - name: table
  12 + type:
  13 + schema: table
  14 + required: true
  1 +version: v1
  2 +kind: Method
  3 +metadata:
  4 + name: getTable
  5 + type: query
  6 + description: 返回表服务
  7 + payload:
  8 + - ref: tableId
  9 + required: true
  10 + result:
  11 + - name: table
  12 + type:
  13 + schema: table
  14 + required: true
  1 +version: v1
  2 +kind: Method
  3 +metadata:
  4 + name: listTable
  5 + type: query
  6 + description: 返回表服务列表
  7 + payload:
  8 + - ref: offset
  9 + required: true
  10 + - ref: limit
  11 + required: true
  12 + result:
  13 + - ref: count
  14 + required: true
  15 + - name: tables
  16 + type:
  17 + array: table
  18 + required: true
  1 +version: v1
  2 +kind: Method
  3 +metadata:
  4 + name: removeTable
  5 + type: command
  6 + description: 移除表服务
  7 + payload:
  8 + - ref: tableId
  9 + required: true
  10 + result:
  11 + - name: table
  12 + type:
  13 + schema: table
  14 + required: true
  1 +version: v1
  2 +kind: Method
  3 +metadata:
  4 + name: search
  5 + type: query
  6 + description: 表搜索
  7 + payload:
  8 + - ref: tableType
  9 + required: true
  10 + result:
  11 + - name: table
  12 + type:
  13 + schema: table
  14 + required: true
  1 +version: v1
  2 +kind: Method
  3 +metadata:
  4 + name: splitDataTable
  5 + type: command
  6 + description: 表拆分
  7 + payload:
  8 + - ref: tableId
  9 + required: true
  10 + result:
  11 + - name: table
  12 + type:
  13 + schema: table
  14 + required: true
  1 +version: v1
  2 +kind: Method
  3 +metadata:
  4 + name: updateTable
  5 + type: command
  6 + description: 更新表服务
  7 + payload:
  8 + - ref: tableId
  9 + required: true
  10 + result:
  11 + - name: table
  12 + type:
  13 + schema: table
  14 + required: true
  1 +version: v1
  2 +kind: Service
  3 +metadata:
  4 + name: table
  5 + description: 表服务
@@ -14,7 +14,7 @@ require ( @@ -14,7 +14,7 @@ require (
14 github.com/golang/snappy v0.0.3 // indirect 14 github.com/golang/snappy v0.0.3 // indirect
15 github.com/google/go-cmp v0.5.6 // indirect 15 github.com/google/go-cmp v0.5.6 // indirect
16 github.com/google/go-querystring v1.1.0 // indirect 16 github.com/google/go-querystring v1.1.0 // indirect
17 - github.com/google/uuid v1.3.0 // indirect 17 + github.com/google/uuid v1.3.0
18 github.com/imkira/go-interpol v1.1.0 // indirect 18 github.com/imkira/go-interpol v1.1.0 // indirect
19 github.com/linmadan/egglib-go v0.0.0-20210313060205-8b5e456b11f7 19 github.com/linmadan/egglib-go v0.0.0-20210313060205-8b5e456b11f7
20 github.com/moul/http2curl v1.0.0 // indirect 20 github.com/moul/http2curl v1.0.0 // indirect
@@ -31,6 +31,8 @@ require ( @@ -31,6 +31,8 @@ require (
31 github.com/yudai/golcs v0.0.0-20170316035057-ecda9a501e82 // indirect 31 github.com/yudai/golcs v0.0.0-20170316035057-ecda9a501e82 // indirect
32 github.com/yudai/pp v2.0.1+incompatible // indirect 32 github.com/yudai/pp v2.0.1+incompatible // indirect
33 golang.org/x/text v0.3.7 33 golang.org/x/text v0.3.7
  34 + gorm.io/driver/mysql v1.3.6
  35 + gorm.io/gorm v1.23.8
34 ) 36 )
35 37
36 replace github.com/linmadan/egglib-go v0.0.0-20210313060205-8b5e456b11f7 => github.com/tiptok/egglib-go v0.0.0-20220421085958-9682d0ac42c1 38 replace github.com/linmadan/egglib-go v0.0.0-20210313060205-8b5e456b11f7 => github.com/tiptok/egglib-go v0.0.0-20220421085958-9682d0ac42c1
@@ -6,6 +6,7 @@ import ( @@ -6,6 +6,7 @@ import (
6 "gitlab.fjmaimaimai.com/allied-creation/character-library-metadata-bastion/pkg/constant" 6 "gitlab.fjmaimaimai.com/allied-creation/character-library-metadata-bastion/pkg/constant"
7 "gitlab.fjmaimaimai.com/allied-creation/character-library-metadata-bastion/pkg/infrastructure/pg" 7 "gitlab.fjmaimaimai.com/allied-creation/character-library-metadata-bastion/pkg/infrastructure/pg"
8 "gitlab.fjmaimaimai.com/allied-creation/character-library-metadata-bastion/pkg/infrastructure/redis" 8 "gitlab.fjmaimaimai.com/allied-creation/character-library-metadata-bastion/pkg/infrastructure/redis"
  9 + "gitlab.fjmaimaimai.com/allied-creation/character-library-metadata-bastion/pkg/infrastructure/starrocks"
9 "gitlab.fjmaimaimai.com/allied-creation/character-library-metadata-bastion/pkg/log" 10 "gitlab.fjmaimaimai.com/allied-creation/character-library-metadata-bastion/pkg/log"
10 "time" 11 "time"
11 12
@@ -25,6 +26,9 @@ func main() { @@ -25,6 +26,9 @@ func main() {
25 log.InitLogHook(constant.ENABLE_KAFKA_LOG, true) 26 log.InitLogHook(constant.ENABLE_KAFKA_LOG, true)
26 redis.InitRedis() 27 redis.InitRedis()
27 pg.Init() 28 pg.Init()
  29 + if err := starrocks.Init(); err != nil {
  30 + log.Logger.Error(err.Error())
  31 + }
28 32
29 time.Sleep(time.Second) 33 time.Sleep(time.Second)
30 log.Logger.Info("server start!") 34 log.Logger.Info("server start!")
@@ -27,3 +27,19 @@ func CreateDeleteFileService(transactionContext application.TransactionContext) @@ -27,3 +27,19 @@ func CreateDeleteFileService(transactionContext application.TransactionContext)
27 func CreateGenerateMainTableService(transactionContext application.TransactionContext) (domain.GenerateMainTableService, error) { 27 func CreateGenerateMainTableService(transactionContext application.TransactionContext) (domain.GenerateMainTableService, error) {
28 return domainService.NewGenerateMainTableService(transactionContext.(*pg.TransactionContext)) 28 return domainService.NewGenerateMainTableService(transactionContext.(*pg.TransactionContext))
29 } 29 }
  30 +
  31 +func CreateCopyDataTableService(transactionContext application.TransactionContext) (domain.CopyDataTableService, error) {
  32 + return domainService.NewCopyDataTableService(transactionContext.(*pg.TransactionContext))
  33 +}
  34 +
  35 +func CreateDeleteDataTableService(transactionContext application.TransactionContext) (domain.DeleteDataTableService, error) {
  36 + return domainService.NewDeleteDataTableService(transactionContext.(*pg.TransactionContext))
  37 +}
  38 +
  39 +func CreateUpdateTableStructService(transactionContext application.TransactionContext) (domain.UpdateTableStructService, error) {
  40 + return domainService.NewUpdateTableStructService(transactionContext.(*pg.TransactionContext))
  41 +}
  42 +
  43 +func CreateAddTableStructService(transactionContext application.TransactionContext) (domain.AddTableStructService, error) {
  44 + return domainService.NewAddTableStructService(transactionContext.(*pg.TransactionContext))
  45 +}
@@ -59,3 +59,55 @@ func FastPgTable(transactionContext application.TransactionContext, id int) (dom @@ -59,3 +59,55 @@ func FastPgTable(transactionContext application.TransactionContext, id int) (dom
59 } 59 }
60 return rep, mod, err 60 return rep, mod, err
61 } 61 }
  62 +
  63 +// FastPgLog 快速返回日志对象
  64 +//
  65 +// transactionContext 事务
  66 +// id 对象唯一标识
  67 +func FastPgLog(transactionContext application.TransactionContext, id int) (domain.LogRepository, *domain.Log, error) {
  68 + var rep domain.LogRepository
  69 + var mod *domain.Log
  70 + var err error
  71 + if value, err := CreateLogRepository(map[string]interface{}{
  72 + "transactionContext": transactionContext,
  73 + }); err != nil {
  74 + return nil, nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
  75 + } else {
  76 + rep = value
  77 + }
  78 + if id > 0 {
  79 + if mod, err = rep.FindOne(map[string]interface{}{"logId": id}); err != nil {
  80 + if err == domain.ErrorNotFound {
  81 + return nil, nil, application.ThrowError(application.RES_NO_FIND_ERROR, "该日志不存在")
  82 + }
  83 + return nil, nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
  84 + }
  85 + }
  86 + return rep, mod, err
  87 +}
  88 +
  89 +// FastPgMappingRule 快速返回匹配规则对象
  90 +//
  91 +// transactionContext 事务
  92 +// id 对象唯一标识
  93 +func FastPgMappingRule(transactionContext application.TransactionContext, id int) (domain.MappingRuleRepository, *domain.MappingRule, error) {
  94 + var rep domain.MappingRuleRepository
  95 + var mod *domain.MappingRule
  96 + var err error
  97 + if value, err := CreateMappingRuleRepository(map[string]interface{}{
  98 + "transactionContext": transactionContext,
  99 + }); err != nil {
  100 + return nil, nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
  101 + } else {
  102 + rep = value
  103 + }
  104 + if id > 0 {
  105 + if mod, err = rep.FindOne(map[string]interface{}{"mappingRuleId": id}); err != nil {
  106 + if err == domain.ErrorNotFound {
  107 + return nil, nil, application.ThrowError(application.RES_NO_FIND_ERROR, "该匹配规则不存在")
  108 + }
  109 + return nil, nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
  110 + }
  111 + }
  112 + return rep, mod, err
  113 +}
@@ -29,3 +29,11 @@ func CreateFileRepository(options map[string]interface{}) (domain.FileRepository @@ -29,3 +29,11 @@ func CreateFileRepository(options map[string]interface{}) (domain.FileRepository
29 } 29 }
30 return repository.NewFileRepository(transactionContext) 30 return repository.NewFileRepository(transactionContext)
31 } 31 }
  32 +
  33 +func CreateMappingRuleRepository(options map[string]interface{}) (domain.MappingRuleRepository, error) {
  34 + var transactionContext *pg.TransactionContext
  35 + if value, ok := options["transactionContext"]; ok {
  36 + transactionContext = value.(*pg.TransactionContext)
  37 + }
  38 + return repository.NewMappingRuleRepository(transactionContext)
  39 +}
@@ -21,6 +21,7 @@ type SearchFileQuery struct { @@ -21,6 +21,7 @@ type SearchFileQuery struct {
21 PageSize int `cname:"页数" json:"pageSize,omitempty"` 21 PageSize int `cname:"页数" json:"pageSize,omitempty"`
22 LastId int `cname:"最后一条记录ID" json:"lastId"` 22 LastId int `cname:"最后一条记录ID" json:"lastId"`
23 FileType domain.FileType `cname:"文件类型" json:"fileType" valid:"Required"` 23 FileType domain.FileType `cname:"文件类型" json:"fileType" valid:"Required"`
  24 + Context *domain.Context
24 } 25 }
25 26
26 func (cmd *SearchFileQuery) Valid(validation *validation.Validation) { 27 func (cmd *SearchFileQuery) Valid(validation *validation.Validation) {
@@ -9,7 +9,7 @@ import ( @@ -9,7 +9,7 @@ import (
9 ) 9 )
10 10
11 // 加载表格数据 11 // 加载表格数据
12 -func (fileService *FileService) LoadDataTable(loadDataTableCommand *command.LoadDataTableCommand) (interface{}, error) { 12 +func (fileService *FileService) LoadDataTable(ctx *domain.Context, loadDataTableCommand *command.LoadDataTableCommand) (interface{}, error) {
13 if err := loadDataTableCommand.ValidateCommand(); err != nil { 13 if err := loadDataTableCommand.ValidateCommand(); err != nil {
14 return nil, application.ThrowError(application.ARG_ERROR, err.Error()) 14 return nil, application.ThrowError(application.ARG_ERROR, err.Error())
15 } 15 }
@@ -25,7 +25,7 @@ func (fileService *FileService) LoadDataTable(loadDataTableCommand *command.Load @@ -25,7 +25,7 @@ func (fileService *FileService) LoadDataTable(loadDataTableCommand *command.Load
25 }() 25 }()
26 26
27 loadDataTableService, _ := factory.CreateLoadDataTableService(transactionContext) 27 loadDataTableService, _ := factory.CreateLoadDataTableService(transactionContext)
28 - if _, err := loadDataTableService.Load(loadDataTableCommand.FileId); err != nil { 28 + if _, err := loadDataTableService.Load(ctx, loadDataTableCommand.FileId); err != nil {
29 return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error()) 29 return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
30 } 30 }
31 31
@@ -37,7 +37,7 @@ func (fileService *FileService) LoadDataTable(loadDataTableCommand *command.Load @@ -37,7 +37,7 @@ func (fileService *FileService) LoadDataTable(loadDataTableCommand *command.Load
37 } 37 }
38 38
39 // 编辑表格数据 39 // 编辑表格数据
40 -func (fileService *FileService) EditDataTable(editDataTableCommand *command.EditDataTableCommand) (interface{}, error) { 40 +func (fileService *FileService) EditDataTable(ctx *domain.Context, editDataTableCommand *command.EditDataTableCommand) (interface{}, error) {
41 if err := editDataTableCommand.ValidateCommand(); err != nil { 41 if err := editDataTableCommand.ValidateCommand(); err != nil {
42 return nil, application.ThrowError(application.ARG_ERROR, err.Error()) 42 return nil, application.ThrowError(application.ARG_ERROR, err.Error())
43 } 43 }
@@ -58,7 +58,7 @@ func (fileService *FileService) EditDataTable(editDataTableCommand *command.Edit @@ -58,7 +58,7 @@ func (fileService *FileService) EditDataTable(editDataTableCommand *command.Edit
58 } 58 }
59 59
60 // 持久化表格数据 60 // 持久化表格数据
61 -func (fileService *FileService) FlushDataTable(flushDataTableCommand *command.FlushDataTableCommand) (interface{}, error) { 61 +func (fileService *FileService) FlushDataTable(ctx *domain.Context, flushDataTableCommand *command.FlushDataTableCommand) (interface{}, error) {
62 if err := flushDataTableCommand.ValidateCommand(); err != nil { 62 if err := flushDataTableCommand.ValidateCommand(); err != nil {
63 return nil, application.ThrowError(application.ARG_ERROR, err.Error()) 63 return nil, application.ThrowError(application.ARG_ERROR, err.Error())
64 } 64 }
@@ -80,7 +80,7 @@ func (fileService *FileService) FlushDataTable(flushDataTableCommand *command.Fl @@ -80,7 +80,7 @@ func (fileService *FileService) FlushDataTable(flushDataTableCommand *command.Fl
80 SQLType: f.Type, 80 SQLType: f.Type,
81 }) 81 })
82 } 82 }
83 - if _, err := flushDataTableService.Flush(flushDataTableCommand.FileId, &domain.Table{ 83 + if _, err := flushDataTableService.Flush(ctx, flushDataTableCommand.FileId, &domain.Table{
84 DataFields: fields, 84 DataFields: fields,
85 RowCount: flushDataTableCommand.RowCount, 85 RowCount: flushDataTableCommand.RowCount,
86 }); err != nil { 86 }); err != nil {
@@ -93,7 +93,7 @@ func (fileService *FileService) FlushDataTable(flushDataTableCommand *command.Fl @@ -93,7 +93,7 @@ func (fileService *FileService) FlushDataTable(flushDataTableCommand *command.Fl
93 } 93 }
94 94
95 // 生成主表 95 // 生成主表
96 -func (fileService *FileService) GenerateMainTable(generateMainTableCommand *command.GenerateMainTableCommand) (interface{}, error) { 96 +func (fileService *FileService) GenerateMainTable(ctx *domain.Context, generateMainTableCommand *command.GenerateMainTableCommand) (interface{}, error) {
97 if err := generateMainTableCommand.ValidateCommand(); err != nil { 97 if err := generateMainTableCommand.ValidateCommand(); err != nil {
98 return nil, application.ThrowError(application.ARG_ERROR, err.Error()) 98 return nil, application.ThrowError(application.ARG_ERROR, err.Error())
99 } 99 }
@@ -109,7 +109,7 @@ func (fileService *FileService) GenerateMainTable(generateMainTableCommand *comm @@ -109,7 +109,7 @@ func (fileService *FileService) GenerateMainTable(generateMainTableCommand *comm
109 }() 109 }()
110 110
111 generateMainTableService, _ := factory.CreateGenerateMainTableService(transactionContext) 111 generateMainTableService, _ := factory.CreateGenerateMainTableService(transactionContext)
112 - _, err = generateMainTableService.GenerateTable(generateMainTableCommand.FileId, generateMainTableCommand.TableName) 112 + _, err = generateMainTableService.GenerateTable(ctx, generateMainTableCommand.FileId, generateMainTableCommand.TableName)
113 if err != nil { 113 if err != nil {
114 return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error()) 114 return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
115 } 115 }
@@ -20,7 +20,7 @@ type FileService struct { @@ -20,7 +20,7 @@ type FileService struct {
20 } 20 }
21 21
22 // 创建文件服务 22 // 创建文件服务
23 -func (fileService *FileService) CreateFile(createFileCommand *command.CreateFileCommand) (interface{}, error) { 23 +func (fileService *FileService) CreateFile(ctx *domain.Context, createFileCommand *command.CreateFileCommand) (interface{}, error) {
24 if err := createFileCommand.ValidateCommand(); err != nil { 24 if err := createFileCommand.ValidateCommand(); err != nil {
25 return nil, application.ThrowError(application.ARG_ERROR, err.Error()) 25 return nil, application.ThrowError(application.ARG_ERROR, err.Error())
26 } 26 }
@@ -43,9 +43,10 @@ func (fileService *FileService) CreateFile(createFileCommand *command.CreateFile @@ -43,9 +43,10 @@ func (fileService *FileService) CreateFile(createFileCommand *command.CreateFile
43 Ext: filepath.Ext(createFileCommand.Name), 43 Ext: filepath.Ext(createFileCommand.Name),
44 }, 44 },
45 SourceFileId: 0, 45 SourceFileId: 0,
46 - Operator: "",  
47 - CreatedAt: time.Now(),  
48 - UpdatedAt: time.Now(), 46 + //Operator: "",
  47 + CreatedAt: time.Now(),
  48 + UpdatedAt: time.Now(),
  49 + Context: ctx,
49 } 50 }
50 fileRepository, _, _ := factory.FastPgFile(transactionContext, 0) 51 fileRepository, _, _ := factory.FastPgFile(transactionContext, 0)
51 file, err := fileRepository.Save(newFile) 52 file, err := fileRepository.Save(newFile)
@@ -53,7 +54,7 @@ func (fileService *FileService) CreateFile(createFileCommand *command.CreateFile @@ -53,7 +54,7 @@ func (fileService *FileService) CreateFile(createFileCommand *command.CreateFile
53 return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error()) 54 return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
54 } 55 }
55 if err = factory.FastLog(transactionContext, domain.CommonLog, file.FileId, &domainService.FileUploadSuccessLog{ 56 if err = factory.FastLog(transactionContext, domain.CommonLog, file.FileId, &domainService.FileUploadSuccessLog{
56 - LogEntry: domain.NewLogEntry(file.FileInfo.Name, domain.SourceFile.ToString(), domain.FileUpload, ""), 57 + LogEntry: domain.NewLogEntry(file.FileInfo.Name, domain.SourceFile.ToString(), domain.FileUpload, ctx),
57 }); err != nil { 58 }); err != nil {
58 return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error()) 59 return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
59 } 60 }
@@ -209,7 +210,7 @@ func (fileService *FileService) RemoveFile(removeFileCommand *command.RemoveFile @@ -209,7 +210,7 @@ func (fileService *FileService) RemoveFile(removeFileCommand *command.RemoveFile
209 return nil, application.ThrowError(application.RES_NO_FIND_ERROR, fmt.Sprintf("%s", string(removeFileCommand.FileId))) 210 return nil, application.ThrowError(application.RES_NO_FIND_ERROR, fmt.Sprintf("%s", string(removeFileCommand.FileId)))
210 } 211 }
211 deleteFileService, _ := factory.CreateDeleteFileService(transactionContext) 212 deleteFileService, _ := factory.CreateDeleteFileService(transactionContext)
212 - err = deleteFileService.Delete(file) 213 + err = deleteFileService.Delete(nil, file)
213 if err != nil { 214 if err != nil {
214 return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error()) 215 return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
215 } 216 }
@@ -2,29 +2,62 @@ package command @@ -2,29 +2,62 @@ package command
2 2
3 import ( 3 import (
4 "fmt" 4 "fmt"
  5 + "gitlab.fjmaimaimai.com/allied-creation/character-library-metadata-bastion/pkg/domain"
5 "reflect" 6 "reflect"
6 "strings" 7 "strings"
  8 + "time"
7 9
8 "github.com/beego/beego/v2/core/validation" 10 "github.com/beego/beego/v2/core/validation"
9 ) 11 )
10 12
11 type SearchLogCommand struct { 13 type SearchLogCommand struct {
  14 + // 日志类型 VerifiedStepLog:校验步骤 CommonLog:常规日志
  15 + LogType string `json:"logType"`
12 // 日志内容 16 // 日志内容
13 - Content string `cname:"日志内容" json:"content,omitempty"` 17 + MatchContent string `cname:"匹配日志内容" json:"matchContent,omitempty"`
  18 + // 源数据ID
  19 + SourceId int `cname:"源数据ID" json:"sourceId"`
  20 + // 多个源数据ID
  21 + InSourceId []int `cname:"多个源数据ID" json:"inSourceId"`
  22 + // 页码
  23 + PageNumber int `cname:"页码" json:"pageNumber,omitempty"`
  24 + // 页数
  25 + PageSize int `cname:"页数" json:"pageSize,omitempty"`
  26 +
  27 + Year int `cname:"年" json:"year,omitempty"`
  28 + Month int `cname:"月" json:"month,omitempty"`
  29 + Day int `cname:"日" json:"day,omitempty"`
  30 + // 开始时间
  31 + BeginTime time.Time `cname:"开始时间" json:"beginTime"`
  32 + // 结束时间
  33 + EndTime time.Time `cname:"结束时间" json:"endTime"`
  34 +
  35 + Context *domain.Context
14 } 36 }
15 37
16 -func (searchLogCommand *SearchLogCommand) Valid(validation *validation.Validation) {  
17 - validation.SetError("CustomValid", "未实现的自定义认证") 38 +func (cmd *SearchLogCommand) Valid(validation *validation.Validation) {
  39 + if cmd.Year > 0 && cmd.Month == 0 && cmd.Day == 0 {
  40 + cmd.BeginTime = time.Date(cmd.Year, 1, 1, 0, 0, 0, 0, time.Local)
  41 + cmd.EndTime = cmd.BeginTime.AddDate(1, 0, 0)
  42 + }
  43 + if cmd.Year > 0 && cmd.Month > 0 && cmd.Day == 0 {
  44 + cmd.BeginTime = time.Date(cmd.Year, time.Month(cmd.Month), 1, 0, 0, 0, 0, time.Local)
  45 + cmd.EndTime = cmd.BeginTime.AddDate(0, 1, 0)
  46 + }
  47 + if cmd.Year > 0 && cmd.Month > 0 && cmd.Day > 0 {
  48 + cmd.BeginTime = time.Date(cmd.Year, time.Month(cmd.Month), cmd.Day, 0, 0, 0, 0, time.Local)
  49 + cmd.EndTime = cmd.BeginTime.AddDate(0, 0, cmd.Day)
  50 + }
18 } 51 }
19 52
20 -func (searchLogCommand *SearchLogCommand) ValidateCommand() error { 53 +func (cmd *SearchLogCommand) ValidateCommand() error {
21 valid := validation.Validation{} 54 valid := validation.Validation{}
22 - b, err := valid.Valid(searchLogCommand) 55 + b, err := valid.Valid(cmd)
23 if err != nil { 56 if err != nil {
24 return err 57 return err
25 } 58 }
26 if !b { 59 if !b {
27 - elem := reflect.TypeOf(searchLogCommand).Elem() 60 + elem := reflect.TypeOf(cmd).Elem()
28 for _, validErr := range valid.Errors { 61 for _, validErr := range valid.Errors {
29 field, isExist := elem.FieldByName(validErr.Field) 62 field, isExist := elem.FieldByName(validErr.Field)
30 if isExist { 63 if isExist {
  1 +package dto
  2 +
  3 +import "gitlab.fjmaimaimai.com/allied-creation/character-library-metadata-bastion/pkg/domain"
  4 +
  5 +type LogDto struct {
  6 + // 日志ID
  7 + LogId int `json:"logId"`
  8 + // 日志类型 1.校验步骤 2.常规日志
  9 + //LogType string `json:"logType"`
  10 + // 源数据ID
  11 + SourceId int `json:"sourceId"`
  12 + // 对象名称 数据表名 / 文件名
  13 + ObjectName string `json:"objectName"`
  14 + // 对象类型 1.主表 2.分表 3.副表 4.源文件 5.校验文件
  15 + ObjectType string `json:"objectType"`
  16 + // 操作类型 1.主表生成 2.主表拆分 3.数据导入 4.分表生成 5.表复制 6.编辑记录 7.文件上传 8.文件校验
  17 + OperationType string `json:"operationType"`
  18 + // 日志内容
  19 + Content string `json:"content"`
  20 + // 操作人名称
  21 + OperatorName string `json:"operatorName"`
  22 + // 创建时间
  23 + CreatedAt string `json:"createdAt"`
  24 +}
  25 +
  26 +func (d *LogDto) Load(m *domain.Log) {
  27 + d.LogId = m.LogId
  28 + d.SourceId = m.SourceId
  29 + d.ObjectName = m.ObjectName
  30 + d.ObjectType = m.ObjectType
  31 + d.OperationType = m.OperationType
  32 + d.Content = m.Content
  33 + d.OperatorName = m.OperatorName
  34 + d.CreatedAt = m.CreatedAt.Local().Format("2006-01-02 15:04:05")
  35 +}
  1 +package dto
  2 +
  3 +import "gitlab.fjmaimaimai.com/allied-creation/character-library-metadata-bastion/pkg/domain"
  4 +
  5 +type VerifiedStepLogDto struct {
  6 + // 日志ID
  7 + LogId int `json:"logId"`
  8 + // 日志类型 1.校验步骤 2.常规日志
  9 + //LogType string `json:"logType"`
  10 + // 源数据ID
  11 + //SourceId int `json:"sourceId"`
  12 + // 对象名称 数据表名 / 文件名
  13 + //ObjectName string `json:"objectName"`
  14 + // 对象类型 1.主表 2.分表 3.副表 4.源文件 5.校验文件
  15 + //ObjectType string `json:"objectType"`
  16 + // 操作类型 1.主表生成 2.主表拆分 3.数据导入 4.分表生成 5.表复制 6.编辑记录 7.文件上传 8.文件校验
  17 + //OperationType string `json:"operationType"`
  18 + // 日志内容
  19 + Content string `json:"content"`
  20 + // 操作人名称
  21 + //OperatorName string `json:"operatorName"`
  22 + // 创建时间
  23 + CreatedAt string `json:"createdAt"`
  24 +}
  25 +
  26 +func (d *VerifiedStepLogDto) Load(m *domain.Log) {
  27 + d.LogId = m.LogId
  28 + //d.SourceId = m.SourceId
  29 + //d.ObjectName = m.ObjectName
  30 + //d.ObjectType = m.ObjectType
  31 + //d.OperationType = m.OperationType
  32 + d.Content = m.Content
  33 + //d.OperatorName = m.OperatorName
  34 + d.CreatedAt = m.CreatedAt.Local().Format("2006-01-02 15:04:05")
  35 +}
@@ -6,8 +6,10 @@ import ( @@ -6,8 +6,10 @@ import (
6 "github.com/linmadan/egglib-go/utils/tool_funs" 6 "github.com/linmadan/egglib-go/utils/tool_funs"
7 "gitlab.fjmaimaimai.com/allied-creation/character-library-metadata-bastion/pkg/application/factory" 7 "gitlab.fjmaimaimai.com/allied-creation/character-library-metadata-bastion/pkg/application/factory"
8 "gitlab.fjmaimaimai.com/allied-creation/character-library-metadata-bastion/pkg/application/log/command" 8 "gitlab.fjmaimaimai.com/allied-creation/character-library-metadata-bastion/pkg/application/log/command"
  9 + "gitlab.fjmaimaimai.com/allied-creation/character-library-metadata-bastion/pkg/application/log/dto"
9 "gitlab.fjmaimaimai.com/allied-creation/character-library-metadata-bastion/pkg/application/log/query" 10 "gitlab.fjmaimaimai.com/allied-creation/character-library-metadata-bastion/pkg/application/log/query"
10 "gitlab.fjmaimaimai.com/allied-creation/character-library-metadata-bastion/pkg/domain" 11 "gitlab.fjmaimaimai.com/allied-creation/character-library-metadata-bastion/pkg/domain"
  12 + "gitlab.fjmaimaimai.com/allied-creation/character-library-metadata-bastion/pkg/infrastructure/utils"
11 ) 13 )
12 14
13 // 日志服务 15 // 日志服务
@@ -168,24 +170,86 @@ func (logService *LogService) RemoveLog(removeLogCommand *command.RemoveLogComma @@ -168,24 +170,86 @@ func (logService *LogService) RemoveLog(removeLogCommand *command.RemoveLogComma
168 } 170 }
169 171
170 // 搜索日志 172 // 搜索日志
171 -func (logService *LogService) SearchLog(searchLogCommand *command.SearchLogCommand) (interface{}, error) { 173 +func (logService *LogService) SearchLog(searchLogCommand *command.SearchLogCommand) (int64, interface{}, error) {
172 if err := searchLogCommand.ValidateCommand(); err != nil { 174 if err := searchLogCommand.ValidateCommand(); err != nil {
173 - return nil, application.ThrowError(application.ARG_ERROR, err.Error()) 175 + return 0, nil, application.ThrowError(application.ARG_ERROR, err.Error())
174 } 176 }
175 transactionContext, err := factory.CreateTransactionContext(nil) 177 transactionContext, err := factory.CreateTransactionContext(nil)
176 if err != nil { 178 if err != nil {
177 - return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error()) 179 + return 0, nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
178 } 180 }
179 if err := transactionContext.StartTransaction(); err != nil { 181 if err := transactionContext.StartTransaction(); err != nil {
180 - return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error()) 182 + return 0, nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
181 } 183 }
182 defer func() { 184 defer func() {
183 transactionContext.RollbackTransaction() 185 transactionContext.RollbackTransaction()
184 }() 186 }()
  187 +
  188 + logRepository, _, _ := factory.FastPgLog(transactionContext, 0)
  189 + count, logs, err := logRepository.Find(utils.ObjectToMap(searchLogCommand))
  190 + if err != nil {
  191 + return 0, nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
  192 + }
  193 +
  194 + var result = make([]*dto.LogDto, 0)
  195 + for _, m := range logs {
  196 + var item = &dto.LogDto{}
  197 + item.Load(m)
  198 + result = append(result, item)
  199 + }
  200 +
185 if err := transactionContext.CommitTransaction(); err != nil { 201 if err := transactionContext.CommitTransaction(); err != nil {
186 - return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error()) 202 + return 0, nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
  203 + }
  204 + return count, result, nil
  205 +}
  206 +
  207 +// 搜索日志
  208 +func (logService *LogService) VerifiedStepLog(searchLogCommand *command.SearchLogCommand) (int64, interface{}, error) {
  209 + if err := searchLogCommand.ValidateCommand(); err != nil {
  210 + return 0, nil, application.ThrowError(application.ARG_ERROR, err.Error())
  211 + }
  212 + transactionContext, err := factory.CreateTransactionContext(nil)
  213 + if err != nil {
  214 + return 0, nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
  215 + }
  216 + if err := transactionContext.StartTransaction(); err != nil {
  217 + return 0, nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
  218 + }
  219 + defer func() {
  220 + transactionContext.RollbackTransaction()
  221 + }()
  222 +
  223 + _, sourceFile, err := factory.FastPgFile(transactionContext, searchLogCommand.SourceId)
  224 + if err != nil {
  225 + return 0, nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
  226 + }
  227 +
  228 + searchLogCommand.LogType = domain.VerifiedStepLog.ToString()
  229 + searchLogCommand.SourceId = 0
  230 + searchLogCommand.InSourceId = []int{sourceFile.FileId, sourceFile.SourceFileId}
  231 + searchLogCommand.PageNumber = 1
  232 + searchLogCommand.PageSize = 1000
  233 +
  234 + logRepository, _, _ := factory.FastPgLog(transactionContext, 0)
  235 + count, logs, err := logRepository.Find(utils.ObjectToMap(searchLogCommand))
  236 + if err != nil {
  237 + return 0, nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
  238 + }
  239 +
  240 + var result = make([]*dto.VerifiedStepLogDto, 0)
  241 + for _, m := range logs {
  242 + var item = &dto.VerifiedStepLogDto{}
  243 + item.Load(m)
  244 + result = append(result, item)
  245 + }
  246 +
  247 + if err := transactionContext.CommitTransaction(); err != nil {
  248 + return 0, nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
187 } 249 }
188 - return nil, nil 250 + return count, map[string]interface{}{
  251 + "logs": result,
  252 + }, nil
189 } 253 }
190 254
191 // 更新日志服务 255 // 更新日志服务
  1 +package command
  2 +
  3 +import (
  4 + "fmt"
  5 + "gitlab.fjmaimaimai.com/allied-creation/character-library-metadata-bastion/pkg/domain"
  6 + "reflect"
  7 + "strings"
  8 +
  9 + "github.com/beego/beego/v2/core/validation"
  10 +)
  11 +
  12 +type CreateMappingRuleCommand struct {
  13 + // 名称
  14 + Name string `cname:"名称" json:"name" valid:"Required"`
  15 + // 表Id
  16 + TableId int `json:"tableId" valid:"Required"`
  17 + // 文件ID
  18 + FileId int `json:"fileId" valid:"Required"`
  19 + // 校验文件列
  20 + MappingFields []*domain.MappingField `json:"mappingFields"`
  21 +}
  22 +
  23 +func (createMappingRuleCommand *CreateMappingRuleCommand) Valid(validation *validation.Validation) {
  24 +
  25 +}
  26 +
  27 +func (createMappingRuleCommand *CreateMappingRuleCommand) ValidateCommand() error {
  28 + valid := validation.Validation{}
  29 + b, err := valid.Valid(createMappingRuleCommand)
  30 + if err != nil {
  31 + return err
  32 + }
  33 + if !b {
  34 + elem := reflect.TypeOf(createMappingRuleCommand).Elem()
  35 + for _, validErr := range valid.Errors {
  36 + field, isExist := elem.FieldByName(validErr.Field)
  37 + if isExist {
  38 + return fmt.Errorf(strings.Replace(validErr.Message, validErr.Field, field.Tag.Get("cname"), -1))
  39 + } else {
  40 + return fmt.Errorf(validErr.Message)
  41 + }
  42 + }
  43 + }
  44 + return nil
  45 +}
  1 +package command
  2 +
  3 +import (
  4 + "fmt"
  5 + "reflect"
  6 + "strings"
  7 +
  8 + "github.com/beego/beego/v2/core/validation"
  9 +)
  10 +
  11 +type PrepareCommand struct {
  12 + // 表Id
  13 + TableId int `cname:"表Id" json:"tableId" valid:"Required"`
  14 + // 文件ID
  15 + FileId int `cname:"文件ID" json:"fileId" valid:"Required"`
  16 +}
  17 +
  18 +func (prepareCommand *PrepareCommand) Valid(validation *validation.Validation) {
  19 +
  20 +}
  21 +
  22 +func (prepareCommand *PrepareCommand) ValidateCommand() error {
  23 + valid := validation.Validation{}
  24 + b, err := valid.Valid(prepareCommand)
  25 + if err != nil {
  26 + return err
  27 + }
  28 + if !b {
  29 + elem := reflect.TypeOf(prepareCommand).Elem()
  30 + for _, validErr := range valid.Errors {
  31 + field, isExist := elem.FieldByName(validErr.Field)
  32 + if isExist {
  33 + return fmt.Errorf(strings.Replace(validErr.Message, validErr.Field, field.Tag.Get("cname"), -1))
  34 + } else {
  35 + return fmt.Errorf(validErr.Message)
  36 + }
  37 + }
  38 + }
  39 + return nil
  40 +}
  1 +package command
  2 +
  3 +import (
  4 + "fmt"
  5 + "reflect"
  6 + "strings"
  7 +
  8 + "github.com/beego/beego/v2/core/validation"
  9 +)
  10 +
  11 +type RemoveMappingRuleCommand struct {
  12 + // 匹配规则ID
  13 + MappingRuleId int `cname:"匹配规则ID" json:"mappingRuleId" valid:"Required"`
  14 +}
  15 +
  16 +func (removeMappingRuleCommand *RemoveMappingRuleCommand) Valid(validation *validation.Validation) {
  17 +
  18 +}
  19 +
  20 +func (removeMappingRuleCommand *RemoveMappingRuleCommand) ValidateCommand() error {
  21 + valid := validation.Validation{}
  22 + b, err := valid.Valid(removeMappingRuleCommand)
  23 + if err != nil {
  24 + return err
  25 + }
  26 + if !b {
  27 + elem := reflect.TypeOf(removeMappingRuleCommand).Elem()
  28 + for _, validErr := range valid.Errors {
  29 + field, isExist := elem.FieldByName(validErr.Field)
  30 + if isExist {
  31 + return fmt.Errorf(strings.Replace(validErr.Message, validErr.Field, field.Tag.Get("cname"), -1))
  32 + } else {
  33 + return fmt.Errorf(validErr.Message)
  34 + }
  35 + }
  36 + }
  37 + return nil
  38 +}
  1 +package command
  2 +
  3 +import (
  4 + "fmt"
  5 + "gitlab.fjmaimaimai.com/allied-creation/character-library-metadata-bastion/pkg/domain"
  6 + "reflect"
  7 + "strings"
  8 +
  9 + "github.com/beego/beego/v2/core/validation"
  10 +)
  11 +
  12 +type SearchCommand struct {
  13 + // 表ID
  14 + TableId int `cname:"表ID" json:"tableId,omitempty"`
  15 +
  16 + Context *domain.Context
  17 +}
  18 +
  19 +func (searchCommand *SearchCommand) Valid(validation *validation.Validation) {
  20 +
  21 +}
  22 +
  23 +func (searchCommand *SearchCommand) ValidateCommand() error {
  24 + valid := validation.Validation{}
  25 + b, err := valid.Valid(searchCommand)
  26 + if err != nil {
  27 + return err
  28 + }
  29 + if !b {
  30 + elem := reflect.TypeOf(searchCommand).Elem()
  31 + for _, validErr := range valid.Errors {
  32 + field, isExist := elem.FieldByName(validErr.Field)
  33 + if isExist {
  34 + return fmt.Errorf(strings.Replace(validErr.Message, validErr.Field, field.Tag.Get("cname"), -1))
  35 + } else {
  36 + return fmt.Errorf(validErr.Message)
  37 + }
  38 + }
  39 + }
  40 + return nil
  41 +}
  1 +package command
  2 +
  3 +import (
  4 + "fmt"
  5 + "gitlab.fjmaimaimai.com/allied-creation/character-library-metadata-bastion/pkg/domain"
  6 + "reflect"
  7 + "strings"
  8 +
  9 + "github.com/beego/beego/v2/core/validation"
  10 +)
  11 +
  12 +type UpdateMappingRuleCommand struct {
  13 + // 匹配规则ID
  14 + MappingRuleId int `cname:"匹配规则ID" json:"mappingRuleId" valid:"Required"`
  15 + // 名称
  16 + Name string `cname:"名称" json:"name" valid:"Required"`
  17 + // 校验文件列
  18 + MappingFields []*domain.MappingField `cname:"匹配规则列表" json:"mappingFields" valid:"Required"`
  19 +}
  20 +
  21 +func (updateMappingRuleCommand *UpdateMappingRuleCommand) Valid(validation *validation.Validation) {
  22 +
  23 +}
  24 +
  25 +func (updateMappingRuleCommand *UpdateMappingRuleCommand) ValidateCommand() error {
  26 + valid := validation.Validation{}
  27 + b, err := valid.Valid(updateMappingRuleCommand)
  28 + if err != nil {
  29 + return err
  30 + }
  31 + if !b {
  32 + elem := reflect.TypeOf(updateMappingRuleCommand).Elem()
  33 + for _, validErr := range valid.Errors {
  34 + field, isExist := elem.FieldByName(validErr.Field)
  35 + if isExist {
  36 + return fmt.Errorf(strings.Replace(validErr.Message, validErr.Field, field.Tag.Get("cname"), -1))
  37 + } else {
  38 + return fmt.Errorf(validErr.Message)
  39 + }
  40 + }
  41 + }
  42 + return nil
  43 +}
  1 +package dto
  2 +
  3 +import "gitlab.fjmaimaimai.com/allied-creation/character-library-metadata-bastion/pkg/domain"
  4 +
  5 +type MappingRuleDto struct {
  6 + // 匹配规则ID
  7 + MappingRuleId int `json:"mappingRuleId"`
  8 + // 名称
  9 + Name string `json:"name"`
  10 + // 表Id
  11 + TableId int `json:"tableId"`
  12 + // 文件ID
  13 + FileId int `json:"fileId"`
  14 + // 主表列
  15 + MainTableFields []*domain.Field `json:"mainTableFields"`
  16 + // 校验文件列
  17 + VerifiedFileFields []*domain.Field `json:"verifiedFileFields"`
  18 + // 校验文件列
  19 + MappingFields []*domain.MappingField `json:"mappingFields"`
  20 +}
  21 +
  22 +func (d *MappingRuleDto) Load(m *domain.MappingRule) {
  23 + d.MappingRuleId = m.MappingRuleId
  24 + d.Name = m.Name
  25 + d.TableId = m.TableId
  26 + d.FileId = m.FileId
  27 + d.MainTableFields = m.MainTableFields
  28 + d.VerifiedFileFields = m.VerifiedFileFields
  29 + d.MappingFields = m.MappingFields
  30 +}
  31 +
  32 +func (d *MappingRuleDto) LoadFromTableAndFile(m *domain.Table, f *domain.File, fm *domain.Table) {
  33 + d.MappingRuleId = 0
  34 + d.Name = m.Name
  35 + d.TableId = m.TableId
  36 + d.FileId = f.FileId
  37 + d.MainTableFields = m.Fields(false)
  38 + d.VerifiedFileFields = fm.Fields(false)
  39 + d.MappingFields = domain.NewMappingFields(d.MainTableFields, d.VerifiedFileFields)
  40 +}
  1 +package query
  2 +
  3 +import (
  4 + "fmt"
  5 + "reflect"
  6 + "strings"
  7 +
  8 + "github.com/beego/beego/v2/core/validation"
  9 +)
  10 +
  11 +type GetMappingRuleQuery struct {
  12 + // 匹配规则ID
  13 + MappingRuleId int `cname:"匹配规则ID" json:"mappingRuleId" valid:"Required"`
  14 +}
  15 +
  16 +func (getMappingRuleQuery *GetMappingRuleQuery) Valid(validation *validation.Validation) {
  17 + validation.SetError("CustomValid", "未实现的自定义认证")
  18 +}
  19 +
  20 +func (getMappingRuleQuery *GetMappingRuleQuery) ValidateQuery() error {
  21 + valid := validation.Validation{}
  22 + b, err := valid.Valid(getMappingRuleQuery)
  23 + if err != nil {
  24 + return err
  25 + }
  26 + if !b {
  27 + elem := reflect.TypeOf(getMappingRuleQuery).Elem()
  28 + for _, validErr := range valid.Errors {
  29 + field, isExist := elem.FieldByName(validErr.Field)
  30 + if isExist {
  31 + return fmt.Errorf(strings.Replace(validErr.Message, validErr.Field, field.Tag.Get("cname"), -1))
  32 + } else {
  33 + return fmt.Errorf(validErr.Message)
  34 + }
  35 + }
  36 + }
  37 + return nil
  38 +}
  1 +package query
  2 +
  3 +import (
  4 + "fmt"
  5 + "reflect"
  6 + "strings"
  7 +
  8 + "github.com/beego/beego/v2/core/validation"
  9 +)
  10 +
  11 +type ListMappingRuleQuery struct {
  12 + // 查询偏离量
  13 + Offset int `cname:"查询偏离量" json:"offset" valid:"Required"`
  14 + // 查询限制
  15 + Limit int `cname:"查询限制" json:"limit" valid:"Required"`
  16 +}
  17 +
  18 +func (listMappingRuleQuery *ListMappingRuleQuery) Valid(validation *validation.Validation) {
  19 + validation.SetError("CustomValid", "未实现的自定义认证")
  20 +}
  21 +
  22 +func (listMappingRuleQuery *ListMappingRuleQuery) ValidateQuery() error {
  23 + valid := validation.Validation{}
  24 + b, err := valid.Valid(listMappingRuleQuery)
  25 + if err != nil {
  26 + return err
  27 + }
  28 + if !b {
  29 + elem := reflect.TypeOf(listMappingRuleQuery).Elem()
  30 + for _, validErr := range valid.Errors {
  31 + field, isExist := elem.FieldByName(validErr.Field)
  32 + if isExist {
  33 + return fmt.Errorf(strings.Replace(validErr.Message, validErr.Field, field.Tag.Get("cname"), -1))
  34 + } else {
  35 + return fmt.Errorf(validErr.Message)
  36 + }
  37 + }
  38 + }
  39 + return nil
  40 +}
  1 +package service
  2 +
  3 +import (
  4 + "fmt"
  5 + "github.com/linmadan/egglib-go/core/application"
  6 + "github.com/linmadan/egglib-go/utils/tool_funs"
  7 + "gitlab.fjmaimaimai.com/allied-creation/character-library-metadata-bastion/pkg/application/factory"
  8 + "gitlab.fjmaimaimai.com/allied-creation/character-library-metadata-bastion/pkg/application/mappingRule/command"
  9 + "gitlab.fjmaimaimai.com/allied-creation/character-library-metadata-bastion/pkg/application/mappingRule/dto"
  10 + "gitlab.fjmaimaimai.com/allied-creation/character-library-metadata-bastion/pkg/application/mappingRule/query"
  11 + "gitlab.fjmaimaimai.com/allied-creation/character-library-metadata-bastion/pkg/domain"
  12 + "gitlab.fjmaimaimai.com/allied-creation/character-library-metadata-bastion/pkg/infrastructure/utils"
  13 + "time"
  14 +)
  15 +
  16 +// 匹配规则服务
  17 +type MappingRuleService struct {
  18 +}
  19 +
  20 +// 创建匹配规则服务
  21 +func (mappingRuleService *MappingRuleService) CreateMappingRule(ctx *domain.Context, cmd *command.CreateMappingRuleCommand) (interface{}, error) {
  22 + if err := cmd.ValidateCommand(); err != nil {
  23 + return nil, application.ThrowError(application.ARG_ERROR, err.Error())
  24 + }
  25 + transactionContext, err := factory.CreateTransactionContext(nil)
  26 + if err != nil {
  27 + return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
  28 + }
  29 + if err := transactionContext.StartTransaction(); err != nil {
  30 + return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
  31 + }
  32 + defer func() {
  33 + transactionContext.RollbackTransaction()
  34 + }()
  35 +
  36 + mappingRuleRepository, _, _ := factory.FastPgMappingRule(transactionContext, 0)
  37 + _, mainTable, err := factory.FastPgTable(transactionContext, cmd.TableId)
  38 + if err != nil {
  39 + return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
  40 + }
  41 + _, file, err := factory.FastPgFile(transactionContext, cmd.FileId)
  42 + if err != nil {
  43 + return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
  44 + }
  45 + _, fileTable, err := factory.FastPgTable(transactionContext, file.FileInfo.TableId)
  46 + if err != nil {
  47 + return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
  48 + }
  49 +
  50 + if duplicateRule, e := mappingRuleRepository.FindOne(map[string]interface{}{"context": ctx, "name": cmd.Name}); e == nil && duplicateRule != nil {
  51 + return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, "方案名称重复")
  52 + }
  53 +
  54 + newMappingRule := &domain.MappingRule{
  55 + Name: cmd.Name,
  56 + TableId: cmd.TableId,
  57 + FileId: cmd.FileId,
  58 + MainTableFields: mainTable.Fields(false),
  59 + VerifiedFileFields: fileTable.Fields(false),
  60 + MappingFields: cmd.MappingFields,
  61 + CreatedAt: time.Now(),
  62 + UpdatedAt: time.Now(),
  63 + Context: ctx,
  64 + }
  65 + if newMappingRule, err = mappingRuleRepository.Save(newMappingRule); err != nil {
  66 + return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
  67 + }
  68 +
  69 + if err := transactionContext.CommitTransaction(); err != nil {
  70 + return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
  71 + }
  72 + return struct{}{}, nil
  73 +}
  74 +
  75 +// 返回匹配规则服务
  76 +func (mappingRuleService *MappingRuleService) GetMappingRule(getMappingRuleQuery *query.GetMappingRuleQuery) (interface{}, error) {
  77 + if err := getMappingRuleQuery.ValidateQuery(); err != nil {
  78 + return nil, application.ThrowError(application.ARG_ERROR, err.Error())
  79 + }
  80 + transactionContext, err := factory.CreateTransactionContext(nil)
  81 + if err != nil {
  82 + return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
  83 + }
  84 + if err := transactionContext.StartTransaction(); err != nil {
  85 + return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
  86 + }
  87 + defer func() {
  88 + transactionContext.RollbackTransaction()
  89 + }()
  90 + var mappingRuleRepository domain.MappingRuleRepository
  91 + if value, err := factory.CreateMappingRuleRepository(map[string]interface{}{
  92 + "transactionContext": transactionContext,
  93 + }); err != nil {
  94 + return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
  95 + } else {
  96 + mappingRuleRepository = value
  97 + }
  98 + mappingRule, err := mappingRuleRepository.FindOne(map[string]interface{}{"mappingRuleId": getMappingRuleQuery.MappingRuleId})
  99 + if err != nil {
  100 + return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
  101 + }
  102 + if mappingRule == nil {
  103 + return nil, application.ThrowError(application.RES_NO_FIND_ERROR, fmt.Sprintf("%s", string(getMappingRuleQuery.MappingRuleId)))
  104 + } else {
  105 + if err := transactionContext.CommitTransaction(); err != nil {
  106 + return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
  107 + }
  108 + return mappingRule, nil
  109 + }
  110 +}
  111 +
  112 +// 返回匹配规则服务列表
  113 +func (mappingRuleService *MappingRuleService) ListMappingRule(listMappingRuleQuery *query.ListMappingRuleQuery) (interface{}, error) {
  114 + if err := listMappingRuleQuery.ValidateQuery(); err != nil {
  115 + return nil, application.ThrowError(application.ARG_ERROR, err.Error())
  116 + }
  117 + transactionContext, err := factory.CreateTransactionContext(nil)
  118 + if err != nil {
  119 + return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
  120 + }
  121 + if err := transactionContext.StartTransaction(); err != nil {
  122 + return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
  123 + }
  124 + defer func() {
  125 + transactionContext.RollbackTransaction()
  126 + }()
  127 + var mappingRuleRepository domain.MappingRuleRepository
  128 + if value, err := factory.CreateMappingRuleRepository(map[string]interface{}{
  129 + "transactionContext": transactionContext,
  130 + }); err != nil {
  131 + return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
  132 + } else {
  133 + mappingRuleRepository = value
  134 + }
  135 + if count, mappingRules, err := mappingRuleRepository.Find(tool_funs.SimpleStructToMap(listMappingRuleQuery)); err != nil {
  136 + return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
  137 + } else {
  138 + if err := transactionContext.CommitTransaction(); err != nil {
  139 + return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
  140 + }
  141 + return map[string]interface{}{
  142 + "count": count,
  143 + "mappingRules": mappingRules,
  144 + }, nil
  145 + }
  146 +}
  147 +
  148 +// 匹配规则预准备(新建规则)
  149 +func (mappingRuleService *MappingRuleService) Prepare(prepareCommand *command.PrepareCommand) (interface{}, error) {
  150 + if err := prepareCommand.ValidateCommand(); err != nil {
  151 + return nil, application.ThrowError(application.ARG_ERROR, err.Error())
  152 + }
  153 + transactionContext, err := factory.CreateTransactionContext(nil)
  154 + if err != nil {
  155 + return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
  156 + }
  157 + if err := transactionContext.StartTransaction(); err != nil {
  158 + return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
  159 + }
  160 + defer func() {
  161 + transactionContext.RollbackTransaction()
  162 + }()
  163 +
  164 + _, mainTable, err := factory.FastPgTable(transactionContext, prepareCommand.TableId)
  165 + if err != nil {
  166 + return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
  167 + }
  168 + _, file, err := factory.FastPgFile(transactionContext, prepareCommand.FileId)
  169 + if err != nil {
  170 + return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
  171 + }
  172 + _, fileTable, err := factory.FastPgTable(transactionContext, file.FileInfo.TableId)
  173 + if err != nil {
  174 + return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
  175 + }
  176 +
  177 + ruleDto := dto.MappingRuleDto{}
  178 + ruleDto.LoadFromTableAndFile(mainTable, file, fileTable)
  179 + if err := transactionContext.CommitTransaction(); err != nil {
  180 + return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
  181 + }
  182 + return ruleDto, nil
  183 +}
  184 +
  185 +// 移除匹配规则服务
  186 +func (mappingRuleService *MappingRuleService) RemoveMappingRule(removeMappingRuleCommand *command.RemoveMappingRuleCommand) (interface{}, error) {
  187 + if err := removeMappingRuleCommand.ValidateCommand(); err != nil {
  188 + return nil, application.ThrowError(application.ARG_ERROR, err.Error())
  189 + }
  190 + transactionContext, err := factory.CreateTransactionContext(nil)
  191 + if err != nil {
  192 + return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
  193 + }
  194 + if err := transactionContext.StartTransaction(); err != nil {
  195 + return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
  196 + }
  197 + defer func() {
  198 + transactionContext.RollbackTransaction()
  199 + }()
  200 + var mappingRuleRepository domain.MappingRuleRepository
  201 + if value, err := factory.CreateMappingRuleRepository(map[string]interface{}{
  202 + "transactionContext": transactionContext,
  203 + }); err != nil {
  204 + return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
  205 + } else {
  206 + mappingRuleRepository = value
  207 + }
  208 + mappingRule, err := mappingRuleRepository.FindOne(map[string]interface{}{"mappingRuleId": removeMappingRuleCommand.MappingRuleId})
  209 + if err != nil {
  210 + return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
  211 + }
  212 + if mappingRule == nil {
  213 + return nil, application.ThrowError(application.RES_NO_FIND_ERROR, fmt.Sprintf("%s", string(removeMappingRuleCommand.MappingRuleId)))
  214 + }
  215 + if _, err := mappingRuleRepository.Remove(mappingRule); err != nil {
  216 + return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
  217 + }
  218 + if err := transactionContext.CommitTransaction(); err != nil {
  219 + return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
  220 + }
  221 + return struct {
  222 + }{}, nil
  223 +}
  224 +
  225 +// 搜索规则
  226 +func (mappingRuleService *MappingRuleService) Search(searchCommand *command.SearchCommand) (interface{}, error) {
  227 + if err := searchCommand.ValidateCommand(); err != nil {
  228 + return nil, application.ThrowError(application.ARG_ERROR, err.Error())
  229 + }
  230 + transactionContext, err := factory.CreateTransactionContext(nil)
  231 + if err != nil {
  232 + return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
  233 + }
  234 + if err := transactionContext.StartTransaction(); err != nil {
  235 + return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
  236 + }
  237 + defer func() {
  238 + transactionContext.RollbackTransaction()
  239 + }()
  240 +
  241 + mappingRuleRepository, _, _ := factory.FastPgMappingRule(transactionContext, 0)
  242 + count, rules, err := mappingRuleRepository.Find(utils.ObjectToMap(searchCommand))
  243 + if err != nil {
  244 + return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
  245 + }
  246 + var result = make([]*dto.MappingRuleDto, 0)
  247 + for _, r := range rules {
  248 + var item = &dto.MappingRuleDto{}
  249 + item.Load(r)
  250 + result = append(result, item)
  251 + }
  252 +
  253 + if err := transactionContext.CommitTransaction(); err != nil {
  254 + return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
  255 + }
  256 + return map[string]interface{}{
  257 + "count": count,
  258 + "rules": result,
  259 + }, nil
  260 +}
  261 +
  262 +// 更新匹配规则服务
  263 +func (mappingRuleService *MappingRuleService) UpdateMappingRule(ctx *domain.Context, cmd *command.UpdateMappingRuleCommand) (interface{}, error) {
  264 + if err := cmd.ValidateCommand(); err != nil {
  265 + return nil, application.ThrowError(application.ARG_ERROR, err.Error())
  266 + }
  267 + transactionContext, err := factory.CreateTransactionContext(nil)
  268 + if err != nil {
  269 + return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
  270 + }
  271 + if err := transactionContext.StartTransaction(); err != nil {
  272 + return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
  273 + }
  274 + defer func() {
  275 + transactionContext.RollbackTransaction()
  276 + }()
  277 +
  278 + mappingRuleRepository, mappingRule, err := factory.FastPgMappingRule(transactionContext, cmd.MappingRuleId)
  279 + if err != nil {
  280 + return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
  281 + }
  282 + _, file, err := factory.FastPgFile(transactionContext, mappingRule.FileId)
  283 + if err != nil {
  284 + return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
  285 + }
  286 + _, fileTable, err := factory.FastPgTable(transactionContext, file.FileInfo.TableId)
  287 + if err != nil {
  288 + return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
  289 + }
  290 +
  291 + if duplicateRule, e := mappingRuleRepository.FindOne(map[string]interface{}{"context": ctx, "name": cmd.Name}); e == nil && duplicateRule != nil && duplicateRule.MappingRuleId != cmd.MappingRuleId {
  292 + return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, "方案名称重复")
  293 + }
  294 +
  295 + mappingRule.Name = cmd.Name
  296 + mappingRule.MappingFields = cmd.MappingFields
  297 + mappingRule.VerifiedFileFields = fileTable.Fields(false)
  298 + mappingRule.UpdatedAt = time.Now()
  299 +
  300 + if mappingRule, err = mappingRuleRepository.Save(mappingRule); err != nil {
  301 + return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
  302 + }
  303 + if err := transactionContext.CommitTransaction(); err != nil {
  304 + return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
  305 + }
  306 + return struct{}{}, nil
  307 +}
  308 +
  309 +func NewMappingRuleService(options map[string]interface{}) *MappingRuleService {
  310 + newMappingRuleService := &MappingRuleService{}
  311 + return newMappingRuleService
  312 +}
  1 +package command
  2 +
  3 +import (
  4 + "fmt"
  5 + "gitlab.fjmaimaimai.com/allied-creation/character-library-metadata-bastion/pkg/domain"
  6 + "reflect"
  7 + "strings"
  8 +
  9 + "github.com/beego/beego/v2/core/validation"
  10 +)
  11 +
  12 +type AddTableStructCommand struct {
  13 + // 表Id
  14 + TableId int `cname:"表Id" json:"parentTableId" valid:"Required"`
  15 + Name string `json:"name"`
  16 + // 数据列
  17 + Fields []*domain.Field `json:"fields"`
  18 +}
  19 +
  20 +func (updateTableCommand *AddTableStructCommand) Valid(validation *validation.Validation) {
  21 +
  22 +}
  23 +
  24 +func (updateTableCommand *AddTableStructCommand) ValidateCommand() error {
  25 + valid := validation.Validation{}
  26 + b, err := valid.Valid(updateTableCommand)
  27 + if err != nil {
  28 + return err
  29 + }
  30 + if !b {
  31 + elem := reflect.TypeOf(updateTableCommand).Elem()
  32 + for _, validErr := range valid.Errors {
  33 + field, isExist := elem.FieldByName(validErr.Field)
  34 + if isExist {
  35 + return fmt.Errorf(strings.Replace(validErr.Message, validErr.Field, field.Tag.Get("cname"), -1))
  36 + } else {
  37 + return fmt.Errorf(validErr.Message)
  38 + }
  39 + }
  40 + }
  41 + return nil
  42 +}
  1 +package command
  2 +
  3 +import (
  4 + "fmt"
  5 + "reflect"
  6 + "strings"
  7 +
  8 + "github.com/beego/beego/v2/core/validation"
  9 +)
  10 +
  11 +type BatchEditSubTableCommand struct {
  12 + // 表Id
  13 + TableId string `cname:"表Id" json:"tableId" valid:"Required"`
  14 +}
  15 +
  16 +func (batchEditSubTableCommand *BatchEditSubTableCommand) Valid(validation *validation.Validation) {
  17 +
  18 +}
  19 +
  20 +func (batchEditSubTableCommand *BatchEditSubTableCommand) ValidateCommand() error {
  21 + valid := validation.Validation{}
  22 + b, err := valid.Valid(batchEditSubTableCommand)
  23 + if err != nil {
  24 + return err
  25 + }
  26 + if !b {
  27 + elem := reflect.TypeOf(batchEditSubTableCommand).Elem()
  28 + for _, validErr := range valid.Errors {
  29 + field, isExist := elem.FieldByName(validErr.Field)
  30 + if isExist {
  31 + return fmt.Errorf(strings.Replace(validErr.Message, validErr.Field, field.Tag.Get("cname"), -1))
  32 + } else {
  33 + return fmt.Errorf(validErr.Message)
  34 + }
  35 + }
  36 + }
  37 + return nil
  38 +}
  1 +package command
  2 +
  3 +import (
  4 + "fmt"
  5 + "reflect"
  6 + "strings"
  7 +
  8 + "github.com/beego/beego/v2/core/validation"
  9 +)
  10 +
  11 +type CopyDataTableCommand struct {
  12 + // 表Id
  13 + TableId int `cname:"表Id" json:"tableId" valid:"Required"`
  14 + // 表名
  15 + TableName string `cname:"表名" json:"tableName" valid:"Required"`
  16 +}
  17 +
  18 +func (copyDataTableCommand *CopyDataTableCommand) Valid(validation *validation.Validation) {
  19 +
  20 +}
  21 +
  22 +func (copyDataTableCommand *CopyDataTableCommand) ValidateCommand() error {
  23 + valid := validation.Validation{}
  24 + b, err := valid.Valid(copyDataTableCommand)
  25 + if err != nil {
  26 + return err
  27 + }
  28 + if !b {
  29 + elem := reflect.TypeOf(copyDataTableCommand).Elem()
  30 + for _, validErr := range valid.Errors {
  31 + field, isExist := elem.FieldByName(validErr.Field)
  32 + if isExist {
  33 + return fmt.Errorf(strings.Replace(validErr.Message, validErr.Field, field.Tag.Get("cname"), -1))
  34 + } else {
  35 + return fmt.Errorf(validErr.Message)
  36 + }
  37 + }
  38 + }
  39 + return nil
  40 +}
  1 +package command
  2 +
  3 +import (
  4 + "fmt"
  5 + "reflect"
  6 + "strings"
  7 +
  8 + "github.com/beego/beego/v2/core/validation"
  9 +)
  10 +
  11 +type CreateTableCommand struct {
  12 + // 名称
  13 + Name string `cname:"名称" json:"name" valid:"Required"`
  14 +}
  15 +
  16 +func (createTableCommand *CreateTableCommand) Valid(validation *validation.Validation) {
  17 +
  18 +}
  19 +
  20 +func (createTableCommand *CreateTableCommand) ValidateCommand() error {
  21 + valid := validation.Validation{}
  22 + b, err := valid.Valid(createTableCommand)
  23 + if err != nil {
  24 + return err
  25 + }
  26 + if !b {
  27 + elem := reflect.TypeOf(createTableCommand).Elem()
  28 + for _, validErr := range valid.Errors {
  29 + field, isExist := elem.FieldByName(validErr.Field)
  30 + if isExist {
  31 + return fmt.Errorf(strings.Replace(validErr.Message, validErr.Field, field.Tag.Get("cname"), -1))
  32 + } else {
  33 + return fmt.Errorf(validErr.Message)
  34 + }
  35 + }
  36 + }
  37 + return nil
  38 +}
  1 +package command
  2 +
  3 +import (
  4 + "fmt"
  5 + "reflect"
  6 + "strings"
  7 +
  8 + "github.com/beego/beego/v2/core/validation"
  9 +)
  10 +
  11 +type RemoveTableCommand struct {
  12 + // 表Id
  13 + TableId int `cname:"表Id" json:"tableId" valid:"Required"`
  14 +}
  15 +
  16 +func (removeTableCommand *RemoveTableCommand) Valid(validation *validation.Validation) {
  17 +
  18 +}
  19 +
  20 +func (removeTableCommand *RemoveTableCommand) ValidateCommand() error {
  21 + valid := validation.Validation{}
  22 + b, err := valid.Valid(removeTableCommand)
  23 + if err != nil {
  24 + return err
  25 + }
  26 + if !b {
  27 + elem := reflect.TypeOf(removeTableCommand).Elem()
  28 + for _, validErr := range valid.Errors {
  29 + field, isExist := elem.FieldByName(validErr.Field)
  30 + if isExist {
  31 + return fmt.Errorf(strings.Replace(validErr.Message, validErr.Field, field.Tag.Get("cname"), -1))
  32 + } else {
  33 + return fmt.Errorf(validErr.Message)
  34 + }
  35 + }
  36 + }
  37 + return nil
  38 +}
  1 +package command
  2 +
  3 +import (
  4 + "fmt"
  5 + "reflect"
  6 + "strings"
  7 +
  8 + "github.com/beego/beego/v2/core/validation"
  9 +)
  10 +
  11 +type SplitDataTableCommand struct {
  12 + // 表Id
  13 + TableId string `cname:"表Id" json:"tableId" valid:"Required"`
  14 +}
  15 +
  16 +func (splitDataTableCommand *SplitDataTableCommand) Valid(validation *validation.Validation) {
  17 +
  18 +}
  19 +
  20 +func (splitDataTableCommand *SplitDataTableCommand) ValidateCommand() error {
  21 + valid := validation.Validation{}
  22 + b, err := valid.Valid(splitDataTableCommand)
  23 + if err != nil {
  24 + return err
  25 + }
  26 + if !b {
  27 + elem := reflect.TypeOf(splitDataTableCommand).Elem()
  28 + for _, validErr := range valid.Errors {
  29 + field, isExist := elem.FieldByName(validErr.Field)
  30 + if isExist {
  31 + return fmt.Errorf(strings.Replace(validErr.Message, validErr.Field, field.Tag.Get("cname"), -1))
  32 + } else {
  33 + return fmt.Errorf(validErr.Message)
  34 + }
  35 + }
  36 + }
  37 + return nil
  38 +}
  1 +package command
  2 +
  3 +import (
  4 + "fmt"
  5 + "gitlab.fjmaimaimai.com/allied-creation/character-library-metadata-bastion/pkg/domain"
  6 + "reflect"
  7 + "strings"
  8 +
  9 + "github.com/beego/beego/v2/core/validation"
  10 +)
  11 +
  12 +type UpdateTableCommand struct {
  13 + // 表Id
  14 + TableId int `cname:"表Id" json:"tableId" valid:"Required"`
  15 + Name string `json:"name"`
  16 + // 数据列
  17 + Fields []*domain.Field `json:"fields"`
  18 +}
  19 +
  20 +func (updateTableCommand *UpdateTableCommand) Valid(validation *validation.Validation) {
  21 +
  22 +}
  23 +
  24 +func (updateTableCommand *UpdateTableCommand) ValidateCommand() error {
  25 + valid := validation.Validation{}
  26 + b, err := valid.Valid(updateTableCommand)
  27 + if err != nil {
  28 + return err
  29 + }
  30 + if !b {
  31 + elem := reflect.TypeOf(updateTableCommand).Elem()
  32 + for _, validErr := range valid.Errors {
  33 + field, isExist := elem.FieldByName(validErr.Field)
  34 + if isExist {
  35 + return fmt.Errorf(strings.Replace(validErr.Message, validErr.Field, field.Tag.Get("cname"), -1))
  36 + } else {
  37 + return fmt.Errorf(validErr.Message)
  38 + }
  39 + }
  40 + }
  41 + return nil
  42 +}
  1 +package command
  2 +
  3 +import (
  4 + "fmt"
  5 + "gitlab.fjmaimaimai.com/allied-creation/character-library-metadata-bastion/pkg/domain"
  6 + "reflect"
  7 + "strings"
  8 +
  9 + "github.com/beego/beego/v2/core/validation"
  10 +)
  11 +
  12 +type UpdateTableStructCommand struct {
  13 + // 表Id
  14 + TableId int `cname:"表Id" json:"tableId" valid:"Required"`
  15 + Name string `json:"name"`
  16 + // 数据列
  17 + Fields []*domain.Field `json:"fields"`
  18 +}
  19 +
  20 +func (updateTableCommand *UpdateTableStructCommand) Valid(validation *validation.Validation) {
  21 +
  22 +}
  23 +
  24 +func (updateTableCommand *UpdateTableStructCommand) ValidateCommand() error {
  25 + valid := validation.Validation{}
  26 + b, err := valid.Valid(updateTableCommand)
  27 + if err != nil {
  28 + return err
  29 + }
  30 + if !b {
  31 + elem := reflect.TypeOf(updateTableCommand).Elem()
  32 + for _, validErr := range valid.Errors {
  33 + field, isExist := elem.FieldByName(validErr.Field)
  34 + if isExist {
  35 + return fmt.Errorf(strings.Replace(validErr.Message, validErr.Field, field.Tag.Get("cname"), -1))
  36 + } else {
  37 + return fmt.Errorf(validErr.Message)
  38 + }
  39 + }
  40 + }
  41 + return nil
  42 +}
  1 +package dto
  2 +
  3 +import "gitlab.fjmaimaimai.com/allied-creation/character-library-metadata-bastion/pkg/domain"
  4 +
  5 +type TableDetailDto struct {
  6 + // 表Id
  7 + TableId int `json:"tableId"`
  8 + // 表类型 MainTable:主表 SideTable:副表 SubTable:分表 ExcelTable:Excel表
  9 + TableType string `json:"tableType"`
  10 + // 名称
  11 + Name string `json:"name"`
  12 + // 父级ID
  13 + ParentId int `json:"parentId"`
  14 + // 主表字段
  15 + MainTableFields []*domain.Field `json:"mainTableFields"`
  16 + // 手动添加的列
  17 + ManualFields []*domain.Field `json:"manualFields"`
  18 + // 数据列
  19 + Fields []*domain.Field `json:"fields"`
  20 +}
  21 +
  22 +func (d *TableDetailDto) Load(m *domain.Table, mainTable *domain.Table) {
  23 + d.TableId = m.TableId
  24 + d.TableType = m.TableType
  25 + d.Name = m.Name
  26 + d.ParentId = m.ParentId
  27 + if m.TableType == domain.MainTable.ToString() || m.TableType == domain.SideTable.ToString() {
  28 + d.MainTableFields = m.Fields(false)
  29 + } else if mainTable != nil {
  30 + d.MainTableFields = mainTable.Fields(false)
  31 + }
  32 + d.Fields = m.Fields(false)
  33 + d.ManualFields = m.ManualFields
  34 +}
  1 +package dto
  2 +
  3 +import "gitlab.fjmaimaimai.com/allied-creation/character-library-metadata-bastion/pkg/domain"
  4 +
  5 +type TableDto struct {
  6 + // 表Id
  7 + TableId int `json:"tableId"`
  8 + // 表类型 MainTable:主表 SideTable:副表 SubTable:分表 ExcelTable:Excel表
  9 + TableType string `json:"tableType"`
  10 + // 名称
  11 + Name string `json:"name"`
  12 + // 父级ID
  13 + ParentId int `json:"parentId"`
  14 +}
  15 +
  16 +func (d *TableDto) Load(m *domain.Table) {
  17 + d.TableId = m.TableId
  18 + d.TableType = m.TableType
  19 + d.Name = m.Name
  20 + d.ParentId = m.ParentId
  21 +}
  1 +package query
  2 +
  3 +import (
  4 + "fmt"
  5 + "reflect"
  6 + "strings"
  7 +
  8 + "github.com/beego/beego/v2/core/validation"
  9 +)
  10 +
  11 +type GetTableQuery struct {
  12 + // 表Id
  13 + TableId int `cname:"表Id" json:"tableId" valid:"Required"`
  14 +}
  15 +
  16 +func (getTableQuery *GetTableQuery) Valid(validation *validation.Validation) {
  17 +
  18 +}
  19 +
  20 +func (getTableQuery *GetTableQuery) ValidateQuery() error {
  21 + valid := validation.Validation{}
  22 + b, err := valid.Valid(getTableQuery)
  23 + if err != nil {
  24 + return err
  25 + }
  26 + if !b {
  27 + elem := reflect.TypeOf(getTableQuery).Elem()
  28 + for _, validErr := range valid.Errors {
  29 + field, isExist := elem.FieldByName(validErr.Field)
  30 + if isExist {
  31 + return fmt.Errorf(strings.Replace(validErr.Message, validErr.Field, field.Tag.Get("cname"), -1))
  32 + } else {
  33 + return fmt.Errorf(validErr.Message)
  34 + }
  35 + }
  36 + }
  37 + return nil
  38 +}
  1 +package query
  2 +
  3 +import (
  4 + "fmt"
  5 + "reflect"
  6 + "strings"
  7 +
  8 + "github.com/beego/beego/v2/core/validation"
  9 +)
  10 +
  11 +type ListTableQuery struct {
  12 + // 查询偏离量
  13 + Offset int `cname:"查询偏离量" json:"offset" valid:"Required"`
  14 + // 查询限制
  15 + Limit int `cname:"查询限制" json:"limit" valid:"Required"`
  16 +}
  17 +
  18 +func (listTableQuery *ListTableQuery) Valid(validation *validation.Validation) {
  19 +
  20 +}
  21 +
  22 +func (listTableQuery *ListTableQuery) ValidateQuery() error {
  23 + valid := validation.Validation{}
  24 + b, err := valid.Valid(listTableQuery)
  25 + if err != nil {
  26 + return err
  27 + }
  28 + if !b {
  29 + elem := reflect.TypeOf(listTableQuery).Elem()
  30 + for _, validErr := range valid.Errors {
  31 + field, isExist := elem.FieldByName(validErr.Field)
  32 + if isExist {
  33 + return fmt.Errorf(strings.Replace(validErr.Message, validErr.Field, field.Tag.Get("cname"), -1))
  34 + } else {
  35 + return fmt.Errorf(validErr.Message)
  36 + }
  37 + }
  38 + }
  39 + return nil
  40 +}
  1 +package query
  2 +
  3 +import (
  4 + "fmt"
  5 + "gitlab.fjmaimaimai.com/allied-creation/character-library-metadata-bastion/pkg/domain"
  6 + "reflect"
  7 + "strings"
  8 +
  9 + "github.com/beego/beego/v2/core/validation"
  10 +)
  11 +
  12 +type SearchTableQuery struct {
  13 + // 表名称
  14 + Name string `cname:"表名称" json:"name"`
  15 + // 表类型 MainTable:主表 SideTable:副表 SubTable:分表 ExcelTable:Excel表
  16 + TableTypes []string `cname:"表类型 MainTable:主表 SideTable:副表 SubTable:分表 ExcelTable:Excel表" json:"tableTypes"`
  17 + // 父级ID
  18 + ParentId int `cname:"父级ID" json:"parentId"`
  19 +
  20 + // 父级ID
  21 + ParentTableId int `cname:"父级ID" json:"parentTableId"`
  22 +
  23 + Context *domain.Context
  24 +}
  25 +
  26 +func (searchQuery *SearchTableQuery) Valid(validation *validation.Validation) {
  27 + if searchQuery.ParentTableId > 0 && searchQuery.ParentId == 0 {
  28 + searchQuery.ParentId = searchQuery.ParentTableId
  29 + }
  30 +}
  31 +
  32 +func (searchQuery *SearchTableQuery) ValidateQuery() error {
  33 + valid := validation.Validation{}
  34 + b, err := valid.Valid(searchQuery)
  35 + if err != nil {
  36 + return err
  37 + }
  38 + if !b {
  39 + elem := reflect.TypeOf(searchQuery).Elem()
  40 + for _, validErr := range valid.Errors {
  41 + field, isExist := elem.FieldByName(validErr.Field)
  42 + if isExist {
  43 + return fmt.Errorf(strings.Replace(validErr.Message, validErr.Field, field.Tag.Get("cname"), -1))
  44 + } else {
  45 + return fmt.Errorf(validErr.Message)
  46 + }
  47 + }
  48 + }
  49 + return nil
  50 +}
  1 +package service
  2 +
  3 +import (
  4 + "fmt"
  5 + "github.com/linmadan/egglib-go/core/application"
  6 + "github.com/linmadan/egglib-go/utils/tool_funs"
  7 + "gitlab.fjmaimaimai.com/allied-creation/character-library-metadata-bastion/pkg/application/factory"
  8 + "gitlab.fjmaimaimai.com/allied-creation/character-library-metadata-bastion/pkg/application/table/command"
  9 + "gitlab.fjmaimaimai.com/allied-creation/character-library-metadata-bastion/pkg/application/table/dto"
  10 + "gitlab.fjmaimaimai.com/allied-creation/character-library-metadata-bastion/pkg/application/table/query"
  11 + "gitlab.fjmaimaimai.com/allied-creation/character-library-metadata-bastion/pkg/domain"
  12 + "gitlab.fjmaimaimai.com/allied-creation/character-library-metadata-bastion/pkg/infrastructure/utils"
  13 +)
  14 +
  15 +// 表服务
  16 +type TableService struct {
  17 +}
  18 +
  19 +// 批量编辑分表
  20 +func (tableService *TableService) BatchEditSubTable(batchEditSubTableCommand *command.BatchEditSubTableCommand) (interface{}, error) {
  21 + if err := batchEditSubTableCommand.ValidateCommand(); err != nil {
  22 + return nil, application.ThrowError(application.ARG_ERROR, err.Error())
  23 + }
  24 + transactionContext, err := factory.CreateTransactionContext(nil)
  25 + if err != nil {
  26 + return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
  27 + }
  28 + if err := transactionContext.StartTransaction(); err != nil {
  29 + return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
  30 + }
  31 + defer func() {
  32 + transactionContext.RollbackTransaction()
  33 + }()
  34 + if err := transactionContext.CommitTransaction(); err != nil {
  35 + return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
  36 + }
  37 + return nil, nil
  38 +}
  39 +
  40 +// 表复制
  41 +func (tableService *TableService) CopyDataTable(ctx *domain.Context, cmd *command.CopyDataTableCommand) (interface{}, error) {
  42 + if err := cmd.ValidateCommand(); err != nil {
  43 + return nil, application.ThrowError(application.ARG_ERROR, err.Error())
  44 + }
  45 + transactionContext, err := factory.CreateTransactionContext(nil)
  46 + if err != nil {
  47 + return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
  48 + }
  49 + if err := transactionContext.StartTransaction(); err != nil {
  50 + return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
  51 + }
  52 + defer func() {
  53 + transactionContext.RollbackTransaction()
  54 + }()
  55 +
  56 + copyDataTableService, err := factory.CreateCopyDataTableService(transactionContext)
  57 +
  58 + if _, err = copyDataTableService.CopyTable(ctx, cmd.TableId, cmd.TableName); err != nil {
  59 + return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
  60 + }
  61 + if err := transactionContext.CommitTransaction(); err != nil {
  62 + return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
  63 + }
  64 + return struct{}{}, nil
  65 +}
  66 +
  67 +// 创建表服务
  68 +func (tableService *TableService) CreateTable(createTableCommand *command.CreateTableCommand) (interface{}, error) {
  69 + if err := createTableCommand.ValidateCommand(); err != nil {
  70 + return nil, application.ThrowError(application.ARG_ERROR, err.Error())
  71 + }
  72 + transactionContext, err := factory.CreateTransactionContext(nil)
  73 + if err != nil {
  74 + return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
  75 + }
  76 + if err := transactionContext.StartTransaction(); err != nil {
  77 + return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
  78 + }
  79 + defer func() {
  80 + transactionContext.RollbackTransaction()
  81 + }()
  82 + newTable := &domain.Table{
  83 + Name: createTableCommand.Name,
  84 + }
  85 + var tableRepository domain.TableRepository
  86 + if value, err := factory.CreateTableRepository(map[string]interface{}{
  87 + "transactionContext": transactionContext,
  88 + }); err != nil {
  89 + return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
  90 + } else {
  91 + tableRepository = value
  92 + }
  93 + if table, err := tableRepository.Save(newTable); err != nil {
  94 + return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
  95 + } else {
  96 + if err := transactionContext.CommitTransaction(); err != nil {
  97 + return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
  98 + }
  99 + return table, nil
  100 + }
  101 +}
  102 +
  103 +// 返回表服务
  104 +func (tableService *TableService) GetTable(getTableQuery *query.GetTableQuery) (interface{}, error) {
  105 + if err := getTableQuery.ValidateQuery(); err != nil {
  106 + return nil, application.ThrowError(application.ARG_ERROR, err.Error())
  107 + }
  108 + transactionContext, err := factory.CreateTransactionContext(nil)
  109 + if err != nil {
  110 + return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
  111 + }
  112 + if err := transactionContext.StartTransaction(); err != nil {
  113 + return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
  114 + }
  115 + defer func() {
  116 + transactionContext.RollbackTransaction()
  117 + }()
  118 + _, table, err := factory.FastPgTable(transactionContext, getTableQuery.TableId)
  119 + if err != nil {
  120 + return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
  121 + }
  122 +
  123 + var mainTable *domain.Table
  124 + if table.ParentId != 0 {
  125 + _, mainTable, _ = factory.FastPgTable(transactionContext, table.ParentId)
  126 + }
  127 + tableDetailDto := dto.TableDetailDto{}
  128 + tableDetailDto.Load(table, mainTable)
  129 + if err := transactionContext.CommitTransaction(); err != nil {
  130 + return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
  131 + }
  132 + return tableDetailDto, nil
  133 +}
  134 +
  135 +// 返回表服务列表
  136 +func (tableService *TableService) ListTable(listTableQuery *query.ListTableQuery) (interface{}, error) {
  137 + if err := listTableQuery.ValidateQuery(); err != nil {
  138 + return nil, application.ThrowError(application.ARG_ERROR, err.Error())
  139 + }
  140 + transactionContext, err := factory.CreateTransactionContext(nil)
  141 + if err != nil {
  142 + return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
  143 + }
  144 + if err := transactionContext.StartTransaction(); err != nil {
  145 + return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
  146 + }
  147 + defer func() {
  148 + transactionContext.RollbackTransaction()
  149 + }()
  150 + var tableRepository domain.TableRepository
  151 + if value, err := factory.CreateTableRepository(map[string]interface{}{
  152 + "transactionContext": transactionContext,
  153 + }); err != nil {
  154 + return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
  155 + } else {
  156 + tableRepository = value
  157 + }
  158 + if count, tables, err := tableRepository.Find(tool_funs.SimpleStructToMap(listTableQuery)); err != nil {
  159 + return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
  160 + } else {
  161 + if err := transactionContext.CommitTransaction(); err != nil {
  162 + return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
  163 + }
  164 + return map[string]interface{}{
  165 + "count": count,
  166 + "tables": tables,
  167 + }, nil
  168 + }
  169 +}
  170 +
  171 +// 移除表服务
  172 +func (tableService *TableService) RemoveTable(ctx *domain.Context, removeTableCommand *command.RemoveTableCommand) (interface{}, error) {
  173 + if err := removeTableCommand.ValidateCommand(); err != nil {
  174 + return nil, application.ThrowError(application.ARG_ERROR, err.Error())
  175 + }
  176 + transactionContext, err := factory.CreateTransactionContext(nil)
  177 + if err != nil {
  178 + return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
  179 + }
  180 + if err := transactionContext.StartTransaction(); err != nil {
  181 + return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
  182 + }
  183 + defer func() {
  184 + transactionContext.RollbackTransaction()
  185 + }()
  186 +
  187 + deleteDataTableService, _ := factory.CreateDeleteDataTableService(transactionContext)
  188 + if _, err := deleteDataTableService.DeleteTable(ctx, removeTableCommand.TableId); err != nil {
  189 + return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
  190 + }
  191 + if err := transactionContext.CommitTransaction(); err != nil {
  192 + return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
  193 + }
  194 + return struct{}{}, nil
  195 +}
  196 +
  197 +// 表搜索
  198 +func (tableService *TableService) Search(searchQuery *query.SearchTableQuery) (interface{}, error) {
  199 + if err := searchQuery.ValidateQuery(); err != nil {
  200 + return nil, application.ThrowError(application.ARG_ERROR, err.Error())
  201 + }
  202 + transactionContext, err := factory.CreateTransactionContext(nil)
  203 + if err != nil {
  204 + return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
  205 + }
  206 + if err := transactionContext.StartTransaction(); err != nil {
  207 + return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
  208 + }
  209 + defer func() {
  210 + transactionContext.RollbackTransaction()
  211 + }()
  212 +
  213 + tableRepository, _, _ := factory.FastPgTable(transactionContext, 0)
  214 + count, tables, err := tableRepository.Find(utils.ObjectToMap(searchQuery))
  215 + if err != nil {
  216 + return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
  217 + }
  218 +
  219 + var result = make([]*dto.TableDto, 0)
  220 + for _, table := range tables {
  221 + var item = &dto.TableDto{}
  222 + item.Load(table)
  223 + result = append(result, item)
  224 + }
  225 +
  226 + if err := transactionContext.CommitTransaction(); err != nil {
  227 + return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
  228 + }
  229 + return map[string]interface{}{
  230 + "count": count,
  231 + "tables": result,
  232 + }, nil
  233 +}
  234 +
  235 +// 表拆分
  236 +func (tableService *TableService) SplitDataTable(splitDataTableCommand *command.SplitDataTableCommand) (interface{}, error) {
  237 + if err := splitDataTableCommand.ValidateCommand(); err != nil {
  238 + return nil, application.ThrowError(application.ARG_ERROR, err.Error())
  239 + }
  240 + transactionContext, err := factory.CreateTransactionContext(nil)
  241 + if err != nil {
  242 + return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
  243 + }
  244 + if err := transactionContext.StartTransaction(); err != nil {
  245 + return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
  246 + }
  247 + defer func() {
  248 + transactionContext.RollbackTransaction()
  249 + }()
  250 + if err := transactionContext.CommitTransaction(); err != nil {
  251 + return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
  252 + }
  253 + return nil, nil
  254 +}
  255 +
  256 +// 更新表服务
  257 +func (tableService *TableService) UpdateTable(updateTableCommand *command.UpdateTableCommand) (interface{}, error) {
  258 + if err := updateTableCommand.ValidateCommand(); err != nil {
  259 + return nil, application.ThrowError(application.ARG_ERROR, err.Error())
  260 + }
  261 + transactionContext, err := factory.CreateTransactionContext(nil)
  262 + if err != nil {
  263 + return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
  264 + }
  265 + if err := transactionContext.StartTransaction(); err != nil {
  266 + return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
  267 + }
  268 + defer func() {
  269 + transactionContext.RollbackTransaction()
  270 + }()
  271 + var tableRepository domain.TableRepository
  272 + if value, err := factory.CreateTableRepository(map[string]interface{}{
  273 + "transactionContext": transactionContext,
  274 + }); err != nil {
  275 + return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
  276 + } else {
  277 + tableRepository = value
  278 + }
  279 + table, err := tableRepository.FindOne(map[string]interface{}{"tableId": updateTableCommand.TableId})
  280 + if err != nil {
  281 + return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
  282 + }
  283 + if table == nil {
  284 + return nil, application.ThrowError(application.RES_NO_FIND_ERROR, fmt.Sprintf("%s", string(updateTableCommand.TableId)))
  285 + }
  286 + if err := table.Update(tool_funs.SimpleStructToMap(updateTableCommand)); err != nil {
  287 + return nil, application.ThrowError(application.BUSINESS_ERROR, err.Error())
  288 + }
  289 + if table, err := tableRepository.Save(table); err != nil {
  290 + return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
  291 + } else {
  292 + if err := transactionContext.CommitTransaction(); err != nil {
  293 + return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
  294 + }
  295 + return table, nil
  296 + }
  297 +}
  298 +
  299 +// 更新表服务
  300 +func (tableService *TableService) UpdateTableStruct(ctx *domain.Context, cmd *command.UpdateTableStructCommand) (interface{}, error) {
  301 + if err := cmd.ValidateCommand(); err != nil {
  302 + return nil, application.ThrowError(application.ARG_ERROR, err.Error())
  303 + }
  304 + transactionContext, err := factory.CreateTransactionContext(nil)
  305 + if err != nil {
  306 + return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
  307 + }
  308 + if err := transactionContext.StartTransaction(); err != nil {
  309 + return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
  310 + }
  311 + defer func() {
  312 + transactionContext.RollbackTransaction()
  313 + }()
  314 +
  315 + UpdateTableStructService, _ := factory.CreateUpdateTableStructService(transactionContext)
  316 + if _, err := UpdateTableStructService.UpdateTableStruct(ctx, cmd.TableId, cmd.Fields, cmd.Name); err != nil {
  317 + return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
  318 + }
  319 + if err := transactionContext.CommitTransaction(); err != nil {
  320 + return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
  321 + }
  322 + return struct{}{}, nil
  323 +}
  324 +
  325 +// 更新表服务
  326 +func (tableService *TableService) AddTableStruct(ctx *domain.Context, cmd *command.AddTableStructCommand) (interface{}, error) {
  327 + if err := cmd.ValidateCommand(); err != nil {
  328 + return nil, application.ThrowError(application.ARG_ERROR, err.Error())
  329 + }
  330 + transactionContext, err := factory.CreateTransactionContext(nil)
  331 + if err != nil {
  332 + return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
  333 + }
  334 + if err := transactionContext.StartTransaction(); err != nil {
  335 + return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
  336 + }
  337 + defer func() {
  338 + transactionContext.RollbackTransaction()
  339 + }()
  340 +
  341 + AddTableStructService, _ := factory.CreateAddTableStructService(transactionContext)
  342 + if _, err := AddTableStructService.AddTableStruct(ctx, cmd.TableId, cmd.Fields, cmd.Name); err != nil {
  343 + return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
  344 + }
  345 + if err := transactionContext.CommitTransaction(); err != nil {
  346 + return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
  347 + }
  348 + return struct{}{}, nil
  349 +}
  350 +
  351 +func NewTableService(options map[string]interface{}) *TableService {
  352 + newTableService := &TableService{}
  353 + return newTableService
  354 +}
  1 +package constant
  2 +
  3 +import "fmt"
  4 +
  5 +var STARROCKS_DB_NAME = "character_library_standard"
  6 +var STARROCKS_USER = "root"
  7 +var STARROCKS_PASSWORD = "eagle1010"
  8 +var STARROCKS_HOST = "118.178.239.45"
  9 +var STARROCKS_PORT = "9030"
  10 +var STARROCKS_MYSQL_DATA_SOURCE = ""
  11 +
  12 +func init() {
  13 + STARROCKS_HOST = Configurator.DefaultString("STARROCKS_HOST", STARROCKS_HOST)
  14 + STARROCKS_PORT = Configurator.DefaultString("STARROCKS_PORT", STARROCKS_PORT)
  15 + STARROCKS_DB_NAME = Configurator.DefaultString("STARROCKS_DB_NAME", STARROCKS_DB_NAME)
  16 + STARROCKS_USER = Configurator.DefaultString("STARROCKS_USER", STARROCKS_USER)
  17 + STARROCKS_PASSWORD = Configurator.DefaultString("STARROCKS_PASSWORD", STARROCKS_PASSWORD)
  18 +
  19 + STARROCKS_MYSQL_DATA_SOURCE = fmt.Sprintf("%v:%v@tcp(%v:%v)/%v?charset=utf8&parseTime=True&loc=Local",
  20 + STARROCKS_USER, STARROCKS_PASSWORD, STARROCKS_HOST, STARROCKS_PORT, STARROCKS_DB_NAME)
  21 +}
  1 +package domain
  2 +
  3 +type Context struct {
  4 + // 公司
  5 + CompanyId int `json:"companyId"`
  6 + // 操作人
  7 + OperatorId int `json:"operatorId"`
  8 + // 操作人名称
  9 + OperatorName string `json:"operatorName"`
  10 + // 租户 (个人、企业)
  11 + TenantId int `json:"tenantId"`
  12 +}
  1 +package domain
  2 +
  3 +type DataTable struct {
  4 + Fields []*Field
  5 + Data [][]string
  6 + Total int
  7 +}
1 package domain 1 package domain
2 2
3 type LoadDataTableService interface { 3 type LoadDataTableService interface {
4 - Load(fileId int) (interface{}, error) 4 + Load(ctx *Context, fileId int) (interface{}, error)
5 GetFileId() int 5 GetFileId() int
6 } 6 }
7 7
8 type FlushDataTableService interface { 8 type FlushDataTableService interface {
9 - Flush(fileId int, table *Table) (interface{}, error) 9 + Flush(ctx *Context, fileId int, table *Table) (interface{}, error)
10 } 10 }
11 11
12 type DeleteFileService interface { 12 type DeleteFileService interface {
13 - Delete(files ...*File) error 13 + Delete(ctx *Context, files ...*File) error
14 } 14 }
15 15
16 type GenerateMainTableService interface { 16 type GenerateMainTableService interface {
17 - GenerateTable(fileId int, tableName string) (interface{}, error) 17 + GenerateTable(ctx *Context, fileId int, tableName string) (interface{}, error)
  18 +}
  19 +
  20 +type CopyDataTableService interface {
  21 + CopyTable(ctx *Context, tableId int, tableName string) (interface{}, error)
  22 +}
  23 +
  24 +type DeleteDataTableService interface {
  25 + DeleteTable(ctx *Context, tableId int) (interface{}, error)
  26 + DeleteTables(ctx *Context, tables ...*Table) error
  27 +}
  28 +
  29 +type UpdateTableStructService interface {
  30 + UpdateTableStruct(ctx *Context, tableId int, fields []*Field, name string) (interface{}, error)
  31 +}
  32 +
  33 +type AddTableStructService interface {
  34 + AddTableStruct(ctx *Context, parentTableId int, fields []*Field, name string) (interface{}, error)
18 } 35 }
@@ -75,6 +75,14 @@ var ( @@ -75,6 +75,14 @@ var (
75 Datetime SQLType = "datetime" 75 Datetime SQLType = "datetime"
76 ) 76 )
77 77
  78 +var SQLTypeMap = map[string]string{
  79 + String.ToString(): "文本",
  80 + Int.ToString(): "整数",
  81 + Float.ToString(): "小数",
  82 + Date.ToString(): "日期",
  83 + Datetime.ToString(): "日期时间",
  84 +}
  85 +
78 type FileType string 86 type FileType string
79 87
80 func (t FileType) ToString() string { 88 func (t FileType) ToString() string {
1 package domain 1 package domain
2 2
  3 +import "fmt"
  4 +
3 // Field 字段 5 // Field 字段
4 type Field struct { 6 type Field struct {
5 // 字段Id 7 // 字段Id
@@ -17,3 +19,73 @@ type Field struct { @@ -17,3 +19,73 @@ type Field struct {
17 // 标识 1:主表字段 2:手动添加 19 // 标识 1:主表字段 2:手动添加
18 Flag int `json:"flag"` 20 Flag int `json:"flag"`
19 } 21 }
  22 +
  23 +func (f *Field) Valid() error {
  24 + if _, ok := SQLTypeMap[f.SQLType]; !ok {
  25 + return fmt.Errorf("unknown sql type:%v", f.SQLType)
  26 + }
  27 + if f.Index == 0 && f.Flag == ManualField {
  28 + return fmt.Errorf("field:%v index is 0", f.Name)
  29 + }
  30 + return nil
  31 +}
  32 +
  33 +type Fields []*Field
  34 +
  35 +func (fields Fields) ToMap() map[string]*Field {
  36 + m := make(map[string]*Field)
  37 + for i := range fields {
  38 + m[fields[i].Name] = fields[i]
  39 + }
  40 + return m
  41 +}
  42 +
  43 +func (fields Fields) Select(options map[string]interface{}) []*Field {
  44 + var result []*Field
  45 + for _, field := range fields {
  46 + if v, ok := options["flag"]; ok {
  47 + if v.(int) != field.Flag {
  48 + continue
  49 + }
  50 + }
  51 + result = append(result, field)
  52 + }
  53 + return result
  54 +}
  55 +
  56 +func ValidFields(fields []*Field) error {
  57 + m := (Fields)(fields).ToMap()
  58 + if len(m) != len(fields) {
  59 + return fmt.Errorf("列名重复发")
  60 + }
  61 +
  62 + for _, f := range fields {
  63 + if err := f.Valid(); err != nil {
  64 + return err
  65 + }
  66 + }
  67 + return nil
  68 +}
  69 +
  70 +func FieldsChange(oldFields []*Field, newFields []*Field) (reserve []*Field, delete []*Field, add []*Field) {
  71 + var oldFieldsMap = (Fields)(oldFields).ToMap()
  72 + var newFieldsMap = (Fields)(newFields).ToMap()
  73 +
  74 + for _, f := range newFields {
  75 + if _, ok := oldFieldsMap[f.Name]; ok {
  76 + reserve = append(reserve, f)
  77 + continue
  78 + } else {
  79 + add = append(add, f)
  80 + continue
  81 + }
  82 + }
  83 +
  84 + for _, f := range oldFields {
  85 + if _, ok := newFieldsMap[f.Name]; !ok {
  86 + delete = append(delete, f)
  87 + continue
  88 + }
  89 + }
  90 + return
  91 +}
@@ -17,7 +17,7 @@ type File struct { @@ -17,7 +17,7 @@ type File struct {
17 // 源文件Id(FileType为TemporaryFile或VerifiedFile时有值) 17 // 源文件Id(FileType为TemporaryFile或VerifiedFile时有值)
18 SourceFileId int `json:"sourceFileId"` 18 SourceFileId int `json:"sourceFileId"`
19 // 操作人 19 // 操作人
20 - Operator string `json:"operator"` 20 + // Operator string `json:"operator"`
21 // 创建时间 21 // 创建时间
22 CreatedAt time.Time `json:"createdAt"` 22 CreatedAt time.Time `json:"createdAt"`
23 // 更新时间 23 // 更新时间
@@ -26,6 +26,8 @@ type File struct { @@ -26,6 +26,8 @@ type File struct {
26 DeletedAt time.Time `json:"deletedAt"` 26 DeletedAt time.Time `json:"deletedAt"`
27 // 版本 27 // 版本
28 Version int `json:"version"` 28 Version int `json:"version"`
  29 + // 扩展
  30 + Context *Context `json:"context"`
29 } 31 }
30 32
31 type FileRepository interface { 33 type FileRepository interface {
@@ -51,7 +53,7 @@ func FileName(fileName string) string { @@ -51,7 +53,7 @@ func FileName(fileName string) string {
51 return strings.Split(base, ".")[0] 53 return strings.Split(base, ".")[0]
52 } 54 }
53 55
54 -func (file *File) CopyTo(fileType FileType) *File { 56 +func (file *File) CopyTo(fileType FileType, ctx *Context) *File {
55 t := &File{ 57 t := &File{
56 FileType: fileType.ToString(), 58 FileType: fileType.ToString(),
57 FileInfo: &FileInfo{ 59 FileInfo: &FileInfo{
@@ -64,7 +66,8 @@ func (file *File) CopyTo(fileType FileType) *File { @@ -64,7 +66,8 @@ func (file *File) CopyTo(fileType FileType) *File {
64 CreatedAt: time.Now(), 66 CreatedAt: time.Now(),
65 UpdatedAt: time.Now(), 67 UpdatedAt: time.Now(),
66 SourceFileId: file.FileId, 68 SourceFileId: file.FileId,
67 - Operator: file.Operator, 69 + //Operator: file.Operator,
  70 + Context: ctx,
68 } 71 }
69 return t 72 return t
70 } 73 }
@@ -24,6 +24,8 @@ type Log struct { @@ -24,6 +24,8 @@ type Log struct {
24 OperatorName string `json:"operatorName"` 24 OperatorName string `json:"operatorName"`
25 // 创建时间 25 // 创建时间
26 CreatedAt time.Time `json:"createdAt"` 26 CreatedAt time.Time `json:"createdAt"`
  27 + // 扩展
  28 + Context *Context `json:"context"`
27 } 29 }
28 30
29 type LogRepository interface { 31 type LogRepository interface {
@@ -12,17 +12,24 @@ type LogEntry struct { @@ -12,17 +12,24 @@ type LogEntry struct {
12 Content string `json:"content"` 12 Content string `json:"content"`
13 // 操作人名称 13 // 操作人名称
14 OperatorName string `json:"operatorName"` 14 OperatorName string `json:"operatorName"`
  15 +
  16 + ctx *Context `json:"-"`
15 } 17 }
16 18
17 func (l LogEntry) Entry() LogEntry { 19 func (l LogEntry) Entry() LogEntry {
18 return l 20 return l
19 } 21 }
20 22
21 -func NewLogEntry(fileOrTableName string, objectType string, operationType OperationType, operatorName string) LogEntry { 23 +func (l LogEntry) Context() *Context {
  24 + return l.ctx
  25 +}
  26 +
  27 +func NewLogEntry(fileOrTableName string, objectType string, operationType OperationType, ctx *Context) LogEntry {
22 return LogEntry{ 28 return LogEntry{
23 ObjectName: fileOrTableName, 29 ObjectName: fileOrTableName,
24 ObjectType: objectType, 30 ObjectType: objectType,
25 OperationType: operationType.ToString(), 31 OperationType: operationType.ToString(),
26 - OperatorName: operatorName, 32 + OperatorName: ctx.OperatorName,
  33 + ctx: ctx,
27 } 34 }
28 } 35 }
  1 +package domain
  2 +
  3 +import "time"
  4 +
  5 +// 匹配规则配置
  6 +type MappingRule struct {
  7 + // 匹配规则ID
  8 + MappingRuleId int `json:"mappingRuleId"`
  9 + // 名称
  10 + Name string `json:"name"`
  11 + // 表Id
  12 + TableId int `json:"tableId"`
  13 + // 文件ID
  14 + FileId int `json:"fileId"`
  15 + // 主表列
  16 + MainTableFields []*Field `json:"mainTableFields"`
  17 + // 校验文件列
  18 + VerifiedFileFields []*Field `json:"verifiedFileFields"`
  19 + // 校验文件列
  20 + MappingFields []*MappingField `json:"mappingFields"`
  21 + // 创建时间
  22 + CreatedAt time.Time `json:"createdAt"`
  23 + // 更新时间
  24 + UpdatedAt time.Time `json:"updatedAt"`
  25 + // 删除时间
  26 + DeletedAt time.Time `json:"deletedAt"`
  27 + // 扩展
  28 + Context *Context `json:"context"`
  29 +}
  30 +
  31 +type MappingRuleRepository interface {
  32 + Save(mappingRule *MappingRule) (*MappingRule, error)
  33 + Remove(mappingRule *MappingRule) (*MappingRule, error)
  34 + FindOne(queryOptions map[string]interface{}) (*MappingRule, error)
  35 + Find(queryOptions map[string]interface{}) (int64, []*MappingRule, error)
  36 +}
  37 +
  38 +func (mappingRule *MappingRule) Identify() interface{} {
  39 + if mappingRule.MappingRuleId == 0 {
  40 + return nil
  41 + }
  42 + return mappingRule.MappingRuleId
  43 +}
  44 +
  45 +func (mappingRule *MappingRule) Update(data map[string]interface{}) error {
  46 + return nil
  47 +}
  48 +
  49 +type MappingField struct {
  50 + MainTableField *Field `json:"mainTableField"`
  51 + VerifiedFileFieldName string `json:"verifiedFileFieldName"`
  52 +}
  53 +
  54 +func NewMappingFields(all []*Field, some []*Field) []*MappingField {
  55 + var result []*MappingField
  56 + //allMap :=(Fields)(all).ToMap()
  57 + someMap := (Fields)(some).ToMap()
  58 + for _, f := range all {
  59 + item := &MappingField{
  60 + MainTableField: f,
  61 + }
  62 + if v, ok := someMap[f.Name]; ok {
  63 + item.VerifiedFileFieldName = v.Name
  64 + }
  65 + result = append(result, item)
  66 + }
  67 + return result
  68 +}
@@ -31,8 +31,11 @@ type Table struct { @@ -31,8 +31,11 @@ type Table struct {
31 // 版本 31 // 版本
32 Version int `json:"version"` 32 Version int `json:"version"`
33 33
  34 + fields []*Field // 所有列 // PKs + DataFields + ManualFields
34 // 业务字段 35 // 业务字段
35 RowCount int `json:"rowCount,omitempty"` 36 RowCount int `json:"rowCount,omitempty"`
  37 + // 扩展
  38 + Context *Context `json:"context"`
36 } 39 }
37 40
38 type TableRepository interface { 41 type TableRepository interface {
@@ -49,6 +52,26 @@ func (table *Table) Identify() interface{} { @@ -49,6 +52,26 @@ func (table *Table) Identify() interface{} {
49 return table.TableId 52 return table.TableId
50 } 53 }
51 54
  55 +func (table *Table) WithContext(ctx *Context) *Table {
  56 + table.Context = ctx
  57 + return table
  58 +}
  59 +
52 func (table *Table) Update(data map[string]interface{}) error { 60 func (table *Table) Update(data map[string]interface{}) error {
53 return nil 61 return nil
54 } 62 }
  63 +
  64 +func (t *Table) Fields(includePK bool) []*Field {
  65 + var fields []*Field
  66 + if len(t.fields) == 0 {
  67 + if includePK {
  68 + fields = append(fields, t.PK)
  69 + }
  70 + fields = append(fields, t.DataFields...)
  71 + if t.TableType == SubTable.ToString() {
  72 + fields = append(fields, t.ManualFields...)
  73 + }
  74 + t.fields = fields
  75 + }
  76 + return t.fields
  77 +}
  1 +package domain
  2 +
  3 +// 用户对象
  4 +type User struct {
  5 + // 用户Id 用户唯一标识
  6 + Id int `json:"id"`
  7 + // 用户姓名
  8 + Name string `json:"name"`
  9 +}
@@ -11,8 +11,14 @@ func ChangeStepLogOwner(ptr *pgTransaction.TransactionContext, from int, to int) @@ -11,8 +11,14 @@ func ChangeStepLogOwner(ptr *pgTransaction.TransactionContext, from int, to int)
11 return err 11 return err
12 } 12 }
13 13
14 -func LogDelete(ptr *pgTransaction.TransactionContext, sourceId int, logType domain.LogType) error { 14 +func LogDirectDelete(ptr *pgTransaction.TransactionContext, sourceId int, logType domain.LogType) error {
15 sql := "delete from metadata.logs where source_id = ? and log_type = ?" 15 sql := "delete from metadata.logs where source_id = ? and log_type = ?"
16 _, err := ptr.PgTx.Exec(sql, sourceId, logType) 16 _, err := ptr.PgTx.Exec(sql, sourceId, logType)
17 return err 17 return err
18 } 18 }
  19 +
  20 +//func LogSoftDelete(ptr *pgTransaction.TransactionContext, sourceIds []int, logType domain.LogType) error {
  21 +// sql := "update metadata.logs set deleted_at = now() where source_id in(?) and log_type = ?"
  22 +// _, err := ptr.PgTx.Exec(sql, pg.In(sourceIds), logType)
  23 +// return err
  24 +//}
  1 +package domainService
  2 +
  3 +import (
  4 + "fmt"
  5 + pgTransaction "github.com/linmadan/egglib-go/transaction/pg"
  6 + "gitlab.fjmaimaimai.com/allied-creation/character-library-metadata-bastion/pkg/domain"
  7 + "gitlab.fjmaimaimai.com/allied-creation/character-library-metadata-bastion/pkg/infrastructure/repository"
  8 +)
  9 +
  10 +type AddTableStructService struct {
  11 + transactionContext *pgTransaction.TransactionContext
  12 +}
  13 +
  14 +func NewAddTableStructService(transactionContext *pgTransaction.TransactionContext) (*AddTableStructService, error) {
  15 + if transactionContext == nil {
  16 + return nil, fmt.Errorf("transactionContext参数不能为nil")
  17 + } else {
  18 + return &AddTableStructService{
  19 + transactionContext: transactionContext,
  20 + }, nil
  21 + }
  22 +}
  23 +
  24 +func (ptr *AddTableStructService) AddTableStruct(ctx *domain.Context, parentTableId int, fields []*domain.Field, name string) (interface{}, error) {
  25 + tableRepository, _ := repository.NewTableRepository(ptr.transactionContext)
  26 + mainTable, err := tableRepository.FindOne(map[string]interface{}{"tableId": parentTableId})
  27 + if err != nil {
  28 + return nil, err
  29 + }
  30 + if !(mainTable.TableType == domain.MainTable.ToString() || mainTable.TableType == domain.SideTable.ToString()) {
  31 + return nil, fmt.Errorf("只有主表、副表允许新建分表")
  32 + }
  33 +
  34 + // 验证表名是否重复
  35 + duplicateTable, err := tableRepository.FindOne(map[string]interface{}{"tableType": domain.SubTable.ToString(), "tableName": name})
  36 + if err == nil && duplicateTable != nil {
  37 + return nil, fmt.Errorf("表名称重复")
  38 + }
  39 +
  40 + fields = MappingFields(mainTable, fields)
  41 + dataFields := (domain.Fields)(fields).Select(map[string]interface{}{"flag": 1})
  42 + manualFields := (domain.Fields)(fields).Select(map[string]interface{}{"flag": 2})
  43 + table := NewTable(domain.SubTable, name, fields, mainTable.RowCount).WithContext(ctx)
  44 + table.DataFieldIndex = mainTable.DataFieldIndex
  45 + table.DataFields = dataFields
  46 + table.ManualFields = manualFields
  47 + table.ParentId = parentTableId
  48 + reserves, deletes, adds := domain.FieldsChange(table.Fields(false), fields)
  49 +
  50 + if err = domain.ValidFields(fields); err != nil {
  51 + return nil, err
  52 + }
  53 + if _, err = tableRepository.Save(table); err != nil {
  54 + return nil, err
  55 + }
  56 + if _, err = tableRepository.Save(mainTable); err != nil {
  57 + return nil, err
  58 + }
  59 +
  60 + // Log
  61 + // 日志
  62 + if err = FastLog(ptr.transactionContext, domain.CommonLog, table.TableId, &SpiltMainTableLog{
  63 + LogEntry: domain.NewLogEntry(table.Name, table.TableType, domain.SpiltMainTable, ctx),
  64 + Reserve: reserves,
  65 + Delete: deletes,
  66 + Add: adds,
  67 + SourceTableName: mainTable.Name,
  68 + }); err != nil {
  69 + return nil, err
  70 + }
  71 +
  72 + // 通知底层
  73 + return struct{}{}, nil
  74 +}
  1 +package domainService
  2 +
  3 +import (
  4 + "fmt"
  5 + pgTransaction "github.com/linmadan/egglib-go/transaction/pg"
  6 + "gitlab.fjmaimaimai.com/allied-creation/character-library-metadata-bastion/pkg/domain"
  7 + "gitlab.fjmaimaimai.com/allied-creation/character-library-metadata-bastion/pkg/infrastructure/repository"
  8 +)
  9 +
  10 +type CopyDataTableService struct {
  11 + transactionContext *pgTransaction.TransactionContext
  12 +}
  13 +
  14 +func NewCopyDataTableService(transactionContext *pgTransaction.TransactionContext) (*CopyDataTableService, error) {
  15 + if transactionContext == nil {
  16 + return nil, fmt.Errorf("transactionContext参数不能为nil")
  17 + } else {
  18 + return &CopyDataTableService{
  19 + transactionContext: transactionContext,
  20 + }, nil
  21 + }
  22 +}
  23 +
  24 +func (ptr *CopyDataTableService) CopyTable(ctx *domain.Context, tableId int, tableName string) (interface{}, error) {
  25 + tableRepository, _ := repository.NewTableRepository(ptr.transactionContext)
  26 + table, err := tableRepository.FindOne(map[string]interface{}{"tableId": tableId})
  27 + if err != nil {
  28 + return nil, err
  29 + }
  30 + // 验证表名是否重复
  31 + duplicateTable, err := tableRepository.FindOne(map[string]interface{}{"tableType": domain.SideTable.ToString(), "tableName": tableName})
  32 + if err == nil && duplicateTable != nil {
  33 + return nil, fmt.Errorf("表名称重复")
  34 + }
  35 +
  36 + sideTable := NewTable(domain.SideTable, tableName, table.DataFields, table.RowCount).WithContext(ctx)
  37 + sideTable.ParentId = table.TableId
  38 + if sideTable, err = tableRepository.Save(sideTable); err != nil {
  39 + return nil, err
  40 + }
  41 + // 日志
  42 + if err = FastLog(ptr.transactionContext, domain.CommonLog, sideTable.TableId, &CopyTableLog{
  43 + LogEntry: domain.NewLogEntry(sideTable.Name, sideTable.TableType, domain.CopyTable, ctx),
  44 + SourceTableName: table.Name,
  45 + }); err != nil {
  46 + return nil, err
  47 + }
  48 +
  49 + // 通知底层
  50 + return nil, nil
  51 +}
  1 +package domainService
  2 +
  3 +import (
  4 + "fmt"
  5 + pgTransaction "github.com/linmadan/egglib-go/transaction/pg"
  6 + "gitlab.fjmaimaimai.com/allied-creation/character-library-metadata-bastion/pkg/domain"
  7 + "gitlab.fjmaimaimai.com/allied-creation/character-library-metadata-bastion/pkg/infrastructure/repository"
  8 +)
  9 +
  10 +type DeleteDataTableService struct {
  11 + transactionContext *pgTransaction.TransactionContext
  12 +}
  13 +
  14 +func NewDeleteDataTableService(transactionContext *pgTransaction.TransactionContext) (*DeleteDataTableService, error) {
  15 + if transactionContext == nil {
  16 + return nil, fmt.Errorf("transactionContext参数不能为nil")
  17 + } else {
  18 + return &DeleteDataTableService{
  19 + transactionContext: transactionContext,
  20 + }, nil
  21 + }
  22 +}
  23 +
  24 +func (ptr *DeleteDataTableService) DeleteTable(ctx *domain.Context, tableId int) (interface{}, error) {
  25 + tableRepository, _ := repository.NewTableRepository(ptr.transactionContext)
  26 + table, err := tableRepository.FindOne(map[string]interface{}{"tableId": tableId})
  27 + if err != nil {
  28 + return nil, err
  29 + }
  30 +
  31 + var subTables []*domain.Table
  32 + if table.TableType == domain.MainTable.ToString() {
  33 + _, subTables, err = tableRepository.Find(map[string]interface{}{"parentId": tableId, "tableTypes": []string{domain.SubTable.ToString()}})
  34 + if err != nil {
  35 + return nil, err
  36 + }
  37 + }
  38 +
  39 + // 日志
  40 + if err = FastLog(ptr.transactionContext, domain.CommonLog, table.TableId, &DeleteTableLog{
  41 + LogEntry: domain.NewLogEntry(table.Name, table.TableType, domain.DeleteTable, ctx),
  42 + SourceTableName: table.Name,
  43 + RowCount: table.RowCount,
  44 + SubTables: subTables,
  45 + }); err != nil {
  46 + return nil, err
  47 + }
  48 +
  49 + if err := ptr.DeleteTables(ctx, table); err != nil {
  50 + return nil, err
  51 + }
  52 + if err := ptr.DeleteTables(ctx, subTables...); err != nil {
  53 + return nil, err
  54 + }
  55 + // 通知底层
  56 +
  57 + return nil, nil
  58 +}
  59 +
  60 +func (ptr *DeleteDataTableService) DeleteTables(ctx *domain.Context, tables ...*domain.Table) error {
  61 + // delete table
  62 + if len(tables) == 0 {
  63 + return nil
  64 + }
  65 + tableRepository, _ := repository.NewTableRepository(ptr.transactionContext)
  66 + var tableIds []int
  67 + for _, t := range tables {
  68 + if _, err := tableRepository.Remove(t); err != nil {
  69 + return err
  70 + }
  71 + tableIds = append(tableIds, t.TableId)
  72 + }
  73 + // delete log
  74 + //if err := dao.LogSoftDelete(ptr.transactionContext, tableIds, domain.CommonLog); err != nil {
  75 + // return err
  76 + //}
  77 + return nil
  78 +}
@@ -21,7 +21,7 @@ func NewDeleteFileService(transactionContext *pgTransaction.TransactionContext) @@ -21,7 +21,7 @@ func NewDeleteFileService(transactionContext *pgTransaction.TransactionContext)
21 } 21 }
22 } 22 }
23 23
24 -func (ptr *DeleteFileService) Delete(files ...*domain.File) error { 24 +func (ptr *DeleteFileService) Delete(ctx *domain.Context, files ...*domain.File) error {
25 for _, file := range files { 25 for _, file := range files {
26 if err := ptr.delete(file); err != nil { 26 if err := ptr.delete(file); err != nil {
27 return err 27 return err
@@ -42,7 +42,7 @@ func (ptr *DeleteFileService) delete(file *domain.File) error { @@ -42,7 +42,7 @@ func (ptr *DeleteFileService) delete(file *domain.File) error {
42 } 42 }
43 } 43 }
44 // delete log 44 // delete log
45 - if err := dao.LogDelete(ptr.transactionContext, file.FileId, domain.VerifiedStepLog); err != nil { 45 + if err := dao.LogDirectDelete(ptr.transactionContext, file.FileId, domain.VerifiedStepLog); err != nil {
46 return err 46 return err
47 } 47 }
48 return nil 48 return nil
@@ -15,7 +15,7 @@ type FlushDataTableService struct { @@ -15,7 +15,7 @@ type FlushDataTableService struct {
15 transactionContext *pgTransaction.TransactionContext 15 transactionContext *pgTransaction.TransactionContext
16 } 16 }
17 17
18 -func (ptr *FlushDataTableService) Flush(fileId int, table *domain.Table) (interface{}, error) { 18 +func (ptr *FlushDataTableService) Flush(ctx *domain.Context, fileId int, table *domain.Table) (interface{}, error) {
19 fileRepository, _ := repository.NewFileRepository(ptr.transactionContext) 19 fileRepository, _ := repository.NewFileRepository(ptr.transactionContext)
20 file, err := fileRepository.FindOne(map[string]interface{}{"fileId": fileId}) 20 file, err := fileRepository.FindOne(map[string]interface{}{"fileId": fileId})
21 if err != nil { 21 if err != nil {
@@ -26,23 +26,22 @@ func (ptr *FlushDataTableService) Flush(fileId int, table *domain.Table) (interf @@ -26,23 +26,22 @@ func (ptr *FlushDataTableService) Flush(fileId int, table *domain.Table) (interf
26 return nil, fmt.Errorf("源文件不存在") 26 return nil, fmt.Errorf("源文件不存在")
27 } 27 }
28 // New Table 28 // New Table
29 - table = NewTable(domain.ExcelTable, file.FileInfo.Name, table.DataFields, table.RowCount)  
30 - 29 + table = NewTable(domain.ExcelTable, file.FileInfo.Name, table.DataFields, table.RowCount).WithContext(ctx)
31 // 来自源文件的 30 // 来自源文件的
32 // 临时文件 -》校验文件 31 // 临时文件 -》校验文件
33 switch sourceFile.FileType { 32 switch sourceFile.FileType {
34 case domain.SourceFile.ToString(): 33 case domain.SourceFile.ToString():
35 - if err = ptr.flushSourceFile(table, file, sourceFile, fileRepository); err != nil { 34 + if err = ptr.flushSourceFile(ctx, table, file, sourceFile, fileRepository); err != nil {
36 return nil, err 35 return nil, err
37 } 36 }
38 case domain.VerifiedFile.ToString(): 37 case domain.VerifiedFile.ToString():
39 - if err = ptr.flushVerifiedFile(table, file, sourceFile, fileRepository); err != nil { 38 + if err = ptr.flushVerifiedFile(ctx, table, file, sourceFile, fileRepository); err != nil {
40 return nil, err 39 return nil, err
41 } 40 }
42 } 41 }
43 // 日志 42 // 日志
44 if err = FastLog(ptr.transactionContext, domain.CommonLog, file.FileId, &FileVerifyLog{ 43 if err = FastLog(ptr.transactionContext, domain.CommonLog, file.FileId, &FileVerifyLog{
45 - LogEntry: domain.NewLogEntry(file.FileInfo.Name, domain.VerifiedFile.ToString(), domain.FileVerify, ""), 44 + LogEntry: domain.NewLogEntry(file.FileInfo.Name, domain.VerifiedFile.ToString(), domain.FileVerify, ctx),
46 Total: table.RowCount, 45 Total: table.RowCount,
47 }); err != nil { 46 }); err != nil {
48 return nil, err 47 return nil, err
@@ -52,7 +51,7 @@ func (ptr *FlushDataTableService) Flush(fileId int, table *domain.Table) (interf @@ -52,7 +51,7 @@ func (ptr *FlushDataTableService) Flush(fileId int, table *domain.Table) (interf
52 return struct{}{}, nil 51 return struct{}{}, nil
53 } 52 }
54 53
55 -func (ptr *FlushDataTableService) flushSourceFile(table *domain.Table, file *domain.File, sourceFile *domain.File, fileRepository domain.FileRepository) error { 54 +func (ptr *FlushDataTableService) flushSourceFile(ctx *domain.Context, table *domain.Table, file *domain.File, sourceFile *domain.File, fileRepository domain.FileRepository) error {
56 var err error 55 var err error
57 // 新增 56 // 新增
58 tableRepository, _ := repository.NewTableRepository(ptr.transactionContext) 57 tableRepository, _ := repository.NewTableRepository(ptr.transactionContext)
@@ -65,22 +64,18 @@ func (ptr *FlushDataTableService) flushSourceFile(table *domain.Table, file *dom @@ -65,22 +64,18 @@ func (ptr *FlushDataTableService) flushSourceFile(table *domain.Table, file *dom
65 if file, err = fileRepository.Save(file); err != nil { 64 if file, err = fileRepository.Save(file); err != nil {
66 return err 65 return err
67 } 66 }
68 - // 删除跟源文件有关系的校验文件  
69 - //if err = dao.FileDeleteBySourceFileId(ptr.transactionContext, sourceFile.FileId, domain.VerifiedFile, file.FileId); err != nil {  
70 - // return err  
71 - //}  
72 - _, files, err := fileRepository.Find(map[string]interface{}{"sourceFileId": sourceFile.FileId, "fileType": domain.VerifiedFile.ToString(), "notInFileIds": []int{file.FileId}}) 67 + _, files, err := fileRepository.Find(map[string]interface{}{"context": ctx, "sourceFileId": sourceFile.FileId, "fileType": domain.VerifiedFile.ToString(), "notInFileIds": []int{file.FileId}})
73 if err != nil { 68 if err != nil {
74 return err 69 return err
75 } 70 }
76 deleteFileService, _ := NewDeleteFileService(ptr.transactionContext) 71 deleteFileService, _ := NewDeleteFileService(ptr.transactionContext)
77 - if err = deleteFileService.Delete(files...); err != nil { 72 + if err = deleteFileService.Delete(ctx, files...); err != nil {
78 return err 73 return err
79 } 74 }
80 return nil 75 return nil
81 } 76 }
82 77
83 -func (ptr *FlushDataTableService) flushVerifiedFile(table *domain.Table, file *domain.File, sourceFile *domain.File, fileRepository domain.FileRepository) error { 78 +func (ptr *FlushDataTableService) flushVerifiedFile(ctx *domain.Context, table *domain.Table, file *domain.File, sourceFile *domain.File, fileRepository domain.FileRepository) error {
84 var err error 79 var err error
85 temporaryFileTableId := table.TableId 80 temporaryFileTableId := table.TableId
86 // 校验文件对应的表更新 81 // 校验文件对应的表更新
@@ -128,7 +123,7 @@ func NewTable(tableType domain.TableType, fileName string, dataFields []*domain. @@ -128,7 +123,7 @@ func NewTable(tableType domain.TableType, fileName string, dataFields []*domain.
128 table.PK = PK() 123 table.PK = PK()
129 table.DataFieldIndex = len(dataFields) 124 table.DataFieldIndex = len(dataFields)
130 for i, field := range dataFields { 125 for i, field := range dataFields {
131 - table.DataFields = append(table.DataFields, DataField(field.Name, field.SQLType, domain.MainTableField, i)) 126 + table.DataFields = append(table.DataFields, DataField(field.Name, field.SQLType, domain.MainTableField, i+1))
132 } 127 }
133 table.ManualFields = make([]*domain.Field, 0) 128 table.ManualFields = make([]*domain.Field, 0)
134 table.CreatedAt = time.Now() 129 table.CreatedAt = time.Now()
@@ -157,7 +152,7 @@ func DataField(name string, sqlType string, flag int, index int) *domain.Field { @@ -157,7 +152,7 @@ func DataField(name string, sqlType string, flag int, index int) *domain.Field {
157 return &domain.Field{ 152 return &domain.Field{
158 Index: index, 153 Index: index,
159 Name: name, 154 Name: name,
160 - SQLName: fieldName(index + 1), 155 + SQLName: fieldName(index),
161 SQLType: sqlType, 156 SQLType: sqlType,
162 Description: "", 157 Description: "",
163 Flag: flag, 158 Flag: flag,
@@ -165,5 +160,5 @@ func DataField(name string, sqlType string, flag int, index int) *domain.Field { @@ -165,5 +160,5 @@ func DataField(name string, sqlType string, flag int, index int) *domain.Field {
165 } 160 }
166 161
167 func fieldName(index int) string { 162 func fieldName(index int) string {
168 - return fmt.Sprintf("col%02d", index) 163 + return fmt.Sprintf("col%d", index)
169 } 164 }
@@ -11,7 +11,7 @@ type GenerateMainTableService struct { @@ -11,7 +11,7 @@ type GenerateMainTableService struct {
11 transactionContext *pgTransaction.TransactionContext 11 transactionContext *pgTransaction.TransactionContext
12 } 12 }
13 13
14 -func (ptr *GenerateMainTableService) GenerateTable(fileId int, tableName string) (interface{}, error) { 14 +func (ptr *GenerateMainTableService) GenerateTable(ctx *domain.Context, fileId int, tableName string) (interface{}, error) {
15 fileRepository, _ := repository.NewFileRepository(ptr.transactionContext) 15 fileRepository, _ := repository.NewFileRepository(ptr.transactionContext)
16 file, err := fileRepository.FindOne(map[string]interface{}{"fileId": fileId}) 16 file, err := fileRepository.FindOne(map[string]interface{}{"fileId": fileId})
17 if err != nil { 17 if err != nil {
@@ -23,12 +23,24 @@ func (ptr *GenerateMainTableService) GenerateTable(fileId int, tableName string) @@ -23,12 +23,24 @@ func (ptr *GenerateMainTableService) GenerateTable(fileId int, tableName string)
23 if err != nil { 23 if err != nil {
24 return nil, fmt.Errorf("文件未校验") 24 return nil, fmt.Errorf("文件未校验")
25 } 25 }
  26 + duplicateTable, err := tableRepository.FindOne(map[string]interface{}{"context": ctx, "tableType": domain.MainTable.ToString(), "tableName": tableName})
  27 + if err == nil && duplicateTable != nil {
  28 + return nil, fmt.Errorf("表名称重复")
  29 + }
26 30
27 - mainTable := NewTable(domain.MainTable, tableName, table.DataFields, table.RowCount) 31 + mainTable := NewTable(domain.MainTable, tableName, table.DataFields, table.RowCount).WithContext(ctx)
28 _, err = tableRepository.Save(mainTable) 32 _, err = tableRepository.Save(mainTable)
29 if err != nil { 33 if err != nil {
30 return nil, err 34 return nil, err
31 } 35 }
  36 +
  37 + // 日志
  38 + if err = FastLog(ptr.transactionContext, domain.CommonLog, mainTable.TableId, &GenerateMainTableLog{
  39 + LogEntry: domain.NewLogEntry(tableName, domain.MainTable.ToString(), domain.GenerateMainTable, ctx),
  40 + FileName: file.FileInfo.Name,
  41 + }); err != nil {
  42 + return nil, err
  43 + }
32 return struct{}{}, nil 44 return struct{}{}, nil
33 } 45 }
34 46
@@ -12,7 +12,7 @@ type LoadDataTableService struct { @@ -12,7 +12,7 @@ type LoadDataTableService struct {
12 transactionContext *pgTransaction.TransactionContext 12 transactionContext *pgTransaction.TransactionContext
13 } 13 }
14 14
15 -func (ptr *LoadDataTableService) Load(fileId int) (interface{}, error) { 15 +func (ptr *LoadDataTableService) Load(ctx *domain.Context, fileId int) (interface{}, error) {
16 fileRepository, _ := repository.NewFileRepository(ptr.transactionContext) 16 fileRepository, _ := repository.NewFileRepository(ptr.transactionContext)
17 file, err := fileRepository.FindOne(map[string]interface{}{"fileId": fileId}) 17 file, err := fileRepository.FindOne(map[string]interface{}{"fileId": fileId})
18 if err != nil { 18 if err != nil {
@@ -21,7 +21,7 @@ func (ptr *LoadDataTableService) Load(fileId int) (interface{}, error) { @@ -21,7 +21,7 @@ func (ptr *LoadDataTableService) Load(fileId int) (interface{}, error) {
21 21
22 // Copy to TemporaryFile 22 // Copy to TemporaryFile
23 if file.FileType != domain.TemporaryFile.ToString() { 23 if file.FileType != domain.TemporaryFile.ToString() {
24 - file = file.CopyTo(domain.TemporaryFile) 24 + file = file.CopyTo(domain.TemporaryFile, ctx)
25 if file, err = fileRepository.Save(file); err != nil { 25 if file, err = fileRepository.Save(file); err != nil {
26 return nil, err 26 return nil, err
27 } 27 }
@@ -5,6 +5,7 @@ import ( @@ -5,6 +5,7 @@ import (
5 pgTransaction "github.com/linmadan/egglib-go/transaction/pg" 5 pgTransaction "github.com/linmadan/egglib-go/transaction/pg"
6 "gitlab.fjmaimaimai.com/allied-creation/character-library-metadata-bastion/pkg/domain" 6 "gitlab.fjmaimaimai.com/allied-creation/character-library-metadata-bastion/pkg/domain"
7 "gitlab.fjmaimaimai.com/allied-creation/character-library-metadata-bastion/pkg/infrastructure/repository" 7 "gitlab.fjmaimaimai.com/allied-creation/character-library-metadata-bastion/pkg/infrastructure/repository"
  8 + "strings"
8 "time" 9 "time"
9 ) 10 )
10 11
@@ -40,6 +41,7 @@ func (ptr *PGLogService) Log(logType domain.LogType, sourceId int, logEntry Log) @@ -40,6 +41,7 @@ func (ptr *PGLogService) Log(logType domain.LogType, sourceId int, logEntry Log)
40 Content: logEntry.Content(), 41 Content: logEntry.Content(),
41 OperatorName: entry.OperatorName, 42 OperatorName: entry.OperatorName,
42 CreatedAt: time.Now(), 43 CreatedAt: time.Now(),
  44 + Context: logEntry.Context(),
43 } 45 }
44 _, err := logRepository.Save(log) 46 _, err := logRepository.Save(log)
45 return err 47 return err
@@ -52,6 +54,7 @@ func (ptr *PGLogService) NewLogEntry() domain.LogEntry { @@ -52,6 +54,7 @@ func (ptr *PGLogService) NewLogEntry() domain.LogEntry {
52 type Log interface { 54 type Log interface {
53 Content() string 55 Content() string
54 Entry() domain.LogEntry 56 Entry() domain.LogEntry
  57 + Context() *domain.Context
55 } 58 }
56 59
57 var _ Log = (*FileUploadSuccessLog)(nil) 60 var _ Log = (*FileUploadSuccessLog)(nil)
@@ -91,3 +94,116 @@ func (l *FileVerifyLog) Content() string { @@ -91,3 +94,116 @@ func (l *FileVerifyLog) Content() string {
91 } 94 }
92 return msg 95 return msg
93 } 96 }
  97 +
  98 +// 3.主表生成日志
  99 +type GenerateMainTableLog struct {
  100 + domain.LogEntry
  101 + // 表名
  102 + TableName string
  103 + // 文件名
  104 + FileName string
  105 +}
  106 +
  107 +func (l *GenerateMainTableLog) Content() string {
  108 + msg := fmt.Sprintf("来源校验文件:%v", l.FileName)
  109 + return msg
  110 +}
  111 +
  112 +// 4.主表拆分
  113 +type SpiltMainTableLog struct {
  114 + domain.LogEntry
  115 + Reserve []*domain.Field
  116 + Delete []*domain.Field
  117 + Add []*domain.Field
  118 + // 表名
  119 + SourceTableName string
  120 +}
  121 +
  122 +func (l *SpiltMainTableLog) Content() string {
  123 + var msg string
  124 + msg += fmt.Sprintf("来源表:%v", l.SourceTableName)
  125 + msg += l.makeMsg(" 删除字段", l.Delete)
  126 + msg += l.makeMsg(" 保留字段", l.Reserve)
  127 + msg += l.makeMsg(" 添加字段", l.Add)
  128 + return msg
  129 +}
  130 +
  131 +func (l *SpiltMainTableLog) makeMsg(title string, fields []*domain.Field) string {
  132 + if len(l.fieldNames(fields)) > 0 {
  133 + return fmt.Sprintf("%s: %s ", title, strings.Join(l.fieldNames(fields), "、"))
  134 + }
  135 + return ""
  136 +}
  137 +
  138 +func (l *SpiltMainTableLog) fieldNames(fields []*domain.Field) []string {
  139 + names := make([]string, 0)
  140 + for _, f := range fields {
  141 + names = append(names, f.Name)
  142 + }
  143 + return names
  144 +}
  145 +
  146 +// 5.分表编辑
  147 +type SubTableEditLog struct {
  148 + domain.LogEntry
  149 +
  150 + Reserve []*domain.Field
  151 + Delete []*domain.Field
  152 + Add []*domain.Field
  153 +}
  154 +
  155 +func (l *SubTableEditLog) Content() string {
  156 + var msg string
  157 + msg += l.makeMsg("删除字段", l.Delete)
  158 + msg += l.makeMsg("保留字段", l.Reserve)
  159 + msg += l.makeMsg("添加字段", l.Add)
  160 + return msg
  161 +}
  162 +
  163 +func (l *SubTableEditLog) makeMsg(title string, fields []*domain.Field) string {
  164 + if len(l.fieldNames(fields)) > 0 {
  165 + return fmt.Sprintf("%s: %s ", title, strings.Join(l.fieldNames(fields), "、"))
  166 + }
  167 + return ""
  168 +}
  169 +
  170 +func (l *SubTableEditLog) fieldNames(fields []*domain.Field) []string {
  171 + names := make([]string, 0)
  172 + for _, f := range fields {
  173 + names = append(names, f.Name)
  174 + }
  175 + return names
  176 +}
  177 +
  178 +// 6.表复制日志
  179 +type CopyTableLog struct {
  180 + domain.LogEntry
  181 + // 表名
  182 + SourceTableName string
  183 +}
  184 +
  185 +func (l *CopyTableLog) Content() string {
  186 + msg := fmt.Sprintf("来源表:%v", l.SourceTableName)
  187 + return msg
  188 +}
  189 +
  190 +// 8.表删除日志
  191 +type DeleteTableLog struct {
  192 + domain.LogEntry
  193 + // 表名
  194 + SourceTableName string
  195 + RowCount int
  196 + SubTables []*domain.Table
  197 +}
  198 +
  199 +func (l *DeleteTableLog) Content() string {
  200 + msg := fmt.Sprintf("共计%v条数据", l.RowCount)
  201 + var tables []string
  202 + for _, t := range l.SubTables {
  203 + tables = append(tables, t.Name+"分表")
  204 + }
  205 + if len(tables) > 0 {
  206 + msg += fmt.Sprintf(",(存在分表)同步删除%s", strings.Join(tables, "/"))
  207 + }
  208 + return msg
  209 +}
  1 +package domainService
  2 +
  3 +import (
  4 + "fmt"
  5 + pgTransaction "github.com/linmadan/egglib-go/transaction/pg"
  6 + "gitlab.fjmaimaimai.com/allied-creation/character-library-metadata-bastion/pkg/domain"
  7 + "gitlab.fjmaimaimai.com/allied-creation/character-library-metadata-bastion/pkg/infrastructure/repository"
  8 + "time"
  9 +)
  10 +
  11 +type UpdateTableStructService struct {
  12 + transactionContext *pgTransaction.TransactionContext
  13 +}
  14 +
  15 +func NewUpdateTableStructService(transactionContext *pgTransaction.TransactionContext) (*UpdateTableStructService, error) {
  16 + if transactionContext == nil {
  17 + return nil, fmt.Errorf("transactionContext参数不能为nil")
  18 + } else {
  19 + return &UpdateTableStructService{
  20 + transactionContext: transactionContext,
  21 + }, nil
  22 + }
  23 +}
  24 +
  25 +func (ptr *UpdateTableStructService) UpdateTableStruct(ctx *domain.Context, tableId int, fields []*domain.Field, name string) (interface{}, error) {
  26 + tableRepository, _ := repository.NewTableRepository(ptr.transactionContext)
  27 + table, err := tableRepository.FindOne(map[string]interface{}{"tableId": tableId})
  28 + if err != nil {
  29 + return nil, err
  30 + }
  31 + if table.TableType != domain.SubTable.ToString() {
  32 + return nil, fmt.Errorf("只有分表允许编辑表结构")
  33 + }
  34 + mainTable, err := tableRepository.FindOne(map[string]interface{}{"tableId": table.ParentId})
  35 + if err != nil {
  36 + return nil, fmt.Errorf("主表不存在")
  37 + }
  38 +
  39 + fields = MappingFields(mainTable, fields)
  40 + reserves, deletes, adds := domain.FieldsChange(table.Fields(false), fields)
  41 + dataFields := (domain.Fields)(fields).Select(map[string]interface{}{"flag": 1})
  42 + manualFields := (domain.Fields)(fields).Select(map[string]interface{}{"flag": 2})
  43 + table.DataFieldIndex = mainTable.DataFieldIndex
  44 + table.DataFields = dataFields
  45 + table.ManualFields = manualFields
  46 + table.UpdatedAt = time.Now()
  47 +
  48 + if err = domain.ValidFields(fields); err != nil {
  49 + return nil, err
  50 + }
  51 +
  52 + if _, err = tableRepository.Save(table); err != nil {
  53 + return nil, err
  54 + }
  55 + if _, err = tableRepository.Save(mainTable); err != nil {
  56 + return nil, err
  57 + }
  58 +
  59 + // Log
  60 + // 日志
  61 + if err = FastLog(ptr.transactionContext, domain.CommonLog, table.TableId, &SubTableEditLog{
  62 + LogEntry: domain.NewLogEntry(table.Name, table.TableType, domain.EditSubTable, ctx),
  63 + Reserve: reserves,
  64 + Delete: deletes,
  65 + Add: adds,
  66 + }); err != nil {
  67 + return nil, err
  68 + }
  69 +
  70 + // 通知底层
  71 + return struct{}{}, nil
  72 +}
  73 +
  74 +func MappingFields(table *domain.Table, fields []*domain.Field) []*domain.Field {
  75 + tableFields := table.Fields(false)
  76 + tableFieldsMap := (domain.Fields)(tableFields).ToMap()
  77 + for i := range fields {
  78 + f := fields[i]
  79 + if v, ok := tableFieldsMap[fields[i].Name]; ok {
  80 + fields[i].Name = v.Name
  81 + fields[i].SQLName = v.SQLName
  82 + fields[i].Index = v.Index
  83 + fields[i].SQLType = v.SQLType
  84 + fields[i].Description = v.Description
  85 + fields[i].Flag = v.Flag
  86 + } else {
  87 + if f.Flag == domain.ManualField && f.Index == 0 {
  88 + table.DataFieldIndex += 1
  89 + fields[i] = DataField(f.Name, f.SQLType, domain.ManualField, table.DataFieldIndex)
  90 + }
  91 + }
  92 + }
  93 + return fields
  94 +}
@@ -28,6 +28,7 @@ func Init() { @@ -28,6 +28,7 @@ func Init() {
28 (*models.File)(nil), 28 (*models.File)(nil),
29 (*models.Table)(nil), 29 (*models.Table)(nil),
30 (*models.Log)(nil), 30 (*models.Log)(nil),
  31 + (*models.MappingRule)(nil),
31 } { 32 } {
32 err := DB.Model(model).CreateTable(&orm.CreateTableOptions{ 33 err := DB.Model(model).CreateTable(&orm.CreateTableOptions{
33 Temp: false, 34 Temp: false,
@@ -16,7 +16,7 @@ type File struct { @@ -16,7 +16,7 @@ type File struct {
16 // 源文件Id(FileType为TemporaryFile或VerifiedFile时有值) 16 // 源文件Id(FileType为TemporaryFile或VerifiedFile时有值)
17 SourceFileId int `comment:"源文件Id(FileType为TemporaryFile或VerifiedFile时有值)"` 17 SourceFileId int `comment:"源文件Id(FileType为TemporaryFile或VerifiedFile时有值)"`
18 // 操作人 18 // 操作人
19 - Operator string `comment:"操作人"` 19 + //Operator string `comment:"操作人"`
20 // 创建时间 20 // 创建时间
21 CreatedAt time.Time `comment:"创建时间"` 21 CreatedAt time.Time `comment:"创建时间"`
22 // 更新时间 22 // 更新时间
@@ -25,4 +25,6 @@ type File struct { @@ -25,4 +25,6 @@ type File struct {
25 DeletedAt time.Time `pg:",soft_delete" comment:"删除时间"` 25 DeletedAt time.Time `pg:",soft_delete" comment:"删除时间"`
26 // 版本 26 // 版本
27 Version int `comment:"版本"` 27 Version int `comment:"版本"`
  28 + // 扩展
  29 + Context *domain.Context `json:"context"`
28 } 30 }
@@ -27,4 +27,6 @@ type Log struct { @@ -27,4 +27,6 @@ type Log struct {
27 OperatorName string `json:"operatorName"` 27 OperatorName string `json:"operatorName"`
28 // 创建时间 28 // 创建时间
29 CreatedAt time.Time `comment:"创建时间"` 29 CreatedAt time.Time `comment:"创建时间"`
  30 + // 扩展
  31 + Context *domain.Context `json:"context"`
30 } 32 }
  1 +package models
  2 +
  3 +import (
  4 + "gitlab.fjmaimaimai.com/allied-creation/character-library-metadata-bastion/pkg/domain"
  5 + "time"
  6 +)
  7 +
  8 +type MappingRule struct {
  9 + tableName string `comment:"匹配规则配置" pg:"metadata.mapping_rules,alias:mapping_rule"`
  10 + // 匹配规则ID
  11 + MappingRuleId int `comment:"匹配规则ID" pg:"pk:mapping_rule_id"`
  12 + // 名称
  13 + Name string `comment:"名称"`
  14 + // 表Id
  15 + TableId int `comment:"表Id"`
  16 + // 文件ID
  17 + FileId int `comment:"文件ID"`
  18 + // 主表列
  19 + MainTableFields []*domain.Field `comment:"主表列"`
  20 + // 校验文件列
  21 + VerifiedFileFields []*domain.Field `comment:"校验文件列"`
  22 + // 校验文件列
  23 + MappingFields []*domain.MappingField `comment:"校验文件列"`
  24 + // 创建时间
  25 + CreatedAt time.Time `comment:"创建时间"`
  26 + // 更新时间
  27 + UpdatedAt time.Time `comment:"更新时间"`
  28 + // 删除时间
  29 + DeletedAt time.Time `comment:"删除时间"`
  30 + // 扩展
  31 + Context *domain.Context `json:"context"`
  32 +}
@@ -33,4 +33,8 @@ type Table struct { @@ -33,4 +33,8 @@ type Table struct {
33 DeletedAt time.Time `pg:",soft_delete" comment:"删除时间"` 33 DeletedAt time.Time `pg:",soft_delete" comment:"删除时间"`
34 // 版本 34 // 版本
35 Version int `comment:"版本"` 35 Version int `comment:"版本"`
  36 + // 行数
  37 + RowCount int `comment:"行数"`
  38 + // 扩展
  39 + Context *domain.Context `json:"context"`
36 } 40 }
@@ -11,10 +11,11 @@ func TransformToFileDomainModelFromPgModels(fileModel *models.File) (*domain.Fil @@ -11,10 +11,11 @@ func TransformToFileDomainModelFromPgModels(fileModel *models.File) (*domain.Fil
11 FileType: fileModel.FileType, 11 FileType: fileModel.FileType,
12 FileInfo: fileModel.FileInfo, 12 FileInfo: fileModel.FileInfo,
13 SourceFileId: fileModel.SourceFileId, 13 SourceFileId: fileModel.SourceFileId,
14 - Operator: fileModel.Operator,  
15 - CreatedAt: fileModel.CreatedAt,  
16 - UpdatedAt: fileModel.UpdatedAt,  
17 - DeletedAt: fileModel.DeletedAt,  
18 - Version: fileModel.Version, 14 + //Operator: fileModel.Operator,
  15 + CreatedAt: fileModel.CreatedAt,
  16 + UpdatedAt: fileModel.UpdatedAt,
  17 + DeletedAt: fileModel.DeletedAt,
  18 + Version: fileModel.Version,
  19 + Context: fileModel.Context,
19 }, nil 20 }, nil
20 } 21 }
@@ -17,5 +17,6 @@ func TransformToLogDomainModelFromPgModels(logModel *models.Log) (*domain.Log, e @@ -17,5 +17,6 @@ func TransformToLogDomainModelFromPgModels(logModel *models.Log) (*domain.Log, e
17 Content: logModel.Content, 17 Content: logModel.Content,
18 OperatorName: logModel.OperatorName, 18 OperatorName: logModel.OperatorName,
19 CreatedAt: logModel.CreatedAt, 19 CreatedAt: logModel.CreatedAt,
  20 + Context: logModel.Context,
20 }, nil 21 }, nil
21 } 22 }
  1 +package transform
  2 +
  3 +import (
  4 + "gitlab.fjmaimaimai.com/allied-creation/character-library-metadata-bastion/pkg/domain"
  5 + "gitlab.fjmaimaimai.com/allied-creation/character-library-metadata-bastion/pkg/infrastructure/pg/models"
  6 +)
  7 +
  8 +func TransformToMappingRuleDomainModelFromPgModels(mappingRuleModel *models.MappingRule) (*domain.MappingRule, error) {
  9 + return &domain.MappingRule{
  10 + MappingRuleId: mappingRuleModel.MappingRuleId,
  11 + Name: mappingRuleModel.Name,
  12 + TableId: mappingRuleModel.TableId,
  13 + FileId: mappingRuleModel.FileId,
  14 + MainTableFields: mappingRuleModel.MainTableFields,
  15 + VerifiedFileFields: mappingRuleModel.VerifiedFileFields,
  16 + MappingFields: mappingRuleModel.MappingFields,
  17 + CreatedAt: mappingRuleModel.CreatedAt,
  18 + UpdatedAt: mappingRuleModel.UpdatedAt,
  19 + DeletedAt: mappingRuleModel.DeletedAt,
  20 + Context: mappingRuleModel.Context,
  21 + }, nil
  22 +}
@@ -20,5 +20,7 @@ func TransformToTableDomainModelFromPgModels(tableModel *models.Table) (*domain. @@ -20,5 +20,7 @@ func TransformToTableDomainModelFromPgModels(tableModel *models.Table) (*domain.
20 UpdatedAt: tableModel.UpdatedAt, 20 UpdatedAt: tableModel.UpdatedAt,
21 DeletedAt: tableModel.DeletedAt, 21 DeletedAt: tableModel.DeletedAt,
22 Version: tableModel.Version, 22 Version: tableModel.Version,
  23 + RowCount: tableModel.RowCount,
  24 + Context: tableModel.Context,
23 }, nil 25 }, nil
24 } 26 }
@@ -29,11 +29,12 @@ func (repository *FileRepository) Save(file *domain.File) (*domain.File, error) @@ -29,11 +29,12 @@ func (repository *FileRepository) Save(file *domain.File) (*domain.File, error)
29 "file_type", 29 "file_type",
30 "file_info", 30 "file_info",
31 "source_file_id", 31 "source_file_id",
32 - "operator", 32 + //"operator",
33 "created_at", 33 "created_at",
34 "updated_at", 34 "updated_at",
35 "deleted_at", 35 "deleted_at",
36 "version", 36 "version",
  37 + "context",
37 } 38 }
38 insertFieldsSnippet := sqlbuilder.SqlFieldsSnippet(sqlbuilder.RemoveSqlFields(sqlBuildFields, "file_id", "deleted_at")) 39 insertFieldsSnippet := sqlbuilder.SqlFieldsSnippet(sqlbuilder.RemoveSqlFields(sqlBuildFields, "file_id", "deleted_at"))
39 insertPlaceHoldersSnippet := sqlbuilder.SqlPlaceHoldersSnippet(sqlbuilder.RemoveSqlFields(sqlBuildFields, "file_id", "deleted_at")) 40 insertPlaceHoldersSnippet := sqlbuilder.SqlPlaceHoldersSnippet(sqlbuilder.RemoveSqlFields(sqlBuildFields, "file_id", "deleted_at"))
@@ -48,20 +49,22 @@ func (repository *FileRepository) Save(file *domain.File) (*domain.File, error) @@ -48,20 +49,22 @@ func (repository *FileRepository) Save(file *domain.File) (*domain.File, error)
48 &file.FileType, 49 &file.FileType,
49 &file.FileInfo, 50 &file.FileInfo,
50 &file.SourceFileId, 51 &file.SourceFileId,
51 - &file.Operator, 52 + //&file.Operator,
52 &file.CreatedAt, 53 &file.CreatedAt,
53 &file.UpdatedAt, 54 &file.UpdatedAt,
54 &file.DeletedAt, 55 &file.DeletedAt,
55 &file.Version, 56 &file.Version,
  57 + &file.Context,
56 ), 58 ),
57 fmt.Sprintf("INSERT INTO metadata.files (%s) VALUES (%s) RETURNING %s", insertFieldsSnippet, insertPlaceHoldersSnippet, returningFieldsSnippet), 59 fmt.Sprintf("INSERT INTO metadata.files (%s) VALUES (%s) RETURNING %s", insertFieldsSnippet, insertPlaceHoldersSnippet, returningFieldsSnippet),
58 file.FileType, 60 file.FileType,
59 file.FileInfo, 61 file.FileInfo,
60 file.SourceFileId, 62 file.SourceFileId,
61 - file.Operator, 63 + //file.Operator,
62 file.CreatedAt, 64 file.CreatedAt,
63 file.UpdatedAt, 65 file.UpdatedAt,
64 file.Version, 66 file.Version,
  67 + file.Context,
65 ); err != nil { 68 ); err != nil {
66 return file, err 69 return file, err
67 } 70 }
@@ -74,20 +77,22 @@ func (repository *FileRepository) Save(file *domain.File) (*domain.File, error) @@ -74,20 +77,22 @@ func (repository *FileRepository) Save(file *domain.File) (*domain.File, error)
74 &file.FileType, 77 &file.FileType,
75 &file.FileInfo, 78 &file.FileInfo,
76 &file.SourceFileId, 79 &file.SourceFileId,
77 - &file.Operator, 80 + //&file.Operator,
78 &file.CreatedAt, 81 &file.CreatedAt,
79 &file.UpdatedAt, 82 &file.UpdatedAt,
80 &file.DeletedAt, 83 &file.DeletedAt,
81 &file.Version, 84 &file.Version,
  85 + &file.Context,
82 ), 86 ),
83 fmt.Sprintf("UPDATE metadata.files SET %s WHERE file_id=? and version=? RETURNING %s", updateFieldsSnippet, returningFieldsSnippet), 87 fmt.Sprintf("UPDATE metadata.files SET %s WHERE file_id=? and version=? RETURNING %s", updateFieldsSnippet, returningFieldsSnippet),
84 file.FileType, 88 file.FileType,
85 file.FileInfo, 89 file.FileInfo,
86 file.SourceFileId, 90 file.SourceFileId,
87 - file.Operator, 91 + //file.Operator,
88 file.CreatedAt, 92 file.CreatedAt,
89 file.UpdatedAt, 93 file.UpdatedAt,
90 file.Version, 94 file.Version,
  95 + file.Context,
91 file.Identify(), 96 file.Identify(),
92 oldVersion, 97 oldVersion,
93 ); err != nil { 98 ); err != nil {
@@ -128,6 +133,7 @@ func (repository *FileRepository) Find(queryOptions map[string]interface{}) (int @@ -128,6 +133,7 @@ func (repository *FileRepository) Find(queryOptions map[string]interface{}) (int
128 var fileModels []*models.File 133 var fileModels []*models.File
129 files := make([]*domain.File, 0) 134 files := make([]*domain.File, 0)
130 query := sqlbuilder.BuildQuery(tx.Model(&fileModels), queryOptions) 135 query := sqlbuilder.BuildQuery(tx.Model(&fileModels), queryOptions)
  136 + WhereContext(query, queryOptions)
131 query.SetWhereByQueryOption("file_id > ?", "lastId") 137 query.SetWhereByQueryOption("file_id > ?", "lastId")
132 query.SetWhereByQueryOption("file_type = ?", "fileType") 138 query.SetWhereByQueryOption("file_type = ?", "fileType")
133 query.SetWhereByQueryOption(fmt.Sprintf("file_info->>'name' like '%%%v%%'", queryOptions["fileName"]), "fileName") 139 query.SetWhereByQueryOption(fmt.Sprintf("file_info->>'name' like '%%%v%%'", queryOptions["fileName"]), "fileName")
@@ -3,6 +3,9 @@ package repository @@ -3,6 +3,9 @@ package repository
3 import ( 3 import (
4 "fmt" 4 "fmt"
5 "github.com/go-pg/pg/v10" 5 "github.com/go-pg/pg/v10"
  6 + "github.com/go-pg/pg/v10/orm"
  7 + "github.com/linmadan/egglib-go/utils/xtime"
  8 + "time"
6 9
7 "github.com/linmadan/egglib-go/persistent/pg/sqlbuilder" 10 "github.com/linmadan/egglib-go/persistent/pg/sqlbuilder"
8 pgTransaction "github.com/linmadan/egglib-go/transaction/pg" 11 pgTransaction "github.com/linmadan/egglib-go/transaction/pg"
@@ -36,6 +39,7 @@ func (repository *LogRepository) Save(log *domain.Log) (*domain.Log, error) { @@ -36,6 +39,7 @@ func (repository *LogRepository) Save(log *domain.Log) (*domain.Log, error) {
36 "operator_name", 39 "operator_name",
37 "created_at", 40 "created_at",
38 "entry", 41 "entry",
  42 + "context",
39 } 43 }
40 insertFieldsSnippet := sqlbuilder.SqlFieldsSnippet(sqlbuilder.RemoveSqlFields(sqlBuildFields, "log_id")) 44 insertFieldsSnippet := sqlbuilder.SqlFieldsSnippet(sqlbuilder.RemoveSqlFields(sqlBuildFields, "log_id"))
41 insertPlaceHoldersSnippet := sqlbuilder.SqlPlaceHoldersSnippet(sqlbuilder.RemoveSqlFields(sqlBuildFields, "log_id")) 45 insertPlaceHoldersSnippet := sqlbuilder.SqlPlaceHoldersSnippet(sqlbuilder.RemoveSqlFields(sqlBuildFields, "log_id"))
@@ -56,6 +60,7 @@ func (repository *LogRepository) Save(log *domain.Log) (*domain.Log, error) { @@ -56,6 +60,7 @@ func (repository *LogRepository) Save(log *domain.Log) (*domain.Log, error) {
56 &log.OperatorName, 60 &log.OperatorName,
57 &log.CreatedAt, 61 &log.CreatedAt,
58 &log.Entry, 62 &log.Entry,
  63 + &log.Context,
59 ), 64 ),
60 fmt.Sprintf("INSERT INTO metadata.logs (%s) VALUES (%s) RETURNING %s", insertFieldsSnippet, insertPlaceHoldersSnippet, returningFieldsSnippet), 65 fmt.Sprintf("INSERT INTO metadata.logs (%s) VALUES (%s) RETURNING %s", insertFieldsSnippet, insertPlaceHoldersSnippet, returningFieldsSnippet),
61 log.LogType, 66 log.LogType,
@@ -67,6 +72,7 @@ func (repository *LogRepository) Save(log *domain.Log) (*domain.Log, error) { @@ -67,6 +72,7 @@ func (repository *LogRepository) Save(log *domain.Log) (*domain.Log, error) {
67 log.OperatorName, 72 log.OperatorName,
68 log.CreatedAt, 73 log.CreatedAt,
69 log.Entry, 74 log.Entry,
  75 + log.Context,
70 ); err != nil { 76 ); err != nil {
71 return log, err 77 return log, err
72 } 78 }
@@ -83,6 +89,7 @@ func (repository *LogRepository) Save(log *domain.Log) (*domain.Log, error) { @@ -83,6 +89,7 @@ func (repository *LogRepository) Save(log *domain.Log) (*domain.Log, error) {
83 &log.OperatorName, 89 &log.OperatorName,
84 &log.CreatedAt, 90 &log.CreatedAt,
85 &log.Entry, 91 &log.Entry,
  92 + &log.Context,
86 ), 93 ),
87 fmt.Sprintf("UPDATE metadata.logs SET %s WHERE log_id=? RETURNING %s", updateFieldsSnippet, returningFieldsSnippet), 94 fmt.Sprintf("UPDATE metadata.logs SET %s WHERE log_id=? RETURNING %s", updateFieldsSnippet, returningFieldsSnippet),
88 log.LogType, 95 log.LogType,
@@ -93,7 +100,8 @@ func (repository *LogRepository) Save(log *domain.Log) (*domain.Log, error) { @@ -93,7 +100,8 @@ func (repository *LogRepository) Save(log *domain.Log) (*domain.Log, error) {
93 log.Content, 100 log.Content,
94 log.OperatorName, 101 log.OperatorName,
95 log.CreatedAt, 102 log.CreatedAt,
96 - &log.Entry, 103 + log.Entry,
  104 + log.Context,
97 log.Identify(), 105 log.Identify(),
98 ); err != nil { 106 ); err != nil {
99 return log, err 107 return log, err
@@ -133,6 +141,29 @@ func (repository *LogRepository) Find(queryOptions map[string]interface{}) (int6 @@ -133,6 +141,29 @@ func (repository *LogRepository) Find(queryOptions map[string]interface{}) (int6
133 var logModels []*models.Log 141 var logModels []*models.Log
134 logs := make([]*domain.Log, 0) 142 logs := make([]*domain.Log, 0)
135 query := sqlbuilder.BuildQuery(tx.Model(&logModels), queryOptions) 143 query := sqlbuilder.BuildQuery(tx.Model(&logModels), queryOptions)
  144 + WhereContext(query, queryOptions)
  145 + query.SetWhereByQueryOption(" log_type = ?", "logType")
  146 + query.SetWhereByQueryOption(" source_id = ?", "sourceId")
  147 + if v, ok := queryOptions["inSourceId"]; ok && len(v.([]int)) > 0 {
  148 + query.Where("source_id in (?)", pg.In(v.([]int)))
  149 + }
  150 + if v, ok := queryOptions["matchContent"]; ok && len(v.(string)) > 0 {
  151 + query.WhereGroup(func(query *orm.Query) (*orm.Query, error) {
  152 + matchContent := v.(string)
  153 + query.WhereOr(fmt.Sprintf("content like '%%%v%%'", matchContent))
  154 + query.WhereOr(fmt.Sprintf("operator_name like '%%%v%%'", matchContent))
  155 + query.WhereOr(fmt.Sprintf("object_name like '%%%v%%'", matchContent))
  156 + return query, nil
  157 + })
  158 + }
  159 +
  160 + if v, ok := queryOptions["beginTime"]; ok && !xtime.IsZero(v.(time.Time)) {
  161 + query.SetWhereByQueryOption(" created_at >= ?", "beginTime")
  162 + }
  163 + if v, ok := queryOptions["endTime"]; ok && !xtime.IsZero(v.(time.Time)) {
  164 + query.SetWhereByQueryOption(" created_at < ?", "endTime")
  165 + }
  166 +
136 query.SetOffsetAndLimit(20) 167 query.SetOffsetAndLimit(20)
137 query.SetOrderDirect("log_id", "DESC") 168 query.SetOrderDirect("log_id", "DESC")
138 if count, err := query.SelectAndCount(); err != nil { 169 if count, err := query.SelectAndCount(); err != nil {
  1 +package repository
  2 +
  3 +import (
  4 + "fmt"
  5 + "github.com/go-pg/pg/v10"
  6 +
  7 + "github.com/linmadan/egglib-go/persistent/pg/sqlbuilder"
  8 + pgTransaction "github.com/linmadan/egglib-go/transaction/pg"
  9 + "github.com/linmadan/egglib-go/utils/snowflake"
  10 + "gitlab.fjmaimaimai.com/allied-creation/character-library-metadata-bastion/pkg/domain"
  11 + "gitlab.fjmaimaimai.com/allied-creation/character-library-metadata-bastion/pkg/infrastructure/pg/models"
  12 + "gitlab.fjmaimaimai.com/allied-creation/character-library-metadata-bastion/pkg/infrastructure/pg/transform"
  13 +)
  14 +
  15 +type MappingRuleRepository struct {
  16 + transactionContext *pgTransaction.TransactionContext
  17 +}
  18 +
  19 +func (repository *MappingRuleRepository) nextIdentify() (int64, error) {
  20 + IdWorker, err := snowflake.NewIdWorker(1)
  21 + if err != nil {
  22 + return 0, err
  23 + }
  24 + id, err := IdWorker.NextId()
  25 + return id, err
  26 +}
  27 +func (repository *MappingRuleRepository) Save(mappingRule *domain.MappingRule) (*domain.MappingRule, error) {
  28 + sqlBuildFields := []string{
  29 + "mapping_rule_id",
  30 + "name",
  31 + "table_id",
  32 + "file_id",
  33 + "main_table_fields",
  34 + "verified_file_fields",
  35 + "mapping_fields",
  36 + "created_at",
  37 + "updated_at",
  38 + "deleted_at",
  39 + "context",
  40 + }
  41 + insertFieldsSnippet := sqlbuilder.SqlFieldsSnippet(sqlbuilder.RemoveSqlFields(sqlBuildFields, "mapping_rule_id", "deleted_at"))
  42 + insertPlaceHoldersSnippet := sqlbuilder.SqlPlaceHoldersSnippet(sqlbuilder.RemoveSqlFields(sqlBuildFields, "mapping_rule_id", "deleted_at"))
  43 + returningFieldsSnippet := sqlbuilder.SqlFieldsSnippet(sqlBuildFields)
  44 + updateFields := sqlbuilder.RemoveSqlFields(sqlBuildFields, "mapping_rule_id", "deleted_at")
  45 + updateFieldsSnippet := sqlbuilder.SqlUpdateFieldsSnippet(updateFields)
  46 + tx := repository.transactionContext.PgTx
  47 + if mappingRule.Identify() == nil {
  48 + if _, err := tx.QueryOne(
  49 + pg.Scan(
  50 + &mappingRule.MappingRuleId,
  51 + &mappingRule.Name,
  52 + &mappingRule.TableId,
  53 + &mappingRule.FileId,
  54 + &mappingRule.MainTableFields,
  55 + &mappingRule.VerifiedFileFields,
  56 + &mappingRule.MappingFields,
  57 + &mappingRule.CreatedAt,
  58 + &mappingRule.UpdatedAt,
  59 + &mappingRule.DeletedAt,
  60 + &mappingRule.Context,
  61 + ),
  62 + fmt.Sprintf("INSERT INTO metadata.mapping_rules (%s) VALUES (%s) RETURNING %s", insertFieldsSnippet, insertPlaceHoldersSnippet, returningFieldsSnippet),
  63 + //mappingRule.MappingRuleId,
  64 + mappingRule.Name,
  65 + mappingRule.TableId,
  66 + mappingRule.FileId,
  67 + mappingRule.MainTableFields,
  68 + mappingRule.VerifiedFileFields,
  69 + mappingRule.MappingFields,
  70 + mappingRule.CreatedAt,
  71 + mappingRule.UpdatedAt,
  72 + mappingRule.Context,
  73 + //mappingRule.DeletedAt,
  74 + ); err != nil {
  75 + return mappingRule, err
  76 + }
  77 + } else {
  78 + if _, err := tx.QueryOne(
  79 + pg.Scan(
  80 + &mappingRule.MappingRuleId,
  81 + &mappingRule.Name,
  82 + &mappingRule.TableId,
  83 + &mappingRule.FileId,
  84 + &mappingRule.MainTableFields,
  85 + &mappingRule.VerifiedFileFields,
  86 + &mappingRule.MappingFields,
  87 + &mappingRule.CreatedAt,
  88 + &mappingRule.UpdatedAt,
  89 + &mappingRule.DeletedAt,
  90 + &mappingRule.Context,
  91 + ),
  92 + fmt.Sprintf("UPDATE metadata.mapping_rules SET %s WHERE mapping_rule_id=? RETURNING %s", updateFieldsSnippet, returningFieldsSnippet),
  93 + //mappingRule.MappingRuleId,
  94 + mappingRule.Name,
  95 + mappingRule.TableId,
  96 + mappingRule.FileId,
  97 + mappingRule.MainTableFields,
  98 + mappingRule.VerifiedFileFields,
  99 + mappingRule.MappingFields,
  100 + mappingRule.CreatedAt,
  101 + mappingRule.UpdatedAt,
  102 + mappingRule.Context,
  103 + //mappingRule.DeletedAt,
  104 + mappingRule.Identify(),
  105 + ); err != nil {
  106 + return mappingRule, err
  107 + }
  108 + }
  109 + return mappingRule, nil
  110 +}
  111 +func (repository *MappingRuleRepository) Remove(mappingRule *domain.MappingRule) (*domain.MappingRule, error) {
  112 + tx := repository.transactionContext.PgTx
  113 + mappingRuleModel := new(models.MappingRule)
  114 + mappingRuleModel.MappingRuleId = mappingRule.Identify().(int)
  115 + if _, err := tx.Model(mappingRuleModel).WherePK().Delete(); err != nil {
  116 + return mappingRule, err
  117 + }
  118 + return mappingRule, nil
  119 +}
  120 +func (repository *MappingRuleRepository) FindOne(queryOptions map[string]interface{}) (*domain.MappingRule, error) {
  121 + tx := repository.transactionContext.PgTx
  122 + mappingRuleModel := new(models.MappingRule)
  123 + query := sqlbuilder.BuildQuery(tx.Model(mappingRuleModel), queryOptions)
  124 + WhereContext(query, queryOptions)
  125 + query.SetWhereByQueryOption("name = ?", "name")
  126 + query.SetWhereByQueryOption("mapping_rule.mapping_rule_id = ?", "mappingRuleId")
  127 + if err := query.First(); err != nil {
  128 + if err.Error() == "pg: no rows in result set" {
  129 + return nil, fmt.Errorf("没有此资源")
  130 + } else {
  131 + return nil, err
  132 + }
  133 + }
  134 + if mappingRuleModel.MappingRuleId == 0 {
  135 + return nil, nil
  136 + } else {
  137 + return transform.TransformToMappingRuleDomainModelFromPgModels(mappingRuleModel)
  138 + }
  139 +}
  140 +func (repository *MappingRuleRepository) Find(queryOptions map[string]interface{}) (int64, []*domain.MappingRule, error) {
  141 + tx := repository.transactionContext.PgTx
  142 + var mappingRuleModels []*models.MappingRule
  143 + mappingRules := make([]*domain.MappingRule, 0)
  144 + query := sqlbuilder.BuildQuery(tx.Model(&mappingRuleModels), queryOptions)
  145 + WhereContext(query, queryOptions)
  146 + //query.SetOffsetAndLimit(20)
  147 + query.SetWhereByQueryOption("table_id = ?", "tableId")
  148 + query.SetOrderDirect("mapping_rule_id", "DESC")
  149 + if count, err := query.SelectAndCount(); err != nil {
  150 + return 0, mappingRules, err
  151 + } else {
  152 + for _, mappingRuleModel := range mappingRuleModels {
  153 + if mappingRule, err := transform.TransformToMappingRuleDomainModelFromPgModels(mappingRuleModel); err != nil {
  154 + return 0, mappingRules, err
  155 + } else {
  156 + mappingRules = append(mappingRules, mappingRule)
  157 + }
  158 + }
  159 + return int64(count), mappingRules, nil
  160 + }
  161 +}
  162 +func NewMappingRuleRepository(transactionContext *pgTransaction.TransactionContext) (*MappingRuleRepository, error) {
  163 + if transactionContext == nil {
  164 + return nil, fmt.Errorf("transactionContext参数不能为nil")
  165 + } else {
  166 + return &MappingRuleRepository{
  167 + transactionContext: transactionContext,
  168 + }, nil
  169 + }
  170 +}
  1 +package repository
  2 +
  3 +import (
  4 + "github.com/linmadan/egglib-go/persistent/pg/sqlbuilder"
  5 + "gitlab.fjmaimaimai.com/allied-creation/character-library-metadata-bastion/pkg/domain"
  6 +)
  7 +
  8 +func WhereContext(query *sqlbuilder.Query, queryOptions map[string]interface{}) {
  9 + if _, ok := queryOptions["context"]; !ok {
  10 + return
  11 + }
  12 + context := queryOptions["context"].(*domain.Context)
  13 + query.Where("context->'companyId'='?'", context.CompanyId)
  14 +}
@@ -39,6 +39,8 @@ func (repository *TableRepository) Save(table *domain.Table) (*domain.Table, err @@ -39,6 +39,8 @@ func (repository *TableRepository) Save(table *domain.Table) (*domain.Table, err
39 "updated_at", 39 "updated_at",
40 "deleted_at", 40 "deleted_at",
41 "version", 41 "version",
  42 + "row_count",
  43 + "context",
42 } 44 }
43 insertFieldsSnippet := sqlbuilder.SqlFieldsSnippet(sqlbuilder.RemoveSqlFields(sqlBuildFields, "table_id", "deleted_at")) 45 insertFieldsSnippet := sqlbuilder.SqlFieldsSnippet(sqlbuilder.RemoveSqlFields(sqlBuildFields, "table_id", "deleted_at"))
44 insertPlaceHoldersSnippet := sqlbuilder.SqlPlaceHoldersSnippet(sqlbuilder.RemoveSqlFields(sqlBuildFields, "table_id", "deleted_at")) 46 insertPlaceHoldersSnippet := sqlbuilder.SqlPlaceHoldersSnippet(sqlbuilder.RemoveSqlFields(sqlBuildFields, "table_id", "deleted_at"))
@@ -62,6 +64,8 @@ func (repository *TableRepository) Save(table *domain.Table) (*domain.Table, err @@ -62,6 +64,8 @@ func (repository *TableRepository) Save(table *domain.Table) (*domain.Table, err
62 &table.UpdatedAt, 64 &table.UpdatedAt,
63 &table.DeletedAt, 65 &table.DeletedAt,
64 &table.Version, 66 &table.Version,
  67 + &table.RowCount,
  68 + &table.Context,
65 ), 69 ),
66 fmt.Sprintf("INSERT INTO metadata.tables (%s) VALUES (%s) RETURNING %s", insertFieldsSnippet, insertPlaceHoldersSnippet, returningFieldsSnippet), 70 fmt.Sprintf("INSERT INTO metadata.tables (%s) VALUES (%s) RETURNING %s", insertFieldsSnippet, insertPlaceHoldersSnippet, returningFieldsSnippet),
67 table.TableType, 71 table.TableType,
@@ -75,6 +79,8 @@ func (repository *TableRepository) Save(table *domain.Table) (*domain.Table, err @@ -75,6 +79,8 @@ func (repository *TableRepository) Save(table *domain.Table) (*domain.Table, err
75 table.CreatedAt, 79 table.CreatedAt,
76 table.UpdatedAt, 80 table.UpdatedAt,
77 table.Version, 81 table.Version,
  82 + table.RowCount,
  83 + table.Context,
78 ); err != nil { 84 ); err != nil {
79 return table, err 85 return table, err
80 } 86 }
@@ -96,6 +102,8 @@ func (repository *TableRepository) Save(table *domain.Table) (*domain.Table, err @@ -96,6 +102,8 @@ func (repository *TableRepository) Save(table *domain.Table) (*domain.Table, err
96 &table.UpdatedAt, 102 &table.UpdatedAt,
97 &table.DeletedAt, 103 &table.DeletedAt,
98 &table.Version, 104 &table.Version,
  105 + &table.RowCount,
  106 + &table.Context,
99 ), 107 ),
100 fmt.Sprintf("UPDATE metadata.tables SET %s WHERE table_id=? and version=? RETURNING %s", updateFieldsSnippet, returningFieldsSnippet), 108 fmt.Sprintf("UPDATE metadata.tables SET %s WHERE table_id=? and version=? RETURNING %s", updateFieldsSnippet, returningFieldsSnippet),
101 table.TableType, 109 table.TableType,
@@ -109,8 +117,10 @@ func (repository *TableRepository) Save(table *domain.Table) (*domain.Table, err @@ -109,8 +117,10 @@ func (repository *TableRepository) Save(table *domain.Table) (*domain.Table, err
109 table.CreatedAt, 117 table.CreatedAt,
110 table.UpdatedAt, 118 table.UpdatedAt,
111 table.Version, 119 table.Version,
112 - oldVersion, 120 + table.RowCount,
  121 + table.Context,
113 table.Identify(), 122 table.Identify(),
  123 + oldVersion,
114 ); err != nil { 124 ); err != nil {
115 return table, err 125 return table, err
116 } 126 }
@@ -130,7 +140,11 @@ func (repository *TableRepository) FindOne(queryOptions map[string]interface{}) @@ -130,7 +140,11 @@ func (repository *TableRepository) FindOne(queryOptions map[string]interface{})
130 tx := repository.transactionContext.PgTx 140 tx := repository.transactionContext.PgTx
131 tableModel := new(models.Table) 141 tableModel := new(models.Table)
132 query := sqlbuilder.BuildQuery(tx.Model(tableModel), queryOptions) 142 query := sqlbuilder.BuildQuery(tx.Model(tableModel), queryOptions)
  143 + WhereContext(query, queryOptions)
133 query.SetWhereByQueryOption("table_id = ?", "tableId") 144 query.SetWhereByQueryOption("table_id = ?", "tableId")
  145 + query.SetWhereByQueryOption(`table_type = ?`, "tableType")
  146 + query.SetWhereByQueryOption("name = ?", "tableName")
  147 + query.SetWhereByQueryOption("parent_id = ?", "parentId")
134 if err := query.First(); err != nil { 148 if err := query.First(); err != nil {
135 if err.Error() == "pg: no rows in result set" { 149 if err.Error() == "pg: no rows in result set" {
136 return nil, fmt.Errorf("没有此资源") 150 return nil, fmt.Errorf("没有此资源")
@@ -149,7 +163,15 @@ func (repository *TableRepository) Find(queryOptions map[string]interface{}) (in @@ -149,7 +163,15 @@ func (repository *TableRepository) Find(queryOptions map[string]interface{}) (in
149 var tableModels []*models.Table 163 var tableModels []*models.Table
150 tables := make([]*domain.Table, 0) 164 tables := make([]*domain.Table, 0)
151 query := sqlbuilder.BuildQuery(tx.Model(&tableModels), queryOptions) 165 query := sqlbuilder.BuildQuery(tx.Model(&tableModels), queryOptions)
152 - query.SetOffsetAndLimit(20) 166 + WhereContext(query, queryOptions)
  167 + query.SetWhereByQueryOption(fmt.Sprintf("name like '%%%v%%'", queryOptions["name"]), "name")
  168 +
  169 + query.SetWhereByQueryOption("parent_id = ?", "parentId")
  170 + if v, ok := queryOptions["tableTypes"]; ok && len(v.([]string)) > 0 {
  171 + query.Where(`table_type in (?)`, pg.In(v.([]string)))
  172 + }
  173 +
  174 + //query.SetOffsetAndLimit(20)
153 query.SetOrderDirect("table_id", "DESC") 175 query.SetOrderDirect("table_id", "DESC")
154 if count, err := query.SelectAndCount(); err != nil { 176 if count, err := query.SelectAndCount(); err != nil {
155 return 0, tables, err 177 return 0, tables, err
  1 +package starrocks
  2 +
  3 +import (
  4 + "database/sql"
  5 + "gitlab.fjmaimaimai.com/allied-creation/character-library-metadata-bastion/pkg/domain"
  6 +)
  7 +
  8 +func Query(params QueryOptions, queryFunc func(params QueryOptions) (*sql.Rows, error)) (*domain.DataTable, error) {
  9 + rows, err := queryFunc(params)
  10 + if err != nil {
  11 + return nil, err
  12 + }
  13 + dataTable := &domain.DataTable{}
  14 + dataTable.Data, _ = ScanRows(rows)
  15 + return nil, nil
  16 +}
  17 +
  18 +type QueryOptions struct {
  19 + TableName string
  20 + Select []*domain.Field
  21 + Where []string
  22 + Order []string
  23 + Offset int
  24 + Limit int
  25 +}
  26 +
  27 +type Condition struct {
  28 + Field *domain.Field
  29 +}
  1 +package starrocks
  2 +
  3 +import (
  4 + "database/sql"
  5 + "gitlab.fjmaimaimai.com/allied-creation/character-library-metadata-bastion/pkg/infrastructure/utils"
  6 + "gitlab.fjmaimaimai.com/allied-creation/character-library-metadata-bastion/pkg/log"
  7 + "reflect"
  8 +)
  9 +
  10 +func ScanRows(rows *sql.Rows) ([][]string, error) {
  11 + defer rows.Close()
  12 + var results [][]string
  13 + cols, err := rows.Columns()
  14 + if err != nil {
  15 + log.Logger.Error(err.Error())
  16 + return nil, err
  17 + }
  18 + for rows.Next() {
  19 + var row = make([]interface{}, 0)
  20 + generic := reflect.TypeOf(row).Elem()
  21 +
  22 + for _ = range cols {
  23 + row = append(row, reflect.New(generic).Interface())
  24 + }
  25 + err := rows.Scan(row...)
  26 + if err != nil {
  27 + log.Logger.Error(err.Error())
  28 + return nil, err
  29 + }
  30 + var rowStrings = make([]string, len(cols))
  31 + for i := range row {
  32 + rowStrings[i] = utils.AssertString(*(row[i].(*interface{})))
  33 + }
  34 + results = append(results, rowStrings)
  35 + }
  36 + return results, nil
  37 +}
  1 +package starrocks
  2 +
  3 +import (
  4 + "github.com/linmadan/egglib-go/utils/json"
  5 + "gitlab.fjmaimaimai.com/allied-creation/character-library-metadata-bastion/pkg/constant"
  6 + "gitlab.fjmaimaimai.com/allied-creation/character-library-metadata-bastion/pkg/infrastructure/utils"
  7 + "gitlab.fjmaimaimai.com/allied-creation/character-library-metadata-bastion/pkg/log"
  8 + "gorm.io/driver/mysql"
  9 + "gorm.io/gorm"
  10 + "reflect"
  11 +)
  12 +
  13 +var DB *gorm.DB
  14 +
  15 +func Init() error {
  16 + var err error
  17 + DB, err = gorm.Open(mysql.Open(constant.STARROCKS_MYSQL_DATA_SOURCE), &gorm.Config{})
  18 +
  19 + //Test1()
  20 + return err
  21 +}
  22 +
  23 +func Test() {
  24 + //var result [][]interface{}
  25 + //query := DB.Raw("select * from sales_performance_area_belongs").Select(&result)
  26 + //if query.Error != nil {
  27 + // log.Logger.Error(query.Error.Error())
  28 + //}
  29 + //if len(result) > 0 {
  30 + //
  31 + //}
  32 +
  33 + //rows, err := DB.Raw("select * from sales_performance_area_belongs").Rows()
  34 +
  35 + rows, err := DB.Table("sales_performance_area_categorys").Rows()
  36 + if err != nil {
  37 + log.Logger.Error(err.Error())
  38 + }
  39 + defer rows.Close()
  40 +
  41 + cols, err := rows.Columns()
  42 + if err != nil {
  43 + log.Logger.Error(err.Error())
  44 + }
  45 + var results [][]interface{}
  46 + for rows.Next() {
  47 + // 1
  48 + var row = make([]interface{}, 0)
  49 + generic := reflect.TypeOf(row).Elem()
  50 +
  51 + for _ = range cols {
  52 + row = append(row, reflect.New(generic).Interface())
  53 + }
  54 + err := rows.Scan(row...)
  55 + if err != nil {
  56 + log.Logger.Error(err.Error())
  57 + }
  58 + for i := range row {
  59 + row[i] = utils.AssertString(*(row[i].(*interface{})))
  60 + }
  61 + results = append(results, row)
  62 +
  63 + // 2
  64 + //var row = make([]interface{}, len(cols))
  65 + //var rowp = make([]interface{}, len(cols))
  66 + //
  67 + //for i := range cols {
  68 + // rowp[i] = &row[i]
  69 + //}
  70 + //err := rows.Scan(rowp...)
  71 + //if err != nil {
  72 + // log.Logger.Error(err.Error())
  73 + //}
  74 + //results = append(results, rowp)
  75 +
  76 + // 3
  77 + //var row = make([]interface{}, 0)
  78 + //var value string
  79 + //generic := reflect.TypeOf(value)
  80 + //
  81 + //for _ = range cols {
  82 + // row = append(row, reflect.New(generic).Interface())
  83 + //}
  84 + //err := rows.Scan(row...)
  85 + //if err != nil {
  86 + // log.Logger.Error(err.Error())
  87 + //}
  88 + //results = append(results, row)
  89 + }
  90 + log.Logger.Debug(json.MarshalToString(results))
  91 +}
  92 +
  93 +func Test1() {
  94 + rows, err := DB.Table("sales_performance_area_categorys").Rows()
  95 + if err != nil {
  96 + log.Logger.Error(err.Error())
  97 + }
  98 + data, _ := ScanRows(rows)
  99 + log.Logger.Debug(json.MarshalToString(data))
  100 +}