package com.bcxin.auth.common.utils;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.alibaba.fastjson.serializer.SimplePropertyPreFilter;
import com.fasterxml.jackson.annotation.JsonIgnore;

import javax.servlet.http.HttpServletRequest;

/**
 * 前端交互返回数据格式
 *
 * @author zhangye
 * @version 1.0
 */
public class Result {
	private String api;// 接口名称
	private String v;// 版本号
	private String retType = "0";// 状态码
	private Object data;// 返回数据对象
	private String msg = SUCCESS_MSG;// 返回信息
	/**
	 * 成功
	 */
	public static final String SUCCESS = "0";

	/**
	 * 服务端内部错误
	 */
	public static final String ERROR = "-1";

	/**
	 * 业务异常错误
	 */
	public static final String BUSINESS_ERROR = "-2";

	/**
	 * 请求token过期
	 */
	public static final String TOKEN_EXPIRED = "1";

	/**
	 * 服务升级状态
	 */
	public static final String SERVICE_UPGRADE = "2";

	/**
	 * 成功消息
	 */
	public static final String SUCCESS_MSG = "操作成功！";

	/**
	 * 查询成功
	 * 
	 * @apiNote 作者 陈堂君
	 * @since 创建于 2018/3/5 17:11
	 */
	public static final String SUCCESS_QUERY_MSG = "";

	/**
	 * 失败消息
	 */
	public static final String ERROR_MSG = "操作失败:";

	public Result() {
		super();
	}

	/**
	 * @param retType
	 *            结果状态码
	 * @param message
	 *            响应结果描述
	 * @param data
	 *            成功时返回的数据结果
	 */
	private Result(String v, String api, String retType, String message, Object data) {
		super();
		this.v = v;
		this.api = api;
		this.retType = retType;
		this.msg = message;
		this.data = data;
	}

	/**
	 * 成功信息返回(Controller层使用)
	 *
	 * @param request
	 * @param successMsg
	 * @param data
	 * @return
	 */
	public static Result success(HttpServletRequest request, String successMsg, Object data) {
		return new Result(request.getParameter("v"), request.getParameter("api"), Result.SUCCESS,
				(successMsg == null) ? "" : successMsg, data);
	}

	/**
	 * 错误信息返回(Controller层使用)
	 *
	 * @param request
	 * @param errorMsg
	 * @param data
	 * @return
	 */
	public static Result fail(HttpServletRequest request, String errorMsg, Object data) {
		return new Result(request.getParameter("v"), request.getParameter("api"), Result.ERROR,
				(errorMsg == null) ? "" : errorMsg, data);
	}

	/**
	 * 成功信息返回
	 *
	 * @param successMsg
	 * @param data
	 * @return
	 */
	public static Result success(String successMsg, Object data) {
		return new Result("", "", Result.SUCCESS, successMsg, data);
	}

	/**
	 * 错误信息返回
	 *
	 * @param errorMsg
	 * @param data
	 * @return
	 */
	public static Result fail(String errorMsg, Object data) {
		return new Result("", "", Result.ERROR, (errorMsg == null || errorMsg.equals("")) ? Result.ERROR_MSG : errorMsg,
				data);
	}

	/**
	 * 成功信息返回
	 *
	 * @param successMsg
	 * @return
	 */
	public static Result success(String successMsg) {
		return new Result("", "", Result.SUCCESS, (successMsg == null || successMsg.equals("")) ? "" : successMsg,
				null);
	}

	/**
	 * 错误信息返回
	 *
	 * @param errorMsg
	 * @return
	 */
	public static Result fail(String errorMsg) {
		return new Result("", "", Result.ERROR, (errorMsg == null || errorMsg.equals("")) ? Result.ERROR_MSG : errorMsg,
				null);
	}

	/**
	 * 请求token过期
	 *
	 * @param errorMsg
	 * @return
	 */
	public static Result tokenExpired(String errorMsg) {
		return new Result("", "", Result.TOKEN_EXPIRED,
				(errorMsg == null || errorMsg.equals("")) ? Result.ERROR_MSG : errorMsg, null);
	}

	public Result setBack(HttpServletRequest request) {
		this.setApi(request.getParameter("api"));
		this.setV(request.getParameter("v"));
		return this;
	}

	public String getRetType() {
		return retType;
	}

	public void setRetType(String retType) {
		this.retType = retType;
	}

	public String getMsg() {
		return msg;
	}

	public void setMsg(String msg) {
		this.msg = msg;
	}

	public Object getData() {
		return data;
	}

	public void setData(Object data) {
		this.data = data;
	}

	public String getApi() {
		return api;
	}

	public void setApi(String api) {
		this.api = api;
	}

	public String getV() {
		return v;
	}

	public void setV(String v) {
		this.v = v;
	}


	/**
	 * APP数据加密工具
	 **/
	@JsonIgnore
	public static String getDecodeData(String data) {
		try {
			return data == null ? null : DES3.decode(data.toString());
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}

	@JsonIgnore
	public static <T> T getDecodeData(String data, Class<T> clazz) {
		return JSON.parseObject(getDecodeData(data), clazz);
	}

	public static Object setEncodeData(Object data) {
		try {
			SimplePropertyPreFilter filter = new SimplePropertyPreFilter();
			filter.getExcludes().add("isDelete");
			filter.getExcludes().add("pageNumber");
			filter.getExcludes().add("pageSize");
			filter.getExcludes().add("appData");
			filter.getExcludes().add("t");
			filter.getExcludes().add("sign");
			filter.getExcludes().add("api");
			filter.getExcludes().add("v");
			JSON.DEFFAULT_DATE_FORMAT = "yyyy-MM-dd HH:mm:ss";
			return DES3.encode(JSON.toJSONString(data, filter, SerializerFeature.WriteDateUseDateFormat,
					SerializerFeature.WriteMapNullValue));
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}

}
