table_row_data_edit_service.go 3.5 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
	}
}

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() {
		return nil, fmt.Errorf("副表才允许编辑数据")
	}

	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
		}
		for _, l := range request.RemoveList {
			if e := ptr.remove(ctx, table, l, request.Where); e != nil {
				log.Logger.Error(e.Error())
			}
		}
	}
	for _, l := range request.UpdateList {
		if e := ptr.update(ctx, table, l, request.Where); e != nil {
			log.Logger.Error(e.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
}