package cn.ibizlab.core.data.dto;

import cn.ibizlab.core.data.domain.DOModel;
import cn.ibizlab.core.data.domain.DTOModel;
import cn.ibizlab.core.data.model.POSchema;
import cn.ibizlab.core.data.model.PojoSchema;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonInclude;

import java.io.Serializable;
import java.util.Map;

@JsonInclude(JsonInclude.Include.ALWAYS)
@JsonIgnoreProperties(ignoreUnknown = true)
public class BaseData extends DataObj
{

	public BaseData set(String key, Object value)
	{
		this.put(key,value);
		return this;
	}

	public Serializable getSerializableValue(String key)
	{
		Object obj=this.get(key);
		if(obj!=null)
			return (Serializable)obj;
		else
			return null;
	}

	public BaseData setAll(Map map)
	{
		if(map!=null)
			this.putAll(map);
		else if(this.size()==0)
			return null;
		return this;
	}

	public POSchema getPOSchema()
	{
		Object obj=this.get("_poschema");
		if(obj!=null)
		{
			if (obj instanceof POSchema)
				return (POSchema) obj;
			else if(obj instanceof Map)
			{
				POSchema poSchema= JSONObject.parseObject(JSON.toJSONString(obj),POSchema.class);
				this.put("_poschema",poSchema);
				return poSchema;
			}
		}
		return null;
	}

	public BaseData setPOSchema(POSchema poSchema)
	{
		return this.set("_poschema",poSchema);
	}

	public DOModel getDOModel()
	{
		Object obj=this.get("_domodel");
		if(obj!=null)
		{
			if (obj instanceof DOModel)
				return (DOModel) obj;
			else if(obj instanceof Map)
			{
				DOModel _domodel= JSONObject.parseObject(JSON.toJSONString(obj),DOModel.class);
				this.put("_domodel",_domodel);
				return _domodel;
			}
		}
		return null;
	}

	public BaseData setDOModel(DOModel doModel)
	{
		return this.set("_domodel",doModel);
	}

	public DTOModel getDTOModel()
	{
		Object obj=this.get("_dtomodel");
		if(obj!=null)
		{
			if (obj instanceof DTOModel)
				return (DTOModel) obj;
			else if(obj instanceof Map)
			{
				DTOModel _dtomodel= JSONObject.parseObject(JSON.toJSONString(obj),DTOModel.class);
				this.put("_dtomodel",_dtomodel);
				return _dtomodel;
			}
		}
		return null;
	}

	public BaseData setDTOModel(DTOModel dtoModel)
	{
		return this.set("_dtomodel",dtoModel);
	}

	public PojoSchema getPojoSchema()
	{
		Object obj=this.get("_pojoschema");
		if(obj==null)
		{
			DOModel doModel=this.getDOModel();
			if(doModel!=null)
				obj=doModel.getSchema();
		}
		if(obj==null)
		{
			DTOModel dtoModel=this.getDTOModel();
			if(dtoModel!=null)
				obj=dtoModel.getSchema();
		}
		if(obj!=null)
		{
			if (obj instanceof PojoSchema)
				return (PojoSchema) obj;
			else if(obj instanceof Map)
			{
				PojoSchema _pojoschema= JSONObject.parseObject(JSON.toJSONString(obj),PojoSchema.class);
				this.put("_pojoschema",_pojoschema);
				return _pojoschema;
			}
		}
		return null;
	}

	public BaseData setKey(Object key)
	{
		return this.set("key",key);
	}
}
