table_edit_data_service.go 3.2 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"
)

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)
	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("副表才允许编辑数据")
	}
	//if table.TableType == domain.SubTable.ToString(){
	//	mainTable, err := tableRepository.FindOne(map[string]interface{}{"tableId": request.TableId})
	//	if err != nil {
	//		return nil, err
	//	}
	//	table.SQLName = mainTable.SQLName
	//}

	for _, l := range request.AddList {
		ptr.add(ctx, table, l, request.Where)
	}
	for _, l := range request.RemoveList {
		ptr.remove(ctx, table, l, request.Where)
	}
	for _, l := range request.UpdateList {
		ptr.update(ctx, table, l, request.Where)
	}

	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 = FastLog(ptr.transactionContext, domain.CommonLog, table.TableId, &RowRemoveLog{
		LogEntry:   domain.NewLogEntry(table.Name, table.TableType, domain.RowEdit, ctx),
		FieldValue: list,
		Where:      where,
	}); err != nil {
		return err
	}

	// 删除记录
	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),
	}); err != nil {
		return err
	}

	// 更新记录
	if err = starrocks.Update(starrocks.DB, table.SQLName, list.FieldValues); err != nil {
		return err
	}
	return nil
}