package com.liangjian.dataplatform.member.service;

import com.liangjian.dataplatform.member.constant.DataNodeConstant;
import com.liangjian.dataplatform.member.entity.po.primary.MetaCore;
import com.liangjian.dataplatform.member.entity.vo.datanodes.DataNode;
import com.liangjian.dataplatform.member.entity.vo.datanodes.DataNodeTransfer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @Author: 黄子奇
 * @Date: 2019/8/26 17:37
 *
 *
 *  * MetaCore -> DataNode 的转换器，
 *  * 利用类似责任链、spi的方式，顺序跑每个实际的Transfer，直到找到匹配的转换器，否则转换成最简单的DataNode
 */
@Service
public class DataNodeTransferService {

    @Autowired
    List<DataNodeTransfer> transfers;

    public DataNode transfer(MetaCore metaCore) {
        String key = metaCore.getMetaMdl();

        for (DataNodeTransfer transfer : transfers) {
            if(transfer.matchTypeKey(key)) {
                return transfer.transfer(metaCore);
            }
        }
        //上面遍历完毕都没有匹配成功
        return new DataNode(
                metaCore.getId(),
                metaCore.getPrtId(),
                metaCore.getName(),
                metaCore.getBizDomain(),
                metaCore.getMetaMdl(),
                false,
                metaCore.getCmt(),
                metaCore.getRemarks(),
                metaCore.getCrtDate(),
                metaCore.getLastUpdate(),
                metaCore.getCrtUser(),
                metaCore.getNamespace()
        ) {
            private static final long serialVersionUID = -8176838135193258421L;

            @Override
            public String getDataKind() {
                return DataNodeConstant.KIND_UNKNOWN;
            }
        };
    }

    public MetaCore reTransfer(MetaCore metaCore, DataNode dataNode) {
        String key = dataNode.getNodeType();

        for (DataNodeTransfer transfer : transfers) {
            if(transfer.matchTypeKey(key)) {
                return transfer.reTransfer(metaCore, dataNode);
            }
        }
        //上面遍历都没成功
        return metaCore;
    }
}
