package com.persagy.adm.server.custom.entity;

import cn.hutool.core.collection.CollUtil;
import com.fasterxml.jackson.databind.node.ObjectNode;
import com.persagy.adm.server.custom.entity.db.BaseAdmDataEntity;
import com.persagy.dmp.common.model.entity.BaseEntity;
import com.persagy.dmp.digital.entity.ObjectRelation;
import lombok.Data;

import java.util.List;

/**
 * 下载数据结构基类
 */
@Data
abstract public class DownLoadData {

	/** bdtp数据库下载同步时间戳 */
	protected Long bdtpDownloadTs;

	/** adm数据库下载同步时间戳 */
	protected Long admDownloadTs;

	abstract public void buildDownloadTs();

	private long max(List<?> list, long maxTs) {
		if(CollUtil.isNotEmpty(list)) {
			for (Object data : list) {
				long ts = -1;
				if(data instanceof BaseAdmDataEntity) {
					if(((BaseAdmDataEntity) data).getTs() != null )
						ts = ((BaseAdmDataEntity) data).getTs().getTime();
				} else if(data instanceof ObjectNode){
					if(((ObjectNode) data).get("ts") != null)
						ts = ((ObjectNode) data).get("ts").asLong();
				} else if(data instanceof BaseEntity) {
					if(((ObjectRelation) data).getTs() != null)
						ts = ((ObjectRelation) data).getTs().getTime();
				}
				if(ts > maxTs)
					maxTs = ts;
			}
		}
		return maxTs;
	}

	protected long getMaxTs(List<?>... lists) {
		long maxTs = -1;
		for(List<?> list : lists) {
			maxTs = max(list, maxTs);
		}
		return maxTs;
	}

}
