field_optional_values.go 3.7 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/domain"
	"gitlab.fjmaimaimai.com/allied-creation/character-library-metadata-bastion/pkg/infrastructure/pg"
	"gitlab.fjmaimaimai.com/allied-creation/character-library-metadata-bastion/pkg/infrastructure/starrocks"
	"gorm.io/gorm"
)

func (tableService *TableService) FieldOptionalValues(ctx *domain.Context, cmd *command.FieldOptionalValuesCommand) (interface{}, error) {
	if err := cmd.ValidateCommand(); err != nil {
		return nil, application.ThrowError(application.ARG_ERROR, err.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 dataTable *domain.DataTable
	var empty = map[string]interface{}{
		"values": []string{},
		"total":  0,
	}
	tableRepository, _, _ := factory.FastPgTable(transactionContext, 0)
	var table *domain.Table
	var db *gorm.DB
	switch cmd.ObjectType {
	case domain.ObjectFile:
		byteCore, _ := factory.CreateByteCoreService(transactionContext)
		response, err := byteCore.FieldOptionalValues(domain.ReqFieldOptionalValues{
			FileId: cmd.ObjectId,
			Field:  &cmd.Field,
			Match:  cmd.Match,
			Where: domain.Where{
				PageNumber: cmd.PageNumber,
				PageSize:   cmd.PageSize,
			},
		})
		if err != nil {
			return nil, factory.FastError(err)
		}
		return map[string]interface{}{
			"values": response.Values,
			"total":  response.Total,
		}, nil
	case domain.ObjectMetaTable:
		table, err = tableRepository.FindOne(map[string]interface{}{"ctx": ctx, "tableId": cmd.ObjectId})
		if err != nil {
			return nil, factory.FastError(err)
		}
		db = starrocks.DB
	case domain.ObjectDBTable:
		table = domain.DBTables[cmd.ObjectId]
		db = pg.GormDB
	}

	field, ok := table.MatchField(&cmd.Field)
	if !ok {
		return nil, factory.FastError(fmt.Errorf("列:%v 不存在", cmd.Field.Name))
	}
	if table.TableType == domain.SubTable.ToString() && field.Flag == domain.ManualField {
		return empty, nil
	}
	match := cmd.Match
	if !domain.SQLType(field.SQLType).IsString() {
		match = ""
	}
	options := &starrocks.QueryOptions{
		Table:     table,
		TableName: table.SQLName,
		Select:    []*domain.Field{field},
		Where: []starrocks.Condition{
			{
				Condition: domain.Condition{
					Field: field,
					Like:  match,
					Order: "ASC",
				},
				Distinct: true,
			},
		},
	}
	if cmd.Where != nil && len(cmd.Where.Conditions) > 0 {
		for _, c := range cmd.Where.Conditions {
			options.Where = append(options.Where, starrocks.Condition{
				Condition: c,
			})
		}
	}
	options.AdditionOptionsByTable(table)
	options.SetOffsetLimit(cmd.PageNumber, cmd.PageSize)

	dataTable, err = starrocks.Query(*options, starrocks.WrapQueryFuncWithDB(db))
	if err != nil {
		return nil, factory.FastError(err)
	}
	dataTable.Total, err = starrocks.WrapQueryCountWithDB(*options, db)()
	if err != nil {
		return nil, factory.FastError(err)
	}

	if err := transactionContext.CommitTransaction(); err != nil {
		return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
	}
	values := dataTable.OptionalValue(cmd.Match, field.SQLType)
	return map[string]interface{}{
		"values": values,
		"total":  len(values),
	}, nil
}