package com.njcn.common.utils;

import cn.hutool.core.text.StrFormatter;
import cn.hutool.core.util.StrUtil;
import com.njcn.common.pojo.response.HttpResult;
import com.njcn.common.pojo.enums.response.CommonResponseEnum;
import com.njcn.common.pojo.exception.BusinessException;

import java.util.Date;

/**
 * @author hongawen
 * @version 1.0.0
 * @date 2021年06月10日 13:42
 */
public class HttpResultUtil {

    /**
     * 组装结果集
     */
    public static <T> HttpResult<T> assembleResult(String code, T result, String message) {
        HttpResult<T> httpResult = new HttpResult<>();
        httpResult.setCode(code);
        httpResult.setMessage(message);
        httpResult.setData(result);
        return httpResult;
    }

    /**
     * 组装通用结果集
     */
    public static <T> HttpResult<T> assembleCommonResponseResult(CommonResponseEnum responseEnum, T result, String methodDescribe) {
        String message = responseEnum.getMessage();
        if (responseEnum.equals(CommonResponseEnum.METHOD_ARGUMENT_NOT_VALID_EXCEPTION)) {
            message = (String) result;
        }
        return assembleResult(responseEnum.getCode(), result, StrFormatter.format("{}{}{}", methodDescribe, StrUtil.C_COMMA, message));
    }

    /**
     * 业务异常组装结果集
     */
    public static <T> HttpResult<T> assembleBusinessExceptionResult(BusinessException businessException, T result, String methodDescribe) {
        return assembleResult(businessException.getCode(), result, StrFormatter.format("{}{}{}", methodDescribe, StrUtil.C_COMMA, businessException.getMessage()));
    }

}
