row_edit.go 4.7 KB
package service

import (
	"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/dto"
	"gitlab.fjmaimaimai.com/allied-creation/character-library-metadata-bastion/pkg/domain"
	"gitlab.fjmaimaimai.com/allied-creation/character-library-metadata-bastion/pkg/infrastructure/domainService"
	"gitlab.fjmaimaimai.com/allied-creation/character-library-metadata-bastion/pkg/infrastructure/starrocks"
)

func (tableService *TableService) RowEdit(ctx *domain.Context, cmd *command.RowEditCommand) (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()
	}()
	if 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 struct{}{}, nil
}

func (tableService *TableService) RowEditV2(ctx *domain.Context, cmd *command.RowEditCommandV2) (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 table *domain.Table
	_, table, err = factory.FastPgTable(transactionContext, cmd.TableId)
	if err != nil {
		return nil, factory.FastError(err)
	}

	var options = starrocks.QueryOptions{
		TableName: table.SQLName,
		Select:    table.Fields(true),
		Where:     []starrocks.Condition{},
	}
	var dataTable *domain.DataTable
	var idList = cmd.IdList()

	if len(idList) > 0 {
		options.Where = append(options.Where, starrocks.Condition{
			Condition: domain.Condition{
				Field: domainService.PK(),
				In:    cmd.IdList(),
			},
		})
		options.SetCondition(cmd.Where.Conditions).SetDefaultOrder()
		options.SetOffsetLimit(0, 2000)
	} else {
		options.SetOffsetLimit(0, 1)
	}
	dataTable, err = factory.FastDataTable(options)
	if err != nil {
		return nil, factory.FastError(err)
	}

	editDataService, _ := factory.CreateTableEditDataService(transactionContext)
	_, err = editDataService.RowEdit(ctx, domain.EditDataRequest{
		TableId:    cmd.TableId,
		Table:      table,
		Where:      cmd.Where,
		UpdateList: MapArrayToFieldValues(cmd.UpdateList, table, dataTable, true),
		AddList:    MapArrayToFieldValues(cmd.AddList, table, dataTable, false),
		RemoveList: MapArrayToFieldValues(cmd.RemoveList, table, dataTable, true),
	})
	if 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 struct{}{}, nil
}

func MapArrayToFieldValues(list []map[string]string, table *domain.Table, dataTable *domain.DataTable, mustMatch bool) []*domain.FieldValues {
	var result = make([]*domain.FieldValues, 0)
	history := dto.ToFieldDataByPK(table, dataTable)
	mapField := domain.Fields(table.Fields(true)).ToMapBySqlName()
	for _, m := range list {
		var fieldValues = &domain.FieldValues{
			FieldValues: make([]*domain.FieldValue, 0),
		}
		matchItem, ok := history[m[domain.DefaultPkField]]
		if mustMatch {
			if !ok {
				continue
			}
		}
		if _, ok := m[domain.DefaultPkField]; !ok {
			m[domain.DefaultPkField] = ""
		}
		for key, value := range m {
			field, ok := mapField[key]
			if !ok || field.Flag == domain.ManualField {
				continue
			}
			fieldValue := &domain.FieldValue{
				Field: field,
				Value: value,
			}
			if mustMatch {
				if oldValue, ok := matchItem[key]; ok {
					fieldValue.OldValue = oldValue
				}
			}
			fieldValues.FieldValues = append(fieldValues.FieldValues, fieldValue)
		}
		result = append(result, fieldValues)
	}
	return result
}