package com.hai.common.utils;



import com.hai.common.exception.BaseException;
import com.hai.common.exception.BizException;

import com.hai.model.ResponseData;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;


public class ResponseMsgUtil {

	static Logger log = LoggerFactory.getLogger(ResponseMsgUtil.class);
	
	/**
	 * 
	 * @Title: success   
	 * @Description: 请求成功后，组装返回给前台的对象
	 * @author: 机器猫 
	 * @param: @param data
	 * @param: @return      
	 * @return: ResponseData      
	 * @throws
	 */
	public static ResponseData success(Object data){
		ResponseData res = new ResponseData();
        res.setReturn_code("000000");
        res.setReturn_data(data);
        return res;
	}
	
	 /**
     * 根据消息码等生成接口返回对象
     *
     * @param code 结果返回码
     * @param msg  结果返回消息
     * @param data 数据对象
     * @param
     * @return
     */
    public static ResponseData builderResponse(String code, String msg, Object data) {
    	ResponseData res = new ResponseData();
        res.setReturn_code(code);
        res.setReturn_msg(msg);
        res.setReturn_data(data);
        return res;
    }

    /**
     * 请求异常返回结果
     *
     * @param
     * @return
     */
    public static ResponseData exception(Exception e) {
    	
    	log.error("---controller error---",e);
    	
    	if(e instanceof BizException){//业务异常处理
    		return builderResponse(((BizException)e).getErrorCode(), ((BizException)e).getErrorMsg(), null);
    	} else if(e instanceof BaseException){//系统异常处理
    		return builderResponse(((BaseException)e).getErrorCode(),"服务异常",null);
    	} else{//未知异常
    		return builderResponse("999999", "未知异常", null);
    	}
    }
	
}
