table_row_data_edit_service.go 5.4 KB
package domainService

import (
	"fmt"
	pgTransaction "github.com/linmadan/egglib-go/transaction/pg"
	"gitlab.fjmaimaimai.com/allied-creation/character-library-metadata-bastion/pkg/domain"
	"gitlab.fjmaimaimai.com/allied-creation/character-library-metadata-bastion/pkg/infrastructure/repository"
	"gitlab.fjmaimaimai.com/allied-creation/character-library-metadata-bastion/pkg/infrastructure/starrocks"
	"gitlab.fjmaimaimai.com/allied-creation/character-library-metadata-bastion/pkg/log"
)

type TableEditDataService struct {
	transactionContext *pgTransaction.TransactionContext
}

func NewTableEditDataService(transactionContext *pgTransaction.TransactionContext) (*TableEditDataService, error) {
	if transactionContext == nil {
		return nil, fmt.Errorf("transactionContext参数不能为nil")
	} else {
		return &TableEditDataService{
			transactionContext: transactionContext,
		}, nil
	}
}

// RowEdit 行数据编辑
func (ptr *TableEditDataService) RowEdit(ctx *domain.Context, request domain.EditDataRequest) (interface{}, error) {
	tableRepository, _ := repository.NewTableRepository(ptr.transactionContext)
	var table *domain.Table = request.Table
	var err error
	if table == nil {
		table, err = tableRepository.FindOne(map[string]interface{}{"tableId": request.TableId})
		if err != nil {
			return nil, err
		}
	}

	if table.TableType != domain.SideTable.ToString() && !request.IgnoreTableType {
		return nil, fmt.Errorf("副表才允许编辑数据")
	}
	defer func() {
		AsyncEvent(domain.NewEventTable(ctx, domain.TableDataEditEvent).WithTable(table))
	}()

	for _, l := range request.AddList {
		if addErr := ptr.add(ctx, table, l, request.Where); addErr != nil {
			log.Logger.Error(addErr.Error())
		}
	}
	if len(request.RemoveList) > 0 {
		// 日志
		if err = FastLog(ptr.transactionContext, domain.CommonLog, table.TableId, &RowRemoveLog{
			LogEntry:       domain.NewLogEntry(table.Name, table.TableType, domain.RowEdit, ctx),
			DeleteRowCount: len(request.RemoveList),
		}); err != nil {
			return nil, err
		}
		if err = starrocks.BatchDelete(starrocks.DB, table.SQLName, request.RemoveList); err != nil {
			return nil, err
		}
	}
	for _, l := range request.UpdateList {
		if e := ptr.update(ctx, table, l, request.Where); e != nil {
			log.Logger.Error(e.Error())
		}
	}

	return nil, nil
}

// BatchAdd 行数据批量添加
func (ptr *TableEditDataService) BatchAdd(ctx *domain.Context, request domain.EditDataRequest) (interface{}, error) {
	tableRepository, _ := repository.NewTableRepository(ptr.transactionContext)
	var table *domain.Table = request.Table
	var err error
	if table == nil {
		table, err = tableRepository.FindOne(map[string]interface{}{"tableId": request.TableId})
		if err != nil {
			return nil, err
		}
	}

	defer func() {
		AsyncEvent(domain.NewEventTable(ctx, domain.TableDataEditEvent).WithTable(table))
	}()

	for _, l := range request.AddList {
		// 添加记录
		if err = starrocks.Insert(starrocks.DB, table.SQLName, l.FieldValues); err != nil {
			log.Logger.Error(fmt.Sprintf("添加记录错误:%v", err.Error()))
		}
	}
	return nil, nil
}

func (ptr *TableEditDataService) add(ctx *domain.Context, table *domain.Table, list *domain.FieldValues, where domain.Where) error {
	var err error

	// 日志
	if err = FastLog(ptr.transactionContext, domain.CommonLog, table.TableId, &RowAddLog{
		LogEntry: domain.NewLogEntry(table.Name, table.TableType, domain.RowEdit, ctx),
	}); err != nil {
		return err
	}

	// 添加记录
	if err = starrocks.Insert(starrocks.DB, table.SQLName, list.FieldValues); err != nil {
		return err
	}
	return nil
}

func (ptr *TableEditDataService) remove(ctx *domain.Context, table *domain.Table, list *domain.FieldValues, where domain.Where) error {
	var err error

	// 删除记录
	if err = starrocks.Delete(starrocks.DB, table.SQLName, list.FieldValues); err != nil {
		return err
	}
	return nil
}

func (ptr *TableEditDataService) update(ctx *domain.Context, table *domain.Table, list *domain.FieldValues, where domain.Where) error {
	var err error

	// 日志
	if err = FastLog(ptr.transactionContext, domain.CommonLog, table.TableId, &RowUpdateLog{
		LogEntry:   domain.NewLogEntry(table.Name, table.TableType, domain.RowEdit, ctx),
		FieldValue: list.FieldValues,
		Number:     list.Number,
	}); err != nil {
		return err
	}

	// 更新记录
	if err = starrocks.Update(starrocks.DB, table.SQLName, list.FieldValues); err != nil {
		return err
	}
	return 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
}