package com.fdkanfang.common.util;

import com.alibaba.fastjson.annotation.JSONField;
import com.fasterxml.jackson.annotation.JsonFormat;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonInclude.Include;

import java.io.Serializable;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

public class R implements Serializable {

	private static final long serialVersionUID = 2719931935414658118L;

	private final Integer status;

	private final String message;

//	@JsonIgnoreProperties(value = { "hibernateLazyInitializer", "handler" })
//	@JsonInclude(value = Include.NON_NULL)

	private final Object data;

	@JsonInclude(value = Include.NON_EMPTY)
//	@JsonInclude(value = Include.NON_NULL)
	@JSONField(serialize = false)
	private final String[] exceptions;

	@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
	private final Date timestamp;

	public R(Integer status, String message) {

		super();
		this.status = status;
		this.message = message;
		this.data = null;
		this.timestamp = addTime();
		this.exceptions = null;

	}



	
	public R(Integer status, Object data) {

		super();

		this.status = status;
		this.message = null;

		this.data = data;
		this.timestamp = addTime();
		this.exceptions = null;

	}
	


	public R(Integer status, String message, String key, Object value) {

		super();

		this.status = status;

		this.message = message;

		Map<String, Object> map = new HashMap<String, Object>();

		if (key == null || ("").equals(key)) {
			map.put("key", value);
		} else {
			map.put(key, value);
		}

		this.data = map;

		this.timestamp = addTime();
		this.exceptions = null;

	}

	public R(Integer status, Throwable ex) {

		super();

		this.status = status;
		this.message = ex.getMessage();
		this.data = null;
		StackTraceElement[] stackTeanceElement = ex.getStackTrace();
		this.exceptions = new String[stackTeanceElement.length];
		for (int i = 0; i < stackTeanceElement.length; i++) {
			this.exceptions[i] = stackTeanceElement[i].toString();
		}
		this.timestamp = addTime();
	}

	public R(Integer status, String message, Throwable ex) {

		super();

		this.status = status;

		this.message = message;

		this.data = null;

		StackTraceElement[] stackTeanceElement = ex.getStackTrace();
		this.exceptions = new String[stackTeanceElement.length];
		for (int i = 0; i < stackTeanceElement.length; i++) {
			this.exceptions[i] = stackTeanceElement[i].toString();
		}

		this.timestamp = addTime();

	}

	public Integer getStatus() {
		return status;
	}

	public String getMessage() {
		return message;
	}

	public Object getData() {
		return data;
	}

	public String[] getExceptions() {
		return exceptions;
	}

	public Date getTimestamp() {
		return timestamp;
	}

	public Date addTime(){
		Calendar nowTime= Calendar.getInstance();
//        nowTime.add(Calendar.MINUTE,15);
        Date time = nowTime.getTime();
        return time;
	}
}
