package com.liangjian.dataplatform.member.entity.vo.datanodes;

import com.google.gson.Gson;
import com.liangjian.dataplatform.member.constant.MetaCoreConstant;
import com.liangjian.dataplatform.member.entity.po.primary.MetaCore;
import com.liangjian.dataplatform.member.entity.pojo.PkAndNullable;
import org.springframework.stereotype.Component;

/**
 * @Author: 黄子奇
 * @Date: 2019/8/26 17:22
 */
@Component
public class PDMColumnTransfer implements DataNodeTransfer<PDMColumn> {
    //本身这个Compon默认是一个单例，在这个Compone内复用同一个gson。
    private Gson gson = new Gson();

    @Override
    public boolean matchTypeKey(String key) {
        return key.equals(MetaCoreConstant.MODEK_PHYSICAL_MODEL_CLM) || key.equals(MetaCoreConstant.RELATION_COLUMN);
    }

    @Override
    public DataNode transfer(MetaCore metaCore) {
        String json = metaCore.getEntity();
        PkAndNullable pkAndNullable = gson.fromJson(json, PkAndNullable.class);

        PDMColumn column = new PDMColumn(
                metaCore.getId(),
                metaCore.getPrtId(),
                metaCore.getName(),
                metaCore.getBizDomain(),
                metaCore.getMetaMdl(),
                false,
                metaCore.getCmt(),
                metaCore.getRemarks(),
                metaCore.getCrtDate(),
                metaCore.getLastUpdate(),
                metaCore.getCrtUser(),
                metaCore.getNamespace(),
                metaCore.getChName(),
                metaCore.getType(),
                metaCore.getLength(),
                metaCore.getPrecision(),
                pkAndNullable
        );
        return column;
    }

    @Override
    public MetaCore reTransfer(MetaCore metaCore, PDMColumn node) {
        metaCore.setPrtId(node.getPrtId());
        metaCore.setName(node.getName());
        metaCore.setBizDomain(node.getBizDomain());
        metaCore.setMetaMdl(node.getNodeType());
        metaCore.setCmt(node.getComment());
        metaCore.setLastUpdate(node.getLastUpdate());
        metaCore.setChName(node.getCnName());
        metaCore.setType(node.type);
        metaCore.setLength(node.getLength());
        metaCore.setPrecision(node.getPrec());
        metaCore.setRemarks(node.getRemark());

        String json = gson.toJson(node.getEntry());
        metaCore.setEntity(json);
        return metaCore;
    }
}
