get_table_detail_logic.go 1.9 KB
package table

import (
	"context"
	"gitlab.fjmaimaimai.com/allied-creation/sumifcc-bchart/cmd/chart-server/interanl/pkg/gateway/bytelib"
	"gitlab.fjmaimaimai.com/allied-creation/sumifcc-bchart/pkg/xerr"

	"gitlab.fjmaimaimai.com/allied-creation/sumifcc-bchart/cmd/chart-server/api/internal/svc"
	"gitlab.fjmaimaimai.com/allied-creation/sumifcc-bchart/cmd/chart-server/api/internal/types"

	"github.com/zeromicro/go-zero/core/logx"
)

type GetTableDetailLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

func NewGetTableDetailLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GetTableDetailLogic {
	return &GetTableDetailLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

func (l *GetTableDetailLogic) GetTableDetail(req *types.GetTableDetailRequest) (resp interface{}, err error) {
	//获取本地数据
	local, err := l.GetLocal(req.TableId)
	if err == nil {
		return local, nil
	}
	response, err := l.svcCtx.ByteMetadataService.TableInfo(l.ctx, &bytelib.TableInfoRequest{TableId: req.TableId, Token: req.Token})
	if err != nil {
		logx.Error(err)
		return resp, xerr.NewErr(err)
	}
	response.Fields = removeIdField(response.Fields)
	resp = response
	return
}

func (l *GetTableDetailLogic) GetLocal(tableId int) (interface{}, error) {
	conn := l.svcCtx.DefaultDBConn()
	//查询表数据
	objectTable, err := l.svcCtx.ObjectTableRepository.FindOneByTableId(l.ctx, conn, tableId)
	if err != nil || objectTable.Id <= 0 {
		return nil, xerr.NewErrMsg("表不存在")
	}
	//查询表字段
	objectField, err := l.svcCtx.ObjectFieldRepository.FindOne(l.ctx, conn, int64(tableId))
	if err != nil || objectField.Id <= 0 {
		return nil, xerr.NewErrMsg("表字段不存在")
	}
	resp := bytelib.TableInfoResponse{
		TableId:   objectTable.TableId,
		TableType: objectTable.TableType,
		Name:      objectTable.Name,
		ParentId:  objectTable.ParentId,
		Fields:    removeIdField(objectField.Fields),
	}
	return resp, nil
}