/*
 * $Id: MessageUtil.java,v 1.2 2016/07/18 02:49:57 wangqx Exp $ *
 */
package cn.com.liandisys.ycjt.ggcx.common.util;

import java.text.MessageFormat;
import java.util.HashMap;
import java.util.Map;

import cn.com.liandisys.ycjt.ggcx.common.constant.MessageConstant;

/**
 * @author wangqx
 * 2016-7-30
 */
public class MessageUtil {

    /***
     * 生成系统级异常的错误信息
     * @param e
     * @return
     */
    public static String buildSystemErrorMessage(Throwable e) {

        return MessageFormat.format("{0}({1})。", "系统错误",
                e instanceof NullPointerException ? MessageConstant.NULL_OBJECT : e.getMessage());
    }

    /**
     * 生成业务级异常的错误信息
     * @param e
     * @return
     */
    public static String buildBusinessErrorMessage(Throwable e) {

        return MessageFormat.format("{0}(详细原因：{1})。", "操作失败",
                e instanceof NullPointerException ? MessageConstant.NULL_OBJECT : e.getMessage());
    }

    /**
     * 生成业务级异常的错误信息
     * @param e
     * @param msg
     * @return msg(详细原因：...)
     */
    public static String buildBusinessErrorMessage(Throwable e, String msg) {

        return MessageFormat.format("{0}(详细原因：{1})。", msg,
                e instanceof NullPointerException ? MessageConstant.NULL_OBJECT : e.getMessage());
    }

    /**
     * 生成操作成功信息
     * @return
     */
    public static String buildSuccessMessage() {

        return MessageConstant.OPERATION_SUCCESS;
    }

    /**
     * 生成响应Map
     * @param flag
     * @param message
     * @param messageMap
     * @return
     */
    public static Map<String, Object> buildResponseMap(boolean flag, String message, Map<String, Object> messageMap) {

        if (messageMap == null)
            messageMap = new HashMap<String, Object>();
        messageMap.put(MessageConstant.KEY.FLAG, flag);
        if (StringUtil.isNotEmpty(message))
            messageMap.put(MessageConstant.KEY.MESSAGE, message);
        return messageMap;
    }

    /**
     * 生成响应Map
     * @param flag
     * @param message
     * @return
     */
    public static Map<String, Object> buildResponseMap(boolean flag, String message) {

        return MessageUtil.buildResponseMap(flag, message, null);
    }

    /**
     * 生成响应Map
     * @param flag
     * @param message
     * @return
     */
    public static Map<String, Object> buildResponseMap(boolean flag, Map<String, Object> messageMap) {

        return MessageUtil.buildResponseMap(flag, StringUtil.EMPTY, messageMap);
    }
}