table_business.go 7.2 KB
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
}