作者 yangfu

业务表同步

... ... @@ -9,7 +9,7 @@ HTTP_PORT = 8081
ENABLE_KAFKA_LOG11 = true
HTTPS_PORT = 8143
ALLIED_CREATION_USER_HOST = http://allied-creation-user-dev.fjmaimaimai.com
# AUTH_SERVER_HOST = http://127.0.0.1:8081
# AUTH_SERVER_HOST = http://127.0.0.1:8081 192.168.100.9:8303
BYTE_CORE_HOST = http://47.97.5.102:8303
METADATA_BASTION_HOST = http://106.75.231.90:9999
... ...
package command
type GenerateBusinessTableRequest struct {
TableName string `json:"mainTableName"`
TableFullName string `json:"tableFullName"`
Fields []FieldSchemas `json:"fields"`
}
... ...
package command
type UpdateBusinessTableRequest struct {
TableName string `json:"tableRemarkName"`
TableFullName string `json:"tableFullName"`
Fields []FieldSchemas `json:"fields"`
ShowTableNameBy int `json:"showTableNameBy"` // 展示表名 0:原表名 1:表名备注
ShowTableFieldNameBy int `json:"showTableFieldNameBy"` // 字段名在字库的显示 0:字段原名 1:字段备注
}
type FieldSchemas struct {
FieldEnName string `json:"fieldEnName"`
FieldType string `json:"fieldType"`
FieldZhName string `json:"fieldZhName"`
}
... ...
package query
type ShowBusinessDatabasesRequest struct {
TableFullName string `json:"tableFullName"`
PageNumber int `json:"pageNumber"`
PageSize int `json:"pageSize"`
}
... ...
package query
type ShowTableDataRequest struct {
TableFullName string `json:"tableFullName"`
PageNumber int `json:"pageNumber"`
PageSize int `json:"pageSize"`
}
... ...
package query
type ShowTablesRequest struct {
DatabaseEnName string `json:"databaseEnName"`
DatabaseType string `json:"databaseType"`
}
... ...
package service
import (
"fmt"
"github.com/linmadan/egglib-go/core/application"
"gitlab.fjmaimaimai.com/allied-creation/character-library-metadata-bastion/pkg/application/factory"
"gitlab.fjmaimaimai.com/allied-creation/character-library-metadata-bastion/pkg/application/table/command"
"gitlab.fjmaimaimai.com/allied-creation/character-library-metadata-bastion/pkg/application/table/query"
"gitlab.fjmaimaimai.com/allied-creation/character-library-metadata-bastion/pkg/constant"
"gitlab.fjmaimaimai.com/allied-creation/character-library-metadata-bastion/pkg/domain"
"gitlab.fjmaimaimai.com/allied-creation/character-library-metadata-bastion/pkg/infrastructure/api/bytelib"
"gitlab.fjmaimaimai.com/allied-creation/character-library-metadata-bastion/pkg/infrastructure/domainService"
)
func (tableService *TableService) ShowBusinessDatabases(ctx *domain.Context, cmd *query.ShowBusinessDatabasesRequest) (interface{}, error) {
byteCoreService := domainService.ByteCoreService{}
response, err := byteCoreService.ShowBusinessDatabases(bytelib.ReqShowBusinessDatabases{})
return response, err
}
func (tableService *TableService) ShowBusinessTables(ctx *domain.Context, cmd *query.ShowTablesRequest) (interface{}, error) {
byteCoreService := domainService.ByteCoreService{}
response, err := byteCoreService.ShowBusinessTables(bytelib.ReqShowBusinessTables{
DatabaseEnName: cmd.DatabaseEnName,
DatabaseType: cmd.DatabaseType,
})
result := make([]map[string]interface{}, 0)
for _, t := range response.TableFullNames {
result = append(result, map[string]interface{}{
"name": t,
})
}
return map[string]interface{}{
"list": result,
}, err
}
func (tableService *TableService) QueryBusinessTable(ctx *domain.Context, cmd *query.ShowTableDataRequest) (interface{}, error) {
byteCoreService := domainService.ByteCoreService{}
response, err := byteCoreService.QueryBusinessTable(bytelib.ReqQueryBusinessTable{
TableFullName: cmd.TableFullName,
PageNumber: cmd.PageNumber,
PageSize: cmd.PageSize,
})
transactionContext, err := factory.CreateTransactionContext(nil)
if err != nil {
return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
}
tableRepository, _, _ := factory.FastPgTable(transactionContext, 0)
table, _ := tableRepository.FindBusinessTable(constant.COMPANY_SU_TIAN_XIA, response.TableFullName)
if table != nil {
response.TableRemarkName = table.Name
response.ShowTableNameBy = table.TableInfo.BusinessTableShowTableNameBy
response.ShowTableFieldNameBy = table.TableInfo.BusinessTableShowTableFieldNameBy
for i, f := range response.FieldSchemas {
for j, jf := range table.DataFields {
if jf.SQLName == f.FieldEnName {
response.FieldSchemas[i].FieldZhName = table.DataFields[j].Name
}
}
}
}
return response, err
}
func (tableService *TableService) UpdateBusinessTable(ctx *domain.Context, cmd *command.UpdateBusinessTableRequest) (interface{}, error) {
transactionContext, err := factory.CreateTransactionContext(nil)
if err != nil {
return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
}
if err := transactionContext.StartTransaction(); err != nil {
return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
}
defer func() {
transactionContext.RollbackTransaction()
}()
var tableRepository domain.TableRepository
if value, err := factory.CreateTableRepository(map[string]interface{}{
"transactionContext": transactionContext,
}); err != nil {
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
} else {
tableRepository = value
}
ctx.CompanyId = int(constant.COMPANY_SU_TIAN_XIA)
var (
fields = make([]*domain.Field, 0)
mainTable *domain.Table
)
for i, f := range cmd.Fields {
fields = append(fields, &domain.Field{
Index: i + 1,
Name: f.FieldZhName,
SQLName: f.FieldEnName,
SQLType: f.FieldType, //TODO:类型转换
Flag: domain.MainTableField,
})
}
table, _ := tableRepository.FindBusinessTable(constant.COMPANY_SU_TIAN_XIA, cmd.TableFullName)
if table == nil {
mainTable = domainService.NewTable(domain.BusinessTable, cmd.TableName, fields, 0).
WithContext(ctx).
WithPrefix(domain.BusinessTable.ToString())
mainTable.SQLName = cmd.TableFullName
mainTable.DataFields = fields
mainTable.TableInfo.BusinessTableShowTableNameBy = cmd.ShowTableNameBy
mainTable.TableInfo.BusinessTableShowTableFieldNameBy = cmd.ShowTableFieldNameBy
mainTable.TableInfo.TableFrom = 1
if mainTable, err = tableRepository.Save(mainTable); err != nil {
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
}
} else {
table.Name = cmd.TableName
table.DataFields = fields
table.TableInfo.BusinessTableShowTableNameBy = cmd.ShowTableNameBy
table.TableInfo.BusinessTableShowTableFieldNameBy = cmd.ShowTableFieldNameBy
if table, err = tableRepository.Save(table); err != nil {
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
}
}
if err = transactionContext.CommitTransaction(); err != nil {
return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
}
return mainTable, nil
}
func (tableService *TableService) GenerateBusinessTable(ctx *domain.Context, cmd *command.GenerateBusinessTableRequest) (interface{}, error) {
transactionContext, err := factory.CreateTransactionContext(nil)
if err != nil {
return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
}
if err := transactionContext.StartTransaction(); err != nil {
return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
}
defer func() {
transactionContext.RollbackTransaction()
}()
var tableRepository domain.TableRepository
if value, err := factory.CreateTableRepository(map[string]interface{}{
"transactionContext": transactionContext,
}); err != nil {
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
} else {
tableRepository = value
}
ctx.CompanyId = int(constant.COMPANY_SU_TIAN_XIA)
duplicateTable, err := tableRepository.FindOne(map[string]interface{}{"context": ctx, "tableName": cmd.TableName,
"tableTypes": []string{string(domain.MainTable), string(domain.SubTable), string(domain.SideTable)}})
if err == nil && duplicateTable != nil {
return nil, fmt.Errorf("表名称重复")
}
var (
fields = make([]*domain.Field, 0)
mainTable *domain.Table
)
for i, f := range cmd.Fields {
fields = append(fields, &domain.Field{
Index: i + 1,
Name: f.FieldZhName,
SQLName: f.FieldEnName,
SQLType: f.FieldType, //TODO:类型转换
Flag: domain.MainTableField,
})
}
mainTable = domainService.NewTable(domain.MainTable, cmd.TableName, fields, 0).
WithContext(ctx).
WithPrefix(domain.MainTable.ToString()).ApplyDefaultModule()
mainTable.SQLName = cmd.TableFullName
mainTable.TableInfo.TableFrom = 1
mainTable.DataFields = fields
if mainTable, err = tableRepository.Save(mainTable); err != nil {
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
}
if err = transactionContext.CommitTransaction(); err != nil {
return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
}
return nil, nil
}
... ...
... ... @@ -31,6 +31,8 @@ var BLACK_LIST_USER int64
var BLACK_LIST_COMPANY int64
var WHITE_LIST_USERS []int
var COMPANY_SU_TIAN_XIA int64 = 1689162984523304960
func init() {
LOG_LEVEL = Configurator.DefaultString("LOG_LEVEL", LOG_LEVEL)
MMM_BYTE_BANK_HOST = Configurator.DefaultString("MMM_BYTE_BANK_HOST", MMM_BYTE_BANK_HOST)
... ... @@ -47,6 +49,8 @@ func init() {
BLACK_LIST_COMPANY = Configurator.DefaultInt64("BLACK_LIST_COMPANY", BLACK_LIST_COMPANY)
DIGITAL_SERVER_HOST = Configurator.DefaultString("DIGITAL_SERVER_HOST", DIGITAL_SERVER_HOST)
COMPANY_SU_TIAN_XIA = Configurator.DefaultInt64("COMPANY_SU_TIAN_XIA", COMPANY_SU_TIAN_XIA)
whiteListUsers := strings.Split(Configurator.DefaultString("WHITE_LIST_USERS", ""), ",")
for _, userId := range whiteListUsers {
v, _ := strconv.Atoi(userId)
... ...
... ... @@ -104,6 +104,7 @@ var (
SubTable TableType = "SubTable"
ExcelTable TableType = "ExcelTable"
TemporaryTable TableType = "TemporaryTable"
BusinessTable TableType = "BusinessTable" // 业务表
)
var (
... ...
... ... @@ -55,6 +55,7 @@ type TableRepository interface {
Save(table *Table) (*Table, error)
Remove(table *Table) (*Table, error)
FindOne(queryOptions map[string]interface{}) (*Table, error)
FindBusinessTable(companyId int64, sqlName string) (*Table, error)
Find(queryOptions map[string]interface{}) (int64, []*Table, error)
}
... ...
... ... @@ -5,6 +5,10 @@ type TableInfo struct {
ApplyOnModule int `json:"module"`
// 依赖关联的表
DependencyTables []int `json:"dependencyTables"`
// 表来源
TableFrom int `json:"tableFrom"` // 0:字库导入 1:字库实表同步
BusinessTableShowTableNameBy int `json:"showTableNameBy"` // 【业务表】展示表名 0:原表名 1:表名备注
BusinessTableShowTableFieldNameBy int `json:"showTableFieldNameBy"` // 【业务表】字段名在字库的显示 0:字段原名 1:字段备注
}
func (t *TableInfo) SetApplyOn(applyOn int) *TableInfo {
... ...
... ... @@ -278,3 +278,36 @@ func (gateway ApiByteLib) FormulasClear(param domain.ReqFormulasClear) (*domain.
var response = domain.DataFormulasClear{}
return &response, nil
}
func (gateway ApiByteLib) ShowBusinessDatabases(request ReqShowBusinessDatabases) (*DataShowBusinessDatabases, error) {
url := gateway.Host() + "/databases/show-business-databases"
method := "post"
var data DataShowBusinessDatabases
err := gateway.FastDoRequest(url, method, request, &data)
if err != nil {
return nil, err
}
return &data, nil
}
func (gateway ApiByteLib) ShowBusinessTables(request ReqShowBusinessTables) (*DataShowBusinessTables, error) {
url := gateway.Host() + "/databases/show-tables"
method := "post"
var data DataShowBusinessTables
err := gateway.FastDoRequest(url, method, request, &data)
if err != nil {
return nil, err
}
return &data, nil
}
func (gateway ApiByteLib) QueryBusinessTable(request ReqQueryBusinessTable) (*DataQueryBusinessTable, error) {
url := gateway.Host() + "/databases/query-table"
method := "post"
var data DataQueryBusinessTable
err := gateway.FastDoRequest(url, method, request, &data)
if err != nil {
return nil, err
}
return &data, nil
}
... ...
... ... @@ -332,3 +332,49 @@ type (
FormulasClearResponse struct {
}
)
type (
ReqShowBusinessDatabases struct {
}
DataShowBusinessDatabases struct {
BusinessDatabases []struct {
DatabaseDescription string `json:"databaseDescription"`
DatabaseEnName string `json:"databaseEnName"`
DatabaseType string `json:"databaseType"`
DatabaseZhName string `json:"databaseZhName"`
} `json:"businessDatabases"`
}
)
type (
ReqShowBusinessTables struct {
DatabaseEnName string `json:"databaseEnName"`
DatabaseType string `json:"databaseType"`
}
DataShowBusinessTables struct {
TableFullNames []string `json:"tableFullNames"`
}
)
type (
ReqQueryBusinessTable struct {
TableFullName string `json:"tableFullName"`
PageNumber int `json:"pageNumber"`
PageSize int `json:"pageSize"`
}
DataQueryBusinessTable struct {
DataCount int `json:"dataCount"`
FieldSchemas []struct {
FieldDescription string `json:"fieldDescription"`
FieldEnName string `json:"fieldEnName"`
FieldType string `json:"fieldType"`
FieldZhName string `json:"fieldZhName"`
IsAllowNull bool `json:"isAllowNull"`
} `json:"fieldSchemas"`
ShowData [][]string `json:"showData"`
TableFullName string `json:"tableFullName"`
TableRemarkName string `json:"tableRemarkName"`
ShowTableNameBy int `json:"showTableNameBy"` // 展示表名 0:原表名 1:表名备注
ShowTableFieldNameBy int `json:"showTableFieldNameBy"` // 字段名在字库的显示 0:字段原名 1:字段备注
}
)
... ...
... ... @@ -170,6 +170,21 @@ func (ptr *ByteCoreService) FormulasClear(param domain.ReqFormulasClear) (*domai
return apiByteLib.FormulasClear(param)
}
func (ptr *ByteCoreService) ShowBusinessDatabases(param bytelib.ReqShowBusinessDatabases) (*bytelib.DataShowBusinessDatabases, error) {
apiByteLib := bytelib.NewApiByteLib(constant.BYTE_CORE_HOST)
return apiByteLib.ShowBusinessDatabases(param)
}
func (ptr *ByteCoreService) ShowBusinessTables(param bytelib.ReqShowBusinessTables) (*bytelib.DataShowBusinessTables, error) {
apiByteLib := bytelib.NewApiByteLib(constant.BYTE_CORE_HOST)
return apiByteLib.ShowBusinessTables(param)
}
func (ptr *ByteCoreService) QueryBusinessTable(param bytelib.ReqQueryBusinessTable) (*bytelib.DataQueryBusinessTable, error) {
apiByteLib := bytelib.NewApiByteLib(constant.BYTE_CORE_HOST)
return apiByteLib.QueryBusinessTable(param)
}
func (ptr *ByteCoreService) ExcelExprCalcPersistence(expr *domain.FieldFormulaExpr, param domain.ReqFormulasGenerate, persistence bool) error {
if len(param.QueryComponents) == 0 {
return nil
... ...
... ... @@ -165,6 +165,28 @@ func (repository *TableRepository) FindOne(queryOptions map[string]interface{})
return transform.TransformToTableDomainModelFromPgModels(tableModel)
}
}
func (repository *TableRepository) FindBusinessTable(companyId int64, sqlName string) (*domain.Table, error) {
tx := repository.transactionContext.DB()
tableModel := new(models.Table)
query := sqlbuilder.BuildQuery(tx.Model(tableModel), nil)
query.Where("context->'companyId'='?'", companyId)
query.Where("sql_name = ?", sqlName)
query.Where("table_type = ?", domain.BusinessTable.ToString())
if err := query.First(); err != nil {
if err.Error() == "pg: no rows in result set" {
return nil, domain.ErrorNotFound
} else {
return nil, err
}
}
if tableModel.TableId == 0 {
return nil, nil
} else {
return transform.TransformToTableDomainModelFromPgModels(tableModel)
}
}
func (repository *TableRepository) Find(queryOptions map[string]interface{}) (int64, []*domain.Table, error) {
tx := repository.transactionContext.DB()
var tableModels []*models.Table
... ... @@ -177,6 +199,9 @@ func (repository *TableRepository) Find(queryOptions map[string]interface{}) (in
if v, ok := queryOptions["tableIds"]; ok && len(v.([]int)) > 0 {
query.Where(`table_id in (?)`, pg.In(v.([]int)))
}
if v, ok := queryOptions["sqlNames"]; ok && len(v.([]string)) > 0 {
query.Where(`sql_name in (?)`, pg.In(v.([]string)))
}
if v, ok := queryOptions["tableTypes"]; ok && len(v.([]string)) > 0 {
query.Where(`table_type in (?)`, pg.In(v.([]string)))
}
... ...
package controllers
import (
"gitlab.fjmaimaimai.com/allied-creation/character-library-metadata-bastion/pkg/application/table/command"
"gitlab.fjmaimaimai.com/allied-creation/character-library-metadata-bastion/pkg/application/table/query"
"gitlab.fjmaimaimai.com/allied-creation/character-library-metadata-bastion/pkg/application/table/service"
)
func (controller *TableController) ShowBusinessDatabases() {
tableService := service.NewTableService(nil)
cmd := &query.ShowBusinessDatabasesRequest{}
Must(controller.Unmarshal(cmd))
data, err := tableService.ShowBusinessDatabases(ParseContext(controller.BaseController), cmd)
controller.Response(data, err)
}
func (controller *TableController) ShowBusinessTables() {
tableService := service.NewTableService(nil)
cmd := &query.ShowTablesRequest{}
Must(controller.Unmarshal(cmd))
data, err := tableService.ShowBusinessTables(ParseContext(controller.BaseController), cmd)
controller.Response(data, err)
}
func (controller *TableController) QueryBusinessTable() {
tableService := service.NewTableService(nil)
cmd := &query.ShowTableDataRequest{}
Must(controller.Unmarshal(cmd))
data, err := tableService.QueryBusinessTable(ParseContext(controller.BaseController), cmd)
controller.Response(data, err)
}
func (controller *TableController) UpdateBusinessTable() {
tableService := service.NewTableService(nil)
cmd := &command.UpdateBusinessTableRequest{}
Must(controller.Unmarshal(cmd))
data, err := tableService.UpdateBusinessTable(ParseContext(controller.BaseController), cmd)
controller.Response(data, err)
}
func (controller *TableController) GenerateBusinessTable() {
tableService := service.NewTableService(nil)
cmd := &command.GenerateBusinessTableRequest{}
Must(controller.Unmarshal(cmd))
data, err := tableService.GenerateBusinessTable(ParseContext(controller.BaseController), cmd)
controller.Response(data, err)
}
... ...
... ... @@ -13,4 +13,10 @@ func init() {
web.Router("/api/app-table-file/append-data", &controllers.FileController{}, "Post:AppendDataAppTableFile")
web.Router("/api/app-table-file/list", &controllers.FileController{}, "Post:ListAppTableFile")
web.Router("/api/app-table-file/update", &controllers.FileController{}, "Post:UpdateAppTableFile")
web.Router("/api/business-table/show-business-databases", &controllers.TableController{}, "Post:ShowBusinessDatabases")
web.Router("/api/business-table/show-tables", &controllers.TableController{}, "Post:ShowBusinessTables")
web.Router("/api/business-table/query-table", &controllers.TableController{}, "Post:QueryBusinessTable")
web.Router("/api/business-table/update", &controllers.TableController{}, "Post:UpdateBusinessTable")
web.Router("/api/business-table/generate", &controllers.TableController{}, "Post:GenerateBusinessTable")
}
... ...