package cn.ibizlab.core.data.dto;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.annotation.JSONField;
import com.fasterxml.jackson.annotation.JsonIgnore;
import org.springframework.util.ObjectUtils;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

public class RequestData<T> extends BaseData
{
	public RequestData set(String key, Object value)
	{
		this.put(key,value);
		return this;
	}



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

	public Serializable getKey()
	{
		Object key=this.get("key");
		if(key!=null)
			return (Serializable)key;
		else
			return null;
	}

	public RequestData setSystem(String system)
	{
		return this.set("system",system);
	}

	public String getSystem()
	{
		return this.getStringValue("system");
	}

	public RequestData setScope(String scope)
	{
		return this.set("scope",scope);
	}

	public String getScope()
	{
		return this.getStringValue("scope","default");
	}

	public RequestData setEntity(String entity)
	{
		return this.set("entity",entity);
	}

	public String getEntity()
	{
		return this.getStringValue("entity",this.getStringValue("dename"));
	}

	public RequestData setMethod(String method)
	{
		return this.set("method",method);
	}

	public String getMethod()
	{
		return this.getStringValue("method",this.getStringValue("action"));
	}

	public RequestData setDataSource(String datasource)
	{
		return this.set("datasource",datasource);
	}

	public String getDataSource()
	{
		return this.getStringValue("datasource");
	}

	public RequestData setBody(T data)
	{
		return this.set("data",data);
	}

	public T getBody()
	{
		if(this.get("data")!=null)
			return (T)this.get("data");
		else
			return null;
	}

	public BaseData getBaseData()
	{
		Object body=getBody();
		if(body!=null&&body instanceof BaseData)
			return (BaseData)body;
		return this;
	}

	public List<BaseData> getListData()
	{
		Object body=getBody();
		if(body!=null&&body instanceof List)
		{
			if(((List) body).size()==0)
				return new ArrayList<>();
			else if(((List) body).get(0) instanceof BaseData)
				return (List<BaseData> )body;
			else
				return JSONArray.parseArray(JSON.toJSONString(body),BaseData.class);
		}
		return new ArrayList<>();
	}


}
