All Downloads are FREE. Search and download functionalities are using the official Maven repository.

net.wicp.tams.common.binlog.alone.dump.bean.DumpEvent Maven / Gradle / Ivy

The newest version!
/*
 * **********************************************************************
 * Copyright (c) 2022 .
 * All rights reserved.
 * 项目名称:common
 * 项目描述:公共的工具集
 * 版权说明:本软件属andy.zhou([email protected])所有。
 * ***********************************************************************
 */
package net.wicp.tams.common.binlog.alone.dump.bean;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.collections.MapUtils;
import org.apache.commons.lang3.ArrayUtils;
import org.apache.commons.lang3.tuple.Pair;

import com.fasterxml.jackson.databind.node.ArrayNode;
import com.fasterxml.jackson.databind.node.JsonNodeFactory;
import com.fasterxml.jackson.databind.node.ObjectNode;

import lombok.Data;
import net.wicp.tams.common.apiext.CollectionUtil;
import net.wicp.tams.common.apiext.StringUtil;
import net.wicp.tams.common.binlog.alone.binlog.bean.RuleItem;
import net.wicp.tams.common.callback.IDbTbConvert;
import net.wicp.tams.common.constant.FieldFormart;

/****
 * dump请求的数据和控制信息
 * 
 * @author 偏锋书生
 *
 *         2018年4月26日
 */
@Data
public class DumpEvent {
	private String targetDb;// 20211209 add 要导入的库名
	private String targetTb;// 20211209 add 要导入的表名
	private String[] beginId;// 开始ID
	private String[] endId;// 结束ID
	private int dbtbIndex;
	/*
	 * private String db;// 库名 private String tb;// 表名 private String[] cols;// 列名
	 * private BinlogType[] colsType;// 类型
	 */
	private Dump dump;
	private List> datas;// 数据
	
	
	public Pair getOriDbtb(){
		return this.dump.getDbtbs().get(dbtbIndex);
	}

	/***
	 * 得到key
	 * 
	 * @param data
	 * @param splitChat
	 * @return
	 */
	public String findKey(Map data, String splitChat) {
		if (this.dump == null || MapUtils.isEmpty(data)) {
			return "";
		}
		String[] keyColNames = this.dump.getPrimarys();
		List keyvalues = new ArrayList();
		for (String keyColName : keyColNames) {
			keyvalues.add(data.get(keyColName));
		}
		return CollectionUtil.listJoin(keyvalues, StringUtil.hasNull(splitChat, "|"));
	}

	public String findKey(Map data) {
		return findKey(data, null);
	}

	/***
	 * 全量数据转为json
	 * 
	 * @param dumpEvent
	 * @return
	 */
	public static ObjectNode convertJson(DumpEvent dumpEvent) {
		ObjectNode retobj = JsonNodeFactory.instance.objectNode();
		retobj.put("optType", "select");
		ArrayNode items =JsonNodeFactory.instance.arrayNode();
		for (Map data : dumpEvent.getDatas()) {
			ObjectNode itemobj = JsonNodeFactory.instance.objectNode();
			itemobj.putPOJO("after", data);
			items.add(itemobj);
		}
		retobj.set("items", items);
		return retobj;
	}

	/***
	 * 按要求转换字段名称
	 * 
	 * @param fieldFormart
	 */
	public void convertEvent(FieldFormart fieldFormart, IDbTbConvert dbTbConvert, Pair dbtbOri) {
		this.getDump().getRule().getItems().get(RuleItem.db);
		if (dbTbConvert != null) {
			Pair dbAndTb = dbTbConvert.getDbAndTb(null, Pair.of(dbtbOri.getLeft(), dbtbOri.getRight()));
			this.targetDb = dbAndTb.getLeft();
			this.targetTb = dbAndTb.getRight();
		}
		if (fieldFormart != null && FieldFormart.ori != fieldFormart) {
			if (ArrayUtils.isNotEmpty(this.dump.getPrimarysLogic())) {
				for (int i = 0; i < this.dump.getPrimarysLogic().length; i++) {
					this.dump.getPrimarysLogic()[i] = fieldFormart.getColName(this.dump.getPrimarysLogic()[i]);
				}
			}
			if (ArrayUtils.isNotEmpty(this.dump.getPrimarys())) {
				for (int i = 0; i < this.dump.getPrimarys().length; i++) {
					this.dump.getPrimarys()[i] = fieldFormart.getColName(this.dump.getPrimarys()[i]);
				}

			}
			if (ArrayUtils.isNotEmpty(this.dump.getNeedCols())) {
				for (int i = 0; i < dump.getNeedCols().length; i++) {
					this.dump.getNeedCols()[i] = fieldFormart.getColName(this.dump.getNeedCols()[i]);
				}
			}

			// 数据处理
			List> newDatas = new ArrayList>();
			for (int i = 0; i < this.datas.size(); i++) {
				Map newmap = new HashMap();
				Map oldmap = this.datas.get(i);
				for (String key : oldmap.keySet()) {
					newmap.put(fieldFormart.getColName(key), oldmap.get(key));
				}
				newDatas.add(newmap);
			}
			this.datas = newDatas;
		}

	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy